@classytic/revenue 1.0.2 → 1.1.2

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.
Files changed (53) hide show
  1. package/README.md +603 -486
  2. package/dist/application/services/index.d.ts +6 -0
  3. package/dist/application/services/index.js +3288 -0
  4. package/dist/application/services/index.js.map +1 -0
  5. package/dist/core/events.d.ts +455 -0
  6. package/dist/core/events.js +122 -0
  7. package/dist/core/events.js.map +1 -0
  8. package/dist/core/index.d.ts +12 -889
  9. package/dist/core/index.js +2361 -705
  10. package/dist/core/index.js.map +1 -1
  11. package/dist/enums/index.d.ts +54 -25
  12. package/dist/enums/index.js +143 -14
  13. package/dist/enums/index.js.map +1 -1
  14. package/dist/escrow.enums-CE0VQsfe.d.ts +76 -0
  15. package/dist/{index-BnJWVXuw.d.ts → index-DxIK0UmZ.d.ts} +281 -26
  16. package/dist/index-EnfKzDbs.d.ts +806 -0
  17. package/dist/{index-ChVD3P9k.d.ts → index-cLJBLUvx.d.ts} +55 -81
  18. package/dist/index.d.ts +16 -15
  19. package/dist/index.js +2583 -2066
  20. package/dist/index.js.map +1 -1
  21. package/dist/infrastructure/plugins/index.d.ts +267 -0
  22. package/dist/infrastructure/plugins/index.js +292 -0
  23. package/dist/infrastructure/plugins/index.js.map +1 -0
  24. package/dist/money-widWVD7r.d.ts +111 -0
  25. package/dist/payment.enums-C1BiGlRa.d.ts +69 -0
  26. package/dist/plugin-Bb9HOE10.d.ts +336 -0
  27. package/dist/providers/index.d.ts +19 -6
  28. package/dist/providers/index.js +22 -3
  29. package/dist/providers/index.js.map +1 -1
  30. package/dist/reconciliation/index.d.ts +215 -0
  31. package/dist/reconciliation/index.js +140 -0
  32. package/dist/reconciliation/index.js.map +1 -0
  33. package/dist/{retry-80lBCmSe.d.ts → retry-D4hFUwVk.d.ts} +1 -41
  34. package/dist/schemas/index.d.ts +1927 -166
  35. package/dist/schemas/index.js +357 -40
  36. package/dist/schemas/index.js.map +1 -1
  37. package/dist/schemas/validation.d.ts +87 -12
  38. package/dist/schemas/validation.js +71 -17
  39. package/dist/schemas/validation.js.map +1 -1
  40. package/dist/settlement.enums-ByC1x0ye.d.ts +130 -0
  41. package/dist/settlement.schema-CpamV7ZY.d.ts +343 -0
  42. package/dist/split.enums-DG3TxQf9.d.ts +42 -0
  43. package/dist/tax-CV8A0sxl.d.ts +60 -0
  44. package/dist/utils/index.d.ts +487 -13
  45. package/dist/utils/index.js +370 -235
  46. package/dist/utils/index.js.map +1 -1
  47. package/package.json +27 -13
  48. package/dist/actions-CwG-b7fR.d.ts +0 -519
  49. package/dist/services/index.d.ts +0 -3
  50. package/dist/services/index.js +0 -1632
  51. package/dist/services/index.js.map +0 -1
  52. package/dist/split.enums-Bh24jw8p.d.ts +0 -255
  53. package/dist/split.schema-DYVP7Wu2.d.ts +0 -958
