np-payment-sdk
Version:
Unified Payment SDK for Nepal (eSewa, Khalti, ConnectIPS, IME Pay, Mobile Banking)
153 lines (152 loc) • 4.95 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaystackGateway = void 0;
const paystack_api_1 = __importDefault(require("paystack-api"));
/**
* Paystack payment gateway implementation (real)
*/
class PaystackGateway {
constructor(config) {
this.config = config;
this.paystack = (0, paystack_api_1.default)(config.secretKey);
}
/**
* Initiate a payment (initialize transaction)
*/
async pay(params) {
try {
const response = await this.paystack.transaction.initialize({
amount: Math.round(params.amount * 100), // Paystack expects kobo
email: params.email,
currency: params.currency,
callback_url: params.returnUrl,
reference: params.transactionId || undefined,
});
return {
gateway: 'paystack',
status: response.status ? 'success' : 'failure',
params: response.data,
message: response.message || 'Paystack transaction initialized',
};
}
catch (err) {
if (err instanceof Error) {
return {
gateway: 'paystack',
status: 'failure',
params: { error: err.message },
message: err.message || 'Paystack payment failed',
};
}
return {
gateway: 'paystack',
status: 'failure',
params: { error: String(err) },
message: 'Paystack payment failed',
};
}
}
/**
* Verify a payment
*/
async verify(params) {
try {
const response = await this.paystack.transaction.verify(params.transactionId);
return {
gateway: 'paystack',
status: response.data.status === 'success' ? 'success' : 'failure',
params: response.data,
message: 'Paystack payment verification',
};
}
catch (err) {
if (err instanceof Error) {
return {
gateway: 'paystack',
status: 'failure',
params: { error: err.message },
message: err.message || 'Paystack verification failed',
};
}
return {
gateway: 'paystack',
status: 'failure',
params: { error: String(err) },
message: 'Paystack verification failed',
};
}
}
/**
* Refund a payment
*/
async refund(params) {
try {
const response = await this.paystack.refund.create({
transaction: params.transactionId,
amount: Math.round(params.amount * 100),
});
return {
gateway: 'paystack',
status: response.status ? 'success' : 'failure',
params: response.data,
message: response.message || 'Paystack refund processed',
};
}
catch (err) {
if (err instanceof Error) {
return {
gateway: 'paystack',
status: 'failure',
params: { error: err.message },
message: err.message || 'Paystack refund failed',
};
}
return {
gateway: 'paystack',
status: 'failure',
params: { error: String(err) },
message: 'Paystack refund failed',
};
}
}
/**
* Create a subscription (not implemented)
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async subscribe(_params) {
return {
gateway: 'paystack',
status: 'cancelled',
params: {},
message: 'Paystack subscription not implemented',
};
}
/**
* Create an invoice (not implemented)
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async createInvoice(_params) {
return {
gateway: 'paystack',
status: 'cancelled',
params: {},
message: 'Paystack invoice not implemented',
};
}
/**
* Wallet operations (not supported)
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async wallet(_params) {
return {
gateway: 'paystack',
status: 'failure',
params: {},
message: 'Paystack does not support wallet operations',
};
}
}
exports.PaystackGateway = PaystackGateway;