ipay-payment-gateway
Version:
Nodejs library to accept ipay (ipay.com.bd) payments on your backend application
147 lines (142 loc) • 5.32 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
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 __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(
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 src_exports = {};
__export(src_exports, {
IpayGateway: () => IpayGateway,
default: () => src_default
});
module.exports = __toCommonJS(src_exports);
// src/exceptions/ipayException.ts
var IpayException = class extends Error {
constructor(message) {
var _a;
super(message);
this.name = "IpayException";
this.stack = (_a = this.stack) != null ? _a : new Error().stack;
}
};
// src/utils/request.ts
var import_node_fetch = __toESM(require("node-fetch"));
async function get(url, additionalHeaders) {
const response = await (0, import_node_fetch.default)(url, {
method: "GET",
headers: __spreadValues({
"content-type": "application/json",
Accept: "application/json"
}, additionalHeaders)
});
const parsed = await response.json();
if (parsed.errorMessage)
throw new IpayException(parsed.errorMessage);
return parsed;
}
async function post(url, payload = {}, additionalHeaders) {
const response = await (0, import_node_fetch.default)(url, {
headers: __spreadValues({
"content-type": "application/json",
Accept: "application/json"
}, additionalHeaders),
body: JSON.stringify(payload),
method: "POST"
});
const parsed = await response.json();
if (parsed.errorMessage)
throw new IpayException(parsed.errorMessage);
return parsed;
}
// src/index.ts
var IpayGateway = class {
constructor(config) {
this.createPayment = async (paymentDetails) => {
const { amount, referenceId, description, successUrl, failureUrl, cancelUrl } = paymentDetails;
const payload = {
amount,
referenceId,
description,
successCallbackUrl: successUrl || this.successUrl,
failureCallbackUrl: failureUrl || this.failureUrl,
cancelCallbackUrl: cancelUrl || this.cancelUrl
};
const headers = {
Authorization: `Bearer ${this.apiKey}`
};
return await post(`${this.baseURL}/order`, payload, headers);
};
this.checkStatusByOrderId = async (orderId) => {
const headers = {
Authorization: `Bearer ${this.apiKey}`
};
return await get(`${this.baseURL}/order/${orderId}/status`, headers);
};
this.checkStatusByRefId = async (refId) => {
const headers = {
Authorization: `Bearer ${this.apiKey}`
};
return await get(`${this.baseURL}/order/referenceId/${refId}/status`, headers);
};
this.validateConfig = (config) => {
const { baseURL, apiKey, successUrl, failureUrl, cancelUrl } = config;
if (!baseURL || baseURL === "")
throw new IpayException("Invalid BaseURL provided");
if (!apiKey || apiKey === "")
throw new IpayException("Invalid API Key provided");
if (!successUrl || successUrl === "")
throw new IpayException("Invalid successCallbackUrl provided");
if (!failureUrl || failureUrl === "")
throw new IpayException("Invalid API failureCallbackUrl provided");
if (!cancelUrl || cancelUrl === "")
throw new IpayException("Invalid API cancelCallbackUrl provided");
};
if (Object.keys(config).length !== 5)
throw new IpayException("Invalid Configuration provided");
this.validateConfig(config);
const { apiKey, baseURL, successUrl, failureUrl, cancelUrl } = config;
this.apiKey = apiKey;
this.baseURL = baseURL;
this.successUrl = successUrl;
this.failureUrl = failureUrl;
this.cancelUrl = cancelUrl;
}
};
var src_default = IpayGateway;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
IpayGateway
});