@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.
- package/README.md +197 -0
- package/bin/cli.cjs +92 -0
- package/dist/index.cjs +988 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +339 -0
- package/dist/index.d.ts +339 -0
- package/dist/index.mjs +966 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# @boostengine/payments
|
|
2
|
+
|
|
3
|
+
> **Unified Multi-Gateway Payment Orchestration Layer** for Indian & Global eCommerce. Seamlessly plug in **Razorpay, Cashfree, PhonePe, Paytm, Stripe, and COD** through a single unified API with smart routing, automatic fallbacks, and signature verification.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## ⥠Why @boostengine/payments?
|
|
8
|
+
|
|
9
|
+
- đ **Unified API**: Write your payment flow once. Switch or combine Razorpay, Cashfree, PhonePe, or Stripe without rewriting checkout logic.
|
|
10
|
+
- đĄī¸ **Smart Fallback**: If your primary payment gateway has a downtime or failure, transactions automatically failover to your secondary gateway!
|
|
11
|
+
- đą **Multi-Currency Auto-Routing**: Automatically route USD/EUR to Stripe and INR to Razorpay/Cashfree/PhonePe.
|
|
12
|
+
- đ **Built-in Signature Verification**: Native verification for Razorpay HMAC, Cashfree webhook, PhonePe SHA-256 + Salt `X-VERIFY`, and Stripe webhooks.
|
|
13
|
+
- đĒļ **Zero Dependency Bloat**: Uses native Node.js `fetch` and `crypto`. No 10 heavy third-party vendor SDKs.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## đĻ Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @boostengine/payments
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## đ Quickstart
|
|
26
|
+
|
|
27
|
+
### 1. Initialize PaymentManager
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { createPaymentManager } from '@boostengine/payments';
|
|
31
|
+
|
|
32
|
+
export const payments = createPaymentManager({
|
|
33
|
+
// Default gateway when none is specified
|
|
34
|
+
defaultGateway: 'cashfree',
|
|
35
|
+
|
|
36
|
+
// Configure any or all gateways
|
|
37
|
+
gateways: {
|
|
38
|
+
cashfree: {
|
|
39
|
+
appId: process.env.CASHFREE_APP_ID!,
|
|
40
|
+
secretKey: process.env.CASHFREE_SECRET_KEY!,
|
|
41
|
+
env: 'PRODUCTION', // or 'SANDBOX'
|
|
42
|
+
},
|
|
43
|
+
razorpay: {
|
|
44
|
+
keyId: process.env.RAZORPAY_KEY_ID!,
|
|
45
|
+
keySecret: process.env.RAZORPAY_KEY_SECRET!,
|
|
46
|
+
webhookSecret: process.env.RAZORPAY_WEBHOOK_SECRET,
|
|
47
|
+
},
|
|
48
|
+
phonepe: {
|
|
49
|
+
merchantId: process.env.PHONEPE_MERCHANT_ID!,
|
|
50
|
+
saltKey: process.env.PHONEPE_SALT_KEY!,
|
|
51
|
+
saltIndex: '1',
|
|
52
|
+
env: 'PRODUCTION', // or 'UAT'
|
|
53
|
+
},
|
|
54
|
+
stripe: {
|
|
55
|
+
secretKey: process.env.STRIPE_SECRET_KEY!,
|
|
56
|
+
webhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
|
|
57
|
+
},
|
|
58
|
+
cod: {
|
|
59
|
+
minOrderValue: 200,
|
|
60
|
+
maxOrderValue: 10000,
|
|
61
|
+
extraFee: 49, // Rs. 49 COD handling charge
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
// Smart Routing & Fallbacks
|
|
66
|
+
smartRouting: {
|
|
67
|
+
currencyMap: {
|
|
68
|
+
USD: 'stripe',
|
|
69
|
+
EUR: 'stripe',
|
|
70
|
+
INR: 'cashfree',
|
|
71
|
+
},
|
|
72
|
+
fallbackChain: ['cashfree', 'razorpay', 'phonepe'],
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
### 2. Create an Order (Single or Multi-Gateway)
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
// Works identically across Cashfree, Razorpay, PhonePe, Stripe, and COD!
|
|
83
|
+
const order = await payments.createOrder({
|
|
84
|
+
amount: 1499.00,
|
|
85
|
+
currency: 'INR',
|
|
86
|
+
receipt: `order_${Date.now()}`,
|
|
87
|
+
customer: {
|
|
88
|
+
name: 'Aman Sharma',
|
|
89
|
+
email: 'aman@example.com',
|
|
90
|
+
phone: '9876543210',
|
|
91
|
+
},
|
|
92
|
+
// Optionally override gateway per checkout button:
|
|
93
|
+
// gateway: 'razorpay' | 'phonepe' | 'cashfree' | 'stripe' | 'cod'
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
console.log(order);
|
|
97
|
+
/*
|
|
98
|
+
Output:
|
|
99
|
+
{
|
|
100
|
+
gateway: 'cashfree',
|
|
101
|
+
orderId: 'order_1709923812',
|
|
102
|
+
gatewayOrderId: 'order_1709923812',
|
|
103
|
+
amount: 1499,
|
|
104
|
+
currency: 'INR',
|
|
105
|
+
status: 'CREATED',
|
|
106
|
+
paymentSessionId: 'session_cf_109283719283', // Ready for Cashfree Dropin
|
|
107
|
+
redirectUrl: '...', // Available for PhonePe / Stripe
|
|
108
|
+
rawResponse: { ... }
|
|
109
|
+
}
|
|
110
|
+
*/
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
### 3. Smart Fallback (High-Availability Checkout)
|
|
116
|
+
|
|
117
|
+
If your primary payment gateway experiences bank server outages or 500 errors, automatic fallback routes the order through the next available gateway:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
const order = await payments.createOrderWithFallback({
|
|
121
|
+
amount: 1499.00,
|
|
122
|
+
currency: 'INR',
|
|
123
|
+
receipt: `order_${Date.now()}`,
|
|
124
|
+
customer: {
|
|
125
|
+
name: 'Aman Sharma',
|
|
126
|
+
email: 'aman@example.com',
|
|
127
|
+
phone: '9876543210',
|
|
128
|
+
},
|
|
129
|
+
fallbackChain: ['razorpay', 'cashfree', 'phonepe'],
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
### 4. Verify Payment & Webhooks
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
// 1. Verify frontend checkout completion:
|
|
139
|
+
const verification = await payments.verifyPayment({
|
|
140
|
+
gateway: 'razorpay',
|
|
141
|
+
orderId: 'order_123',
|
|
142
|
+
paymentId: 'pay_456',
|
|
143
|
+
signature: req.body.razorpay_signature,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
if (verification.isSuccessful) {
|
|
147
|
+
console.log(`Payment confirmed! ID: ${verification.paymentId}`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 2. Universal Webhook Authenticator:
|
|
151
|
+
const webhook = await payments.verifyWebhook({
|
|
152
|
+
gateway: 'cashfree', // or 'razorpay' | 'phonepe' | 'stripe'
|
|
153
|
+
rawBody: req.rawBody,
|
|
154
|
+
headers: req.headers,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
if (webhook.isValid) {
|
|
158
|
+
console.log(`Verified webhook event: ${webhook.event}`, webhook.data);
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
### 5. Instant Refunds
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
const refund = await payments.refund({
|
|
168
|
+
gateway: 'razorpay',
|
|
169
|
+
paymentId: 'pay_456',
|
|
170
|
+
amount: 1499.00, // full or partial
|
|
171
|
+
reason: 'Customer cancelled before dispatch',
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
console.log('Refund ID:', refund.refundId, 'Status:', refund.status);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## đ ī¸ CLI Quickstart
|
|
180
|
+
|
|
181
|
+
Generate an environment template or check supported gateways:
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
# List all 6 supported gateways
|
|
185
|
+
npx @boostengine/payments list
|
|
186
|
+
|
|
187
|
+
# Generate .env.payments.example template
|
|
188
|
+
npx @boostengine/payments init-env
|
|
189
|
+
|
|
190
|
+
# Compute quick SHA-256 hash
|
|
191
|
+
npx @boostengine/payments hash "my_test_payload"
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## đ License
|
|
197
|
+
MIT Š Boost Engine Team
|
package/bin/cli.cjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const crypto = require('crypto');
|
|
6
|
+
|
|
7
|
+
const args = process.argv.slice(2);
|
|
8
|
+
const command = args[0] || 'help';
|
|
9
|
+
|
|
10
|
+
console.log('\n=======================================================');
|
|
11
|
+
console.log('⥠@boostengine/payments - Multi-Gateway CLI');
|
|
12
|
+
console.log('=======================================================\n');
|
|
13
|
+
|
|
14
|
+
switch (command) {
|
|
15
|
+
case 'list': {
|
|
16
|
+
console.log('Supported Payment Gateways:\n');
|
|
17
|
+
const gateways = [
|
|
18
|
+
{ name: 'Razorpay', key: 'razorpay', type: 'Cards, UPI, Netbanking, Smart Collect', region: 'India' },
|
|
19
|
+
{ name: 'Cashfree', key: 'cashfree', type: 'Drop-in, Seamless UPI/Cards, Verification, Payouts', region: 'India' },
|
|
20
|
+
{ name: 'PhonePe', key: 'phonepe', type: 'Standard Hosted Pay, Mobile App Intent, UPI Collect', region: 'India' },
|
|
21
|
+
{ name: 'Paytm', key: 'paytm', type: 'All-In-One Checkout SDK, txnToken, Status', region: 'India' },
|
|
22
|
+
{ name: 'Stripe', key: 'stripe', type: 'Checkout Sessions, PaymentIntents, Global Cards', region: 'Global' },
|
|
23
|
+
{ name: 'COD', key: 'cod', type: 'Cash On Delivery with fee rules & limit thresholds', region: 'Local' },
|
|
24
|
+
];
|
|
25
|
+
console.table(gateways);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
case 'init-env': {
|
|
30
|
+
const envContent = `# Boost Engine Payments - Multi-Gateway Environment Variables
|
|
31
|
+
|
|
32
|
+
# Default Gateway (razorpay | cashfree | phonepe | paytm | stripe | cod)
|
|
33
|
+
DEFAULT_PAYMENT_GATEWAY=cashfree
|
|
34
|
+
|
|
35
|
+
# Razorpay
|
|
36
|
+
RAZORPAY_KEY_ID=rzp_test_your_key_id
|
|
37
|
+
RAZORPAY_KEY_SECRET=your_razorpay_secret
|
|
38
|
+
RAZORPAY_WEBHOOK_SECRET=your_razorpay_webhook_secret
|
|
39
|
+
|
|
40
|
+
# Cashfree
|
|
41
|
+
CASHFREE_APP_ID=your_cashfree_app_id
|
|
42
|
+
CASHFREE_SECRET_KEY=your_cashfree_secret_key
|
|
43
|
+
CASHFREE_ENV=SANDBOX
|
|
44
|
+
|
|
45
|
+
# PhonePe
|
|
46
|
+
PHONEPE_MERCHANT_ID=PGTESTPAYUAT
|
|
47
|
+
PHONEPE_SALT_KEY=099eb0cd-02cf-4e2a-8aca-3e6c6aff0399
|
|
48
|
+
PHONEPE_SALT_INDEX=1
|
|
49
|
+
PHONEPE_ENV=UAT
|
|
50
|
+
|
|
51
|
+
# Paytm
|
|
52
|
+
PAYTM_MID=YOUR_PAYTM_MID
|
|
53
|
+
PAYTM_MERCHANT_KEY=YOUR_PAYTM_MERCHANT_KEY
|
|
54
|
+
PAYTM_ENV=STAGE
|
|
55
|
+
|
|
56
|
+
# Stripe
|
|
57
|
+
STRIPE_SECRET_KEY=sk_test_your_stripe_secret
|
|
58
|
+
STRIPE_WEBHOOK_SECRET=whsec_your_stripe_webhook_secret
|
|
59
|
+
|
|
60
|
+
# Cash On Delivery (COD) Rules
|
|
61
|
+
COD_MIN_ORDER=200
|
|
62
|
+
COD_MAX_ORDER=10000
|
|
63
|
+
COD_EXTRA_FEE=49
|
|
64
|
+
`;
|
|
65
|
+
const targetFile = path.join(process.cwd(), '.env.payments.example');
|
|
66
|
+
fs.writeFileSync(targetFile, envContent, 'utf8');
|
|
67
|
+
console.log(`â
Created environment template: ${targetFile}`);
|
|
68
|
+
console.log('Copy desired variables into your project .env file.\n');
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
case 'hash': {
|
|
73
|
+
const text = args[1];
|
|
74
|
+
if (!text) {
|
|
75
|
+
console.log('Usage: npx @boostengine/payments hash "<text>"');
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
const hash = crypto.createHash('sha256').update(text).digest('hex');
|
|
79
|
+
console.log(`Input: ${text}`);
|
|
80
|
+
console.log(`SHA256: ${hash}\n`);
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
default: {
|
|
85
|
+
console.log('Usage:');
|
|
86
|
+
console.log(' npx @boostengine/payments list - List all 6 supported gateways');
|
|
87
|
+
console.log(' npx @boostengine/payments init-env - Generate .env.payments.example template');
|
|
88
|
+
console.log(' npx @boostengine/payments hash <text> - Calculate SHA256 checksum');
|
|
89
|
+
console.log('\nDocumentation: https://github.com/boostengine/payments\n');
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|