np-payment-sdk
Version:
Unified Payment SDK for Nepal (eSewa, Khalti, ConnectIPS, IME Pay, Mobile Banking)
187 lines (186 loc) • 8.09 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.eventBus = exports.PaymentSDK = exports.GatewayType = void 0;
const esewa_1 = require("./gateways/esewa");
const khalti_1 = require("./gateways/khalti");
const connectips_1 = require("./gateways/connectips");
const imepay_1 = require("./gateways/imepay");
const mobilebanking_1 = require("./gateways/mobilebanking");
const types_1 = require("./types");
const txStore = __importStar(require("./utils/transactionStore"));
const eventBus_1 = require("./utils/eventBus");
Object.defineProperty(exports, "eventBus", { enumerable: true, get: function () { return eventBus_1.eventBus; } });
var GatewayType;
(function (GatewayType) {
GatewayType["ESEWA"] = "esewa";
GatewayType["KHALTI"] = "khalti";
GatewayType["CONNECTIPS"] = "connectips";
GatewayType["IMEPAY"] = "imepay";
GatewayType["MOBILEBANKING"] = "mobilebanking";
GatewayType["STRIPE"] = "stripe";
GatewayType["PAYPAL"] = "paypal";
GatewayType["RAZORPAY"] = "razorpay";
GatewayType["CASHFREE"] = "cashfree";
GatewayType["FLUTTERWAVE"] = "flutterwave";
GatewayType["PAYSTACK"] = "paystack";
// ...add more as needed
})(GatewayType || (exports.GatewayType = GatewayType = {}));
class PaymentSDK {
constructor(config) {
this.config = config;
this.gateways = new Map();
// Local gateways
if (config.gateways.esewa) {
this.gateways.set(GatewayType.ESEWA, new esewa_1.EsewaGateway(config.gateways.esewa));
}
if (config.gateways.khalti) {
this.gateways.set(GatewayType.KHALTI, new khalti_1.KhaltiGateway(config.gateways.khalti));
}
if (config.gateways.connectips) {
this.gateways.set(GatewayType.CONNECTIPS, new connectips_1.ConnectIPSGateway(config.gateways.connectips));
}
if (config.gateways.imepay) {
this.gateways.set(GatewayType.IMEPAY, new imepay_1.IMEPayGateway(config.gateways.imepay));
}
if (config.gateways.mobilebanking) {
this.gateways.set(GatewayType.MOBILEBANKING, new mobilebanking_1.MobileBankingGateway(config.gateways.mobilebanking));
}
// Global gateways (to be implemented)
if (config.gateways.stripe) {
// Placeholder: will import and use StripeGateway
// this.gateways.set(GatewayType.STRIPE, new StripeGateway(config.gateways.stripe));
}
if (config.gateways.paypal) {
// Placeholder: will import and use PayPalGateway
}
// ...add more as needed
// Custom providers
if (config.customProviders) {
for (const [key, provider] of Object.entries(config.customProviders)) {
this.gateways.set(key, provider);
}
}
}
/**
* Register a new payment provider at runtime
*/
registerProvider(key, provider) {
this.gateways.set(key, provider);
}
async pay(params) {
if (!params || typeof params !== 'object') {
throw new types_1.PaymentError('Invalid payment parameters', 'INVALID_PARAMS');
}
if (!params.gateway) {
throw new types_1.PaymentError('Missing gateway in payment parameters', 'MISSING_GATEWAY');
}
if (!params.amount || typeof params.amount !== 'number' || params.amount <= 0) {
throw new types_1.PaymentError('Invalid or missing amount', 'INVALID_AMOUNT');
}
if (!params.currency) {
throw new types_1.PaymentError('Missing currency', 'MISSING_CURRENCY');
}
if (!params.returnUrl) {
throw new types_1.PaymentError('Missing returnUrl', 'MISSING_RETURN_URL');
}
const gateway = this.gateways.get(params.gateway);
if (!gateway) {
throw new types_1.PaymentError('Gateway not configured: ' + params.gateway, 'GATEWAY_NOT_CONFIGURED');
}
const result = await gateway.pay(params);
eventBus_1.eventBus.emit('pay', { gateway: params.gateway, params, result });
return result;
}
async verify(params) {
const gateway = this.gateways.get(params.gateway);
if (!gateway) {
throw new types_1.PaymentError('Gateway not configured: ' + params.gateway, 'GATEWAY_NOT_CONFIGURED');
}
const result = await gateway.verify(params);
eventBus_1.eventBus.emit('verify', { gateway: params.gateway, params, result });
return result;
}
async refund(params) {
const gateway = this.gateways.get(params.gateway);
if (!gateway) {
throw new types_1.PaymentError('Gateway not configured: ' + params.gateway, 'GATEWAY_NOT_CONFIGURED');
}
const result = await gateway.refund(params);
eventBus_1.eventBus.emit('refund', { gateway: params.gateway, params, result });
return result;
}
async subscribe(params) {
const gateway = this.gateways.get(params.gateway);
if (!gateway || !gateway.subscribe) {
throw new types_1.PaymentError('Subscription not supported for gateway: ' + params.gateway, 'SUBSCRIPTION_NOT_SUPPORTED');
}
const result = await gateway.subscribe(params);
eventBus_1.eventBus.emit('subscribe', { gateway: params.gateway, params, result });
return result;
}
async createInvoice(params) {
const gateway = this.gateways.get(params.gateway);
if (!gateway || !gateway.createInvoice) {
throw new types_1.PaymentError('Invoice not supported for gateway: ' + params.gateway, 'INVOICE_NOT_SUPPORTED');
}
const result = await gateway.createInvoice(params);
eventBus_1.eventBus.emit('createInvoice', { gateway: params.gateway, params, result });
return result;
}
async wallet(params) {
const gateway = this.gateways.get(params.gateway);
if (!gateway || !gateway.wallet) {
throw new types_1.PaymentError('Wallet not supported for gateway: ' + params.gateway, 'WALLET_NOT_SUPPORTED');
}
const result = await gateway.wallet(params);
eventBus_1.eventBus.emit('wallet', { gateway: params.gateway, params, result });
return result;
}
// Transaction history and reconciliation tools
addTransaction(record) {
txStore.addTransaction(record);
}
updateTransaction(transactionId, updates) {
txStore.updateTransaction(transactionId, updates);
}
getTransaction(transactionId) {
return txStore.getTransaction(transactionId);
}
listTransactions() {
return txStore.listTransactions();
}
}
exports.PaymentSDK = PaymentSDK;