aamarpay.v2
Version:
A simple library for ammar pay payment gateway
217 lines (210 loc) • 6.5 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
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 __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);
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/index.ts
var src_exports = {};
__export(src_exports, {
Payment: () => Main_default
});
module.exports = __toCommonJS(src_exports);
// src/PaymentInit.ts
var import_axios = __toESM(require("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 import_axios.default.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
var import_axios2 = __toESM(require("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 import_axios2.default.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;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Payment
});