@classytic/revenue 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +454 -0
- package/core/builder.js +170 -0
- package/core/container.js +119 -0
- package/core/errors.js +262 -0
- package/enums/index.js +70 -0
- package/enums/monetization.enums.js +15 -0
- package/enums/payment.enums.js +43 -0
- package/enums/subscription.enums.js +33 -0
- package/enums/transaction.enums.js +53 -0
- package/index.js +58 -0
- package/package.json +62 -0
- package/providers/base.js +162 -0
- package/providers/manual.js +171 -0
- package/revenue.d.ts +290 -0
- package/schemas/index.js +21 -0
- package/schemas/subscription/index.js +17 -0
- package/schemas/subscription/info.schema.js +115 -0
- package/schemas/subscription/plan.schema.js +48 -0
- package/schemas/transaction/common.schema.js +22 -0
- package/schemas/transaction/gateway.schema.js +69 -0
- package/schemas/transaction/index.js +20 -0
- package/schemas/transaction/payment.schema.js +110 -0
- package/services/payment.service.js +400 -0
- package/services/subscription.service.js +537 -0
- package/services/transaction.service.js +142 -0
- package/utils/hooks.js +44 -0
- package/utils/index.js +8 -0
- package/utils/logger.js +36 -0
- package/utils/transaction-type.js +254 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subscription Schemas
|
|
3
|
+
* @classytic/revenue
|
|
4
|
+
*
|
|
5
|
+
* Re-exports all subscription-related schemas
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * from './plan.schema.js';
|
|
9
|
+
export * from './info.schema.js';
|
|
10
|
+
|
|
11
|
+
import planSchemas from './plan.schema.js';
|
|
12
|
+
import infoSchemas from './info.schema.js';
|
|
13
|
+
|
|
14
|
+
export default {
|
|
15
|
+
...planSchemas,
|
|
16
|
+
...infoSchemas,
|
|
17
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subscription Info Schema
|
|
3
|
+
* @classytic/revenue
|
|
4
|
+
*
|
|
5
|
+
* Complete subscription information schema
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Schema } from 'mongoose';
|
|
9
|
+
import { subscriptionPlanSchema } from './plan.schema.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Subscription Info Schema
|
|
13
|
+
* Use this in your model: subscription: { type: subscriptionInfoSchema }
|
|
14
|
+
*
|
|
15
|
+
* Complete subscription information
|
|
16
|
+
*/
|
|
17
|
+
export const subscriptionInfoSchema = new Schema({
|
|
18
|
+
isActive: {
|
|
19
|
+
type: Boolean,
|
|
20
|
+
default: false,
|
|
21
|
+
index: true
|
|
22
|
+
},
|
|
23
|
+
plan: {
|
|
24
|
+
type: subscriptionPlanSchema,
|
|
25
|
+
required: true
|
|
26
|
+
},
|
|
27
|
+
startDate: {
|
|
28
|
+
type: Date,
|
|
29
|
+
index: true
|
|
30
|
+
},
|
|
31
|
+
endDate: {
|
|
32
|
+
type: Date,
|
|
33
|
+
index: true
|
|
34
|
+
},
|
|
35
|
+
autoRenew: {
|
|
36
|
+
type: Boolean,
|
|
37
|
+
default: true
|
|
38
|
+
},
|
|
39
|
+
renewalCount: {
|
|
40
|
+
type: Number,
|
|
41
|
+
default: 0,
|
|
42
|
+
min: 0
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
// Cancellation
|
|
46
|
+
canceledAt: {
|
|
47
|
+
type: Date
|
|
48
|
+
},
|
|
49
|
+
cancelAt: {
|
|
50
|
+
type: Date
|
|
51
|
+
},
|
|
52
|
+
cancellationReason: {
|
|
53
|
+
type: String
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
// Pause/Resume
|
|
57
|
+
pausedAt: {
|
|
58
|
+
type: Date
|
|
59
|
+
},
|
|
60
|
+
pauseReason: {
|
|
61
|
+
type: String
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
// Scheduled Plan Changes (Upgrade/Downgrade)
|
|
65
|
+
scheduledChange: {
|
|
66
|
+
type: new Schema({
|
|
67
|
+
// New plan details
|
|
68
|
+
newPlan: {
|
|
69
|
+
type: subscriptionPlanSchema,
|
|
70
|
+
required: true
|
|
71
|
+
},
|
|
72
|
+
// When the change takes effect
|
|
73
|
+
effectiveDate: {
|
|
74
|
+
type: Date,
|
|
75
|
+
required: true
|
|
76
|
+
},
|
|
77
|
+
// Type of change
|
|
78
|
+
changeType: {
|
|
79
|
+
type: String,
|
|
80
|
+
enum: ['upgrade', 'downgrade'],
|
|
81
|
+
required: true
|
|
82
|
+
},
|
|
83
|
+
// Scheduled date
|
|
84
|
+
scheduledAt: {
|
|
85
|
+
type: Date,
|
|
86
|
+
default: Date.now
|
|
87
|
+
},
|
|
88
|
+
// Optional admin price override (for upgrades)
|
|
89
|
+
priceOverride: {
|
|
90
|
+
type: Number,
|
|
91
|
+
min: 0
|
|
92
|
+
},
|
|
93
|
+
// Who scheduled the change
|
|
94
|
+
scheduledBy: {
|
|
95
|
+
type: Schema.Types.ObjectId,
|
|
96
|
+
ref: 'User'
|
|
97
|
+
},
|
|
98
|
+
// Calculation details (for audit trail)
|
|
99
|
+
calculation: {
|
|
100
|
+
type: Schema.Types.Mixed
|
|
101
|
+
}
|
|
102
|
+
}, { _id: false }),
|
|
103
|
+
default: null
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
// Metadata
|
|
107
|
+
metadata: {
|
|
108
|
+
type: Schema.Types.Mixed,
|
|
109
|
+
default: {}
|
|
110
|
+
},
|
|
111
|
+
}, { _id: false });
|
|
112
|
+
|
|
113
|
+
export default {
|
|
114
|
+
subscriptionInfoSchema,
|
|
115
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subscription Plan Schema
|
|
3
|
+
* @classytic/revenue
|
|
4
|
+
*
|
|
5
|
+
* Schema for subscription plan definitions
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Schema } from 'mongoose';
|
|
9
|
+
import { PLAN_KEY_VALUES } from '../../enums/index.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Subscription Plan Schema
|
|
13
|
+
* Embedded in subscription info
|
|
14
|
+
*/
|
|
15
|
+
export const subscriptionPlanSchema = new Schema({
|
|
16
|
+
key: {
|
|
17
|
+
type: String,
|
|
18
|
+
required: true,
|
|
19
|
+
enum: PLAN_KEY_VALUES,
|
|
20
|
+
},
|
|
21
|
+
label: {
|
|
22
|
+
type: String,
|
|
23
|
+
required: true,
|
|
24
|
+
},
|
|
25
|
+
duration: {
|
|
26
|
+
type: Number,
|
|
27
|
+
required: true,
|
|
28
|
+
min: 1,
|
|
29
|
+
},
|
|
30
|
+
durationUnit: {
|
|
31
|
+
type: String,
|
|
32
|
+
default: 'days',
|
|
33
|
+
},
|
|
34
|
+
price: {
|
|
35
|
+
type: Number,
|
|
36
|
+
required: true,
|
|
37
|
+
min: 0,
|
|
38
|
+
},
|
|
39
|
+
discount: {
|
|
40
|
+
type: Number,
|
|
41
|
+
default: 0,
|
|
42
|
+
min: 0,
|
|
43
|
+
},
|
|
44
|
+
}, { _id: false });
|
|
45
|
+
|
|
46
|
+
export default {
|
|
47
|
+
subscriptionPlanSchema,
|
|
48
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common Transaction Fields
|
|
3
|
+
* @classytic/revenue
|
|
4
|
+
*
|
|
5
|
+
* Common field definitions for transaction-related models
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Common Field Definitions
|
|
10
|
+
* Use these for consistent field definitions across models
|
|
11
|
+
*/
|
|
12
|
+
export const commonFields = {
|
|
13
|
+
isNewRequest: {
|
|
14
|
+
type: Boolean,
|
|
15
|
+
default: false,
|
|
16
|
+
index: true,
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default {
|
|
21
|
+
commonFields,
|
|
22
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway and Commission Schemas
|
|
3
|
+
* @classytic/revenue
|
|
4
|
+
*
|
|
5
|
+
* Schemas for payment gateway and commission tracking
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Schema } from 'mongoose';
|
|
9
|
+
import { PAYMENT_GATEWAY_TYPE_VALUES } from '../../enums/index.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Gateway Schema
|
|
13
|
+
* For payment gateway integration details
|
|
14
|
+
*/
|
|
15
|
+
export const gatewaySchema = new Schema({
|
|
16
|
+
type: {
|
|
17
|
+
type: String,
|
|
18
|
+
enum: PAYMENT_GATEWAY_TYPE_VALUES,
|
|
19
|
+
default: 'manual'
|
|
20
|
+
},
|
|
21
|
+
paymentIntentId: { type: String },
|
|
22
|
+
sessionId: { type: String },
|
|
23
|
+
paymentUrl: { type: String },
|
|
24
|
+
expiresAt: { type: Date },
|
|
25
|
+
metadata: { type: Schema.Types.Mixed },
|
|
26
|
+
}, { _id: false });
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Commission Schema
|
|
30
|
+
* Commission tracking for marketplace transactions
|
|
31
|
+
*/
|
|
32
|
+
export const commissionSchema = new Schema({
|
|
33
|
+
rate: {
|
|
34
|
+
type: Number,
|
|
35
|
+
min: 0,
|
|
36
|
+
max: 1
|
|
37
|
+
},
|
|
38
|
+
grossAmount: {
|
|
39
|
+
type: Number,
|
|
40
|
+
min: 0
|
|
41
|
+
},
|
|
42
|
+
gatewayFeeRate: {
|
|
43
|
+
type: Number,
|
|
44
|
+
min: 0,
|
|
45
|
+
max: 1
|
|
46
|
+
},
|
|
47
|
+
gatewayFeeAmount: {
|
|
48
|
+
type: Number,
|
|
49
|
+
min: 0
|
|
50
|
+
},
|
|
51
|
+
netAmount: {
|
|
52
|
+
type: Number,
|
|
53
|
+
min: 0
|
|
54
|
+
},
|
|
55
|
+
status: {
|
|
56
|
+
type: String,
|
|
57
|
+
enum: ['pending', 'due', 'paid', 'waived'],
|
|
58
|
+
default: 'pending'
|
|
59
|
+
},
|
|
60
|
+
dueDate: { type: Date },
|
|
61
|
+
paidDate: { type: Date },
|
|
62
|
+
paidBy: { type: Schema.Types.ObjectId, ref: 'User' },
|
|
63
|
+
notes: { type: String },
|
|
64
|
+
}, { _id: false });
|
|
65
|
+
|
|
66
|
+
export default {
|
|
67
|
+
gatewaySchema,
|
|
68
|
+
commissionSchema,
|
|
69
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transaction Schemas
|
|
3
|
+
* @classytic/revenue
|
|
4
|
+
*
|
|
5
|
+
* Re-exports all transaction-related schemas
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * from './payment.schema.js';
|
|
9
|
+
export * from './gateway.schema.js';
|
|
10
|
+
export * from './common.schema.js';
|
|
11
|
+
|
|
12
|
+
import paymentSchemas from './payment.schema.js';
|
|
13
|
+
import gatewaySchemas from './gateway.schema.js';
|
|
14
|
+
import commonSchemas from './common.schema.js';
|
|
15
|
+
|
|
16
|
+
export default {
|
|
17
|
+
...paymentSchemas,
|
|
18
|
+
...gatewaySchemas,
|
|
19
|
+
...commonSchemas,
|
|
20
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payment Schemas for Transaction Model
|
|
3
|
+
* @classytic/revenue
|
|
4
|
+
*
|
|
5
|
+
* Schemas for payment tracking in transactions
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Schema } from 'mongoose';
|
|
9
|
+
import {
|
|
10
|
+
PAYMENT_STATUS,
|
|
11
|
+
PAYMENT_STATUS_VALUES,
|
|
12
|
+
} from '../../enums/index.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Current Payment Schema
|
|
16
|
+
* Use this in your model: currentPayment: { type: currentPaymentSchema }
|
|
17
|
+
*
|
|
18
|
+
* Tracks the latest payment transaction for an entity
|
|
19
|
+
*/
|
|
20
|
+
export const currentPaymentSchema = new Schema({
|
|
21
|
+
transactionId: {
|
|
22
|
+
type: Schema.Types.ObjectId,
|
|
23
|
+
ref: 'Transaction',
|
|
24
|
+
index: true
|
|
25
|
+
},
|
|
26
|
+
amount: {
|
|
27
|
+
type: Number,
|
|
28
|
+
min: 0
|
|
29
|
+
},
|
|
30
|
+
status: {
|
|
31
|
+
type: String,
|
|
32
|
+
enum: PAYMENT_STATUS_VALUES,
|
|
33
|
+
default: 'pending',
|
|
34
|
+
index: true
|
|
35
|
+
},
|
|
36
|
+
method: {
|
|
37
|
+
type: String,
|
|
38
|
+
// Users define payment methods in their transaction model
|
|
39
|
+
},
|
|
40
|
+
reference: {
|
|
41
|
+
type: String,
|
|
42
|
+
trim: true
|
|
43
|
+
},
|
|
44
|
+
verifiedAt: {
|
|
45
|
+
type: Date
|
|
46
|
+
},
|
|
47
|
+
verifiedBy: {
|
|
48
|
+
type: Schema.Types.ObjectId,
|
|
49
|
+
ref: 'User'
|
|
50
|
+
},
|
|
51
|
+
}, { _id: false });
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Payment Summary Schema
|
|
55
|
+
* Use this in your model: paymentSummary: { type: paymentSummarySchema }
|
|
56
|
+
*
|
|
57
|
+
* Tracks payment history and totals
|
|
58
|
+
*/
|
|
59
|
+
export const paymentSummarySchema = new Schema({
|
|
60
|
+
totalPayments: {
|
|
61
|
+
type: Number,
|
|
62
|
+
default: 0,
|
|
63
|
+
min: 0
|
|
64
|
+
},
|
|
65
|
+
totalAmountPaid: {
|
|
66
|
+
type: Number,
|
|
67
|
+
default: 0,
|
|
68
|
+
min: 0
|
|
69
|
+
},
|
|
70
|
+
lastPaymentDate: {
|
|
71
|
+
type: Date
|
|
72
|
+
},
|
|
73
|
+
lastPaymentAmount: {
|
|
74
|
+
type: Number,
|
|
75
|
+
min: 0
|
|
76
|
+
},
|
|
77
|
+
}, { _id: false });
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Payment Details Schema (for manual payments)
|
|
81
|
+
* Embedded in Transaction model
|
|
82
|
+
*/
|
|
83
|
+
export const paymentDetailsSchema = new Schema({
|
|
84
|
+
provider: { type: String },
|
|
85
|
+
walletNumber: { type: String },
|
|
86
|
+
walletType: { type: String },
|
|
87
|
+
trxId: { type: String },
|
|
88
|
+
bankName: { type: String },
|
|
89
|
+
accountNumber: { type: String },
|
|
90
|
+
accountName: { type: String },
|
|
91
|
+
proofUrl: { type: String },
|
|
92
|
+
}, { _id: false });
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Tenant Snapshot Schema
|
|
96
|
+
* Captures organization payment details at transaction time (audit trail)
|
|
97
|
+
*/
|
|
98
|
+
export const tenantSnapshotSchema = new Schema({
|
|
99
|
+
paymentInstructions: { type: String },
|
|
100
|
+
bkashNumber: { type: String },
|
|
101
|
+
nagadNumber: { type: String },
|
|
102
|
+
bankAccount: { type: String },
|
|
103
|
+
}, { _id: false });
|
|
104
|
+
|
|
105
|
+
export default {
|
|
106
|
+
currentPaymentSchema,
|
|
107
|
+
paymentSummarySchema,
|
|
108
|
+
paymentDetailsSchema,
|
|
109
|
+
tenantSnapshotSchema,
|
|
110
|
+
};
|