A unified payment gateway package for Node.js. One API for multiple payment providers.
Supports Stripe, SSLCommerz, bKash, and Nagad through a single, consistent interface.
npm install bdpaymentsThen install only the gateway SDKs you need:
# For Stripe
npm install stripe
# SSLCommerz β no extra SDK needed (uses REST API via fetch)
# bKash & Nagad β no extra SDK needed (uses REST API via fetch)import { configure } from 'bdpayments';
configure({
stripe: {
apiKey: process.env.STRIPE_API_KEY,
},
bkash: {
appKey: process.env.BKASH_APP_KEY,
appSecret: process.env.BKASH_APP_SECRET,
username: process.env.BKASH_USERNAME,
password: process.env.BKASH_PASSWORD,
sandbox: true,
},
});Or skip configure() entirely and set environment variables β the package reads them automatically.
import { charge } from 'bdpayments';
// Stripe
const stripeResult = await charge({
gateway: 'stripe',
amount: 1000, // $10.00 in cents
currency: 'usd',
paymentMethod: 'pm_card_visa',
confirm: true,
});
// bKash
const bkashResult = await charge({
gateway: 'bkash',
amount: 500,
invoiceNumber: 'INV-001',
callbackURL: 'https://example.com/callback',
});
// SSLCommerz
const sslResult = await charge({
gateway: 'sslcommerz',
amount: 1000,
currency: 'BDT',
transactionId: 'TXN-001',
successUrl: 'https://example.com/success',
failUrl: 'https://example.com/fail',
cancelUrl: 'https://example.com/cancel',
});import { refund } from 'bdpayments';
const result = await refund({
gateway: 'stripe',
transactionId: 'pi_...',
amount: 500, // Partial refund
});import { retrieve } from 'bdpayments';
// Retrieve Stripe payment details
const stripeResult = await retrieve({
gateway: 'stripe',
transactionId: 'pi_...',
});
// Validate SSLCommerz payment callback or IPN using validation ID
const sslcommerzResult = await retrieve({
gateway: 'sslcommerz',
valId: 'val_id_from_callback',
});
console.log(stripeResult.status); // e.g. "succeeded"
console.log(sslcommerzResult.success); // true if validated successfullyimport { execute } from 'bdpayments';
// bKash (execute tokenized payment)
const result = await execute({
gateway: 'bkash',
paymentID: 'TR0011...',
});
console.log(result.status); // e.g. "Completed"Three ways to provide API keys (highest priority wins):
| Priority | Method | Example |
|---|---|---|
| 1 (highest) | Per-call options | charge({ gateway: 'stripe', apiKey: 'sk_...', ... }) |
| 2 | Global configure() |
configure({ stripe: { apiKey: 'sk_...' } }) |
| 3 (lowest) | Environment variables | STRIPE_API_KEY=sk_... |
| Gateway | Variables |
|---|---|
| Stripe | STRIPE_API_KEY |
| SSLCommerz | SSLCOMMERZ_STORE_ID, SSLCOMMERZ_STORE_PASSWORD, SSLCOMMERZ_SANDBOX |
| bKash | BKASH_APP_KEY, BKASH_APP_SECRET, BKASH_USERNAME, BKASH_PASSWORD, BKASH_SANDBOX |
| Nagad | NAGAD_MERCHANT_ID, NAGAD_PUBLIC_KEY, NAGAD_PRIVATE_KEY, NAGAD_SANDBOX |
| Gateway | Charge | Execute | Refund | Retrieve | Auth Method |
|---|---|---|---|---|---|
| Stripe | β PaymentIntents | β | β | β | API Key |
| SSLCommerz | β Session | β | β | β | Store ID/Password |
| bKash | β Tokenized | β | β | β | Token Grant |
| Nagad | β Checkout | β | β | β | RSA Encrypted |
All functions return a normalized response:
{
success: true,
transactionId: 'pi_...',
status: 'succeeded',
amount: 1000,
currency: 'usd',
gatewayResponse: { /* raw gateway response */ },
}import { charge, PaymentError, GatewayNotFoundError, ConfigurationError } from 'bdpayments';
try {
await charge({ gateway: 'stripe', amount: 1000 });
} catch (error) {
if (error instanceof ConfigurationError) {
console.log('Missing credentials:', error.missingKeys);
} else if (error instanceof GatewayNotFoundError) {
console.log('Unknown gateway');
} else if (error instanceof PaymentError) {
console.log(`${error.gateway} error: ${error.message}`);
console.log('Original error:', error.originalError);
}
}Full TypeScript definitions are included. Options are narrowed by gateway name:
import { charge, type StripeChargeOptions } from 'bdpayments';
// TypeScript knows exactly which fields are available
const result = await charge({
gateway: 'stripe',
amount: 1000,
currency: 'usd',
paymentMethod: 'pm_card_visa',
});Released under the MIT License. Β© 2024 Sabbir Mahmud