@collectapp/checkout-sdk
Version:
Collect Africa Checkout library
3 lines (2 loc) • 4.61 kB
JavaScript
!function(){"use strict";const e={HOST_KEY:"collect-pv-data",DEFAULT_CURRENCY:"NGN",DEFAULT_ITEM_IMAGE:"/development/shoe.png",MIN_AMOUNT:.1,Z_INDEX:"9999999",ID_PREFIX:"collect-pv-build-",WIDGET_ID_PREFIX:"collect-pv-widget-",LOADER_ID:"ct-px__app-loader",RANDOM_ID_MIN:1e4,RANDOM_ID_MAX:9e3};class t{constructor(t){this.clientConfiguration=t,this.hostKey=e.HOST_KEY,this._handleMessageAddEventListener=e=>this.handleMessageAddEventListener(e);const i=this.validateConfiguration(this.clientConfiguration);if(void 0!==i)throw new Error(`Configuration is invalid: ${i.join(", ")}`);if("undefined"==typeof window)throw new Error("Window object does not exist");if(this.clientConfiguration.localUrl)this.hostUrl=this.clientConfiguration.localUrl;else{this.clientConfiguration.publicKey.startsWith("pk_live_");this.hostUrl="https://stablecoin-checkout.vercel.app"}this.collectAppID=Math.floor(e.RANDOM_ID_MIN+Math.random()*e.RANDOM_ID_MAX),this.collectAppLoader=document.createElement("div"),this.collectAppWidget=document.createElement("iframe")}setup(){this.collectAppLoader.id=`${e.ID_PREFIX}${this.collectAppID}`,Object.assign(this.collectAppLoader.style,{position:"fixed",left:"0px",top:"0px",height:"100%",width:"100%",overflow:"hidden",zIndex:e.Z_INDEX,backgroundColor:"rgba(0, 0, 0, 0.75)"}),this.collectAppLoader.innerHTML=`\n <div id="${e.LOADER_ID}" style="position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);">\n <div style="border: 3px solid #f3f3f3;border-radius: 50%;border-top: 3px solid #3498db;width: 30px;height: 30px;-webkit-animation: spin 2s linear infinite; animation: spin 2s linear infinite;"></div>\n <style>\n @-webkit-keyframes spin { 0% { -webkit-transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); } }\n spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }\n </style>\n </div>\n `,this.collectAppWidget.id=`${e.WIDGET_ID_PREFIX}${this.collectAppID}`,this.collectAppWidget.src=this.hostUrl,this.collectAppWidget.setAttribute("allowtransparency","true"),Object.assign(this.collectAppWidget.style,{position:"fixed",left:"0px",top:"0px",margin:"0px",padding:"0px",height:"100%",width:"100%",overflow:"hidden",zIndex:e.Z_INDEX,visibility:"visible",backgroundColor:"transparent",borderStyle:"none",borderWidth:"0px"}),this.collectAppWidget.style.setProperty("-webkit-tap-highlight-color","transparent"),this.collectAppWidget.onload=()=>{var t;const{email:i,firstName:o,lastName:n="",reference:s,amount:r,currency:l=e.DEFAULT_CURRENCY,paymentLinkCode:a="",invoiceCode:c="",planCode:d="",itemImage:p=e.DEFAULT_ITEM_IMAGE,publicKey:h}=this.clientConfiguration;null===(t=this.collectAppWidget.contentWindow)||void 0===t||t.postMessage({key:this.hostKey,email:i,firstName:o,lastName:n,reference:s,amount:r,currency:l,paymentLinkCode:a,invoiceCode:c,planCode:d,itemImage:p,publicKey:h},this.hostUrl),window.addEventListener("message",this._handleMessageAddEventListener,!1)}}open(){document.body.appendChild(this.collectAppLoader),document.body.appendChild(this.collectAppWidget)}async close(){"function"==typeof this.clientConfiguration.beforeClose&&await this.clientConfiguration.beforeClose("Before close checkout"),document.body.contains(this.collectAppLoader)&&document.body.removeChild(this.collectAppLoader),document.body.contains(this.collectAppWidget)&&document.body.removeChild(this.collectAppWidget),window.removeEventListener("message",this._handleMessageAddEventListener,!1),"function"==typeof this.clientConfiguration.onClose&&this.clientConfiguration.onClose("closed checkout")}handleMessageAddEventListener(t){var i;if(t.origin!==this.hostUrl||(null===(i=t.data)||void 0===i?void 0:i.key)!==this.hostKey)return;const{type:o,data:n=""}=t.data;switch(o){case"loading":const t=this.collectAppLoader.querySelector(`div#${e.LOADER_ID}`);t&&(t.style.display="none");break;case"close":this.close();break;case"success":"function"==typeof this.clientConfiguration.onSuccess&&this.clientConfiguration.onSuccess(n);break;case"error":"function"==typeof this.clientConfiguration.onError&&this.clientConfiguration.onError(n)}}validateConfiguration(t){const i=[];return t.email||i.push("email is required"),t.firstName||i.push("first name is required"),t.reference||i.push("reference is required"),t.amount?t.amount<e.MIN_AMOUNT&&i.push("amount must be greater than 100 NGN"):i.push("amount is required"),t.publicKey||i.push("public key is required"),i.length>0?i:void 0}}"undefined"!=typeof window&&(window.CollectCheckout=t)}();
//# sourceMappingURL=collect-widget.js.map