np-payment-sdk
Version:
Unified Payment SDK for Nepal (eSewa, Khalti, ConnectIPS, IME Pay, Mobile Banking)
82 lines (81 loc) • 3.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KhaltiGateway = void 0;
const axios_1 = __importDefault(require("axios"));
const types_1 = require("../types");
class KhaltiGateway {
constructor(config) {
this.config = config;
this.baseUrl = config.baseUrl || 'https://khalti.com/api/v2';
}
async pay(params) {
try {
const payload = {
return_url: params.returnUrl,
website_url: params.websiteUrl || params.returnUrl,
amount: params.amount * 100, // Khalti expects amount in paisa
purchase_order_id: params.transactionId,
purchase_order_name: params.purchaseOrderName || 'Order',
customer_info: params.customerInfo || {},
};
const response = await axios_1.default.post(`${this.baseUrl}/epayment/initiate/`, payload, {
headers: {
Authorization: `Key ${this.config.secretKey}`,
'Content-Type': 'application/json',
},
});
return {
gateway: 'khalti',
status: 'success',
params: response.data,
message: 'Payment initiated',
};
}
catch (err) {
if (err && typeof err === 'object' && 'response' in err && err.response && typeof err.response === 'object' && 'data' in err.response && err.response.data && typeof err.response.data === 'object' && 'detail' in err.response.data) {
// @ts-expect-error: Type mismatch due to third-party library
throw new types_1.PaymentError(err.response.data.detail || err.message || 'Khalti payment failed', 'KHALTI_PAYMENT_ERROR');
}
if (err instanceof Error) {
throw new types_1.PaymentError(err.message || 'Khalti payment failed', 'KHALTI_PAYMENT_ERROR');
}
throw new types_1.PaymentError('Khalti payment failed', 'KHALTI_PAYMENT_ERROR');
}
}
async verify(params) {
try {
const response = await axios_1.default.post(`${this.baseUrl}/epayment/lookup/`, {
pidx: params.transactionId,
}, {
headers: {
Authorization: `Key ${this.config.secretKey}`,
'Content-Type': 'application/json',
},
});
return {
gateway: 'khalti',
status: response.data.status === 'Completed' ? 'success' : 'failure',
params: response.data,
message: 'Payment verification result',
};
}
catch (err) {
if (err && typeof err === 'object' && 'response' in err && err.response && typeof err.response === 'object' && 'data' in err.response && err.response.data && typeof err.response.data === 'object' && 'detail' in err.response.data) {
// @ts-expect-error: Type mismatch due to third-party library
throw new types_1.PaymentError(err.response.data.detail || err.message || 'Khalti verification failed', 'KHALTI_VERIFY_ERROR');
}
if (err instanceof Error) {
throw new types_1.PaymentError(err.message || 'Khalti verification failed', 'KHALTI_VERIFY_ERROR');
}
throw new types_1.PaymentError('Khalti verification failed', 'KHALTI_VERIFY_ERROR');
}
}
async refund() {
// Khalti does not provide a public refund API as of now.
throw new types_1.PaymentError('Khalti refund is not supported via public API', 'KHALTI_REFUND_UNSUPPORTED');
}
}
exports.KhaltiGateway = KhaltiGateway;