np-payment-sdk
Version:
Unified Payment SDK for Nepal (eSewa, Khalti, ConnectIPS, IME Pay, Mobile Banking)
153 lines (152 loc) • 5.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CashfreeGateway = void 0;
const cashfree_pg_1 = require("cashfree-pg");
const configuration_1 = require("cashfree-pg/dist/configuration");
/**
* Cashfree payment gateway implementation (real)
*/
class CashfreeGateway {
constructor(config) {
var _a;
this.config = config;
const env = (_a = config.environment) !== null && _a !== void 0 ? _a : configuration_1.CFEnvironment.SANDBOX;
// @ts-expect-error: SDK expects enum, type mismatch with type definition
this.cashfree = new cashfree_pg_1.Cashfree(config.clientId, config.clientSecret, env);
}
/**
* Initiate a payment (create order)
*/
async pay(params) {
try {
const order = await this.cashfree.PGCreateOrder({
order_amount: Number(params.amount),
order_currency: params.currency,
customer_details: {
customer_id: String(params.customerId),
customer_email: String(params.email),
customer_phone: String(params.phone),
},
});
return {
gateway: 'cashfree',
status: 'success',
params: { orderId: order.data.order_id, orderStatus: order.data.order_status },
message: 'Cashfree order created',
};
}
catch (err) {
if (err instanceof Error) {
return {
gateway: 'cashfree',
status: 'failure',
params: { error: err.message },
message: err.message || 'Cashfree payment failed',
};
}
return {
gateway: 'cashfree',
status: 'failure',
params: { error: String(err) },
message: 'Cashfree payment failed',
};
}
}
/**
* Verify a payment (fetch order)
*/
async verify(params) {
try {
const order = await this.cashfree.PGFetchOrder(params.transactionId);
return {
gateway: 'cashfree',
status: 'success',
params: { orderId: order.data.order_id, orderStatus: order.data.order_status },
message: 'Cashfree order verification',
};
}
catch (err) {
if (err instanceof Error) {
return {
gateway: 'cashfree',
status: 'failure',
params: { error: err.message },
message: err.message || 'Cashfree verification failed',
};
}
return {
gateway: 'cashfree',
status: 'failure',
params: { error: String(err) },
message: 'Cashfree verification failed',
};
}
}
/**
* Refund a payment
*/
async refund(params) {
try {
const refund = await this.cashfree.PGOrderRefund(params.transactionId, params.amount);
return {
gateway: 'cashfree',
status: 'success',
params: { refundId: refund.data.refund_id, refundStatus: refund.data.refund_status },
message: 'Cashfree refund processed',
};
}
catch (err) {
if (err instanceof Error) {
return {
gateway: 'cashfree',
status: 'failure',
params: { error: err.message },
message: err.message || 'Cashfree refund failed',
};
}
return {
gateway: 'cashfree',
status: 'failure',
params: { error: String(err) },
message: 'Cashfree refund failed',
};
}
}
/**
* Create a subscription (not implemented)
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async subscribe(_params) {
return {
gateway: 'cashfree',
status: 'cancelled',
params: {},
message: 'Cashfree subscription not implemented',
};
}
/**
* Create an invoice (not implemented)
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async createInvoice(_params) {
return {
gateway: 'cashfree',
status: 'cancelled',
params: {},
message: 'Cashfree invoice not implemented',
};
}
/**
* Wallet operations (not supported)
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async wallet(_params) {
return {
gateway: 'cashfree',
status: 'failure',
params: {},
message: 'Cashfree does not support wallet operations',
};
}
}
exports.CashfreeGateway = CashfreeGateway;