@classytic/revenue 1.1.2 → 1.1.4

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 (89) hide show
  1. package/README.md +8 -7
  2. package/dist/application/services/index.d.mts +4 -0
  3. package/dist/application/services/index.mjs +3 -0
  4. package/dist/base-CsTlVQJe.d.mts +136 -0
  5. package/dist/base-DCoyIUj6.mjs +152 -0
  6. package/dist/category-resolver-DV83N8ok.mjs +284 -0
  7. package/dist/commission-split-BzB8cd39.mjs +485 -0
  8. package/dist/core/events.d.mts +294 -0
  9. package/dist/core/events.mjs +100 -0
  10. package/dist/core/index.d.mts +9 -0
  11. package/dist/core/index.mjs +8 -0
  12. package/dist/enums/index.d.mts +157 -0
  13. package/dist/enums/index.mjs +56 -0
  14. package/dist/errors-rRdOqnWx.d.mts +787 -0
  15. package/dist/escrow.enums-CZGrrdg7.mjs +101 -0
  16. package/dist/{escrow.enums-CE0VQsfe.d.ts → escrow.enums-DwdLuuve.d.mts} +30 -28
  17. package/dist/idempotency-DaYcUGY1.mjs +172 -0
  18. package/dist/index-Dsp7H5Wb.d.mts +471 -0
  19. package/dist/index.d.mts +9 -0
  20. package/dist/index.mjs +38 -0
  21. package/dist/infrastructure/plugins/{index.d.ts → index.d.mts} +81 -109
  22. package/dist/infrastructure/plugins/index.mjs +345 -0
  23. package/dist/money-CvrDOijQ.mjs +271 -0
  24. package/dist/money-DPG8AtJ8.d.mts +112 -0
  25. package/dist/{payment.enums-C1BiGlRa.d.ts → payment.enums-HAuAS9Pp.d.mts} +14 -13
  26. package/dist/payment.enums-tEFVa-Xp.mjs +69 -0
  27. package/dist/plugin-BbK0OVHy.d.mts +327 -0
  28. package/dist/plugin-Cd_V04Em.mjs +210 -0
  29. package/dist/providers/index.d.mts +3 -0
  30. package/dist/providers/index.mjs +3 -0
  31. package/dist/reconciliation/{index.d.ts → index.d.mts} +90 -112
  32. package/dist/reconciliation/index.mjs +192 -0
  33. package/dist/retry-HHCOXYdn.d.mts +186 -0
  34. package/dist/revenue-BhdS7nXh.mjs +553 -0
  35. package/dist/schemas/index.d.mts +2665 -0
  36. package/dist/schemas/index.mjs +717 -0
  37. package/dist/schemas/validation.d.mts +375 -0
  38. package/dist/schemas/validation.mjs +325 -0
  39. package/dist/{settlement.enums-ByC1x0ye.d.ts → settlement.enums-DFhkqZEY.d.mts} +31 -29
  40. package/dist/settlement.schema-DnNSFpGd.d.mts +344 -0
  41. package/dist/settlement.service-DjzAjezU.d.mts +594 -0
  42. package/dist/settlement.service-DmdKv0Zu.mjs +2511 -0
  43. package/dist/split.enums-BrjabxIX.mjs +86 -0
  44. package/dist/split.enums-DmskfLOM.d.mts +43 -0
  45. package/dist/tax-BoCt5cEd.d.mts +61 -0
  46. package/dist/tax-EQ15DO81.mjs +162 -0
  47. package/dist/transaction.enums-pCyMFT4Z.mjs +96 -0
  48. package/dist/utils/{index.d.ts → index.d.mts} +91 -161
  49. package/dist/utils/index.mjs +346 -0
  50. package/package.json +39 -37
  51. package/dist/application/services/index.d.ts +0 -6
  52. package/dist/application/services/index.js +0 -3288
  53. package/dist/application/services/index.js.map +0 -1
  54. package/dist/core/events.d.ts +0 -455
  55. package/dist/core/events.js +0 -122
  56. package/dist/core/events.js.map +0 -1
  57. package/dist/core/index.d.ts +0 -13
  58. package/dist/core/index.js +0 -4591
  59. package/dist/core/index.js.map +0 -1
  60. package/dist/enums/index.d.ts +0 -159
  61. package/dist/enums/index.js +0 -296
  62. package/dist/enums/index.js.map +0 -1
  63. package/dist/index-DxIK0UmZ.d.ts +0 -633
  64. package/dist/index-EnfKzDbs.d.ts +0 -806
  65. package/dist/index-cLJBLUvx.d.ts +0 -478
  66. package/dist/index.d.ts +0 -43
  67. package/dist/index.js +0 -4864
  68. package/dist/index.js.map +0 -1
  69. package/dist/infrastructure/plugins/index.js +0 -292
  70. package/dist/infrastructure/plugins/index.js.map +0 -1
  71. package/dist/money-widWVD7r.d.ts +0 -111
  72. package/dist/plugin-Bb9HOE10.d.ts +0 -336
  73. package/dist/providers/index.d.ts +0 -145
  74. package/dist/providers/index.js +0 -141
  75. package/dist/providers/index.js.map +0 -1
  76. package/dist/reconciliation/index.js +0 -140
  77. package/dist/reconciliation/index.js.map +0 -1
  78. package/dist/retry-D4hFUwVk.d.ts +0 -194
  79. package/dist/schemas/index.d.ts +0 -2655
  80. package/dist/schemas/index.js +0 -841
  81. package/dist/schemas/index.js.map +0 -1
  82. package/dist/schemas/validation.d.ts +0 -384
  83. package/dist/schemas/validation.js +0 -303
  84. package/dist/schemas/validation.js.map +0 -1
  85. package/dist/settlement.schema-CpamV7ZY.d.ts +0 -343
  86. package/dist/split.enums-DG3TxQf9.d.ts +0 -42
  87. package/dist/tax-CV8A0sxl.d.ts +0 -60
  88. package/dist/utils/index.js +0 -1202
  89. package/dist/utils/index.js.map +0 -1