@@ -1,4 +1,5 @@
1
- import { E as MonetizationCreateParams, F as MonetizationCreateResult, G as ActivateOptions, e as SubscriptionDocument, I as RenewalParams, J as CancelOptions, K as PauseOptions, N as ResumeOptions, Q as ListOptions, U as PaymentVerifyOptions, V as PaymentVerifyResult, X as PaymentStatusResult, Y as RefundOptions, Z as PaymentRefundResult, _ as WebhookResult, d as TransactionDocument, w as PaymentProviderInterface, $ as TransactionListResult, a0 as HoldOptions, a1 as ReleaseOptions, a2 as ReleaseResult, a3 as CancelHoldOptions, o as SplitRule, a4 as SplitResult, a5 as EscrowStatusResult } from './index-ChVD3P9k.js';
1
+ import { o as MonetizationCreateParams, p as MonetizationCreateResult, A as ActivateOptions, S as SubscriptionDocument, q as RenewalParams, r as CancelOptions, s as PauseOptions, t as ResumeOptions, u as ListOptions, v as PaymentVerifyOptions, w as PaymentVerifyResult, x as PaymentStatusResult, y as RefundOptions, z as PaymentRefundResult, B as WebhookResult, T as TransactionDocument, k as PaymentProviderInterface, E as TransactionListResult, H as HoldOptions, G as ReleaseOptions, I as ReleaseResult, J as CancelHoldOptions, c as SplitRule, K as SplitResult, N as EscrowStatusResult } from './index-cLJBLUvx.js';
2
+ import { S as SettlementDocument } from './settlement.schema-CpamV7ZY.js';
2
3
 
3
4
  /**
4
5
  * Dependency Injection Container
@@ -69,14 +70,31 @@ declare class Container {
69
70
  /**
70
71
  * Monetization Service
71
72
  * Uses DI container for all dependencies
73
+ *
74
+ * Architecture:
75
+ * - PluginManager: Wraps operations with lifecycle hooks (before/after)
76
+ * - EventBus: Fire-and-forget notifications for completed operations
72
77
  */
73
78
  declare class MonetizationService {
74
79
  private readonly models;
75
80
  private readonly providers;
76
81
  private readonly config;
77
- private readonly hooks;
82
+ private readonly plugins;
78
83
  private readonly logger;
84
+ private readonly events;
85
+ private readonly retryConfig;
86
+ private readonly circuitBreaker;
79
87
  constructor(container: Container);
88
+ /**
89
+ * Create plugin context for hook execution
90
+ * @private
91
+ */
92
+ private getPluginContext;
93
+ /**
94
+ * Execute provider call with retry and circuit breaker protection
95
+ * @private
96
+ */
97
+ private executeProviderCall;
80
98
  /**
81
99
  * Create a new monetization (purchase, subscription, or free item)
82
100
  *
@@ -88,8 +106,8 @@ declare class MonetizationService {
88
106
  * data: {
89
107
  * organizationId: '...',
90
108
  * customerId: '...',
91
- * referenceId: order._id,
92
- * referenceModel: 'Order',
109
+ * sourceId: order._id,
110
+ * sourceModel: 'Order',
93
111
  * },
94
112
  * planKey: 'one_time',
95
113
  * monetizationType: 'purchase',
@@ -102,8 +120,8 @@ declare class MonetizationService {
102
120
  * data: {
103
121
  * organizationId: '...',
104
122
  * customerId: '...',
105
- * referenceId: subscription._id,
106
- * referenceModel: 'Subscription',
123
+ * sourceId: subscription._id,
124
+ * sourceModel: 'Subscription',
107
125
  * },
108
126
  * planKey: 'monthly',
109
127
  * monetizationType: 'subscription',
@@ -174,11 +192,6 @@ declare class MonetizationService {
174
192
  * @private
175
193
  */
176
194
  private _calculatePeriodEnd;
177
- /**
178
- * Trigger event hook (fire-and-forget, non-blocking)
179
- * @private
180
- */
181
- private _triggerHook;
182
195
  }
183
196
 
184
197
  /**
@@ -192,14 +205,31 @@ declare class MonetizationService {
192
205
  /**
193
206
  * Payment Service
194
207
  * Uses DI container for all dependencies
208
+ *
209
+ * Architecture:
210
+ * - PluginManager: Wraps operations with lifecycle hooks (before/after)
211
+ * - EventBus: Fire-and-forget notifications for completed operations
195
212
  */
