UNPKG

payeth-checkout

Version:

Inline Payeth Checkout SDK

126 lines (100 loc) 3.62 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); /** * * @param {Object} Payload * * @param {string} key * @param {number} amount * @param {string} currency * @param {string} email * @param {string} nanameme * */ var PayethCheckoutInline = /*#__PURE__*/function () { function PayethCheckoutInline(payload) { this.sdkUrl = "https://payeth-checkout-qa.herokuapp.com/"; this.key = payload.key; if (this.key.length < 1) { throw new Error("Key cannot be empty"); } this.amount = payload.amount; if (this.amount <= 0) { throw new Error("Amount must be greater than 0"); } this.currency = payload.currency == undefined ? "" : payload.currency; this.email = payload.email == undefined ? "" : payload.email; this.name = payload.name == undefined ? "" : payload.name; this.onSuccess = payload.onSuccess; if (this.onSuccess == undefined || typeof this.onSuccess != "function") { throw new Error("onSuccess must be a function"); } this.onCancel = payload.onCancel; if (this.onCancel == undefined || typeof this.onCancel != "function") { throw new Error("onCancel must be a function"); } var params = ""; params += "?x_public_key=" + this.key; params += "&amount=" + this.amount; if (this.currency.length > 0) { params += "&currency=" + this.currency; } if (this.email.length > 0) { params += "&customer_email=" + encodeURI(this.email); } if (this.name.length > 0) { params += "&customer_name=" + encodeURI(this.name); } var checkoutUrl = this.sdkUrl + params; var iframe = document.createElement("iframe"); iframe.style.display = "none"; iframe.style.border = "none"; iframe.style.width = "100vw"; iframe.style.height = "100vh"; iframe.style.position = "absolute"; iframe.style.top = "0"; iframe.style.zIndex = "24500302"; iframe.allow = "payment"; // create second iframe iframe.src = checkoutUrl; this.iframe = iframe; document.body.appendChild(iframe); var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent"; var eventer = window[eventMethod]; var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message"; var self = this; eventer(messageEvent, function (e) { var key = e.message ? "message" : "data"; var data = e[key]; self.handleMessage(data); }, false); } var _proto = PayethCheckoutInline.prototype; _proto.initiate = function initiate() { this.iframe.style.display = "flex"; }; _proto.handleMessage = function handleMessage(value) { switch (value) { case "payeth.modal.cancelled": this.closeModal(); break; case "payeth.modal.success": this.paymentSuccess(); break; } }; _proto.closeModal = function closeModal() { var _this$iframe$contentW; this.iframe.style.display = "none"; this.onCancel(); (_this$iframe$contentW = this.iframe.contentWindow) == null ? void 0 : _this$iframe$contentW.postMessage('payeth.modal.close.handle', '*'); }; _proto.paymentSuccess = function paymentSuccess() { var _this$iframe$contentW2; this.iframe.style.display = "none"; this.onSuccess(); (_this$iframe$contentW2 = this.iframe.contentWindow) == null ? void 0 : _this$iframe$contentW2.postMessage('payeth.modal.close.handle', '*'); }; return PayethCheckoutInline; }(); exports.PayethCheckoutInline = PayethCheckoutInline; //# sourceMappingURL=payeth-checkout.cjs.development.js.map