@classytic/revenue 0.2.4 → 1.0.0
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/README.md +498 -501
- package/dist/actions-CwG-b7fR.d.ts +519 -0
- package/dist/core/index.d.ts +884 -0
- package/dist/core/index.js +2941 -0
- package/dist/core/index.js.map +1 -0
- package/dist/enums/index.d.ts +130 -0
- package/dist/enums/index.js +167 -0
- package/dist/enums/index.js.map +1 -0
- package/dist/index-BnJWVXuw.d.ts +378 -0
- package/dist/index-ChVD3P9k.d.ts +504 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +4353 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/index.d.ts +132 -0
- package/dist/providers/index.js +122 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/retry-80lBCmSe.d.ts +234 -0
- package/dist/schemas/index.d.ts +894 -0
- package/dist/schemas/index.js +524 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/validation.d.ts +309 -0
- package/dist/schemas/validation.js +249 -0
- package/dist/schemas/validation.js.map +1 -0
- package/dist/services/index.d.ts +3 -0
- package/dist/services/index.js +1632 -0
- package/dist/services/index.js.map +1 -0
- package/dist/split.enums-DHdM1YAV.d.ts +255 -0
- package/dist/split.schema-BPdFZMbU.d.ts +958 -0
- package/dist/utils/index.d.ts +24 -0
- package/dist/utils/index.js +1067 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +48 -32
- package/core/builder.js +0 -219
- package/core/container.js +0 -119
- package/core/errors.js +0 -262
- package/dist/types/core/builder.d.ts +0 -97
- package/dist/types/core/container.d.ts +0 -57
- package/dist/types/core/errors.d.ts +0 -122
- package/dist/types/enums/escrow.enums.d.ts +0 -24
- package/dist/types/enums/index.d.ts +0 -69
- package/dist/types/enums/monetization.enums.d.ts +0 -6
- package/dist/types/enums/payment.enums.d.ts +0 -16
- package/dist/types/enums/split.enums.d.ts +0 -25
- package/dist/types/enums/subscription.enums.d.ts +0 -15
- package/dist/types/enums/transaction.enums.d.ts +0 -24
- package/dist/types/index.d.ts +0 -22
- package/dist/types/providers/base.d.ts +0 -128
- package/dist/types/schemas/escrow/hold.schema.d.ts +0 -54
- package/dist/types/schemas/escrow/index.d.ts +0 -6
- package/dist/types/schemas/index.d.ts +0 -506
- package/dist/types/schemas/split/index.d.ts +0 -8
- package/dist/types/schemas/split/split.schema.d.ts +0 -142
- package/dist/types/schemas/subscription/index.d.ts +0 -152
- package/dist/types/schemas/subscription/info.schema.d.ts +0 -128
- package/dist/types/schemas/subscription/plan.schema.d.ts +0 -39
- package/dist/types/schemas/transaction/common.schema.d.ts +0 -12
- package/dist/types/schemas/transaction/gateway.schema.d.ts +0 -86
- package/dist/types/schemas/transaction/index.d.ts +0 -202
- package/dist/types/schemas/transaction/payment.schema.d.ts +0 -145
- package/dist/types/services/escrow.service.d.ts +0 -51
- package/dist/types/services/monetization.service.d.ts +0 -193
- package/dist/types/services/payment.service.d.ts +0 -117
- package/dist/types/services/transaction.service.d.ts +0 -40
- package/dist/types/utils/category-resolver.d.ts +0 -46
- package/dist/types/utils/commission-split.d.ts +0 -56
- package/dist/types/utils/commission.d.ts +0 -29
- package/dist/types/utils/hooks.d.ts +0 -17
- package/dist/types/utils/index.d.ts +0 -6
- package/dist/types/utils/logger.d.ts +0 -12
- package/dist/types/utils/subscription/actions.d.ts +0 -28
- package/dist/types/utils/subscription/index.d.ts +0 -2
- package/dist/types/utils/subscription/period.d.ts +0 -47
- package/dist/types/utils/transaction-type.d.ts +0 -102
- package/enums/escrow.enums.js +0 -36
- package/enums/index.d.ts +0 -116
- package/enums/index.js +0 -110
- package/enums/monetization.enums.js +0 -15
- package/enums/payment.enums.js +0 -64
- package/enums/split.enums.js +0 -37
- package/enums/subscription.enums.js +0 -33
- package/enums/transaction.enums.js +0 -69
- package/index.js +0 -76
- package/providers/base.js +0 -162
- package/schemas/escrow/hold.schema.js +0 -62
- package/schemas/escrow/index.js +0 -15
- package/schemas/index.d.ts +0 -33
- package/schemas/index.js +0 -27
- package/schemas/split/index.js +0 -16
- package/schemas/split/split.schema.js +0 -86
- package/schemas/subscription/index.js +0 -17
- package/schemas/subscription/info.schema.js +0 -115
- package/schemas/subscription/plan.schema.js +0 -48
- package/schemas/transaction/common.schema.js +0 -22
- package/schemas/transaction/gateway.schema.js +0 -69
- package/schemas/transaction/index.js +0 -20
- package/schemas/transaction/payment.schema.js +0 -110
- package/services/escrow.service.js +0 -353
- package/services/monetization.service.js +0 -675
- package/services/payment.service.js +0 -535
- package/services/transaction.service.js +0 -142
- package/utils/category-resolver.js +0 -74
- package/utils/commission-split.js +0 -180
- package/utils/commission.js +0 -83
- package/utils/hooks.js +0 -44
- package/utils/index.d.ts +0 -164
- package/utils/index.js +0 -16
- package/utils/logger.js +0 -36
- package/utils/subscription/actions.js +0 -68
- package/utils/subscription/index.js +0 -20
- package/utils/subscription/period.js +0 -123
- package/utils/transaction-type.js +0 -254
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
import { R as Result } from './retry-80lBCmSe.js';
|
|
2
|
+
import { d as TransactionDocument, ac as TransactionTypeOptions, ad as FieldUpdateValidationResult, L as Logger, C as CommissionInfo, o as SplitRule, p as SplitInfo, ab as CommissionWithSplitsOptions, h as MonetizationTypeValue, a6 as PeriodRangeParams, a7 as PeriodRangeResult, a8 as ProratedAmountParams, a9 as DurationResult, e as SubscriptionDocument, aa as SubscriptionEntity } from './index-ChVD3P9k.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Money Utility - Integer-safe currency handling
|
|
6
|
+
* @classytic/revenue
|
|
7
|
+
*
|
|
8
|
+
* Never use floating point for money!
|
|
9
|
+
* All amounts stored as smallest unit (cents, paisa, etc.)
|
|
10
|
+
*
|
|
11
|
+
* Inspired by: Stripe, Dinero.js, tc39/proposal-decimal
|
|
12
|
+
*/
|
|
13
|
+
interface MoneyValue {
|
|
14
|
+
/** Amount in smallest currency unit (cents, paisa, etc.) */
|
|
15
|
+
readonly amount: number;
|
|
16
|
+
/** ISO 4217 currency code */
|
|
17
|
+
readonly currency: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Money class - immutable money representation
|
|
21
|
+
*/
|
|
22
|
+
declare class Money implements MoneyValue {
|
|
23
|
+
readonly amount: number;
|
|
24
|
+
readonly currency: string;
|
|
25
|
+
private constructor();
|
|
26
|
+
/**
|
|
27
|
+
* Create money from smallest unit (cents, paisa)
|
|
28
|
+
* @example Money.cents(1999, 'USD') // $19.99
|
|
29
|
+
*/
|
|
30
|
+
static cents(amount: number, currency?: string): Money;
|
|
31
|
+
/**
|
|
32
|
+
* Create money from major unit (dollars, taka)
|
|
33
|
+
* @example Money.of(19.99, 'USD') // $19.99 (stored as 1999 cents)
|
|
34
|
+
*/
|
|
35
|
+
static of(amount: number, currency?: string): Money;
|
|
36
|
+
/**
|
|
37
|
+
* Create zero money
|
|
38
|
+
*/
|
|
39
|
+
static zero(currency?: string): Money;
|
|
40
|
+
static usd(cents: number): Money;
|
|
41
|
+
static eur(cents: number): Money;
|
|
42
|
+
static gbp(pence: number): Money;
|
|
43
|
+
static bdt(paisa: number): Money;
|
|
44
|
+
static inr(paisa: number): Money;
|
|
45
|
+
static jpy(yen: number): Money;
|
|
46
|
+
/**
|
|
47
|
+
* Add two money values (must be same currency)
|
|
48
|
+
*/
|
|
49
|
+
add(other: Money): Money;
|
|
50
|
+
/**
|
|
51
|
+
* Subtract money (must be same currency)
|
|
52
|
+
*/
|
|
53
|
+
subtract(other: Money): Money;
|
|
54
|
+
/**
|
|
55
|
+
* Multiply by a factor (rounds to nearest integer)
|
|
56
|
+
*/
|
|
57
|
+
multiply(factor: number): Money;
|
|
58
|
+
/**
|
|
59
|
+
* Divide by a divisor (rounds to nearest integer)
|
|
60
|
+
*/
|
|
61
|
+
divide(divisor: number): Money;
|
|
62
|
+
/**
|
|
63
|
+
* Calculate percentage
|
|
64
|
+
* @example money.percentage(10) // 10% of money
|
|
65
|
+
*/
|
|
66
|
+
percentage(percent: number): Money;
|
|
67
|
+
/**
|
|
68
|
+
* Allocate money among recipients (handles rounding)
|
|
69
|
+
* @example Money.usd(100).allocate([1, 1, 1]) // [34, 33, 33] cents
|
|
70
|
+
*/
|
|
71
|
+
allocate(ratios: number[]): Money[];
|
|
72
|
+
/**
|
|
73
|
+
* Split equally among n recipients
|
|
74
|
+
*/
|
|
75
|
+
split(parts: number): Money[];
|
|
76
|
+
isZero(): boolean;
|
|
77
|
+
isPositive(): boolean;
|
|
78
|
+
isNegative(): boolean;
|
|
79
|
+
equals(other: Money): boolean;
|
|
80
|
+
greaterThan(other: Money): boolean;
|
|
81
|
+
lessThan(other: Money): boolean;
|
|
82
|
+
greaterThanOrEqual(other: Money): boolean;
|
|
83
|
+
lessThanOrEqual(other: Money): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Get amount in major unit (dollars, taka)
|
|
86
|
+
*/
|
|
87
|
+
toUnit(): number;
|
|
88
|
+
/**
|
|
89
|
+
* Format for display
|
|
90
|
+
* @example Money.usd(1999).format() // "$19.99"
|
|
91
|
+
*/
|
|
92
|
+
format(locale?: string): string;
|
|
93
|
+
/**
|
|
94
|
+
* Format without currency symbol
|
|
95
|
+
*/
|
|
96
|
+
formatAmount(locale?: string): string;
|
|
97
|
+
/**
|
|
98
|
+
* Convert to JSON-serializable object
|
|
99
|
+
*/
|
|
100
|
+
toJSON(): MoneyValue;
|
|
101
|
+
/**
|
|
102
|
+
* Create from JSON
|
|
103
|
+
*/
|
|
104
|
+
static fromJSON(json: MoneyValue): Money;
|
|
105
|
+
toString(): string;
|
|
106
|
+
private assertSameCurrency;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Helper functions for legacy compatibility
|
|
110
|
+
*/
|
|
111
|
+
declare function toSmallestUnit(amount: number, currency?: string): number;
|
|
112
|
+
declare function fromSmallestUnit(amount: number, currency?: string): number;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Idempotency Utilities
|
|
116
|
+
* @classytic/revenue
|
|
117
|
+
*
|
|
118
|
+
* Prevent duplicate operations with idempotency keys
|
|
119
|
+
* Inspired by: Stripe, Amazon SQS deduplication
|
|
120
|
+
*/
|
|
121
|
+
|
|
122
|
+
interface IdempotencyRecord<T = unknown> {
|
|
123
|
+
/** Idempotency key */
|
|
124
|
+
key: string;
|
|
125
|
+
/** Operation result (if completed) */
|
|
126
|
+
result?: T;
|
|
127
|
+
/** Operation status */
|
|
128
|
+
status: 'pending' | 'completed' | 'failed';
|
|
129
|
+
/** Creation timestamp */
|
|
130
|
+
createdAt: Date;
|
|
131
|
+
/** Completion timestamp */
|
|
132
|
+
completedAt?: Date;
|
|
133
|
+
/** Request hash for validation */
|
|
134
|
+
requestHash: string;
|
|
135
|
+
/** TTL - when record expires */
|
|
136
|
+
expiresAt: Date;
|
|
137
|
+
}
|
|
138
|
+
interface IdempotencyStore {
|
|
139
|
+
/** Get record by key */
|
|
140
|
+
get<T>(key: string): Promise<IdempotencyRecord<T> | null>;
|
|
141
|
+
/** Set or update record */
|
|
142
|
+
set<T>(key: string, record: IdempotencyRecord<T>): Promise<void>;
|
|
143
|
+
/** Delete record */
|
|
144
|
+
delete(key: string): Promise<void>;
|
|
145
|
+
/** Check if key exists */
|
|
146
|
+
exists(key: string): Promise<boolean>;
|
|
147
|
+
}
|
|
148
|
+
interface IdempotencyConfig {
|
|
149
|
+
/** TTL in milliseconds (default: 24 hours) */
|
|
150
|
+
ttl?: number;
|
|
151
|
+
/** Custom store implementation */
|
|
152
|
+
store?: IdempotencyStore;
|
|
153
|
+
/** Key prefix */
|
|
154
|
+
prefix?: string;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Simple in-memory idempotency store
|
|
158
|
+
* Use Redis/database store in production
|
|
159
|
+
*/
|
|
160
|
+
declare class MemoryIdempotencyStore implements IdempotencyStore {
|
|
161
|
+
private records;
|
|
162
|
+
private cleanupInterval;
|
|
163
|
+
constructor(cleanupIntervalMs?: number);
|
|
164
|
+
get<T>(key: string): Promise<IdempotencyRecord<T> | null>;
|
|
165
|
+
set<T>(key: string, record: IdempotencyRecord<T>): Promise<void>;
|
|
166
|
+
delete(key: string): Promise<void>;
|
|
167
|
+
exists(key: string): Promise<boolean>;
|
|
168
|
+
private cleanup;
|
|
169
|
+
destroy(): void;
|
|
170
|
+
}
|
|
171
|
+
declare class IdempotencyError extends Error {
|
|
172
|
+
readonly code: 'DUPLICATE_REQUEST' | 'REQUEST_IN_PROGRESS' | 'REQUEST_MISMATCH';
|
|
173
|
+
constructor(message: string, code: 'DUPLICATE_REQUEST' | 'REQUEST_IN_PROGRESS' | 'REQUEST_MISMATCH');
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Idempotency manager
|
|
177
|
+
*/
|
|
178
|
+
declare class IdempotencyManager {
|
|
179
|
+
private store;
|
|
180
|
+
private ttl;
|
|
181
|
+
private prefix;
|
|
182
|
+
constructor(config?: IdempotencyConfig);
|
|
183
|
+
/**
|
|
184
|
+
* Generate a unique idempotency key
|
|
185
|
+
*/
|
|
186
|
+
generateKey(): string;
|
|
187
|
+
/**
|
|
188
|
+
* Hash request parameters for validation
|
|
189
|
+
*/
|
|
190
|
+
private hashRequest;
|
|
191
|
+
/**
|
|
192
|
+
* Execute operation with idempotency protection
|
|
193
|
+
*/
|
|
194
|
+
execute<T>(key: string, params: unknown, operation: () => Promise<T>): Promise<Result<T, IdempotencyError>>;
|
|
195
|
+
/**
|
|
196
|
+
* Check if operation with key was already completed
|
|
197
|
+
*/
|
|
198
|
+
wasCompleted(key: string): Promise<boolean>;
|
|
199
|
+
/**
|
|
200
|
+
* Get cached result for key
|
|
201
|
+
*/
|
|
202
|
+
getCached<T>(key: string): Promise<T | null>;
|
|
203
|
+
/**
|
|
204
|
+
* Invalidate a key (force re-execution on next call)
|
|
205
|
+
*/
|
|
206
|
+
invalidate(key: string): Promise<void>;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Create idempotency manager
|
|
210
|
+
*/
|
|
211
|
+
declare function createIdempotencyManager(config?: IdempotencyConfig): IdempotencyManager;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Transaction Type Detection & Classification
|
|
215
|
+
*
|
|
216
|
+
* Distinguishes between:
|
|
217
|
+
* - Monetization-managed transactions (library-controlled, strict rules)
|
|
218
|
+
* - Manual admin transactions (flexible, admin-controlled)
|
|
219
|
+
*
|
|
220
|
+
* @module @classytic/revenue/utils/transaction-type
|
|
221
|
+
*/
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Transaction types with different protection rules
|
|
225
|
+
*/
|
|
226
|
+
declare const TRANSACTION_MANAGEMENT_TYPE: {
|
|
227
|
+
readonly MONETIZATION: "monetization";
|
|
228
|
+
readonly MANUAL: "manual";
|
|
229
|
+
};
|
|
230
|
+
type TransactionManagementType = typeof TRANSACTION_MANAGEMENT_TYPE[keyof typeof TRANSACTION_MANAGEMENT_TYPE];
|
|
231
|
+
/**
|
|
232
|
+
* Check if transaction is monetization-managed
|
|
233
|
+
*
|
|
234
|
+
* Monetization-managed means:
|
|
235
|
+
* - Created through subscription/purchase flows via the library
|
|
236
|
+
* - Status controlled by payment webhooks/verification
|
|
237
|
+
* - Amount/commission calculated by library
|
|
238
|
+
* - Protected fields: status, amount, commission, gateway, verifiedAt, verifiedBy
|
|
239
|
+
*
|
|
240
|
+
* @param transaction - Transaction document or data
|
|
241
|
+
* @param options - Options
|
|
242
|
+
*/
|
|
243
|
+
declare function isMonetizationTransaction(transaction: Partial<TransactionDocument>, options?: TransactionTypeOptions): boolean;
|
|
244
|
+
/**
|
|
245
|
+
* Check if transaction is manual admin transaction
|
|
246
|
+
*
|
|
247
|
+
* Manual transactions:
|
|
248
|
+
* - Created directly by admins for operational expenses/income
|
|
249
|
+
* - Can be self-verified by admins
|
|
250
|
+
* - More flexible updates allowed
|
|
251
|
+
* - No commission/gateway complexity
|
|
252
|
+
*
|
|
253
|
+
* @param transaction - Transaction document or data
|
|
254
|
+
* @param options - Options (same as isMonetizationTransaction)
|
|
255
|
+
*/
|
|
256
|
+
declare function isManualTransaction(transaction: Partial<TransactionDocument>, options?: TransactionTypeOptions): boolean;
|
|
257
|
+
/**
|
|
258
|
+
* Get transaction type
|
|
259
|
+
*
|
|
260
|
+
* @param transaction - Transaction document or data
|
|
261
|
+
* @param options - Options (same as isMonetizationTransaction)
|
|
262
|
+
*/
|
|
263
|
+
declare function getTransactionType(transaction: Partial<TransactionDocument>, options?: TransactionTypeOptions): TransactionManagementType;
|
|
264
|
+
/**
|
|
265
|
+
* Protected fields for monetization transactions
|
|
266
|
+
* These fields cannot be updated directly by admins
|
|
267
|
+
*/
|
|
268
|
+
declare const PROTECTED_MONETIZATION_FIELDS: readonly ["status", "amount", "platformCommission", "netAmount", "verifiedAt", "verifiedBy", "gateway", "webhook", "metadata.commission", "metadata.gateway", "type", "category", "referenceModel", "referenceId"];
|
|
269
|
+
/**
|
|
270
|
+
* Editable fields for monetization transactions (before verification)
|
|
271
|
+
* These fields can be updated by frontend/customer before payment is verified
|
|
272
|
+
*/
|
|
273
|
+
declare const EDITABLE_MONETIZATION_FIELDS_PRE_VERIFICATION: readonly ["reference", "paymentDetails", "notes"];
|
|
274
|
+
/**
|
|
275
|
+
* Allowed fields for manual transaction creation
|
|
276
|
+
*/
|
|
277
|
+
declare const MANUAL_TRANSACTION_CREATE_FIELDS: readonly ["organizationId", "type", "category", "amount", "method", "reference", "paymentDetails", "notes", "date", "description"];
|
|
278
|
+
/**
|
|
279
|
+
* Allowed fields for manual transaction updates
|
|
280
|
+
*/
|
|
281
|
+
declare const MANUAL_TRANSACTION_UPDATE_FIELDS: readonly ["amount", "method", "reference", "paymentDetails", "notes", "date", "description"];
|
|
282
|
+
/**
|
|
283
|
+
* Get allowed update fields based on transaction type and status
|
|
284
|
+
*
|
|
285
|
+
* @param transaction - Transaction document
|
|
286
|
+
* @param options - Options for transaction type detection
|
|
287
|
+
*/
|
|
288
|
+
declare function getAllowedUpdateFields(transaction: Partial<TransactionDocument>, options?: TransactionTypeOptions): readonly string[];
|
|
289
|
+
/**
|
|
290
|
+
* Validate if field update is allowed
|
|
291
|
+
*
|
|
292
|
+
* @param transaction - Transaction document
|
|
293
|
+
* @param fieldName - Field being updated
|
|
294
|
+
* @param options - Options for transaction type detection
|
|
295
|
+
*/
|
|
296
|
+
declare function validateFieldUpdate(transaction: Partial<TransactionDocument>, fieldName: string, options?: TransactionTypeOptions): FieldUpdateValidationResult;
|
|
297
|
+
/**
|
|
298
|
+
* Check if transaction can be self-verified by admin
|
|
299
|
+
*
|
|
300
|
+
* @param transaction - Transaction document
|
|
301
|
+
* @param options - Options for transaction type detection
|
|
302
|
+
*/
|
|
303
|
+
declare function canSelfVerify(transaction: Partial<TransactionDocument>, options?: TransactionTypeOptions): boolean;
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Logger Abstraction for Monetization Library
|
|
307
|
+
*
|
|
308
|
+
* Defaults to console for standalone usage
|
|
309
|
+
* Can be overridden with custom logger (pino, winston, etc)
|
|
310
|
+
*
|
|
311
|
+
* Usage:
|
|
312
|
+
* ```typescript
|
|
313
|
+
* import { setLogger } from '@classytic/revenue';
|
|
314
|
+
*
|
|
315
|
+
* // Optional: Use your own logger
|
|
316
|
+
* setLogger(myPinoLogger);
|
|
317
|
+
* ```
|
|
318
|
+
*/
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Set custom logger implementation
|
|
322
|
+
* @param customLogger - Logger instance with info, warn, error, debug methods
|
|
323
|
+
*/
|
|
324
|
+
declare function setLogger(customLogger: Logger): void;
|
|
325
|
+
/**
|
|
326
|
+
* Logger proxy - delegates to current logger implementation
|
|
327
|
+
*/
|
|
328
|
+
declare const logger: Logger;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Commission Calculation Utility
|
|
332
|
+
* @classytic/revenue
|
|
333
|
+
*
|
|
334
|
+
* Handles platform commission calculation with gateway fee deduction
|
|
335
|
+
*/
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Build commission object for transaction
|
|
339
|
+
*
|
|
340
|
+
* @param amount - Transaction amount
|
|
341
|
+
* @param commissionRate - Commission rate (0 to 1, e.g., 0.10 for 10%)
|
|
342
|
+
* @param gatewayFeeRate - Gateway fee rate (0 to 1, e.g., 0.018 for 1.8%)
|
|
343
|
+
* @returns Commission object or null
|
|
344
|
+
*/
|
|
345
|
+
declare function calculateCommission(amount: number, commissionRate: number, gatewayFeeRate?: number): CommissionInfo | null;
|
|
346
|
+
/**
|
|
347
|
+
* Reverse commission on refund (proportional)
|
|
348
|
+
*
|
|
349
|
+
* @param originalCommission - Original commission object
|
|
350
|
+
* @param originalAmount - Original transaction amount
|
|
351
|
+
* @param refundAmount - Amount being refunded
|
|
352
|
+
* @returns Reversed commission or null
|
|
353
|
+
*/
|
|
354
|
+
declare function reverseCommission(originalCommission: CommissionInfo | null | undefined, originalAmount: number, refundAmount: number): CommissionInfo | null;
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Commission Split Utilities
|
|
358
|
+
* @classytic/revenue
|
|
359
|
+
*
|
|
360
|
+
* Multi-party commission split calculation for affiliate/referral systems
|
|
361
|
+
*/
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Calculate multi-party commission splits
|
|
365
|
+
*
|
|
366
|
+
* @param amount - Transaction amount
|
|
367
|
+
* @param splitRules - Split configuration
|
|
368
|
+
* @param gatewayFeeRate - Gateway fee rate (optional)
|
|
369
|
+
* @returns Split objects
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* calculateSplits(1000, [
|
|
373
|
+
* { type: 'platform_commission', recipientId: 'platform', recipientType: 'platform', rate: 0.10 },
|
|
374
|
+
* { type: 'affiliate_commission', recipientId: 'affiliate-123', recipientType: 'user', rate: 0.02 },
|
|
375
|
+
* ], 0.018);
|
|
376
|
+
*
|
|
377
|
+
* Returns:
|
|
378
|
+
* [
|
|
379
|
+
* { type: 'platform_commission', recipientId: 'platform', grossAmount: 100, gatewayFeeAmount: 18, netAmount: 82, ... },
|
|
380
|
+
* { type: 'affiliate_commission', recipientId: 'affiliate-123', grossAmount: 20, gatewayFeeAmount: 0, netAmount: 20, ... },
|
|
381
|
+
* ]
|
|
382
|
+
*/
|
|
383
|
+
declare function calculateSplits(amount: number, splitRules?: SplitRule[], gatewayFeeRate?: number): SplitInfo[];
|
|
384
|
+
/**
|
|
385
|
+
* Calculate organization payout after splits
|
|
386
|
+
*
|
|
387
|
+
* @param amount - Total transaction amount
|
|
388
|
+
* @param splits - Calculated splits
|
|
389
|
+
* @returns Amount organization receives
|
|
390
|
+
*/
|
|
391
|
+
declare function calculateOrganizationPayout(amount: number, splits?: SplitInfo[]): number;
|
|
392
|
+
/**
|
|
393
|
+
* Reverse splits proportionally on refund
|
|
394
|
+
*
|
|
395
|
+
* @param originalSplits - Original split objects
|
|
396
|
+
* @param originalAmount - Original transaction amount
|
|
397
|
+
* @param refundAmount - Amount being refunded
|
|
398
|
+
* @returns Reversed splits
|
|
399
|
+
*/
|
|
400
|
+
declare function reverseSplits(originalSplits: SplitInfo[] | undefined | null, originalAmount: number, refundAmount: number): SplitInfo[];
|
|
401
|
+
/**
|
|
402
|
+
* Build commission object with splits support
|
|
403
|
+
* Backward compatible with existing calculateCommission
|
|
404
|
+
*
|
|
405
|
+
* @param amount - Transaction amount
|
|
406
|
+
* @param commissionRate - Platform commission rate
|
|
407
|
+
* @param gatewayFeeRate - Gateway fee rate
|
|
408
|
+
* @param options - Additional options
|
|
409
|
+
* @returns Commission with optional splits
|
|
410
|
+
*/
|
|
411
|
+
declare function calculateCommissionWithSplits(amount: number, commissionRate: number, gatewayFeeRate?: number, options?: CommissionWithSplitsOptions): CommissionInfo | null;
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Category Resolver Utility
|
|
415
|
+
* @classytic/revenue
|
|
416
|
+
*
|
|
417
|
+
* Resolves transaction category based on referenceModel and categoryMappings
|
|
418
|
+
*/
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Resolve category for a transaction based on entity and monetizationType
|
|
422
|
+
*
|
|
423
|
+
* Resolution Logic:
|
|
424
|
+
* 1. If categoryMappings[entity] exists → use it
|
|
425
|
+
* 2. Otherwise → fall back to default library category
|
|
426
|
+
*
|
|
427
|
+
* @param entity - The logical entity/identifier (e.g., 'Order', 'PlatformSubscription', 'Membership')
|
|
428
|
+
* NOTE: This is NOT a database model name - it's just a logical identifier
|
|
429
|
+
* @param monetizationType - The monetization type ('subscription', 'purchase', 'free')
|
|
430
|
+
* @param categoryMappings - User-defined category mappings from config
|
|
431
|
+
* @returns Category name for the transaction
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* // With mapping defined
|
|
435
|
+
* resolveCategory('Order', 'subscription', { Order: 'order_subscription' })
|
|
436
|
+
* // Returns: 'order_subscription'
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* // Without mapping, falls back to library default
|
|
440
|
+
* resolveCategory('Order', 'subscription', {})
|
|
441
|
+
* // Returns: 'subscription'
|
|
442
|
+
*
|
|
443
|
+
* @example
|
|
444
|
+
* // Different entities with different mappings
|
|
445
|
+
* const mappings = {
|
|
446
|
+
* Order: 'order_subscription',
|
|
447
|
+
* PlatformSubscription: 'platform_subscription',
|
|
448
|
+
* TenantUpgrade: 'tenant_upgrade',
|
|
449
|
+
* Membership: 'gym_membership',
|
|
450
|
+
* Enrollment: 'course_enrollment',
|
|
451
|
+
* };
|
|
452
|
+
* resolveCategory('PlatformSubscription', 'subscription', mappings)
|
|
453
|
+
* // Returns: 'platform_subscription'
|
|
454
|
+
*/
|
|
455
|
+
declare function resolveCategory(entity: string | null | undefined, monetizationType: MonetizationTypeValue, categoryMappings?: Record<string, string>): string;
|
|
456
|
+
/**
|
|
457
|
+
* Validate that a category is defined in user's Transaction model enum
|
|
458
|
+
* This is informational - actual validation happens at Mongoose schema level
|
|
459
|
+
*
|
|
460
|
+
* @param category - Category to validate
|
|
461
|
+
* @param allowedCategories - List of allowed categories
|
|
462
|
+
* @returns Whether category is valid
|
|
463
|
+
*/
|
|
464
|
+
declare function isCategoryValid(category: string, allowedCategories?: string[]): boolean;
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Subscription Period Utilities
|
|
468
|
+
* @classytic/revenue/utils/subscription
|
|
469
|
+
*
|
|
470
|
+
* Universal period calculation, proration, and date utilities
|
|
471
|
+
*/
|
|
472
|
+
|
|
473
|
+
type DurationUnit = 'days' | 'day' | 'weeks' | 'week' | 'months' | 'month' | 'years' | 'year';
|
|
474
|
+
/**
|
|
475
|
+
* Add duration to date
|
|
476
|
+
*/
|
|
477
|
+
declare function addDuration(startDate: Date, duration: number, unit?: DurationUnit): Date;
|
|
478
|
+
/**
|
|
479
|
+
* Calculate subscription period start/end dates
|
|
480
|
+
*/
|
|
481
|
+
declare function calculatePeriodRange(params: PeriodRangeParams): PeriodRangeResult;
|
|
482
|
+
/**
|
|
483
|
+
* Calculate prorated refund amount for unused period
|
|
484
|
+
*/
|
|
485
|
+
declare function calculateProratedAmount(params: ProratedAmountParams): number;
|
|
486
|
+
/**
|
|
487
|
+
* Convert interval + count to duration/unit
|
|
488
|
+
*/
|
|
489
|
+
declare function resolveIntervalToDuration(interval?: string, intervalCount?: number): DurationResult;
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Subscription Action Utilities
|
|
493
|
+
* @classytic/revenue/utils/subscription
|
|
494
|
+
*
|
|
495
|
+
* Eligibility checks for subscription actions
|
|
496
|
+
*/
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Check if subscription is active
|
|
500
|
+
*/
|
|
501
|
+
declare function isSubscriptionActive(subscription: Partial<SubscriptionDocument> | null | undefined): boolean;
|
|
502
|
+
/**
|
|
503
|
+
* Check if can renew
|
|
504
|
+
*/
|
|
505
|
+
declare function canRenewSubscription(entity: SubscriptionEntity | null | undefined): boolean;
|
|
506
|
+
/**
|
|
507
|
+
* Check if can cancel
|
|
508
|
+
*/
|
|
509
|
+
declare function canCancelSubscription(entity: SubscriptionEntity | null | undefined): boolean;
|
|
510
|
+
/**
|
|
511
|
+
* Check if can pause
|
|
512
|
+
*/
|
|
513
|
+
declare function canPauseSubscription(entity: SubscriptionEntity | null | undefined): boolean;
|
|
514
|
+
/**
|
|
515
|
+
* Check if can resume
|
|
516
|
+
*/
|
|
517
|
+
declare function canResumeSubscription(entity: SubscriptionEntity | null | undefined): boolean;
|
|
518
|
+
|
|
519
|
+
export { MANUAL_TRANSACTION_UPDATE_FIELDS as A, addDuration as B, calculatePeriodRange as C, calculateProratedAmount as D, EDITABLE_MONETIZATION_FIELDS_PRE_VERIFICATION as E, resolveIntervalToDuration as F, isSubscriptionActive as G, canRenewSubscription as H, IdempotencyManager as I, canCancelSubscription as J, canPauseSubscription as K, canResumeSubscription as L, Money as M, type TransactionManagementType as N, type DurationUnit as O, PROTECTED_MONETIZATION_FIELDS as P, TRANSACTION_MANAGEMENT_TYPE as T, type MoneyValue as a, MemoryIdempotencyStore as b, IdempotencyError as c, createIdempotencyManager as d, type IdempotencyRecord as e, fromSmallestUnit as f, type IdempotencyStore as g, type IdempotencyConfig as h, calculateCommission as i, calculateSplits as j, calculateOrganizationPayout as k, logger as l, reverseSplits as m, calculateCommissionWithSplits as n, resolveCategory as o, isCategoryValid as p, isMonetizationTransaction as q, reverseCommission as r, setLogger as s, toSmallestUnit as t, isManualTransaction as u, getTransactionType as v, getAllowedUpdateFields as w, validateFieldUpdate as x, canSelfVerify as y, MANUAL_TRANSACTION_CREATE_FIELDS as z };
|