UNPKG

esewa-pay

Version:

A TypeScript SDK for integrating eSewa payments (supports both ESM & CJS)

114 lines (108 loc) 4.44 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { EsewaClient: () => EsewaClient }); module.exports = __toCommonJS(index_exports); // src/esewa.ts var import_axios = __toESM(require("axios")); // src/utils/crypto.ts var import_crypto = __toESM(require("crypto")); function generateHmacSha256(data, secret) { return import_crypto.default.createHmac("sha256", secret).update(data).digest("base64"); } // src/config.ts var API_URLS = { development: "https://rc-epay.esewa.com.np/api/epay/main/v2/form", production: "https://epay.esewa.com.np/api/epay/main/v2/form" }; var STATUS_URLS = { development: "https://rc.esewa.com.np/api/epay/transaction/status/", production: "https://epay.esewa.com.np/api/epay/transaction/status/" }; // src/esewa.ts var EsewaClient = class { constructor(config) { this.config = config; this.baseUrl = API_URLS[config.env || "development"]; this.statusUrl = STATUS_URLS[config.env || "development"]; } generateSignature(total_amount, transaction_uuid, product_code) { const data = `total_amount=${total_amount},transaction_uuid=${transaction_uuid},product_code=${product_code}`; return generateHmacSha256(data, this.config.secretKey); } async initiatePayment(params) { const signature = this.generateSignature(params.total_amount, params.transaction_uuid, this.config.productCode); const payload = { amount: params.amount, tax_amount: params.tax_amount || "0", product_service_charge: params.product_service_charge || "0", product_delivery_charge: params.product_delivery_charge || "0", total_amount: params.total_amount, transaction_uuid: params.transaction_uuid, product_code: this.config.productCode, success_url: this.config.successUrl, failure_url: this.config.failureUrl, signed_field_names: "total_amount,transaction_uuid,product_code", signature }; const paymentConfig = { url: this.baseUrl, data: payload, headers: { "Content-Type": "application/x-www-form-urlencoded" }, responseHandler: (response) => response.request?.res?.responseUrl }; const paymentReq = await import_axios.default.post(String(paymentConfig.url), paymentConfig.data, { headers: paymentConfig.headers }); const paymentUrl = paymentConfig.responseHandler(paymentReq); if (!paymentUrl) { throw new Error("Failed to get payment URL"); } return paymentUrl; } async verifyPayment(data) { const decodedData = Buffer.from(data, "base64").toString("utf-8"); const parsed = JSON.parse(decodedData); return parsed; } async getTransactionStatus(transaction_uuid, total_amount) { const url = `${this.statusUrl}?product_code=${this.config.productCode}&total_amount=${total_amount}&transaction_uuid=${transaction_uuid}`; const { data } = await import_axios.default.get(url); return data; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { EsewaClient });