@classytic/revenue 0.0.21 → 0.0.23

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/revenue.d.ts CHANGED
@@ -1,296 +1,350 @@
1
- /**
2
- * TypeScript definitions for @classytic/revenue
3
- * Enterprise Revenue Management System
4
- *
5
- * Thin, focused, production-ready library with smart defaults.
6
- *
7
- * @version 1.0.0
8
- */
9
-
10
- import { Schema, Model, Document } from 'mongoose';
11
-
12
- // ============ CORE API ============
13
-
14
- // Container
15
- export class Container {
16
- register(name: string, implementation: any, options?: { singleton?: boolean; factory?: boolean }): this;
17
- singleton(name: string, implementation: any): this;
18
- transient(name: string, factory: Function): this;
19
- get(name: string): any;
20
- has(name: string): boolean;
21
- keys(): string[];
22
- clear(): void;
23
- createScope(): Container;
24
- }
25
-
26
- // Provider System
27
- export interface PaymentIntentParams {
28
- amount: number;
29
- currency?: string;
30
- metadata?: Record<string, any>;
31
- }
32
-
33
- export class PaymentIntent {
34
- id: string;
35
- provider: string;
36
- status: string;
37
- amount: number;
38
- currency: string;
39
- metadata: Record<string, any>;
40
- clientSecret?: string;
41
- paymentUrl?: string;
42
- instructions?: string;
43
- raw?: any;
44
- }
45
-
46
- export class PaymentResult {
47
- id: string;
48
- provider: string;
49
- status: string;
50
- amount: number;
51
- currency: string;
52
- paidAt?: Date;
53
- metadata: Record<string, any>;
54
- raw?: any;
55
- }
56
-
57
- export class RefundResult {
58
- id: string;
59
- provider: string;
60
- status: string;
61
- amount: number;
62
- currency: string;
63
- refundedAt?: Date;
64
- reason?: string;
65
- metadata: Record<string, any>;
66
- raw?: any;
67
- }
68
-
69
- export class WebhookEvent {
70
- id: string;
71
- provider: string;
72
- type: string;
73
- data: any;
74
- createdAt: Date;
75
- raw?: any;
76
- }
77
-
78
- export abstract class PaymentProvider {
79
- name: string;
80
- config: any;
81
-
82
- createIntent(params: PaymentIntentParams): Promise<PaymentIntent>;
83
- verifyPayment(intentId: string): Promise<PaymentResult>;
84
- getStatus(intentId: string): Promise<PaymentResult>;
85
- refund(paymentId: string, amount?: number, options?: any): Promise<RefundResult>;
86
- handleWebhook(payload: any, headers?: any): Promise<WebhookEvent>;
87
- verifyWebhookSignature(payload: any, signature: string): boolean;
88
- getCapabilities(): {
89
- supportsWebhooks: boolean;
90
- supportsRefunds: boolean;
91
- supportsPartialRefunds: boolean;
92
- requiresManualVerification: boolean;
93
- };
94
- }
95
-
96
- // Note: ManualProvider moved to @classytic/revenue-manual (separate package)
97
-
98
- // Services
99
- export class SubscriptionService {
100
- constructor(container: Container);
101
-
102
- create(params: {
103
- data: any;
104
- planKey: string;
105
- amount: number;
106
- currency?: string;
107
- gateway?: string;
108
- entity?: string;
109
- monetizationType?: 'free' | 'subscription' | 'purchase';
110
- paymentData?: any;
111
- metadata?: Record<string, any>;
112
- idempotencyKey?: string;
113
- }): Promise<{ subscription: any; transaction: any; paymentIntent: PaymentIntent | null }>;
114
-
115
- activate(subscriptionId: string, options?: { timestamp?: Date }): Promise<any>;
116
- renew(subscriptionId: string, params?: {
117
- gateway?: string;
118
- entity?: string;
119
- paymentData?: any;
120
- metadata?: Record<string, any>;
121
- idempotencyKey?: string;
122
- }): Promise<{ subscription: any; transaction: any; paymentIntent: PaymentIntent }>;
123
- cancel(subscriptionId: string, options?: { immediate?: boolean; reason?: string }): Promise<any>;
124
- pause(subscriptionId: string, options?: { reason?: string }): Promise<any>;
125
- resume(subscriptionId: string, options?: { extendPeriod?: boolean }): Promise<any>;
126
- list(filters?: any, options?: any): Promise<any[]>;
127
- get(subscriptionId: string): Promise<any>;
128
- }
129
-
130
- export class PaymentService {
131
- constructor(container: Container);
132
-
133
- verify(paymentIntentId: string, options?: { verifiedBy?: string }): Promise<{ transaction: any; paymentResult: PaymentResult; status: string }>;
134
- getStatus(paymentIntentId: string): Promise<{ transaction: any; paymentResult: PaymentResult; status: string; provider: string }>;
135
- refund(paymentId: string, amount?: number, options?: { reason?: string }): Promise<{ transaction: any; refundResult: RefundResult; status: string }>;
136
- handleWebhook(providerName: string, payload: any, headers?: any): Promise<{ event: WebhookEvent; transaction: any; status: string }>;
137
- list(filters?: any, options?: any): Promise<any[]>;
138
- get(transactionId: string): Promise<any>;
139
- getProvider(providerName: string): PaymentProvider;
140
- }
141
-
142
- export class TransactionService {
143
- constructor(container: Container);
144
-
145
- get(transactionId: string): Promise<any>;
146
- list(filters?: any, options?: any): Promise<{ transactions: any[]; total: number; page: number; limit: number; pages: number }>;
147
- update(transactionId: string, updates: any): Promise<any>;
148
- }
149
-
150
- // Error Classes
151
- export class RevenueError extends Error {
152
- code: string;
153
- retryable: boolean;
154
- metadata: Record<string, any>;
155
- toJSON(): { name: string; message: string; code: string; retryable: boolean; metadata: Record<string, any> };
156
- }
157
-
158
- export class ConfigurationError extends RevenueError {}
159
- export class ModelNotRegisteredError extends ConfigurationError {}
160
- export class ProviderError extends RevenueError {}
161
- export class ProviderNotFoundError extends ProviderError {}
162
- export class ProviderCapabilityError extends ProviderError {}
163
- export class PaymentIntentCreationError extends ProviderError {}
164
- export class PaymentVerificationError extends ProviderError {}
165
- export class NotFoundError extends RevenueError {}
166
- export class SubscriptionNotFoundError extends NotFoundError {}
167
- export class TransactionNotFoundError extends NotFoundError {}
168
- export class ValidationError extends RevenueError {}
169
- export class InvalidAmountError extends ValidationError {}
170
- export class MissingRequiredFieldError extends ValidationError {}
171
- export class StateError extends RevenueError {}
172
- export class AlreadyVerifiedError extends StateError {}
173
- export class InvalidStateTransitionError extends StateError {}
174
- export class SubscriptionNotActiveError extends StateError {}
175
- export class OperationError extends RevenueError {}
176
- export class RefundNotSupportedError extends OperationError {}
177
- export class RefundError extends OperationError {}
178
-
179
- export function isRetryable(error: Error): boolean;
180
- export function isRevenueError(error: Error): boolean;
181
-
182
- // Revenue Instance (Immutable)
183
- export interface Revenue {
184
- readonly container: Container;
185
- readonly providers: Readonly<Record<string, PaymentProvider>>;
186
- readonly config: Readonly<any>;
187
-
188
- readonly subscriptions: SubscriptionService;
189
- readonly payments: PaymentService;
190
- readonly transactions: TransactionService;
191
-
192
- getProvider(name: string): PaymentProvider;
193
- }
194
-
195
- export interface RevenueOptions {
196
- models: {
197
- Transaction: Model<any>;
198
- Subscription?: Model<any>;
199
- [key: string]: Model<any> | undefined;
200
- };
201
- providers?: Record<string, PaymentProvider>;
202
- hooks?: Record<string, Function[]>;
203
- config?: {
204
- /**
205
- * Maps logical entity identifiers to custom transaction category names
206
- *
207
- * Entity identifiers are NOT database model names - they are logical identifiers
208
- * you choose to organize your business logic.
209
- *
210
- * @example
211
- * categoryMappings: {
212
- * Order: 'order_subscription', // Customer orders
213
- * PlatformSubscription: 'platform_subscription', // Tenant/org subscriptions
214
- * TenantUpgrade: 'tenant_upgrade', // Tenant upgrades
215
- * Membership: 'gym_membership', // User memberships
216
- * Enrollment: 'course_enrollment', // Course enrollments
217
- * }
218
- *
219
- * If not specified, falls back to library defaults: 'subscription' or 'purchase'
220
- */
221
- categoryMappings?: Record<string, string>;
222
- [key: string]: any;
223
- };
224
- logger?: Console | any;
225
- }
226
-
227
- export function createRevenue(options: RevenueOptions): Revenue;
228
-
229
- // ============ ENUMS ============
230
-
231
- export const TRANSACTION_STATUS: {
232
- PENDING: 'pending';
233
- PAYMENT_INITIATED: 'payment_initiated';
234
- PROCESSING: 'processing';
235
- REQUIRES_ACTION: 'requires_action';
236
- VERIFIED: 'verified';
237
- COMPLETED: 'completed';
238
- FAILED: 'failed';
239
- CANCELLED: 'cancelled';
240
- EXPIRED: 'expired';
241
- REFUNDED: 'refunded';
242
- PARTIALLY_REFUNDED: 'partially_refunded';
243
- };
244
-
245
- export const PAYMENT_GATEWAY_TYPE: {
246
- MANUAL: 'manual';
247
- STRIPE: 'stripe';
248
- SSLCOMMERZ: 'sslcommerz';
249
- };
250
-
251
- export const SUBSCRIPTION_STATUS: {
252
- ACTIVE: 'active';
253
- PAUSED: 'paused';
254
- CANCELLED: 'cancelled';
255
- EXPIRED: 'expired';
256
- PENDING: 'pending';
257
- INACTIVE: 'inactive';
258
- };
259
-
260
- export const PLAN_KEYS: {
261
- MONTHLY: 'monthly';
262
- QUARTERLY: 'quarterly';
263
- YEARLY: 'yearly';
264
- };
265
-
266
- export const MONETIZATION_TYPES: {
267
- FREE: 'free';
268
- PURCHASE: 'purchase';
269
- SUBSCRIPTION: 'subscription';
270
- };
271
-
272
- // ============ SCHEMAS ============
273
-
274
- export const currentPaymentSchema: Schema;
275
- export const paymentSummarySchema: Schema;
276
- export const subscriptionInfoSchema: Schema;
277
- export const subscriptionPlanSchema: Schema;
278
- export const gatewaySchema: Schema;
279
- export const commissionSchema: Schema;
280
- export const paymentDetailsSchema: Schema;
281
-
282
- // ============ UTILITIES ============
283
-
284
- export const logger: Console | any;
285
- export function setLogger(logger: Console | any): void;
286
-
287
- // ============ DEFAULT EXPORT ============
288
-
289
- declare const _default: {
290
- createRevenue: typeof createRevenue;
291
- PaymentProvider: typeof PaymentProvider;
292
- RevenueError: typeof RevenueError;
293
- Container: typeof Container;
294
- };
295
-
296
- export default _default;
1
+ /**
2
+ * TypeScript definitions for @classytic/revenue
3
+ * Enterprise Revenue Management System
4
+ *
5
+ * Thin, focused, production-ready library with smart defaults.
6
+ *
7
+ * @version 1.0.0
8
+ */
9
+
10
+ import { Schema, Model, Document } from 'mongoose';
11
+
12
+ // ============ CORE API ============
13
+
14
+ // Container
15
+ export class Container {
16
+ register(name: string, implementation: any, options?: { singleton?: boolean; factory?: boolean }): this;
17
+ singleton(name: string, implementation: any): this;
18
+ transient(name: string, factory: Function): this;
19
+ get(name: string): any;
20
+ has(name: string): boolean;
21
+ keys(): string[];
22
+ clear(): void;
23
+ createScope(): Container;
24
+ }
25
+
26
+ // Provider System
27
+ export interface PaymentIntentParams {
28
+ amount: number;
29
+ currency?: string;
30
+ metadata?: Record<string, any>;
31
+ }
32
+
33
+ export class PaymentIntent {
34
+ id: string;
35
+ provider: string;
36
+ status: string;
37
+ amount: number;
38
+ currency: string;
39
+ metadata: Record<string, any>;
40
+ clientSecret?: string;
41
+ paymentUrl?: string;
42
+ instructions?: string;
43
+ raw?: any;
44
+ }
45
+
46
+ export class PaymentResult {
47
+ id: string;
48
+ provider: string;
49
+ status: string;
50
+ amount: number;
51
+ currency: string;
52
+ paidAt?: Date;
53
+ metadata: Record<string, any>;
54
+ raw?: any;
55
+ }
56
+
57
+ export class RefundResult {
58
+ id: string;
59
+ provider: string;
60
+ status: string;
61
+ amount: number;
62
+ currency: string;
63
+ refundedAt?: Date;
64
+ reason?: string;
65
+ metadata: Record<string, any>;
66
+ raw?: any;
67
+ }
68
+
69
+ export class WebhookEvent {
70
+ id: string;
71
+ provider: string;
72
+ type: string;
73
+ data: any;
74
+ createdAt: Date;
75
+ raw?: any;
76
+ }
77
+
78
+ export abstract class PaymentProvider {
79
+ name: string;
80
+ config: any;
81
+
82
+ createIntent(params: PaymentIntentParams): Promise<PaymentIntent>;
83
+ verifyPayment(intentId: string): Promise<PaymentResult>;
84
+ getStatus(intentId: string): Promise<PaymentResult>;
85
+ refund(paymentId: string, amount?: number, options?: any): Promise<RefundResult>;
86
+ handleWebhook(payload: any, headers?: any): Promise<WebhookEvent>;
87
+ verifyWebhookSignature(payload: any, signature: string): boolean;
88
+ getCapabilities(): {
89
+ supportsWebhooks: boolean;
90
+ supportsRefunds: boolean;
91
+ supportsPartialRefunds: boolean;
92
+ requiresManualVerification: boolean;
93
+ };
94
+ }
95
+
96
+ // Note: ManualProvider moved to @classytic/revenue-manual (separate package)
97
+
98
+ // Services
99
+ export class SubscriptionService {
100
+ constructor(container: Container);
101
+
102
+ create(params: {
103
+ data: any;
104
+ planKey: string;
105
+ amount: number;
106
+ currency?: string;
107
+ gateway?: string;
108
+ entity?: string;
109
+ monetizationType?: 'free' | 'subscription' | 'purchase';
110
+ paymentData?: any;
111
+ metadata?: Record<string, any>;
112
+ idempotencyKey?: string;
113
+ }): Promise<{ subscription: any; transaction: any; paymentIntent: PaymentIntent | null }>;
114
+
115
+ activate(subscriptionId: string, options?: { timestamp?: Date }): Promise<any>;
116
+ renew(subscriptionId: string, params?: {
117
+ gateway?: string;
118
+ entity?: string;
119
+ paymentData?: any;
120
+ metadata?: Record<string, any>;
121
+ idempotencyKey?: string;
122
+ }): Promise<{ subscription: any; transaction: any; paymentIntent: PaymentIntent }>;
123
+ cancel(subscriptionId: string, options?: { immediate?: boolean; reason?: string }): Promise<any>;
124
+ pause(subscriptionId: string, options?: { reason?: string }): Promise<any>;
125
+ resume(subscriptionId: string, options?: { extendPeriod?: boolean }): Promise<any>;
126
+ list(filters?: any, options?: any): Promise<any[]>;
127
+ get(subscriptionId: string): Promise<any>;
128
+ }
129
+
130
+ export class PaymentService {
131
+ constructor(container: Container);
132
+
133
+ verify(paymentIntentId: string, options?: { verifiedBy?: string }): Promise<{ transaction: any; paymentResult: PaymentResult; status: string }>;
134
+ getStatus(paymentIntentId: string): Promise<{ transaction: any; paymentResult: PaymentResult; status: string; provider: string }>;
135
+ refund(paymentId: string, amount?: number, options?: { reason?: string }): Promise<{ transaction: any; refundTransaction: any; refundResult: RefundResult; status: string }>;
136
+ handleWebhook(providerName: string, payload: any, headers?: any): Promise<{ event: WebhookEvent; transaction: any; status: string }>;
137
+ list(filters?: any, options?: any): Promise<any[]>;
138
+ get(transactionId: string): Promise<any>;
139
+ getProvider(providerName: string): PaymentProvider;
140
+ }
141
+
142
+ export class TransactionService {
143
+ constructor(container: Container);
144
+
145
+ get(transactionId: string): Promise<any>;
146
+ list(filters?: any, options?: any): Promise<{ transactions: any[]; total: number; page: number; limit: number; pages: number }>;
147
+ update(transactionId: string, updates: any): Promise<any>;
148
+ }
149
+
150
+ // Error Classes
151
+ export class RevenueError extends Error {
152
+ code: string;
153
+ retryable: boolean;
154
+ metadata: Record<string, any>;
155
+ toJSON(): { name: string; message: string; code: string; retryable: boolean; metadata: Record<string, any> };
156
+ }
157
+
158
+ export class ConfigurationError extends RevenueError {}
159
+ export class ModelNotRegisteredError extends ConfigurationError {}
160
+ export class ProviderError extends RevenueError {}
161
+ export class ProviderNotFoundError extends ProviderError {}
162
+ export class ProviderCapabilityError extends ProviderError {}
163
+ export class PaymentIntentCreationError extends ProviderError {}
164
+ export class PaymentVerificationError extends ProviderError {}
165
+ export class NotFoundError extends RevenueError {}
166
+ export class SubscriptionNotFoundError extends NotFoundError {}
167
+ export class TransactionNotFoundError extends NotFoundError {}
168
+ export class ValidationError extends RevenueError {}
169
+ export class InvalidAmountError extends ValidationError {}
170
+ export class MissingRequiredFieldError extends ValidationError {}
171
+ export class StateError extends RevenueError {}
172
+ export class AlreadyVerifiedError extends StateError {}
173
+ export class InvalidStateTransitionError extends StateError {}
174
+ export class SubscriptionNotActiveError extends StateError {}
175
+ export class OperationError extends RevenueError {}
176
+ export class RefundNotSupportedError extends OperationError {}
177
+ export class RefundError extends OperationError {}
178
+
179
+ export function isRetryable(error: Error): boolean;
180
+ export function isRevenueError(error: Error): boolean;
181
+
182
+ // Revenue Instance (Immutable)
183
+ export interface Revenue {
184
+ readonly container: Container;
185
+ readonly providers: Readonly<Record<string, PaymentProvider>>;
186
+ readonly config: Readonly<any>;
187
+
188
+ readonly subscriptions: SubscriptionService;
189
+ readonly payments: PaymentService;
190
+ readonly transactions: TransactionService;
191
+
192
+ getProvider(name: string): PaymentProvider;
193
+ }
194
+
195
+ export interface RevenueOptions {
196
+ models: {
197
+ Transaction: Model<any>;
198
+ Subscription?: Model<any>;
199
+ [key: string]: Model<any> | undefined;
200
+ };
201
+ providers?: Record<string, PaymentProvider>;
202
+ hooks?: Record<string, Function[]>;
203
+ config?: {
204
+ /**
205
+ * Maps logical entity identifiers to custom transaction category names
206
+ *
207
+ * Entity identifiers are NOT database model names - they are logical identifiers
208
+ * you choose to organize your business logic.
209
+ *
210
+ * @example
211
+ * categoryMappings: {
212
+ * Order: 'order_subscription', // Customer orders
213
+ * PlatformSubscription: 'platform_subscription', // Tenant/org subscriptions
214
+ * TenantUpgrade: 'tenant_upgrade', // Tenant upgrades
215
+ * Membership: 'gym_membership', // User memberships
216
+ * Enrollment: 'course_enrollment', // Course enrollments
217
+ * }
218
+ *
219
+ * If not specified, falls back to library defaults: 'subscription' or 'purchase'
220
+ */
221
+ categoryMappings?: Record<string, string>;
222
+
223
+ /**
224
+ * Maps transaction types to income/expense for your accounting system
225
+ *
226
+ * Allows you to control how different transaction types are recorded:
227
+ * - 'income': Money coming in (payments, subscriptions)
228
+ * - 'expense': Money going out (refunds)
229
+ *
230
+ * @example
231
+ * transactionTypeMapping: {
232
+ * subscription: 'income',
233
+ * subscription_renewal: 'income',
234
+ * purchase: 'income',
235
+ * refund: 'expense',
236
+ * }
237
+ *
238
+ * If not specified, library defaults to 'income' for all payment transactions
239
+ */
240
+ transactionTypeMapping?: Record<string, 'income' | 'expense'>;
241
+
242
+ /**
243
+ * Commission rates by category (0 to 1)
244
+ *
245
+ * Automatically calculates platform commission for each transaction.
246
+ * Gateway fees are automatically deducted from gross commission.
247
+ *
248
+ * @example
249
+ * commissionRates: {
250
+ * 'course_enrollment': 0.10, // 10% commission
251
+ * 'product_order': 0.05, // 5% commission
252
+ * 'gym_membership': 0, // No commission
253
+ * }
254
+ */
255
+ commissionRates?: Record<string, number>;
256
+
257
+ /**
258
+ * Gateway fee rates by provider (0 to 1)
259
+ *
260
+ * Gateway fees are deducted from gross commission.
261
+ *
262
+ * @example
263
+ * gatewayFeeRates: {
264
+ * 'bkash': 0.018, // 1.8% fee
265
+ * 'stripe': 0.029, // 2.9% fee
266
+ * 'manual': 0, // No fee
267
+ * }
268
+ */
269
+ gatewayFeeRates?: Record<string, number>;
270
+
271
+ [key: string]: any;
272
+ };
273
+ logger?: Console | any;
274
+ }
275
+
276
+ export function createRevenue(options: RevenueOptions): Revenue;
277
+
278
+ // ============ ENUMS ============
279
+
280
+ export const TRANSACTION_TYPE: {
281
+ INCOME: 'income';
282
+ EXPENSE: 'expense';
283
+ };
284
+
285
+ export const TRANSACTION_STATUS: {
286
+ PENDING: 'pending';
287
+ PAYMENT_INITIATED: 'payment_initiated';
288
+ PROCESSING: 'processing';
289
+ REQUIRES_ACTION: 'requires_action';
290
+ VERIFIED: 'verified';
291
+ COMPLETED: 'completed';
292
+ FAILED: 'failed';
293
+ CANCELLED: 'cancelled';
294
+ EXPIRED: 'expired';
295
+ REFUNDED: 'refunded';
296
+ PARTIALLY_REFUNDED: 'partially_refunded';
297
+ };
298
+
299
+ export const PAYMENT_GATEWAY_TYPE: {
300
+ MANUAL: 'manual';
301
+ STRIPE: 'stripe';
302
+ SSLCOMMERZ: 'sslcommerz';
303
+ };
304
+
305
+ export const SUBSCRIPTION_STATUS: {
306
+ ACTIVE: 'active';
307
+ PAUSED: 'paused';
308
+ CANCELLED: 'cancelled';
309
+ EXPIRED: 'expired';
310
+ PENDING: 'pending';
311
+ INACTIVE: 'inactive';
312
+ };
313
+
314
+ export const PLAN_KEYS: {
315
+ MONTHLY: 'monthly';
316
+ QUARTERLY: 'quarterly';
317
+ YEARLY: 'yearly';
318
+ };
319
+
320
+ export const MONETIZATION_TYPES: {
321
+ FREE: 'free';
322
+ PURCHASE: 'purchase';
323
+ SUBSCRIPTION: 'subscription';
324
+ };
325
+
326
+ // ============ SCHEMAS ============
327
+
328
+ export const currentPaymentSchema: Schema;
329
+ export const paymentSummarySchema: Schema;
330
+ export const subscriptionInfoSchema: Schema;
331
+ export const subscriptionPlanSchema: Schema;
332
+ export const gatewaySchema: Schema;
333
+ export const commissionSchema: Schema;
334
+ export const paymentDetailsSchema: Schema;
335
+
336
+ // ============ UTILITIES ============
337
+
338
+ export const logger: Console | any;
339
+ export function setLogger(logger: Console | any): void;
340
+
341
+ // ============ DEFAULT EXPORT ============
342
+
343
+ declare const _default: {
344
+ createRevenue: typeof createRevenue;
345
+ PaymentProvider: typeof PaymentProvider;
346
+ RevenueError: typeof RevenueError;
347
+ Container: typeof Container;
348
+ };
349
+
350
+ export default _default;