UNPKG

np-payment-sdk

Version:

Unified Payment SDK for Nepal (eSewa, Khalti, ConnectIPS, IME Pay, Mobile Banking)

224 lines (223 loc) 7.85 kB
"use strict"; 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.PayPalGateway = void 0; const paypal = __importStar(require("@paypal/checkout-server-sdk")); /** * PayPal payment gateway implementation (real) */ class PayPalGateway { constructor(config) { this.config = config; const env = config.environment === 'production' ? new paypal.LiveEnvironment(config.clientId, config.clientSecret) : new paypal.SandboxEnvironment(config.clientId, config.clientSecret); this.client = new paypal.PayPalHttpClient(env); } /** * Initiate a payment (create order) */ async pay(params) { try { const request = new paypal.orders.OrdersCreateRequest(); request.prefer('return=representation'); request.requestBody({ intent: 'CAPTURE', purchase_units: [{ amount: { currency_code: params.currency, value: params.amount.toString(), }, }], application_context: { return_url: params.returnUrl, cancel_url: params.cancelUrl || params.returnUrl, }, }); const order = await this.client.execute(request); if (order.result.status && (order.result.status === 'CREATED' || order.result.status === 'APPROVED')) { return { gateway: 'paypal', status: 'success', params: order.result, message: 'PayPal order created', }; } else { return { gateway: 'paypal', status: 'failure', params: order.result, message: 'PayPal order not created', }; } } catch (err) { if (err instanceof Error) { return { gateway: 'paypal', status: 'failure', params: { error: err.message }, message: err.message || 'PayPal payment failed', }; } return { gateway: 'paypal', status: 'failure', params: {}, message: 'PayPal payment failed', }; } } /** * Verify a payment (get order) */ async verify(params) { try { const request = new paypal.orders.OrdersGetRequest(params.transactionId); const order = await this.client.execute(request); return { gateway: 'paypal', status: order.result.status === 'COMPLETED' ? 'success' : 'failure', params: order.result, message: 'PayPal order verification', }; } catch (err) { if (err instanceof Error) { return { gateway: 'paypal', status: 'failure', params: { error: err.message }, message: err.message || 'PayPal verification failed', }; } return { gateway: 'paypal', status: 'failure', params: {}, message: 'PayPal verification failed', }; } } /** * Refund a payment (capture refund) */ async refund(params) { try { // First, get the capture ID from the order const orderRequest = new paypal.orders.OrdersGetRequest(params.transactionId); const order = await this.client.execute(orderRequest); const captureId = order.result.purchase_units[0].payments.captures[0].id; const refundRequest = new paypal.payments.CapturesRefundRequest(captureId); refundRequest.requestBody({ amount: { value: params.amount.toString(), currency_code: order.result.purchase_units[0].amount.currency_code, }, }); const refund = await this.client.execute(refundRequest); if (refund.result.status && refund.result.status === 'COMPLETED') { return { gateway: 'paypal', status: 'success', params: refund.result, message: 'PayPal refund processed', }; } else { return { gateway: 'paypal', status: 'failure', params: refund.result, message: 'PayPal refund not completed', }; } } catch (err) { if (err instanceof Error) { return { gateway: 'paypal', status: 'failure', params: { error: err.message }, message: err.message || 'PayPal refund failed', }; } return { gateway: 'paypal', status: 'failure', params: {}, message: 'PayPal refund failed', }; } } /** * Create a subscription (not implemented) */ // eslint-disable-next-line @typescript-eslint/no-unused-vars async subscribe(_params) { return { gateway: 'paypal', status: 'cancelled', params: {}, message: 'Paypal subscription not implemented', }; } /** * Create an invoice (not implemented) */ // eslint-disable-next-line @typescript-eslint/no-unused-vars async createInvoice(_params) { return { gateway: 'paypal', status: 'cancelled', params: {}, message: 'Paypal invoice not implemented', }; } /** * Wallet operations (not supported) */ // eslint-disable-next-line @typescript-eslint/no-unused-vars async wallet(_params) { return { gateway: 'paypal', status: 'failure', params: {}, message: 'Paypal does not support wallet operations', }; } } exports.PayPalGateway = PayPalGateway;