196
213
  declare class PaymentService {
197
214
  private readonly models;
198
215
  private readonly providers;
199
216
  private readonly config;
200
- private readonly hooks;
217
+ private readonly plugins;
201
218
  private readonly logger;
219
+ private readonly events;
220
+ private readonly retryConfig;
221
+ private readonly circuitBreaker;
202
222
  constructor(container: Container);
223
+ /**
224
+ * Create plugin context for hook execution
225
+ * @private
226
+ */
227
+ private getPluginContext;
228
+ /**
229
+ * Execute provider call with retry and circuit breaker protection
230
+ * @private
231
+ */
232
+ private executeProviderCall;
203
233
  /**
204
234
  * Verify a payment
205
235
  *
@@ -255,11 +285,6 @@ declare class PaymentService {
255
285
  * @returns Provider instance
256
286
  */
257
287
  getProvider(providerName: string): PaymentProviderInterface;
258
- /**
259
- * Trigger event hook (fire-and-forget, non-blocking)
260
- * @private
261
- */
262
- private _triggerHook;
263
288
  /**
264
289
  * Find transaction by sessionId, paymentIntentId, or transaction ID
265
290
  * @private
@@ -283,12 +308,22 @@ declare class PaymentService {
283
308
  /**
284
309
  * Transaction Service
285
310
  * Focused on core transaction lifecycle operations
311
+ *
312
+ * Architecture:
313
+ * - PluginManager: Wraps operations with lifecycle hooks (before/after)
314
+ * - EventBus: Fire-and-forget notifications for completed operations
286
315
  */
287
316
  declare class TransactionService {
288
317
  private readonly models;
289
- private readonly hooks;
318
+ private readonly plugins;
319
+ private readonly events;
290
320
  private readonly logger;
291
321
  constructor(container: Container);
322
+ /**
323
+ * Create plugin context for hook execution
324
+ * @private
325
+ */
326
+ private getPluginContext;
292
327
  /**
293
328
  * Get transaction by ID
294
329
  *
@@ -312,11 +347,6 @@ declare class TransactionService {
312
347
  * @returns Updated transaction
313
348
  */
314
349
  update(transactionId: string, updates: Partial<TransactionDocument>): Promise<TransactionDocument>;
315
- /**
316
- * Trigger event hook (fire-and-forget, non-blocking)
317
- * @private
318
- */
319
- private _triggerHook;
320
350
  }
321
351
 
322
352
  /**
@@ -327,11 +357,25 @@ declare class TransactionService {
327
357
  * Hold funds → Verify → Split/Deduct → Release to organization
328
358
  */
329
359
 
360
+ /**
361
+ * Escrow Service
362
+ * Uses DI container for all dependencies
363
+ *
364
+ * Architecture:
365
+ * - PluginManager: Wraps operations with lifecycle hooks (before/after)
366
+ * - EventBus: Fire-and-forget notifications for completed operations
367
+ */
330
368
  declare class EscrowService {
331
369
  private readonly models;
332
- private readonly hooks;
370
+ private readonly plugins;
333
371
  private readonly logger;
372
+ private readonly events;
334
373
  constructor(container: Container);
374
+ /**
375
+ * Create plugin context for hook execution
376
+ * @private
377
+ */
378
+ private getPluginContext;
335
379
  /**
336
380
  * Hold funds in escrow
337
381
  *
@@ -372,7 +416,218 @@ declare class EscrowService {
372
416
  * @returns Escrow status
373
417
  */
374
418
  getStatus(transactionId: string): Promise<EscrowStatusResult>;
375
- private _triggerHook;
376
419
  }
377
420
 
378
- export { Container as C, EscrowService as E, MonetizationService as M, PaymentService as P, TransactionService as T };
421
+ /**
422
+ * Settlement Service
423
+ * @classytic/revenue
424
+ *
425
+ * Tracks payouts from platform to vendors/affiliates/partners
426
+ * Manages the flow: Transaction → Split → Settlement → Bank Transfer
427
+ */
428
+
429
+ interface CreateFromSplitsOptions {
430
+ scheduledAt?: Date;
431
+ payoutMethod?: 'bank_transfer' | 'mobile_wallet' | 'platform_balance' | 'crypto' | 'manual';
432
+ metadata?: Record<string, unknown>;
433
+ }
434
+ interface ScheduleSettlementParams {
435
+ organizationId: string;
436
+ recipientId: string;
437
+ recipientType: 'platform' | 'organization' | 'user' | 'affiliate' | 'partner';
438
+ type: 'split_payout' | 'platform_withdrawal' | 'manual_payout' | 'escrow_release';
439
+ amount: number;
440
+ currency?: string;
441
+ payoutMethod: 'bank_transfer' | 'mobile_wallet' | 'platform_balance' | 'crypto' | 'manual';
442
+ sourceTransactionIds?: string[];
443
+ sourceSplitIds?: string[];
444
+ scheduledAt?: Date;
445
+ bankTransferDetails?: {
446
+ accountNumber?: string;
447
+ accountName?: string;
448
+ bankName?: string;
449
+ routingNumber?: string;
450
+ swiftCode?: string;
451
+ iban?: string;
452
+ };
453
+ mobileWalletDetails?: {
454
+ provider?: string;
455
+ phoneNumber?: string;
456
+ accountNumber?: string;
457
+ };
458
+ cryptoDetails?: {
459
+ network?: string;
460
+ walletAddress?: string;
461
+ };
462
+ notes?: string;
463
+ metadata?: Record<string, unknown>;
464
+ }
465
+ interface ProcessOptions {
466
+ limit?: number;
467
+ organizationId?: string;
468
+ payoutMethod?: string;
469
+ dryRun?: boolean;
470
+ }
471
+ interface ProcessResult {
472
+ processed: number;
473
+ succeeded: number;
474
+ failed: number;
475
+ settlements: SettlementDocument[];
476
+ errors: Array<{
477
+ settlementId: string;
478
+ error: string;
479
+ }>;
480
+ }
481
+ interface CompletionDetails {
482
+ transferReference?: string;
483
+ transferredAt?: Date;
484
+ transactionHash?: string;
485
+ notes?: string;
486
+ metadata?: Record<string, unknown>;
487
+ }
488
+ interface SettlementFilters {
489
+ organizationId?: string;
490
+ recipientId?: string;
491
+ status?: string | string[];
492
+ type?: string;
493
+ payoutMethod?: string;
494
+ scheduledAfter?: Date;
495
+ scheduledBefore?: Date;
496
+ limit?: number;
497
+ skip?: number;
498
+ sort?: Record<string, 1 | -1>;
499
+ }
500
+ interface SummaryOptions {
501
+ organizationId?: string;
502
+ startDate?: Date;
503
+ endDate?: Date;
504
+ }
505
+ interface SettlementSummary {
506
+ recipientId: string;
507
+ totalPending: number;
508
+ totalProcessing: number;
509
+ totalCompleted: number;
510
+ totalFailed: number;
511
+ amountPending: number;
512
+ amountCompleted: number;
513
+ amountFailed: number;
514
+ currency: string;
515
+ lastSettlementDate?: Date;
516
+ settlements: {
517
+ pending: number;
518
+ processing: number;
519
+ completed: number;
520
+ failed: number;
521
+ cancelled: number;
522
+ };
523
+ }
524
+ /**
525
+ * Settlement Service
526
+ *
527
+ * Handles payout tracking after splits are released from escrow:
528
+ *
529
+ * Flow:
530
+ * 1. Transaction has splits → 2. Escrow released → 3. Create settlements → 4. Process payouts → 5. Mark complete
531
+ *
532
+ * @example
533
+ * ```typescript
534
+ * // Auto-create settlements from transaction splits
535
+ * await revenue.settlement.createFromSplits(transactionId);
536
+ *
537
+ * // Schedule a manual payout
538
+ * await revenue.settlement.schedule({
539
+ * recipientId: vendorId,
540
+ * amount: 8500,
541
+ * payoutMethod: 'bank_transfer',
542
+ * });
543
+ *
544
+ * // Process pending payouts
545
+ * const result = await revenue.settlement.processPending({ limit: 100 });
546
+ *
547
+ * // Mark as completed after bank confirms
548
+ * await revenue.settlement.complete(settlementId, { transferReference: 'TRF123' });
549
+ * ```
550
+ */
551
+ /**
552
+ * Settlement Service
553
+ * Uses DI container for all dependencies
554
+ *
555
+ * Architecture:
556
+ * - PluginManager: Wraps operations with lifecycle hooks (before/after)
557
+ * - EventBus: Fire-and-forget notifications for completed operations
558
+ */
559
+ declare class SettlementService {
560
+ private readonly models;
561
+ private readonly plugins;
562
+ private readonly logger;
563
+ private readonly events;
564
+ constructor(container: Container);
565
+ /**
566
+ * Create settlements from transaction splits
567
+ * Typically called after escrow is released
568
+ *
569
+ * @param transactionId - Transaction ID with splits
570
+ * @param options - Creation options
571
+ * @returns Array of created settlements
572
+ */
573
+ createFromSplits(transactionId: string, options?: CreateFromSplitsOptions): Promise<SettlementDocument[]>;
574
+ /**
575
+ * Schedule a payout
576
+ *
577
+ * @param params - Settlement parameters
578
+ * @returns Created settlement
579
+ */
580
+ schedule(params: ScheduleSettlementParams): Promise<SettlementDocument>;
581
+ /**
582
+ * Process pending settlements
583
+ * Batch process settlements that are due
584
+ *
585
+ * @param options - Processing options
586
+ * @returns Processing result
587
+ */
588
+ processPending(options?: ProcessOptions): Promise<ProcessResult>;
589
+ /**
590
+ * Mark settlement as completed
591
+ * Call this after bank confirms the transfer
592
+ *
593
+ * @param settlementId - Settlement ID
594
+ * @param details - Completion details
595
+ * @returns Updated settlement
596
+ */
597
+ complete(settlementId: string, details?: CompletionDetails): Promise<SettlementDocument>;
598
+ /**
599
+ * Mark settlement as failed
600
+ *
601
+ * @param settlementId - Settlement ID
602
+ * @param reason - Failure reason
603
+ * @returns Updated settlement
604
+ */
605
+ fail(settlementId: string, reason: string, options?: {
606
+ code?: string;
607
+ retry?: boolean;
608
+ }): Promise<SettlementDocument>;
609
+ /**
610
+ * List settlements with filters
611
+ *
612
+ * @param filters - Query filters
613
+ * @returns Settlements
614
+ */
615
+ list(filters?: SettlementFilters): Promise<SettlementDocument[]>;
616
+ /**
617
+ * Get payout summary for recipient
618
+ *
619
+ * @param recipientId - Recipient ID
620
+ * @param options - Summary options
621
+ * @returns Settlement summary
622
+ */
623
+ getSummary(recipientId: string, options?: SummaryOptions): Promise<SettlementSummary>;
624
+ /**
625
+ * Get settlement by ID
626
+ *
627
+ * @param settlementId - Settlement ID
628
+ * @returns Settlement
629
+ */
630
+ get(settlementId: string): Promise<SettlementDocument>;
631
+ }
632
+
633
+ export { Container as C, EscrowService as E, MonetizationService as M, PaymentService as P, SettlementService as S, TransactionService as T };