@@ -0,0 +1,375 @@
1
+ import * as z from "zod";
2
+
3
+ //#region src/schemas/validation.d.ts
4
+ /**
5
+ * MongoDB ObjectId pattern
6
+ */
7
+ declare const ObjectIdSchema: z.ZodString;
8
+ /**
9
+ * Currency code (ISO 4217)
10
+ */
11
+ declare const CurrencySchema: z.ZodDefault<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>>;
12
+ /**
13
+ * Money amount in smallest unit (cents, paisa)
14
+ */
15
+ declare const MoneyAmountSchema: z.ZodNumber;
16
+ /**
17
+ * Money object
18
+ */
19
+ declare const MoneySchema: z.ZodObject<{
20
+ amount: z.ZodNumber;
21
+ currency: z.ZodDefault<z.ZodString>;
22
+ }, z.core.$strip>;
23
+ /**
24
+ * Email address
25
+ */
26
+ declare const EmailSchema: z.ZodString;
27
+ /**
28
+ * Idempotency key (optional, auto-generated if not provided)
29
+ */
30
+ declare const IdempotencyKeySchema: z.ZodOptional<z.ZodString>;
31
+ /**
32
+ * Metadata object - Zod v4 record syntax
33
+ */
34
+ declare const MetadataSchema: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
35
+ /**
36
+ * Create payment intent params
37
+ */
38
+ declare const CreatePaymentSchema: z.ZodObject<{
39
+ amount: z.ZodNumber;
40
+ currency: z.ZodDefault<z.ZodString>;
41
+ customerId: z.ZodString;
42
+ organizationId: z.ZodString;
43
+ provider: z.ZodString;
44
+ idempotencyKey: z.ZodOptional<z.ZodString>;
45
+ description: z.ZodOptional<z.ZodString>;
46
+ metadata: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
47
+ successUrl: z.ZodOptional<z.ZodString>;
48
+ cancelUrl: z.ZodOptional<z.ZodString>;
49
+ }, z.core.$strip>;
50
+ type CreatePaymentInput = z.infer<typeof CreatePaymentSchema>;
51
+ /**
52
+ * Verify payment params
53
+ */
54
+ declare const VerifyPaymentSchema: z.ZodObject<{
55
+ id: z.ZodString;
56
+ provider: z.ZodOptional<z.ZodString>;
57
+ data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
58
+ }, z.core.$strip>;
59
+ type VerifyPaymentInput = z.infer<typeof VerifyPaymentSchema>;
60
+ /**
61
+ * Refund params
62
+ */
63
+ declare const RefundSchema: z.ZodObject<{
64
+ transactionId: z.ZodString;
65
+ amount: z.ZodOptional<z.ZodNumber>;
66
+ reason: z.ZodOptional<z.ZodString>;
67
+ idempotencyKey: z.ZodOptional<z.ZodString>;
68
+ metadata: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
69
+ }, z.core.$strip>;
70
+ type RefundInput = z.infer<typeof RefundSchema>;
71
+ /**
72
+ * Subscription status
73
+ */
74
+ declare const SubscriptionStatusSchema: z.ZodEnum<{
75
+ pending: "pending";
76
+ cancelled: "cancelled";
77
+ expired: "expired";
78
+ active: "active";
79
+ paused: "paused";
80
+ past_due: "past_due";
81
+ }>;
82
+ type SubscriptionStatus = z.infer<typeof SubscriptionStatusSchema>;
83
+ /**
84
+ * Subscription interval
85
+ */
86
+ declare const IntervalSchema: z.ZodEnum<{
87
+ day: "day";
88
+ week: "week";
89
+ month: "month";
90
+ year: "year";
91
+ one_time: "one_time";
92
+ }>;
93
+ type Interval = z.infer<typeof IntervalSchema>;
94
+ /**
95
+ * Create subscription params
96
+ */
97
+ declare const CreateSubscriptionSchema: z.ZodObject<{
98
+ customerId: z.ZodString;
99
+ organizationId: z.ZodString;
100
+ planKey: z.ZodString;
101
+ amount: z.ZodNumber;
102
+ currency: z.ZodDefault<z.ZodString>;
103
+ interval: z.ZodDefault<z.ZodEnum<{
104
+ day: "day";
105
+ week: "week";
106
+ month: "month";
107
+ year: "year";
108
+ one_time: "one_time";
109
+ }>>;
110
+ intervalCount: z.ZodDefault<z.ZodNumber>;
111
+ provider: z.ZodString;
112
+ sourceId: z.ZodOptional<z.ZodString>;
113
+ sourceModel: z.ZodOptional<z.ZodString>;
114
+ idempotencyKey: z.ZodOptional<z.ZodString>;
115
+ metadata: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
116
+ trialDays: z.ZodOptional<z.ZodNumber>;
117
+ }, z.core.$strip>;
118
+ type CreateSubscriptionInput = z.infer<typeof CreateSubscriptionSchema>;
119
+ /**
120
+ * Cancel subscription params
121
+ */
122
+ declare const CancelSubscriptionSchema: z.ZodObject<{
123
+ subscriptionId: z.ZodString;
124
+ immediate: z.ZodDefault<z.ZodBoolean>;
125
+ reason: z.ZodOptional<z.ZodString>;
126
+ }, z.core.$strip>;
127
+ type CancelSubscriptionInput = z.infer<typeof CancelSubscriptionSchema>;
128
+ /**
129
+ * Monetization type
130
+ */
131
+ declare const MonetizationTypeSchema: z.ZodEnum<{
132
+ subscription: "subscription";
133
+ purchase: "purchase";
134
+ free: "free";
135
+ }>;
136
+ type MonetizationType = z.infer<typeof MonetizationTypeSchema>;
137
+ /**
138
+ * Create monetization params (unified API)
139
+ */
140
+ declare const CreateMonetizationSchema: z.ZodObject<{
141
+ type: z.ZodDefault<z.ZodEnum<{
142
+ subscription: "subscription";
143
+ purchase: "purchase";
144
+ free: "free";
145
+ }>>;
146
+ amount: z.ZodOptional<z.ZodNumber>;
147
+ currency: z.ZodDefault<z.ZodString>;
148
+ customerId: z.ZodString;
149
+ organizationId: z.ZodString;
150
+ provider: z.ZodString;
151
+ planKey: z.ZodOptional<z.ZodString>;
152
+ sourceId: z.ZodOptional<z.ZodString>;
153
+ sourceModel: z.ZodOptional<z.ZodString>;
154
+ idempotencyKey: z.ZodOptional<z.ZodString>;
155
+ metadata: z.ZodDefault<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
156
+ interval: z.ZodOptional<z.ZodEnum<{
157
+ day: "day";
158
+ week: "week";
159
+ month: "month";
160
+ year: "year";
161
+ one_time: "one_time";
162
+ }>>;
163
+ trialDays: z.ZodOptional<z.ZodNumber>;
164
+ }, z.core.$strip>;
165
+ type CreateMonetizationInput = z.infer<typeof CreateMonetizationSchema>;
166
+ /**
167
+ * Commission split recipient
168
+ */
169
+ declare const SplitRecipientSchema: z.ZodObject<{
170
+ recipientId: z.ZodString;
171
+ recipientType: z.ZodDefault<z.ZodString>;
172
+ percentage: z.ZodNumber;
173
+ role: z.ZodOptional<z.ZodString>;
174
+ }, z.core.$strip>;
175
+ type SplitRecipient = z.infer<typeof SplitRecipientSchema>;
176
+ /**
177
+ * Commission configuration
178
+ */
179
+ declare const CommissionConfigSchema: z.ZodObject<{
180
+ platformRate: z.ZodDefault<z.ZodNumber>;
181
+ gatewayFeeRate: z.ZodDefault<z.ZodNumber>;
182
+ gatewayFixedFee: z.ZodDefault<z.ZodNumber>;
183
+ splits: z.ZodOptional<z.ZodArray<z.ZodObject<{
184
+ recipientId: z.ZodString;
185
+ recipientType: z.ZodDefault<z.ZodString>;
186
+ percentage: z.ZodNumber;
187
+ role: z.ZodOptional<z.ZodString>;
188
+ }, z.core.$strip>>>;
189
+ affiliate: z.ZodOptional<z.ZodObject<{
190
+ recipientId: z.ZodString;
191
+ recipientType: z.ZodDefault<z.ZodString>;
192
+ rate: z.ZodNumber;
193
+ }, z.core.$strip>>;
194
+ }, z.core.$strip>;
195
+ type CommissionConfig = z.infer<typeof CommissionConfigSchema>;
196
+ /**
197
+ * Payment status enum
198
+ */
199
+ declare const PaymentStatusEnumSchema: z.ZodEnum<{
200
+ pending: "pending";
201
+ verified: "verified";
202
+ failed: "failed";
203
+ cancelled: "cancelled";
204
+ refunded: "refunded";
205
+ }>;
206
+ type PaymentStatusEnum = z.infer<typeof PaymentStatusEnumSchema>;
207
+ /**
208
+ * Individual payment entry for split/multi-payment scenarios
209
+ * e.g., cash + bank transfer + mobile wallet
210
+ */
211
+ declare const PaymentEntrySchema: z.ZodObject<{
212
+ method: z.ZodString;
213
+ amount: z.ZodNumber;
214
+ reference: z.ZodOptional<z.ZodString>;
215
+ details: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
216
+ }, z.core.$strip>;
217
+ type PaymentEntryInput = z.infer<typeof PaymentEntrySchema>;
218
+ /**
219
+ * Current payment schema with split payment support
220
+ * Backward compatible - single payments work without the payments array
221
+ */
222
+ declare const CurrentPaymentInputSchema: z.ZodObject<{
223
+ transactionId: z.ZodOptional<z.ZodString>;
224
+ amount: z.ZodNumber;
225
+ status: z.ZodDefault<z.ZodEnum<{
226
+ pending: "pending";
227
+ verified: "verified";
228
+ failed: "failed";
229
+ cancelled: "cancelled";
230
+ refunded: "refunded";
231
+ }>>;
232
+ method: z.ZodString;
233
+ reference: z.ZodOptional<z.ZodString>;
234
+ payments: z.ZodOptional<z.ZodArray<z.ZodObject<{
235
+ method: z.ZodString;
236
+ amount: z.ZodNumber;
237
+ reference: z.ZodOptional<z.ZodString>;
238
+ details: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
239
+ }, z.core.$strip>>>;
240
+ verifiedAt: z.ZodOptional<z.ZodDate>;
241
+ verifiedBy: z.ZodOptional<z.ZodString>;
242
+ }, z.core.$strip>;
243
+ type CurrentPaymentInput = z.infer<typeof CurrentPaymentInputSchema>;
244
+ /**
245
+ * Hold status
246
+ */
247
+ declare const HoldStatusSchema: z.ZodEnum<{
248
+ cancelled: "cancelled";
249
+ held: "held";
250
+ released: "released";
251
+ none: "none";
252
+ partial_release: "partial_release";
253
+ }>;
254
+ type HoldStatus = z.infer<typeof HoldStatusSchema>;
255
+ /**
256
+ * Create hold params
257
+ */
258
+ declare const CreateHoldSchema: z.ZodObject<{
259
+ transactionId: z.ZodString;
260
+ amount: z.ZodOptional<z.ZodNumber>;
261
+ holdUntil: z.ZodOptional<z.ZodDate>;
262
+ reason: z.ZodOptional<z.ZodString>;
263
+ }, z.core.$strip>;
264
+ type CreateHoldInput = z.infer<typeof CreateHoldSchema>;
265
+ /**
266
+ * Release hold params
267
+ */
268
+ declare const ReleaseHoldSchema: z.ZodObject<{
269
+ transactionId: z.ZodString;
270
+ amount: z.ZodOptional<z.ZodNumber>;
271
+ recipientId: z.ZodString;
272
+ recipientType: z.ZodDefault<z.ZodString>;
273
+ notes: z.ZodOptional<z.ZodString>;
274
+ }, z.core.$strip>;
275
+ type ReleaseHoldInput = z.infer<typeof ReleaseHoldSchema>;
276
+ /**
277
+ * Provider configuration
278
+ */
279
+ declare const ProviderConfigSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
280
+ /**
281
+ * Retry configuration
282
+ */
283
+ declare const RetryConfigSchema: z.ZodObject<{
284
+ maxAttempts: z.ZodDefault<z.ZodNumber>;
285
+ baseDelay: z.ZodDefault<z.ZodNumber>;
286
+ maxDelay: z.ZodDefault<z.ZodNumber>;
287
+ backoffMultiplier: z.ZodDefault<z.ZodNumber>;
288
+ jitter: z.ZodDefault<z.ZodNumber>;
289
+ }, z.core.$strip>;
290
+ type RetryConfig = z.infer<typeof RetryConfigSchema>;
291
+ /**
292
+ * Revenue configuration
293
+ */
294
+ declare const RevenueConfigSchema: z.ZodObject<{
295
+ defaultCurrency: z.ZodDefault<z.ZodString>;
296
+ commission: z.ZodOptional<z.ZodObject<{
297
+ platformRate: z.ZodDefault<z.ZodNumber>;
298
+ gatewayFeeRate: z.ZodDefault<z.ZodNumber>;
299
+ gatewayFixedFee: z.ZodDefault<z.ZodNumber>;
300
+ splits: z.ZodOptional<z.ZodArray<z.ZodObject<{
301
+ recipientId: z.ZodString;
302
+ recipientType: z.ZodDefault<z.ZodString>;
303
+ percentage: z.ZodNumber;
304
+ role: z.ZodOptional<z.ZodString>;
305
+ }, z.core.$strip>>>;
306
+ affiliate: z.ZodOptional<z.ZodObject<{
307
+ recipientId: z.ZodString;
308
+ recipientType: z.ZodDefault<z.ZodString>;
309
+ rate: z.ZodNumber;
310
+ }, z.core.$strip>>;
311
+ }, z.core.$strip>>;
312
+ retry: z.ZodOptional<z.ZodObject<{
313
+ maxAttempts: z.ZodDefault<z.ZodNumber>;
314
+ baseDelay: z.ZodDefault<z.ZodNumber>;
315
+ maxDelay: z.ZodDefault<z.ZodNumber>;
316
+ backoffMultiplier: z.ZodDefault<z.ZodNumber>;
317
+ jitter: z.ZodDefault<z.ZodNumber>;
318
+ }, z.core.$strip>>;
319
+ debug: z.ZodDefault<z.ZodBoolean>;
320
+ environment: z.ZodDefault<z.ZodEnum<{
321
+ development: "development";
322
+ staging: "staging";
323
+ production: "production";
324
+ }>>;
325
+ }, z.core.$strip>;
326
+ type RevenueConfigInput = z.infer<typeof RevenueConfigSchema>;
327
+ /**
328
+ * Validate input against schema
329
+ */
330
+ declare function validate<T extends z.ZodType>(schema: T, data: unknown): z.infer<T>;
331
+ /**
332
+ * Safe validate (returns result, doesn't throw)
333
+ */
334
+ declare function safeValidate<T extends z.ZodType>(schema: T, data: unknown): {
335
+ success: true;
336
+ data: z.infer<T>;
337
+ } | {
338
+ success: false;
339
+ error: z.ZodError;
340
+ };
341
+ /**
342
+ * Format Zod error for display
343
+ * Zod v4 uses `issues` property
344
+ */
345
+ declare function formatZodError(error: z.ZodError): string;
346
+ /**
347
+ * Validates that split payment totals match the transaction amount
348
+ * Returns true for single payments (no payments array)
349
+ *
350
+ * @param currentPayment - The current payment object to validate
351
+ * @returns true if valid, false if split totals don't match
352
+ *
353
+ * @example
354
+ * // Single payment - always valid
355
+ * validateSplitPayments({ amount: 50000, method: 'cash', status: 'verified' }) // true
356
+ *
357
+ * // Split payment - totals must match
358
+ * validateSplitPayments({
359
+ * amount: 50000,
360
+ * method: 'split',
361
+ * status: 'verified',
362
+ * payments: [
363
+ * { method: 'cash', amount: 10000 },
364
+ * { method: 'bkash', amount: 40000 },
365
+ * ]
366
+ * }) // true (10000 + 40000 = 50000)
367
+ */
368
+ declare function validateSplitPayments(currentPayment: {
369
+ amount: number;
370
+ payments?: Array<{
371
+ amount: number;
372
+ }>;
373
+ }): boolean;
374
+ //#endregion
375
+ export { CancelSubscriptionInput, CancelSubscriptionSchema, CommissionConfig, CommissionConfigSchema, CreateHoldInput, CreateHoldSchema, CreateMonetizationInput, CreateMonetizationSchema, CreatePaymentInput, CreatePaymentSchema, CreateSubscriptionInput, CreateSubscriptionSchema, CurrencySchema, CurrentPaymentInput, CurrentPaymentInputSchema, EmailSchema, HoldStatus, HoldStatusSchema, IdempotencyKeySchema, Interval, IntervalSchema, MetadataSchema, MonetizationType, MonetizationTypeSchema, MoneyAmountSchema, MoneySchema, ObjectIdSchema, PaymentEntryInput, PaymentEntrySchema, PaymentStatusEnum, PaymentStatusEnumSchema, ProviderConfigSchema, RefundInput, RefundSchema, ReleaseHoldInput, ReleaseHoldSchema, RetryConfig, RetryConfigSchema, RevenueConfigInput, RevenueConfigSchema, SplitRecipient, SplitRecipientSchema, SubscriptionStatus, SubscriptionStatusSchema, VerifyPaymentInput, VerifyPaymentSchema, formatZodError, safeValidate, validate, validateSplitPayments };
@@ -0,0 +1,325 @@
1
+ import * as z from "zod";
2
+
3
+ //#region src/schemas/validation.ts
4
+ /**
5
+ * Zod Validation Schemas
6
+ * @classytic/revenue
7
+ *
8
+ * Runtime validation with TypeScript inference
9
+ * Using Zod v4 - Modern schema validation
10
+ *
11
+ * Inspired by: tRPC, Zod best practices
12
+ */
13
+ /**
14
+ * MongoDB ObjectId pattern
15
+ */
16
+ const ObjectIdSchema = z.string().regex(/^[a-fA-F0-9]{24}$/, "Invalid ObjectId format");
17
+ /**
18
+ * Currency code (ISO 4217)
19
+ */
20
+ const CurrencySchema = z.string().length(3, "Currency must be 3 characters").transform((val) => val.toUpperCase()).default("USD");
21
+ /**
22
+ * Money amount in smallest unit (cents, paisa)
23
+ */
24
+ const MoneyAmountSchema = z.number().int("Amount must be integer (smallest unit)").nonnegative("Amount cannot be negative");
25
+ /**
26
+ * Money object
27
+ */
28
+ const MoneySchema = z.object({
29
+ amount: MoneyAmountSchema,
30
+ currency: z.string().length(3).default("USD")
31
+ });
32
+ /**
33
+ * Email address
34
+ */
35
+ const EmailSchema = z.string().email();
36
+ /**
37
+ * Idempotency key (optional, auto-generated if not provided)
38
+ */
39
+ const IdempotencyKeySchema = z.string().min(1).max(255).optional();
40
+ /**
41
+ * Metadata object - Zod v4 record syntax
42
+ */
43
+ const MetadataSchema = z.record(z.string(), z.unknown()).optional().default({});
44
+ /**
45
+ * Create payment intent params
46
+ */
47
+ const CreatePaymentSchema = z.object({
48
+ amount: MoneyAmountSchema,
49
+ currency: z.string().length(3).default("USD"),
50
+ customerId: z.string().min(1, "Customer ID is required"),
51
+ organizationId: z.string().min(1, "Organization ID is required"),
52
+ provider: z.string().min(1, "Provider is required"),
53
+ idempotencyKey: IdempotencyKeySchema,
54
+ description: z.string().optional(),
55
+ metadata: MetadataSchema,
56
+ successUrl: z.string().url().optional(),
57
+ cancelUrl: z.string().url().optional()
58
+ });
59
+ /**
60
+ * Verify payment params
61
+ */
62
+ const VerifyPaymentSchema = z.object({
63
+ id: z.string().min(1),
64
+ provider: z.string().optional(),
65
+ data: z.record(z.string(), z.unknown()).optional()
66
+ });
67
+ /**
68
+ * Refund params
69
+ */
70
+ const RefundSchema = z.object({
71
+ transactionId: z.string().min(1),
72
+ amount: MoneyAmountSchema.optional(),
73
+ reason: z.string().optional(),
74
+ idempotencyKey: IdempotencyKeySchema,
75
+ metadata: MetadataSchema
76
+ });
77
+ /**
78
+ * Subscription status
79
+ */
80
+ const SubscriptionStatusSchema = z.enum([
81
+ "pending",
82
+ "active",
83
+ "paused",
84
+ "cancelled",
85
+ "expired",
86
+ "past_due"
87
+ ]);
88
+ /**
89
+ * Subscription interval
90
+ */
91
+ const IntervalSchema = z.enum([
92
+ "day",
93
+ "week",
94
+ "month",
95
+ "year",
96
+ "one_time"
97
+ ]);
98
+ /**
99
+ * Create subscription params
100
+ */
101
+ const CreateSubscriptionSchema = z.object({
102
+ customerId: z.string().min(1),
103
+ organizationId: z.string().min(1),
104
+ planKey: z.string().min(1),
105
+ amount: MoneyAmountSchema,
106
+ currency: z.string().length(3).default("USD"),
107
+ interval: IntervalSchema.default("month"),
108
+ intervalCount: z.number().int().positive().default(1),
109
+ provider: z.string().min(1),
110
+ sourceId: z.string().optional(),
111
+ sourceModel: z.string().optional(),
112
+ idempotencyKey: IdempotencyKeySchema,
113
+ metadata: MetadataSchema,
114
+ trialDays: z.number().int().nonnegative().optional()
115
+ });
116
+ /**
117
+ * Cancel subscription params
118
+ */
119
+ const CancelSubscriptionSchema = z.object({
120
+ subscriptionId: z.string().min(1),
121
+ immediate: z.boolean().default(false),
122
+ reason: z.string().optional()
123
+ });
124
+ /**
125
+ * Monetization type
126
+ */
127
+ const MonetizationTypeSchema = z.enum([
128
+ "purchase",
129
+ "subscription",
130
+ "free"
131
+ ]);
132
+ /**
133
+ * Create monetization params (unified API)
134
+ */
135
+ const CreateMonetizationSchema = z.object({
136
+ type: MonetizationTypeSchema.default("purchase"),
137
+ amount: MoneyAmountSchema.optional(),
138
+ currency: z.string().length(3).default("USD"),
139
+ customerId: z.string().min(1),
140
+ organizationId: z.string().min(1),
141
+ provider: z.string().min(1),
142
+ planKey: z.string().optional(),
143
+ sourceId: z.string().optional(),
144
+ sourceModel: z.string().optional(),
145
+ idempotencyKey: IdempotencyKeySchema,
146
+ metadata: MetadataSchema,
147
+ interval: IntervalSchema.optional(),
148
+ trialDays: z.number().int().nonnegative().optional()
149
+ }).refine((data) => {
150
+ if (data.type !== "free" && !data.amount) return false;
151
+ return true;
152
+ }, { message: "Amount is required for non-free monetization types" });
153
+ /**
154
+ * Commission split recipient
155
+ */
156
+ const SplitRecipientSchema = z.object({
157
+ recipientId: z.string().min(1),
158
+ recipientType: z.string().default("user"),
159
+ percentage: z.number().min(0).max(1),
160
+ role: z.string().optional()
161
+ });
162
+ /**
163
+ * Commission configuration
164
+ */
165
+ const CommissionConfigSchema = z.object({
166
+ platformRate: z.number().min(0).max(1).default(0),
167
+ gatewayFeeRate: z.number().min(0).max(1).default(0),
168
+ gatewayFixedFee: MoneyAmountSchema.default(0),
169
+ splits: z.array(SplitRecipientSchema).optional(),
170
+ affiliate: z.object({
171
+ recipientId: z.string(),
172
+ recipientType: z.string().default("user"),
173
+ rate: z.number().min(0).max(1)
174
+ }).optional()
175
+ });
176
+ /**
177
+ * Payment status enum
178
+ */
179
+ const PaymentStatusEnumSchema = z.enum([
180
+ "pending",
181
+ "verified",
182
+ "failed",
183
+ "refunded",
184
+ "cancelled"
185
+ ]);
186
+ /**
187
+ * Individual payment entry for split/multi-payment scenarios
188
+ * e.g., cash + bank transfer + mobile wallet
189
+ */
190
+ const PaymentEntrySchema = z.object({
191
+ method: z.string().min(1, "Payment method is required"),
192
+ amount: MoneyAmountSchema,
193
+ reference: z.string().optional(),
194
+ details: z.record(z.string(), z.unknown()).optional()
195
+ });
196
+ /**
197
+ * Current payment schema with split payment support
198
+ * Backward compatible - single payments work without the payments array
199
+ */
200
+ const CurrentPaymentInputSchema = z.object({
201
+ transactionId: z.string().optional(),
202
+ amount: MoneyAmountSchema,
203
+ status: PaymentStatusEnumSchema.default("pending"),
204
+ method: z.string().min(1, "Payment method is required"),
205
+ reference: z.string().optional(),
206
+ payments: z.array(PaymentEntrySchema).optional(),
207
+ verifiedAt: z.date().optional(),
208
+ verifiedBy: z.string().optional()
209
+ }).refine((data) => {
210
+ if (data.payments && data.payments.length > 0) return data.payments.reduce((sum, p) => sum + p.amount, 0) === data.amount;
211
+ return true;
212
+ }, { message: "Split payments total must equal the transaction amount" });
213
+ /**
214
+ * Hold status
215
+ */
216
+ const HoldStatusSchema = z.enum([
217
+ "none",
218
+ "held",
219
+ "partial_release",
220
+ "released",
221
+ "cancelled"
222
+ ]);
223
+ /**
224
+ * Create hold params
225
+ */
226
+ const CreateHoldSchema = z.object({
227
+ transactionId: z.string().min(1),
228
+ amount: MoneyAmountSchema.optional(),
229
+ holdUntil: z.date().optional(),
230
+ reason: z.string().optional()
231
+ });
232
+ /**
233
+ * Release hold params
234
+ */
235
+ const ReleaseHoldSchema = z.object({
236
+ transactionId: z.string().min(1),
237
+ amount: MoneyAmountSchema.optional(),
238
+ recipientId: z.string().min(1),
239
+ recipientType: z.string().default("user"),
240
+ notes: z.string().optional()
241
+ });
242
+ /**
243
+ * Provider configuration
244
+ */
245
+ const ProviderConfigSchema = z.record(z.string(), z.unknown());
246
+ /**
247
+ * Retry configuration
248
+ */
249
+ const RetryConfigSchema = z.object({
250
+ maxAttempts: z.number().int().positive().default(3),
251
+ baseDelay: z.number().positive().default(1e3),
252
+ maxDelay: z.number().positive().default(3e4),
253
+ backoffMultiplier: z.number().positive().default(2),
254
+ jitter: z.number().min(0).max(1).default(.1)
255
+ });
256
+ /**
257
+ * Revenue configuration
258
+ */
259
+ const RevenueConfigSchema = z.object({
260
+ defaultCurrency: z.string().length(3).default("USD"),
261
+ commission: CommissionConfigSchema.optional(),
262
+ retry: RetryConfigSchema.optional(),
263
+ debug: z.boolean().default(false),
264
+ environment: z.enum([
265
+ "development",
266
+ "staging",
267
+ "production"
268
+ ]).default("development")
269
+ });
270
+ /**
271
+ * Validate input against schema
272
+ */
273
+ function validate(schema, data) {
274
+ return schema.parse(data);
275
+ }
276
+ /**
277
+ * Safe validate (returns result, doesn't throw)
278
+ */
279
+ function safeValidate(schema, data) {
280
+ const result = schema.safeParse(data);
281
+ if (result.success) return {
282
+ success: true,
283
+ data: result.data
284
+ };
285
+ return {
286
+ success: false,
287
+ error: result.error
288
+ };
289
+ }
290
+ /**
291
+ * Format Zod error for display
292
+ * Zod v4 uses `issues` property
293
+ */
294
+ function formatZodError(error) {
295
+ return error.issues.map((issue) => `${issue.path.join(".")}: ${issue.message}`).join(", ");
296
+ }
297
+ /**
298
+ * Validates that split payment totals match the transaction amount
299
+ * Returns true for single payments (no payments array)
300
+ *
301
+ * @param currentPayment - The current payment object to validate
302
+ * @returns true if valid, false if split totals don't match
303
+ *
304
+ * @example
305
+ * // Single payment - always valid
306
+ * validateSplitPayments({ amount: 50000, method: 'cash', status: 'verified' }) // true
307
+ *
308
+ * // Split payment - totals must match
309
+ * validateSplitPayments({
310
+ * amount: 50000,
311
+ * method: 'split',
312
+ * status: 'verified',
313
+ * payments: [
314
+ * { method: 'cash', amount: 10000 },
315
+ * { method: 'bkash', amount: 40000 },
316
+ * ]
317
+ * }) // true (10000 + 40000 = 50000)
318
+ */
319
+ function validateSplitPayments(currentPayment) {
320
+ if (!currentPayment.payments?.length) return true;
321
+ return currentPayment.payments.reduce((sum, p) => sum + p.amount, 0) === currentPayment.amount;
322
+ }
323
+
324
+ //#endregion
325
+ export { CancelSubscriptionSchema, CommissionConfigSchema, CreateHoldSchema, CreateMonetizationSchema, CreatePaymentSchema, CreateSubscriptionSchema, CurrencySchema, CurrentPaymentInputSchema, EmailSchema, HoldStatusSchema, IdempotencyKeySchema, IntervalSchema, MetadataSchema, MonetizationTypeSchema, MoneyAmountSchema, MoneySchema, ObjectIdSchema, PaymentEntrySchema, PaymentStatusEnumSchema, ProviderConfigSchema, RefundSchema, ReleaseHoldSchema, RetryConfigSchema, RevenueConfigSchema, SplitRecipientSchema, SubscriptionStatusSchema, VerifyPaymentSchema, formatZodError, safeValidate, validate, validateSplitPayments };