@adsesugh/monnify-sdk
Version:
The Monnify Web SDK is now available as a Node.js package, offering an easy and efficient way to integrate payment gateway solutions into your web applications
43 lines (42 loc) • 2.22 kB
JavaScript
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());
});
};
export default class Monnify {
constructor({ publicKey, contractCode, mode = 'TEST' }) {
this.publicKey = publicKey;
this.contractCode = contractCode;
this.mode = mode;
}
// Method to dynamically load the Monnify SDK script
loadMonnifyScript() {
return new Promise((resolve, reject) => {
if (typeof window === 'undefined') {
return reject(new Error('Monnify SDK requires a browser environment.'));
}
const existingScript = document.getElementById('monnify-sdk');
if (existingScript) {
return resolve(); // Script already loaded
}
const script = document.createElement('script');
script.src = 'https://sdk.monnify.com/plugin/monnify.js';
script.id = 'monnify-sdk';
script.onload = () => resolve();
script.onerror = () => reject(new Error('Failed to load the Monnify SDK script'));
document.body.appendChild(script);
});
}
initializePayment(paymentDetails) {
return __awaiter(this, void 0, void 0, function* () {
yield this.loadMonnifyScript();
return new Promise((resolve, reject) => {
window.MonnifySDK.initialize(Object.assign(Object.assign({}, paymentDetails), { apiKey: this.publicKey, contractCode: this.contractCode, mode: this.mode, onComplete: (response) => resolve(response), onClose: (response) => reject(response) }));
});
});
}
}