@boostengine/payments 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.
@@ -0,0 +1,339 @@
1
+ type GatewayName = 'razorpay' | 'cashfree' | 'phonepe' | 'paytm' | 'stripe' | 'cod';
2
+ interface UnifiedCustomer {
3
+ id?: string;
4
+ name: string;
5
+ email: string;
6
+ phone: string;
7
+ }
8
+ interface UnifiedOrderItem {
9
+ name: string;
10
+ quantity: number;
11
+ price: number;
12
+ sku?: string;
13
+ }
14
+ interface UnifiedCreateOrderOptions {
15
+ /** Amount in standard currency units (e.g. 1499.00 for INR, 25.00 for USD) */
16
+ amount: number;
17
+ /** 3-letter ISO currency code (e.g. 'INR', 'USD', 'EUR') */
18
+ currency: string;
19
+ /** Merchant order or receipt reference ID */
20
+ receipt: string;
21
+ /** Customer details */
22
+ customer: UnifiedCustomer;
23
+ /** Line items */
24
+ items?: UnifiedOrderItem[];
25
+ /** Merchant notes/metadata */
26
+ notes?: Record<string, any>;
27
+ /** Customer return/redirect URL after payment */
28
+ redirectUrl?: string;
29
+ /** Server-to-server webhook callback URL */
30
+ callbackUrl?: string;
31
+ /** Explicitly override gateway for this transaction */
32
+ gateway?: GatewayName;
33
+ }
34
+ interface UnifiedOrderResult {
35
+ gateway: GatewayName;
36
+ orderId: string;
37
+ gatewayOrderId: string;
38
+ amount: number;
39
+ currency: string;
40
+ status: 'CREATED' | 'PENDING' | 'PAID' | 'FAILED';
41
+ /** Cashfree payment_session_id for Drop-in UI or SDK */
42
+ paymentSessionId?: string;
43
+ /** Redirect or hosted pay URL (PhonePe, Stripe, Cashfree) */
44
+ redirectUrl?: string;
45
+ /** Paytm checkout txnToken */
46
+ txnToken?: string;
47
+ /** Raw response object from gateway */
48
+ rawResponse: any;
49
+ }
50
+ interface UnifiedPaymentVerificationOptions {
51
+ gateway: GatewayName;
52
+ orderId: string;
53
+ paymentId?: string;
54
+ signature?: string;
55
+ rawPayload?: any;
56
+ }
57
+ interface UnifiedPaymentVerificationResult {
58
+ gateway: GatewayName;
59
+ isSuccessful: boolean;
60
+ paymentId: string;
61
+ orderId: string;
62
+ amount: number;
63
+ currency: string;
64
+ paymentMethod?: string;
65
+ rawResponse: any;
66
+ }
67
+ interface UnifiedRefundOptions {
68
+ gateway: GatewayName;
69
+ paymentId: string;
70
+ orderId?: string;
71
+ /** Partial or full refund amount in currency units */
72
+ amount?: number;
73
+ reason?: string;
74
+ }
75
+ interface UnifiedRefundResult {
76
+ gateway: GatewayName;
77
+ refundId: string;
78
+ paymentId: string;
79
+ amount: number;
80
+ status: 'SUCCESS' | 'PENDING' | 'FAILED';
81
+ rawResponse: any;
82
+ }
83
+ interface WebhookVerificationOptions {
84
+ gateway: GatewayName;
85
+ rawBody: string | Buffer;
86
+ headers: Record<string, string | string[] | undefined>;
87
+ webhookSecret?: string;
88
+ }
89
+ interface WebhookVerificationResult {
90
+ isValid: boolean;
91
+ event?: string;
92
+ gateway: GatewayName;
93
+ data?: any;
94
+ error?: string;
95
+ }
96
+ interface RazorpayConfig {
97
+ keyId: string;
98
+ keySecret: string;
99
+ webhookSecret?: string;
100
+ }
101
+ interface CashfreeConfig {
102
+ appId: string;
103
+ secretKey: string;
104
+ env?: 'SANDBOX' | 'PRODUCTION';
105
+ apiVersion?: string;
106
+ }
107
+ interface PhonePeConfig {
108
+ merchantId: string;
109
+ saltKey: string;
110
+ saltIndex?: string;
111
+ env?: 'UAT' | 'PRODUCTION';
112
+ }
113
+ interface PaytmConfig {
114
+ mid: string;
115
+ merchantKey: string;
116
+ website?: string;
117
+ env?: 'STAGE' | 'PRODUCTION';
118
+ }
119
+ interface StripeConfig {
120
+ secretKey: string;
121
+ webhookSecret?: string;
122
+ }
123
+ interface CODConfig {
124
+ minOrderValue?: number;
125
+ maxOrderValue?: number;
126
+ extraFee?: number;
127
+ allowedPincodes?: string[];
128
+ }
129
+ interface GatewayConfigs {
130
+ razorpay?: RazorpayConfig;
131
+ cashfree?: CashfreeConfig;
132
+ phonepe?: PhonePeConfig;
133
+ paytm?: PaytmConfig;
134
+ stripe?: StripeConfig;
135
+ cod?: CODConfig;
136
+ }
137
+ interface SmartRoutingConfig {
138
+ /** Map currency to default gateway, e.g. { 'USD': 'stripe', 'INR': 'cashfree' } */
139
+ currencyMap?: Record<string, GatewayName>;
140
+ /** Automatic fallback order if primary gateway fails, e.g. ['razorpay', 'cashfree', 'phonepe'] */
141
+ fallbackChain?: GatewayName[];
142
+ }
143
+ interface PaymentManagerOptions {
144
+ defaultGateway?: GatewayName;
145
+ gateways: GatewayConfigs;
146
+ smartRouting?: SmartRoutingConfig;
147
+ }
148
+
149
+ declare abstract class BasePaymentAdapter {
150
+ abstract readonly name: GatewayName;
151
+ /**
152
+ * Create an order or payment session on the gateway.
153
+ */
154
+ abstract createOrder(options: UnifiedCreateOrderOptions): Promise<UnifiedOrderResult>;
155
+ /**
156
+ * Verify checkout completion signature or status query.
157
+ */
158
+ abstract verifyPayment(options: UnifiedPaymentVerificationOptions): Promise<UnifiedPaymentVerificationResult>;
159
+ /**
160
+ * Initiate a refund back to the customer.
161
+ */
162
+ abstract refund(options: UnifiedRefundOptions): Promise<UnifiedRefundResult>;
163
+ /**
164
+ * Verify server-to-server webhook authenticity and parse payload.
165
+ */
166
+ abstract verifyWebhook(options: WebhookVerificationOptions): Promise<WebhookVerificationResult>;
167
+ /**
168
+ * Safe helper to extract single string header value from request headers.
169
+ */
170
+ protected getHeader(headers: Record<string, string | string[] | undefined>, name: string): string | undefined;
171
+ /**
172
+ * Helper to perform HTTP JSON requests with standard error extraction.
173
+ */
174
+ protected fetchJson<T = any>(url: string, options?: {
175
+ method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
176
+ headers?: Record<string, string>;
177
+ body?: any;
178
+ }): Promise<T>;
179
+ }
180
+
181
+ declare class PaymentManager {
182
+ private readonly adapters;
183
+ private readonly defaultGateway?;
184
+ private readonly smartRouting?;
185
+ constructor(options: PaymentManagerOptions);
186
+ /**
187
+ * Returns an active adapter instance by gateway name.
188
+ */
189
+ getAdapter(gateway: GatewayName): BasePaymentAdapter;
190
+ /**
191
+ * Lists all currently registered gateway names.
192
+ */
193
+ listConfiguredGateways(): GatewayName[];
194
+ /**
195
+ * Resolves the optimal gateway based on currency rules, explicit override, or default.
196
+ */
197
+ resolveGateway(options: {
198
+ gateway?: GatewayName;
199
+ currency?: string;
200
+ }): GatewayName;
201
+ /**
202
+ * Create an order using the chosen or automatically resolved gateway.
203
+ */
204
+ createOrder(options: UnifiedCreateOrderOptions): Promise<UnifiedOrderResult>;
205
+ /**
206
+ * Smart Fallback: Attempts creation on primary gateway. If it throws an error or fails,
207
+ * it automatically routes through fallback gateways in sequence!
208
+ */
209
+ createOrderWithFallback(options: UnifiedCreateOrderOptions & {
210
+ fallbackChain?: GatewayName[];
211
+ }): Promise<UnifiedOrderResult>;
212
+ /**
213
+ * Verifies payment completion signature or status query.
214
+ */
215
+ verifyPayment(options: UnifiedPaymentVerificationOptions): Promise<UnifiedPaymentVerificationResult>;
216
+ /**
217
+ * Initiates a customer refund.
218
+ */
219
+ refund(options: UnifiedRefundOptions): Promise<UnifiedRefundResult>;
220
+ /**
221
+ * Verifies incoming webhook authenticity and decodes payload.
222
+ */
223
+ verifyWebhook(options: WebhookVerificationOptions): Promise<WebhookVerificationResult>;
224
+ }
225
+ /**
226
+ * Factory function to instantiate a PaymentManager.
227
+ */
228
+ declare function createPaymentManager(options: PaymentManagerOptions): PaymentManager;
229
+
230
+ /**
231
+ * Computes HMAC-SHA256 hex digest.
232
+ */
233
+ declare function hmacSha256(data: string | Buffer, secret: string): string;
234
+ /**
235
+ * Computes standard SHA-256 hex digest.
236
+ */
237
+ declare function sha256(data: string | Buffer): string;
238
+ /**
239
+ * Encodes string into Base64.
240
+ */
241
+ declare function base64Encode(data: string | object): string;
242
+ /**
243
+ * Decodes Base64 into utf-8 string.
244
+ */
245
+ declare function base64Decode(encoded: string): string;
246
+ /**
247
+ * Constant-time safe string comparison to prevent timing attacks on signatures.
248
+ */
249
+ declare function safeCompare(a: string, b: string): boolean;
250
+
251
+ declare class PaymentError extends Error {
252
+ readonly gateway?: GatewayName;
253
+ readonly statusCode?: number;
254
+ readonly rawError?: any;
255
+ constructor(message: string, options?: {
256
+ gateway?: GatewayName;
257
+ statusCode?: number;
258
+ rawError?: any;
259
+ });
260
+ }
261
+ declare class GatewayNotConfiguredError extends PaymentError {
262
+ constructor(gateway: GatewayName);
263
+ }
264
+ declare class SignatureVerificationError extends PaymentError {
265
+ constructor(gateway: GatewayName, details?: string);
266
+ }
267
+
268
+ declare class RazorpayAdapter extends BasePaymentAdapter {
269
+ private readonly config;
270
+ readonly name: GatewayName;
271
+ private readonly baseUrl;
272
+ constructor(config: RazorpayConfig);
273
+ private getAuthHeader;
274
+ createOrder(options: UnifiedCreateOrderOptions): Promise<UnifiedOrderResult>;
275
+ verifyPayment(options: UnifiedPaymentVerificationOptions): Promise<UnifiedPaymentVerificationResult>;
276
+ refund(options: UnifiedRefundOptions): Promise<UnifiedRefundResult>;
277
+ verifyWebhook(options: WebhookVerificationOptions): Promise<WebhookVerificationResult>;
278
+ }
279
+
280
+ declare class CashfreeAdapter extends BasePaymentAdapter {
281
+ private readonly config;
282
+ readonly name: GatewayName;
283
+ private readonly baseUrl;
284
+ private readonly apiVersion;
285
+ constructor(config: CashfreeConfig);
286
+ private getHeaders;
287
+ createOrder(options: UnifiedCreateOrderOptions): Promise<UnifiedOrderResult>;
288
+ verifyPayment(options: UnifiedPaymentVerificationOptions): Promise<UnifiedPaymentVerificationResult>;
289
+ refund(options: UnifiedRefundOptions): Promise<UnifiedRefundResult>;
290
+ verifyWebhook(options: WebhookVerificationOptions): Promise<WebhookVerificationResult>;
291
+ }
292
+
293
+ declare class PhonePeAdapter extends BasePaymentAdapter {
294
+ private readonly config;
295
+ readonly name: GatewayName;
296
+ private readonly baseUrl;
297
+ private readonly saltIndex;
298
+ constructor(config: PhonePeConfig);
299
+ private calculateXVerify;
300
+ createOrder(options: UnifiedCreateOrderOptions): Promise<UnifiedOrderResult>;
301
+ verifyPayment(options: UnifiedPaymentVerificationOptions): Promise<UnifiedPaymentVerificationResult>;
302
+ refund(options: UnifiedRefundOptions): Promise<UnifiedRefundResult>;
303
+ verifyWebhook(options: WebhookVerificationOptions): Promise<WebhookVerificationResult>;
304
+ }
305
+
306
+ declare class PaytmAdapter extends BasePaymentAdapter {
307
+ private readonly config;
308
+ readonly name: GatewayName;
309
+ private readonly baseUrl;
310
+ constructor(config: PaytmConfig);
311
+ createOrder(options: UnifiedCreateOrderOptions): Promise<UnifiedOrderResult>;
312
+ verifyPayment(options: UnifiedPaymentVerificationOptions): Promise<UnifiedPaymentVerificationResult>;
313
+ refund(options: UnifiedRefundOptions): Promise<UnifiedRefundResult>;
314
+ verifyWebhook(options: WebhookVerificationOptions): Promise<WebhookVerificationResult>;
315
+ }
316
+
317
+ declare class StripeAdapter extends BasePaymentAdapter {
318
+ private readonly config;
319
+ readonly name: GatewayName;
320
+ private readonly baseUrl;
321
+ constructor(config: StripeConfig);
322
+ private getAuthHeader;
323
+ createOrder(options: UnifiedCreateOrderOptions): Promise<UnifiedOrderResult>;
324
+ verifyPayment(options: UnifiedPaymentVerificationOptions): Promise<UnifiedPaymentVerificationResult>;
325
+ refund(options: UnifiedRefundOptions): Promise<UnifiedRefundResult>;
326
+ verifyWebhook(options: WebhookVerificationOptions): Promise<WebhookVerificationResult>;
327
+ }
328
+
329
+ declare class CODAdapter extends BasePaymentAdapter {
330
+ private readonly config;
331
+ readonly name: GatewayName;
332
+ constructor(config?: CODConfig);
333
+ createOrder(options: UnifiedCreateOrderOptions): Promise<UnifiedOrderResult>;
334
+ verifyPayment(options: UnifiedPaymentVerificationOptions): Promise<UnifiedPaymentVerificationResult>;
335
+ refund(options: UnifiedRefundOptions): Promise<UnifiedRefundResult>;
336
+ verifyWebhook(_options: WebhookVerificationOptions): Promise<WebhookVerificationResult>;
337
+ }
338
+
339
+ export { BasePaymentAdapter, CODAdapter, type CODConfig, CashfreeAdapter, type CashfreeConfig, type GatewayConfigs, type GatewayName, GatewayNotConfiguredError, PaymentError, PaymentManager, type PaymentManagerOptions, PaytmAdapter, type PaytmConfig, PhonePeAdapter, type PhonePeConfig, RazorpayAdapter, type RazorpayConfig, SignatureVerificationError, type SmartRoutingConfig, StripeAdapter, type StripeConfig, type UnifiedCreateOrderOptions, type UnifiedCustomer, type UnifiedOrderItem, type UnifiedOrderResult, type UnifiedPaymentVerificationOptions, type UnifiedPaymentVerificationResult, type UnifiedRefundOptions, type UnifiedRefundResult, type WebhookVerificationOptions, type WebhookVerificationResult, base64Decode, base64Encode, createPaymentManager, hmacSha256, safeCompare, sha256 };