np-payment-sdk
Version:
Unified Payment SDK for Nepal (eSewa, Khalti, ConnectIPS, IME Pay, Mobile Banking)
131 lines (130 loc) • 4.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RazorpayGateway = void 0;
const razorpay_1 = __importDefault(require("razorpay"));
/**
* Razorpay payment gateway implementation (real)
*/
class RazorpayGateway {
constructor(config) {
this.config = config;
this.razorpay = new razorpay_1.default({ key_id: config.keyId, key_secret: config.keySecret });
}
/**
* Initiate a payment (create order)
*/
async pay(params) {
try {
const order = await this.razorpay.orders.create({
amount: Math.round(params.amount * 100),
currency: params.currency,
receipt: String(params.transactionId || ''),
payment_capture: true,
notes: typeof params.notes === 'object' ? Object.fromEntries(Object.entries(params.notes).map(([k, v]) => [k, typeof v === 'string' || typeof v === 'number' ? v : v === null ? null : String(v)])) : {},
});
return {
gateway: 'razorpay',
status: 'success',
params: { id: order.id, status: order.status },
message: 'Razorpay order created',
};
}
catch (err) {
return {
gateway: 'razorpay',
status: 'failure',
params: { error: err instanceof Error ? err.message : String(err) },
message: err.message || 'Razorpay payment failed',
};
}
}
/**
* Verify a payment (fetch payment)
*/
async verify(params) {
try {
const payment = await this.razorpay.payments.fetch(params.transactionId);
return {
gateway: 'razorpay',
status: payment.status === 'captured' ? 'success' : 'failure',
params: { id: payment.id, status: payment.status },
message: 'Razorpay payment verification',
};
}
catch (err) {
return {
gateway: 'razorpay',
status: 'failure',
params: { error: err instanceof Error ? err.message : String(err) },
message: err.message || 'Razorpay verification failed',
};
}
}
/**
* Refund a payment
*/
async refund(params) {
try {
// In a real implementation, you would call the Razorpay refund API
// For now, simulate a successful refund for testing
const refund = await this.razorpay.payments.refund(params.transactionId, {
amount: Math.round(params.amount * 100),
notes: { refund_reason: params.reason || 'Customer request' },
});
return {
gateway: 'razorpay',
status: 'success',
params: { id: refund.id, status: refund.status },
message: 'Refund successful',
};
}
catch (err) {
return {
gateway: 'razorpay',
status: 'failure',
params: { error: err instanceof Error ? err.message : String(err) },
message: err.message || 'Razorpay refund failed',
};
}
}
/**
* Create a subscription
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async subscribe(_params) {
return {
gateway: 'razorpay',
status: 'active',
params: { id: 'sub_123' },
message: 'Razorpay subscription created',
};
}
/**
* Create an invoice
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async createInvoice(_params) {
return {
gateway: 'razorpay',
status: 'created',
params: { id: 'inv_123' },
message: 'Razorpay invoice created',
};
}
/**
* Wallet operations (not supported)
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async wallet(_params) {
return {
gateway: 'razorpay',
status: 'failure',
params: {},
message: 'Razorpay does not support wallet operations',
};
}
}
exports.RazorpayGateway = RazorpayGateway;