UNPKG

aamarpay.v2

Version:

A simple library for ammar pay payment gateway

183 lines (177 loc) 4.92 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/PaymentInit.ts import axios from "axios"; // src/util.ts function verifyAamarpayData(data) { const validationChecks = [ { field: "amount", type: "string", check: (value) => /^\d+(\.\d*)?$/.test(value), errorMessage: "\u{1F6D1} amount should be a valid string representation of a number. \u{1F6D1}" }, { field: "currency", type: "string", errorMessage: "\u{1F6D1} currency should be a string." }, { field: "desc", type: "string", errorMessage: "\u{1F6D1} desc should be a string." }, { field: "cus_name", type: "string", errorMessage: "\u{1F6D1} cus_name should be a string." }, { field: "cus_email", type: "string", check: isValidEmail, errorMessage: " \u{1F6D1} cus_email should be a valid email address." }, { field: "cus_phone", type: "string", errorMessage: " \u{1F6D1} cus_phone should be a string." } ]; for (const check of validationChecks) { const { field, type, check: validationCheck, errorMessage } = check; if (typeof data[field] !== type || validationCheck && !validationCheck(data[field])) { throw new Error(errorMessage); } } return data; } function isValidEmail(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } // src/PaymentInit.ts var PaymentInit = (_0) => __async(void 0, [_0], function* ({ data, fail_url, cancel_url, success_url, store_id, signature_key, live }) { var _a, _b; try { let apiUrl = "https://sandbox.aamarpay.com/jsonpost.php"; if (live) { apiUrl = "https://secure.aamarpay.com"; } const requestData_pay = __spreadProps(__spreadValues({}, verifyAamarpayData(data)), { store_id, signature_key, fail_url, cancel_url, success_url, type: "json" }); const response = yield axios.post(apiUrl, requestData_pay); if ((_a = response.data) == null ? void 0 : _a.result) { return (_b = response.data) == null ? void 0 : _b.payment_url; } else { throw new Error(JSON.stringify(response.data)); } } catch (error) { throw error; } }); // src/Search.ts import axios2 from "axios"; var Search = (_0) => __async(void 0, [_0], function* ({ live, request_id, store_id, signature_key }) { let apiUrl = "https://sandbox.aamarpay.com/api/v1/trxcheck/request.php"; if (live) { apiUrl = "https://secure.aamarpay.com/api/v1/trxcheck/request.php"; } try { const res = yield axios2.get( `${apiUrl}?request_id=${request_id}&store_id=${store_id}&signature_key=${signature_key}&type=json` ); return res.data; } catch (error) { throw error; } }); // src/Main.ts var Payment = class { constructor(storeID, signature_key, live) { this.storeID = storeID; this.signature_key = signature_key; this.live = live; } init(_0) { return __async(this, arguments, function* ({ data, fail_url, cancel_url, success_url }) { return PaymentInit({ data, fail_url, cancel_url, success_url, live: this.live, signature_key: this.signature_key, store_id: this.storeID }); }); } Search(transition_id) { return Search({ live: this.live, request_id: transition_id, store_id: this.storeID, signature_key: this.signature_key }); } }; var Main_default = Payment; export { Main_default as Payment };