@classytic/revenue 0.2.4 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +4362 -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 +906 -0
- package/dist/schemas/index.js +533 -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-CETjPq10.d.ts +976 -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,132 @@
|
|
|
1
|
+
import { q as CreateIntentParams, r as PaymentIntentData, s as PaymentResultData, t as RefundResultData, u as WebhookEventData, v as ProviderCapabilities } from '../index-ChVD3P9k.js';
|
|
2
|
+
import 'mongoose';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Payment Provider Base Class
|
|
6
|
+
* @classytic/revenue
|
|
7
|
+
*
|
|
8
|
+
* Abstract base class for all payment providers
|
|
9
|
+
* Inspired by: Vercel AI SDK, Stripe SDK
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Payment Intent - standardized response from createIntent
|
|
14
|
+
*/
|
|
15
|
+
declare class PaymentIntent implements PaymentIntentData {
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly sessionId: string | null;
|
|
18
|
+
readonly paymentIntentId: string | null;
|
|
19
|
+
readonly provider: string;
|
|
20
|
+
readonly status: string;
|
|
21
|
+
readonly amount: number;
|
|
22
|
+
readonly currency: string;
|
|
23
|
+
readonly metadata: Record<string, unknown>;
|
|
24
|
+
readonly clientSecret?: string;
|
|
25
|
+
readonly paymentUrl?: string;
|
|
26
|
+
readonly instructions?: string;
|
|
27
|
+
readonly raw?: unknown;
|
|
28
|
+
constructor(data: PaymentIntentData);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Payment Result - standardized response from verifyPayment
|
|
32
|
+
*/
|
|
33
|
+
declare class PaymentResult implements PaymentResultData {
|
|
34
|
+
readonly id: string;
|
|
35
|
+
readonly provider: string;
|
|
36
|
+
readonly status: 'succeeded' | 'failed' | 'processing';
|
|
37
|
+
readonly amount?: number;
|
|
38
|
+
readonly currency: string;
|
|
39
|
+
readonly paidAt?: Date;
|
|
40
|
+
readonly metadata: Record<string, unknown>;
|
|
41
|
+
readonly raw?: unknown;
|
|
42
|
+
constructor(data: PaymentResultData);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Refund Result - standardized response from refund
|
|
46
|
+
*/
|
|
47
|
+
declare class RefundResult implements RefundResultData {
|
|
48
|
+
readonly id: string;
|
|
49
|
+
readonly provider: string;
|
|
50
|
+
readonly status: 'succeeded' | 'failed' | 'processing';
|
|
51
|
+
readonly amount?: number;
|
|
52
|
+
readonly currency: string;
|
|
53
|
+
readonly refundedAt?: Date;
|
|
54
|
+
readonly reason?: string;
|
|
55
|
+
readonly metadata: Record<string, unknown>;
|
|
56
|
+
readonly raw?: unknown;
|
|
57
|
+
constructor(data: RefundResultData);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Webhook Event - standardized webhook event
|
|
61
|
+
*/
|
|
62
|
+
declare class WebhookEvent implements WebhookEventData {
|
|
63
|
+
readonly id: string;
|
|
64
|
+
readonly provider: string;
|
|
65
|
+
readonly type: string;
|
|
66
|
+
readonly data: {
|
|
67
|
+
sessionId?: string;
|
|
68
|
+
paymentIntentId?: string;
|
|
69
|
+
[key: string]: unknown;
|
|
70
|
+
};
|
|
71
|
+
readonly createdAt?: Date;
|
|
72
|
+
readonly raw?: unknown;
|
|
73
|
+
constructor(data: WebhookEventData);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Base Payment Provider
|
|
77
|
+
* All payment providers must extend this class
|
|
78
|
+
*/
|
|
79
|
+
declare abstract class PaymentProvider {
|
|
80
|
+
readonly config: Record<string, unknown>;
|
|
81
|
+
readonly name: string;
|
|
82
|
+
constructor(config?: Record<string, unknown>);
|
|
83
|
+
/**
|
|
84
|
+
* Create a payment intent
|
|
85
|
+
* @param params - Payment parameters
|
|
86
|
+
* @returns Promise<PaymentIntent>
|
|
87
|
+
*/
|
|
88
|
+
abstract createIntent(params: CreateIntentParams): Promise<PaymentIntent>;
|
|
89
|
+
/**
|
|
90
|
+
* Verify a payment
|
|
91
|
+
* @param intentId - Payment intent ID
|
|
92
|
+
* @returns Promise<PaymentResult>
|
|
93
|
+
*/
|
|
94
|
+
abstract verifyPayment(intentId: string): Promise<PaymentResult>;
|
|
95
|
+
/**
|
|
96
|
+
* Get payment status
|
|
97
|
+
* @param intentId - Payment intent ID
|
|
98
|
+
* @returns Promise<PaymentResult>
|
|
99
|
+
*/
|
|
100
|
+
abstract getStatus(intentId: string): Promise<PaymentResult>;
|
|
101
|
+
/**
|
|
102
|
+
* Refund a payment
|
|
103
|
+
* @param paymentId - Payment ID
|
|
104
|
+
* @param amount - Amount to refund (optional, full refund if not provided)
|
|
105
|
+
* @param options - Refund options
|
|
106
|
+
* @returns Promise<RefundResult>
|
|
107
|
+
*/
|
|
108
|
+
abstract refund(paymentId: string, amount?: number | null, options?: {
|
|
109
|
+
reason?: string;
|
|
110
|
+
}): Promise<RefundResult>;
|
|
111
|
+
/**
|
|
112
|
+
* Handle webhook from provider
|
|
113
|
+
* @param payload - Webhook payload
|
|
114
|
+
* @param headers - Request headers (for signature verification)
|
|
115
|
+
* @returns Promise<WebhookEvent>
|
|
116
|
+
*/
|
|
117
|
+
abstract handleWebhook(payload: unknown, headers?: Record<string, string>): Promise<WebhookEvent>;
|
|
118
|
+
/**
|
|
119
|
+
* Verify webhook signature (optional)
|
|
120
|
+
* @param payload - Webhook payload
|
|
121
|
+
* @param signature - Webhook signature
|
|
122
|
+
* @returns boolean
|
|
123
|
+
*/
|
|
124
|
+
verifyWebhookSignature(_payload: unknown, _signature: string): boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Get provider capabilities
|
|
127
|
+
* @returns ProviderCapabilities
|
|
128
|
+
*/
|
|
129
|
+
getCapabilities(): ProviderCapabilities;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export { PaymentIntent, PaymentProvider, PaymentProvider as PaymentProviderDefault, PaymentResult, RefundResult, WebhookEvent };
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// @classytic/revenue - Enterprise Revenue Management System
|
|
2
|
+
|
|
3
|
+
// src/providers/base.ts
|
|
4
|
+
var PaymentIntent = class {
|
|
5
|
+
id;
|
|
6
|
+
sessionId;
|
|
7
|
+
paymentIntentId;
|
|
8
|
+
provider;
|
|
9
|
+
status;
|
|
10
|
+
amount;
|
|
11
|
+
currency;
|
|
12
|
+
metadata;
|
|
13
|
+
clientSecret;
|
|
14
|
+
paymentUrl;
|
|
15
|
+
instructions;
|
|
16
|
+
raw;
|
|
17
|
+
constructor(data) {
|
|
18
|
+
this.id = data.id;
|
|
19
|
+
this.sessionId = data.sessionId ?? null;
|
|
20
|
+
this.paymentIntentId = data.paymentIntentId ?? null;
|
|
21
|
+
this.provider = data.provider;
|
|
22
|
+
this.status = data.status;
|
|
23
|
+
this.amount = data.amount;
|
|
24
|
+
this.currency = data.currency ?? "BDT";
|
|
25
|
+
this.metadata = data.metadata ?? {};
|
|
26
|
+
this.clientSecret = data.clientSecret;
|
|
27
|
+
this.paymentUrl = data.paymentUrl;
|
|
28
|
+
this.instructions = data.instructions;
|
|
29
|
+
this.raw = data.raw;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var PaymentResult = class {
|
|
33
|
+
id;
|
|
34
|
+
provider;
|
|
35
|
+
status;
|
|
36
|
+
amount;
|
|
37
|
+
currency;
|
|
38
|
+
paidAt;
|
|
39
|
+
metadata;
|
|
40
|
+
raw;
|
|
41
|
+
constructor(data) {
|
|
42
|
+
this.id = data.id;
|
|
43
|
+
this.provider = data.provider;
|
|
44
|
+
this.status = data.status;
|
|
45
|
+
this.amount = data.amount;
|
|
46
|
+
this.currency = data.currency ?? "BDT";
|
|
47
|
+
this.paidAt = data.paidAt;
|
|
48
|
+
this.metadata = data.metadata ?? {};
|
|
49
|
+
this.raw = data.raw;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var RefundResult = class {
|
|
53
|
+
id;
|
|
54
|
+
provider;
|
|
55
|
+
status;
|
|
56
|
+
amount;
|
|
57
|
+
currency;
|
|
58
|
+
refundedAt;
|
|
59
|
+
reason;
|
|
60
|
+
metadata;
|
|
61
|
+
raw;
|
|
62
|
+
constructor(data) {
|
|
63
|
+
this.id = data.id;
|
|
64
|
+
this.provider = data.provider;
|
|
65
|
+
this.status = data.status;
|
|
66
|
+
this.amount = data.amount;
|
|
67
|
+
this.currency = data.currency ?? "BDT";
|
|
68
|
+
this.refundedAt = data.refundedAt;
|
|
69
|
+
this.reason = data.reason;
|
|
70
|
+
this.metadata = data.metadata ?? {};
|
|
71
|
+
this.raw = data.raw;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var WebhookEvent = class {
|
|
75
|
+
id;
|
|
76
|
+
provider;
|
|
77
|
+
type;
|
|
78
|
+
data;
|
|
79
|
+
createdAt;
|
|
80
|
+
raw;
|
|
81
|
+
constructor(data) {
|
|
82
|
+
this.id = data.id;
|
|
83
|
+
this.provider = data.provider;
|
|
84
|
+
this.type = data.type;
|
|
85
|
+
this.data = data.data;
|
|
86
|
+
this.createdAt = data.createdAt;
|
|
87
|
+
this.raw = data.raw;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
var PaymentProvider = class {
|
|
91
|
+
config;
|
|
92
|
+
name;
|
|
93
|
+
constructor(config = {}) {
|
|
94
|
+
this.config = config;
|
|
95
|
+
this.name = "base";
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Verify webhook signature (optional)
|
|
99
|
+
* @param payload - Webhook payload
|
|
100
|
+
* @param signature - Webhook signature
|
|
101
|
+
* @returns boolean
|
|
102
|
+
*/
|
|
103
|
+
verifyWebhookSignature(_payload, _signature) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Get provider capabilities
|
|
108
|
+
* @returns ProviderCapabilities
|
|
109
|
+
*/
|
|
110
|
+
getCapabilities() {
|
|
111
|
+
return {
|
|
112
|
+
supportsWebhooks: false,
|
|
113
|
+
supportsRefunds: false,
|
|
114
|
+
supportsPartialRefunds: false,
|
|
115
|
+
requiresManualVerification: true
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export { PaymentIntent, PaymentProvider, PaymentResult, RefundResult, WebhookEvent };
|
|
121
|
+
//# sourceMappingURL=index.js.map
|
|
122
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/providers/base.ts"],"names":[],"mappings":";;;AAoBO,IAAM,gBAAN,MAAiD;AAAA,EACtC,EAAA;AAAA,EACA,SAAA;AAAA,EACA,eAAA;AAAA,EACA,QAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,YAAA;AAAA,EACA,UAAA;AAAA,EACA,YAAA;AAAA,EACA,GAAA;AAAA,EAEhB,YAAY,IAAA,EAAyB;AACnC,IAAA,IAAA,CAAK,KAAK,IAAA,CAAK,EAAA;AACf,IAAA,IAAA,CAAK,SAAA,GAAY,KAAK,SAAA,IAAa,IAAA;AACnC,IAAA,IAAA,CAAK,eAAA,GAAkB,KAAK,eAAA,IAAmB,IAAA;AAC/C,IAAA,IAAA,CAAK,WAAW,IA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AAA;AAAA,EAqDA,sBAAA,CAAuB,UAAmB,UAAA,EAA6B;AAErE,IAAA,OAAO,IAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,eAAA,GAAwC;AACtC,IAAA,OAAO;AAAA,MACL,gBAAA,EAAkB,KAAA;AAAA,MAClB,eAAA,EAAiB,KAAA;AAAA,MACjB,sBAAA,EAAwB,KAAA;AAAA,MACxB,0BAAA,EAA4B;AAAA,KAC9B;AAAA,EACF;AACF","file":"index.js","sourcesContent":["/**\r\n * Payment Provider Base Class\r\n * @classytic/revenue\r\n *\r\n * Abstract base class for all payment providers\r\n * Inspired by: Vercel AI SDK, Stripe SDK\r\n */\r\n\r\nimport type {\r\n CreateIntentParams,\r\n PaymentIntentData,\r\n PaymentResultData,\r\n RefundResultData,\r\n WebhookEventData,\r\n ProviderCapabilities,\r\n} from '../types/index.js';\r\n\r\n/**\r\n * Payment Intent - standardized response from createIntent\r\n */\r\nexport class PaymentIntent implements PaymentIntentData {\r\n public readonly id: string;\r\n public readonly sessionId: string | null;\r\n public readonly paymentIntentId: string | null;\r\n public readonly provider: string;\r\n public readonly status: string;\r\n public readonly amount: number;\r\n public readonly currency: string;\r\n public readonly metadata: Record<string, unknown>;\r\n public readonly clientSecret?: string;\r\n public readonly paymentUrl?: string;\r\n public readonly instructions?: string;\r\n public readonly raw?: unknown;\r\n\r\n constructor(data: PaymentIntentData) {\r\n this.id = data.id;\r\n this.sessionId = data.sessionId ?? null;\r\n this.paymentIntentId = data.paymentIntentId ?? null;\r\n this.provider = data.provider;\r\n this.status = data.status;\r\n this.amount = data.amount;\r\n this.currency = data.currency ?? 'BDT';\r\n this.metadata = data.metadata ?? {};\r\n this.clientSecret = data.clientSecret;\r\n this.paymentUrl = data.paymentUrl;\r\n this.instructions = data.instructions;\r\n this.raw = data.raw;\r\n }\r\n}\r\n\r\n/**\r\n * Payment Result - standardized response from verifyPayment\r\n */\r\nexport class PaymentResult implements PaymentResultData {\r\n public readonly id: string;\r\n public readonly provider: string;\r\n public readonly status: 'succeeded' | 'failed' | 'processing';\r\n public readonly amount?: number;\r\n public readonly currency: string;\r\n public readonly paidAt?: Date;\r\n public readonly metadata: Record<string, unknown>;\r\n public readonly raw?: unknown;\r\n\r\n constructor(data: PaymentResultData) {\r\n this.id = data.id;\r\n this.provider = data.provider;\r\n this.status = data.status;\r\n this.amount = data.amount;\r\n this.currency = data.currency ?? 'BDT';\r\n this.paidAt = data.paidAt;\r\n this.metadata = data.metadata ?? {};\r\n this.raw = data.raw;\r\n }\r\n}\r\n\r\n/**\r\n * Refund Result - standardized response from refund\r\n */\r\nexport class RefundResult implements RefundResultData {\r\n public readonly id: string;\r\n public readonly provider: string;\r\n public readonly status: 'succeeded' | 'failed' | 'processing';\r\n public readonly amount?: number;\r\n public readonly currency: string;\r\n public readonly refundedAt?: Date;\r\n public readonly reason?: string;\r\n public readonly metadata: Record<string, unknown>;\r\n public readonly raw?: unknown;\r\n\r\n constructor(data: RefundResultData) {\r\n this.id = data.id;\r\n this.provider = data.provider;\r\n this.status = data.status;\r\n this.amount = data.amount;\r\n this.currency = data.currency ?? 'BDT';\r\n this.refundedAt = data.refundedAt;\r\n this.reason = data.reason;\r\n this.metadata = data.metadata ?? {};\r\n this.raw = data.raw;\r\n }\r\n}\r\n\r\n/**\r\n * Webhook Event - standardized webhook event\r\n */\r\nexport class WebhookEvent implements WebhookEventData {\r\n public readonly id: string;\r\n public readonly provider: string;\r\n public readonly type: string;\r\n public readonly data: { sessionId?: string; paymentIntentId?: string; [key: string]: unknown };\r\n public readonly createdAt?: Date;\r\n public readonly raw?: unknown;\r\n\r\n constructor(data: WebhookEventData) {\r\n this.id = data.id;\r\n this.provider = data.provider;\r\n this.type = data.type;\r\n this.data = data.data;\r\n this.createdAt = data.createdAt;\r\n this.raw = data.raw;\r\n }\r\n}\r\n\r\n/**\r\n * Base Payment Provider\r\n * All payment providers must extend this class\r\n */\r\nexport abstract class PaymentProvider {\r\n public readonly config: Record<string, unknown>;\r\n public readonly name: string;\r\n\r\n constructor(config: Record<string, unknown> = {}) {\r\n this.config = config;\r\n this.name = 'base'; // Override in subclass\r\n }\r\n\r\n /**\r\n * Create a payment intent\r\n * @param params - Payment parameters\r\n * @returns Promise<PaymentIntent>\r\n */\r\n abstract createIntent(params: CreateIntentParams): Promise<PaymentIntent>;\r\n\r\n /**\r\n * Verify a payment\r\n * @param intentId - Payment intent ID\r\n * @returns Promise<PaymentResult>\r\n */\r\n abstract verifyPayment(intentId: string): Promise<PaymentResult>;\r\n\r\n /**\r\n * Get payment status\r\n * @param intentId - Payment intent ID\r\n * @returns Promise<PaymentResult>\r\n */\r\n abstract getStatus(intentId: string): Promise<PaymentResult>;\r\n\r\n /**\r\n * Refund a payment\r\n * @param paymentId - Payment ID\r\n * @param amount - Amount to refund (optional, full refund if not provided)\r\n * @param options - Refund options\r\n * @returns Promise<RefundResult>\r\n */\r\n abstract refund(\r\n paymentId: string,\r\n amount?: number | null,\r\n options?: { reason?: string }\r\n ): Promise<RefundResult>;\r\n\r\n /**\r\n * Handle webhook from provider\r\n * @param payload - Webhook payload\r\n * @param headers - Request headers (for signature verification)\r\n * @returns Promise<WebhookEvent>\r\n */\r\n abstract handleWebhook(\r\n payload: unknown,\r\n headers?: Record<string, string>\r\n ): Promise<WebhookEvent>;\r\n\r\n /**\r\n * Verify webhook signature (optional)\r\n * @param payload - Webhook payload\r\n * @param signature - Webhook signature\r\n * @returns boolean\r\n */\r\n verifyWebhookSignature(_payload: unknown, _signature: string): boolean {\r\n // Override in subclass if provider supports webhook signatures\r\n return true;\r\n }\r\n\r\n /**\r\n * Get provider capabilities\r\n * @returns ProviderCapabilities\r\n */\r\n getCapabilities(): ProviderCapabilities {\r\n return {\r\n supportsWebhooks: false,\r\n supportsRefunds: false,\r\n supportsPartialRefunds: false,\r\n requiresManualVerification: true,\r\n };\r\n }\r\n}\r\n\r\nexport default PaymentProvider;\r\n\r\n"]}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result Type - Rust-inspired error handling
|
|
3
|
+
* @classytic/revenue
|
|
4
|
+
*
|
|
5
|
+
* No more try/catch - explicit, type-safe error handling
|
|
6
|
+
* Inspired by: Rust Result<T, E>, neverthrow, Effect-TS
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Success result
|
|
10
|
+
*/
|
|
11
|
+
interface Ok<T> {
|
|
12
|
+
readonly ok: true;
|
|
13
|
+
readonly value: T;
|
|
14
|
+
readonly error?: never;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Error result
|
|
18
|
+
*/
|
|
19
|
+
interface Err<E> {
|
|
20
|
+
readonly ok: false;
|
|
21
|
+
readonly error: E;
|
|
22
|
+
readonly value?: never;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Create a success result
|
|
26
|
+
*/
|
|
27
|
+
declare function ok<T>(value: T): Ok<T>;
|
|
28
|
+
/**
|
|
29
|
+
* Create an error result
|
|
30
|
+
*/
|
|
31
|
+
declare function err<E>(error: E): Err<E>;
|
|
32
|
+
/**
|
|
33
|
+
* Check if result is Ok
|
|
34
|
+
*/
|
|
35
|
+
declare function isOk<T, E>(result: Result<T, E>): result is Ok<T>;
|
|
36
|
+
/**
|
|
37
|
+
* Check if result is Err
|
|
38
|
+
*/
|
|
39
|
+
declare function isErr<T, E>(result: Result<T, E>): result is Err<E>;
|
|
40
|
+
/**
|
|
41
|
+
* Unwrap a result, throwing if it's an error
|
|
42
|
+
* Use sparingly - prefer pattern matching
|
|
43
|
+
*/
|
|
44
|
+
declare function unwrap<T, E>(result: Result<T, E>): T;
|
|
45
|
+
/**
|
|
46
|
+
* Unwrap a result with a default value
|
|
47
|
+
*/
|
|
48
|
+
declare function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T;
|
|
49
|
+
/**
|
|
50
|
+
* Map over a successful result
|
|
51
|
+
*/
|
|
52
|
+
declare function map<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
|
|
53
|
+
/**
|
|
54
|
+
* Map over an error result
|
|
55
|
+
*/
|
|
56
|
+
declare function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
|
|
57
|
+
/**
|
|
58
|
+
* Flat map (chain) results
|
|
59
|
+
*/
|
|
60
|
+
declare function flatMap<T, U, E>(result: Result<T, E>, fn: (value: T) => Result<U, E>): Result<U, E>;
|
|
61
|
+
/**
|
|
62
|
+
* Try-catch wrapper that returns Result
|
|
63
|
+
*/
|
|
64
|
+
declare function tryCatch<T, E = Error>(fn: () => Promise<T>, mapError?: (e: unknown) => E): Promise<Result<T, E>>;
|
|
65
|
+
/**
|
|
66
|
+
* Synchronous try-catch wrapper
|
|
67
|
+
*/
|
|
68
|
+
declare function tryCatchSync<T, E = Error>(fn: () => T, mapError?: (e: unknown) => E): Result<T, E>;
|
|
69
|
+
/**
|
|
70
|
+
* Combine multiple results - all must succeed
|
|
71
|
+
*/
|
|
72
|
+
declare function all<T extends readonly Result<unknown, unknown>[]>(results: T): Result<{
|
|
73
|
+
[K in keyof T]: T[K] extends Result<infer U, unknown> ? U : never;
|
|
74
|
+
}, T[number] extends Result<unknown, infer E> ? E : never>;
|
|
75
|
+
/**
|
|
76
|
+
* Pattern match on result
|
|
77
|
+
*/
|
|
78
|
+
declare function match<T, E, U>(result: Result<T, E>, handlers: {
|
|
79
|
+
ok: (value: T) => U;
|
|
80
|
+
err: (error: E) => U;
|
|
81
|
+
}): U;
|
|
82
|
+
/**
|
|
83
|
+
* Result type - either Ok<T> or Err<E>
|
|
84
|
+
*/
|
|
85
|
+
type Result<T, E = Error> = Ok<T> | Err<E>;
|
|
86
|
+
declare const Result: {
|
|
87
|
+
readonly ok: typeof ok;
|
|
88
|
+
readonly err: typeof err;
|
|
89
|
+
readonly isOk: typeof isOk;
|
|
90
|
+
readonly isErr: typeof isErr;
|
|
91
|
+
readonly unwrap: typeof unwrap;
|
|
92
|
+
readonly unwrapOr: typeof unwrapOr;
|
|
93
|
+
readonly map: typeof map;
|
|
94
|
+
readonly mapErr: typeof mapErr;
|
|
95
|
+
readonly flatMap: typeof flatMap;
|
|
96
|
+
readonly tryCatch: typeof tryCatch;
|
|
97
|
+
readonly tryCatchSync: typeof tryCatchSync;
|
|
98
|
+
readonly all: typeof all;
|
|
99
|
+
readonly match: typeof match;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Retry Utilities
|
|
104
|
+
* @classytic/revenue
|
|
105
|
+
*
|
|
106
|
+
* Exponential backoff with jitter for resilient operations
|
|
107
|
+
* Inspired by: AWS SDK retry, Netflix Hystrix, resilience4j
|
|
108
|
+
*/
|
|
109
|
+
|
|
110
|
+
interface RetryConfig {
|
|
111
|
+
/** Maximum number of retry attempts (default: 3) */
|
|
112
|
+
maxAttempts: number;
|
|
113
|
+
/** Base delay in milliseconds (default: 1000) */
|
|
114
|
+
baseDelay: number;
|
|
115
|
+
/** Maximum delay in milliseconds (default: 30000) */
|
|
116
|
+
maxDelay: number;
|
|
117
|
+
/** Backoff multiplier (default: 2) */
|
|
118
|
+
backoffMultiplier: number;
|
|
119
|
+
/** Jitter factor 0-1 (default: 0.1) */
|
|
120
|
+
jitter: number;
|
|
121
|
+
/** Which errors are retryable */
|
|
122
|
+
retryIf?: (error: unknown) => boolean;
|
|
123
|
+
/** Callback on each retry */
|
|
124
|
+
onRetry?: (error: unknown, attempt: number, delay: number) => void;
|
|
125
|
+
}
|
|
126
|
+
interface RetryState {
|
|
127
|
+
attempt: number;
|
|
128
|
+
totalDelay: number;
|
|
129
|
+
errors: Error[];
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Calculate delay with exponential backoff and jitter
|
|
133
|
+
*/
|
|
134
|
+
declare function calculateDelay(attempt: number, config: RetryConfig): number;
|
|
135
|
+
/**
|
|
136
|
+
* Check if error is retryable by default
|
|
137
|
+
*/
|
|
138
|
+
declare function isRetryableError(error: unknown): boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Execute operation with retry logic
|
|
141
|
+
*/
|
|
142
|
+
declare function retry<T>(operation: () => Promise<T>, config?: Partial<RetryConfig>): Promise<T>;
|
|
143
|
+
/**
|
|
144
|
+
* Execute operation with retry, returning Result instead of throwing
|
|
145
|
+
*/
|
|
146
|
+
declare function retryWithResult<T>(operation: () => Promise<T>, config?: Partial<RetryConfig>): Promise<Result<T, RetryExhaustedError>>;
|
|
147
|
+
/**
|
|
148
|
+
* Error thrown when all retries are exhausted
|
|
149
|
+
*/
|
|
150
|
+
declare class RetryExhaustedError extends Error {
|
|
151
|
+
readonly attempts: number;
|
|
152
|
+
readonly errors: Error[];
|
|
153
|
+
constructor(message: string, errors: Error[]);
|
|
154
|
+
/**
|
|
155
|
+
* Get the last error
|
|
156
|
+
*/
|
|
157
|
+
get lastError(): Error | undefined;
|
|
158
|
+
/**
|
|
159
|
+
* Get the first error
|
|
160
|
+
*/
|
|
161
|
+
get firstError(): Error | undefined;
|
|
162
|
+
}
|
|
163
|
+
type CircuitState = 'closed' | 'open' | 'half-open';
|
|
164
|
+
interface CircuitBreakerConfig {
|
|
165
|
+
/** Number of failures before opening circuit */
|
|
166
|
+
failureThreshold: number;
|
|
167
|
+
/** Time in ms to wait before half-opening */
|
|
168
|
+
resetTimeout: number;
|
|
169
|
+
/** Number of successes in half-open to close circuit */
|
|
170
|
+
successThreshold: number;
|
|
171
|
+
/** Monitor window in ms */
|
|
172
|
+
monitorWindow: number;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Circuit breaker for preventing cascade failures
|
|
176
|
+
* Inspired by: Netflix Hystrix, resilience4j
|
|
177
|
+
*/
|
|
178
|
+
declare class CircuitBreaker {
|
|
179
|
+
private state;
|
|
180
|
+
private failures;
|
|
181
|
+
private successes;
|
|
182
|
+
private lastFailure?;
|
|
183
|
+
private config;
|
|
184
|
+
constructor(config?: Partial<CircuitBreakerConfig>);
|
|
185
|
+
/**
|
|
186
|
+
* Execute operation through circuit breaker
|
|
187
|
+
*/
|
|
188
|
+
execute<T>(operation: () => Promise<T>): Promise<T>;
|
|
189
|
+
/**
|
|
190
|
+
* Execute with Result type
|
|
191
|
+
*/
|
|
192
|
+
executeWithResult<T>(operation: () => Promise<T>): Promise<Result<T, CircuitOpenError | Error>>;
|
|
193
|
+
private onSuccess;
|
|
194
|
+
private onFailure;
|
|
195
|
+
private shouldAttemptReset;
|
|
196
|
+
private cleanOldFailures;
|
|
197
|
+
private reset;
|
|
198
|
+
/**
|
|
199
|
+
* Get current circuit state
|
|
200
|
+
*/
|
|
201
|
+
getState(): CircuitState;
|
|
202
|
+
/**
|
|
203
|
+
* Manually reset circuit
|
|
204
|
+
*/
|
|
205
|
+
forceReset(): void;
|
|
206
|
+
/**
|
|
207
|
+
* Get circuit statistics
|
|
208
|
+
*/
|
|
209
|
+
getStats(): {
|
|
210
|
+
state: CircuitState;
|
|
211
|
+
failures: number;
|
|
212
|
+
successes: number;
|
|
213
|
+
lastFailure?: Date;
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Error thrown when circuit is open
|
|
218
|
+
*/
|
|
219
|
+
declare class CircuitOpenError extends Error {
|
|
220
|
+
constructor(message: string);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Create a circuit breaker
|
|
224
|
+
*/
|
|
225
|
+
declare function createCircuitBreaker(config?: Partial<CircuitBreakerConfig>): CircuitBreaker;
|
|
226
|
+
/**
|
|
227
|
+
* Execute with both retry and circuit breaker
|
|
228
|
+
*/
|
|
229
|
+
declare function resilientExecute<T>(operation: () => Promise<T>, options?: {
|
|
230
|
+
retry?: Partial<RetryConfig>;
|
|
231
|
+
circuitBreaker?: CircuitBreaker;
|
|
232
|
+
}): Promise<T>;
|
|
233
|
+
|
|
234
|
+
export { CircuitBreaker as C, type Err as E, type Ok as O, Result as R, isErr as a, unwrapOr as b, mapErr as c, tryCatchSync as d, err as e, flatMap as f, all as g, match as h, isOk as i, retryWithResult as j, calculateDelay as k, isRetryableError as l, map as m, RetryExhaustedError as n, ok as o, createCircuitBreaker as p, CircuitOpenError as q, retry as r, resilientExecute as s, tryCatch as t, unwrap as u, type CircuitState as v, type CircuitBreakerConfig as w, type RetryConfig as x, type RetryState as y };
|