idpay-node
Version:
A NodeJS library for IDPay payment gateway
140 lines (139 loc) • 5.85 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IDPay = void 0;
const axios_1 = __importDefault(require("axios"));
const cfg_1 = require("./cfg");
const interfaces_1 = require("./interfaces");
const uuid = __importStar(require("uuid"));
const constants_1 = require("./constants");
class IDPay {
/**
*
* @param apiKey API-KEY provided by IDPay
* @param sandbox Flag to enable sandbox mode for testing purposes
*/
constructor(apiKey, sandBox) {
this.sandBox = false;
if (!apiKey)
throw new Error('API Key not provided.');
this.apiKey = apiKey;
if (sandBox) {
this.sandBox = sandBox;
}
}
/**
* @description
* @param payment Payment request object
* @returns Payment Response
*/
paymentRequest(payment) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
if (!payment.order_id || !payment.order_id.length) {
payment.order_id = uuid.v4();
}
try {
console.log(payment);
const response = yield axios_1.default.post(cfg_1.config.createPaymentURL, payment, {
headers: {
'Content-type': 'application/json',
'X-API-KEY': this.apiKey,
'X-SANDBOX': this.sandBox ? 1 : 0,
}
});
return response.data;
}
catch (err) {
if ((_a = err.response) === null || _a === void 0 ? void 0 : _a.data) {
const errorDesc = constants_1.IDPayErrorCodes[(_b = err.response) === null || _b === void 0 ? void 0 : _b.data.error_code];
throw new Error(errorDesc);
}
else {
throw new Error(err.message);
}
}
});
}
/**
* @description Callback is optional as the user can implement custom logic outside of
* the function and user can use this function to just verify the callback result.
*/
processPaymentRequest(cbResult, cb) {
if (cb && typeof cb !== 'function')
throw new Error('Callback provided is not a function');
if (cbResult.status !== interfaces_1.IDPayTrasactionStatus.WAITING_FOR_APPROVAL)
throw new Error(constants_1.IDPayTrasactionStatusDesc[cbResult.status]);
if (!cb)
return true;
cb(cbResult);
return;
}
/**
* @description Callback is optional as the user can implement custom logic outside of
* the function. User can use this send a the payment result to verify the payment.
* @returns Payment verification result if a callback is not provided
*/
verifyPayment(id, order_id, cb) {
return __awaiter(this, void 0, void 0, function* () {
if (!id)
throw new Error('Transaction id not provided');
if (typeof id !== 'string')
throw new Error('Trasaction id provided is not a string');
if (!order_id)
throw new Error('Transaction order_id not provided');
if (typeof order_id !== 'string')
throw new Error('Trasaction order_id provided is not a string');
if (cb && typeof cb !== 'function')
throw new Error('Callback provided is not a function');
let result;
try {
result = (yield axios_1.default.post(cfg_1.config.verifyPayment, {
id,
order_id,
})).data;
}
catch (err) {
throw new Error(constants_1.IDPayErrorCodes[err.response.data.error_code]);
}
const { status } = result;
if (status !== interfaces_1.IDPayTrasactionStatus.PAYMENT_ACCEPTED) {
throw new Error(constants_1.IDPayTrasactionStatusDesc[status]);
}
if (!cb)
return result;
cb(result);
});
}
}
exports.IDPay = IDPay;