kora-checkout
Version:
A JavaScript SDK for integrating with Kora's Checkout Standard payment gateway
201 lines (191 loc) • 6.09 kB
JavaScript
function asyncGeneratorStep(n, t, e, r, o, a, c) {
try {
var i = n[a](c),
u = i.value;
} catch (n) {
return void e(n);
}
i.done ? t(u) : Promise.resolve(u).then(r, o);
}
function _asyncToGenerator(n) {
return function () {
var t = this,
e = arguments;
return new Promise(function (r, o) {
var a = n.apply(t, e);
function _next(n) {
asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
}
function _throw(n) {
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
}
_next(void 0);
});
};
}
function _defineProperty(e, r, t) {
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
value: t,
enumerable: !0,
configurable: !0,
writable: !0
}) : e[r] = t, e;
}
function ownKeys(e, r) {
var t = Object.keys(e);
if (Object.getOwnPropertySymbols) {
var o = Object.getOwnPropertySymbols(e);
r && (o = o.filter(function (r) {
return Object.getOwnPropertyDescriptor(e, r).enumerable;
})), t.push.apply(t, o);
}
return t;
}
function _objectSpread2(e) {
for (var r = 1; r < arguments.length; r++) {
var t = null != arguments[r] ? arguments[r] : {};
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
_defineProperty(e, r, t[r]);
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
});
}
return e;
}
function _toPrimitive(t, r) {
if ("object" != typeof t || !t) return t;
var e = t[Symbol.toPrimitive];
if (void 0 !== e) {
var i = e.call(t, r || "default");
if ("object" != typeof i) return i;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return ("string" === r ? String : Number)(t);
}
function _toPropertyKey(t) {
var i = _toPrimitive(t, "string");
return "symbol" == typeof i ? i : i + "";
}
/**
* KoraPayment SDK
* A simple wrapper for Kora's Checkout Standard payment gateway
*/
class KoraPayment {
constructor() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
this.scriptLoaded = false;
this.scriptSrc = "https://korablobstorage.blob.core.windows.net/modal-bucket/korapay-collections.min.js";
this.config = config;
this.initialize = this.initialize.bind(this);
this.close = this.close.bind(this);
this.loadScript = this.loadScript.bind(this);
}
/**
* Loads the Kora payment script if not already loaded
* @returns {Promise} - Resolves when script is loaded
*/
loadScript() {
return new Promise((resolve, reject) => {
if (this.scriptLoaded || window.Korapay) {
this.scriptLoaded = true;
return resolve();
}
var script = document.createElement('script');
script.src = this.scriptSrc;
script.async = true;
script.onload = () => {
this.scriptLoaded = true;
resolve();
};
script.onerror = () => {
reject(new Error('Failed to load Kora Payment script'));
};
document.body.appendChild(script);
});
}
/**
* Initialize the payment gateway
* @param {Object} options - Payment configuration options
* @returns {Promise} - Resolves when initialized
*/
initialize() {
var _arguments = arguments,
_this = this;
return _asyncToGenerator(function* () {
var options = _arguments.length > 0 && _arguments[0] !== undefined ? _arguments[0] : {};
try {
var paymentOptions = _objectSpread2(_objectSpread2({}, _this.config), options);
_this.validateOptions(paymentOptions);
yield _this.loadScript();
if (window.Korapay) {
window.Korapay.initialize(paymentOptions);
return true;
}
throw new Error('Kora Payment script loaded but Korapay object not found');
} catch (error) {
console.error('Failed to initialize Kora Payment:', error);
throw error;
}
})();
}
/**
* Validate required options for payment
* @param {Object} options - Payment options to validate
*/
validateOptions(options) {
var requiredFields = ['key', 'reference', 'amount', 'customer'];
var customerRequiredFields = ['name', 'email'];
requiredFields.forEach(field => {
if (!options[field]) {
throw new Error("Missing required field: ".concat(field));
}
});
if (options.customer) {
customerRequiredFields.forEach(field => {
if (!options.customer[field]) {
throw new Error("Missing required customer field: ".concat(field));
}
});
}
if (options.amount && isNaN(parseFloat(options.amount))) {
throw new Error('Amount must be a number');
}
if (options.metadata && typeof options.metadata === 'object') {
var metadataKeys = Object.keys(options.metadata);
if (metadataKeys.length > 5) {
throw new Error('Metadata can have a maximum of 5 keys');
}
metadataKeys.forEach(key => {
if (key.length > 20) {
throw new Error("Metadata key \"".concat(key, "\" exceeds maximum length of 20 characters"));
}
if (!/^[A-Za-z0-9\-]+$/.test(key)) {
throw new Error("Metadata key \"".concat(key, "\" contains invalid characters. Only A-Z, a-z, 0-9, and - are allowed"));
}
});
}
}
/**
* Close the payment modal programmatically
*/
close() {
if (this.scriptLoaded && window.Korapay) {
window.Korapay.close();
return true;
}
return false;
}
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = KoraPayment;
}
/**
* Kora Payment SDK
* A wrapper for Kora's Checkout Standard payment gateway
*/
function createKoraPayment() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return new KoraPayment(config);
}
export { KoraPayment, createKoraPayment, KoraPayment as default };
//# sourceMappingURL=index.esm.js.map