@payos-inc/payos-wallet-onboard-js
Version:
PayOS Wallet Onboard JavaScript SDK for easy integration of the PayOS onboarding flow.
2 lines • 5.58 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.PayOSWalletOnboard=t():e.PayOSWalletOnboard=t()}(this,(()=>(()=>{"use strict";var e={d:(t,n)=>{for(var o in n)e.o(n,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:n[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{default:()=>a});var n=function(){return n=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},n.apply(this,arguments)};const o=function(){function e(e){if(this.container=null,this.iframe=null,this.messageListener=null,this.options=n({environment:"sandbox",height:"550px"},e),this.validateOptions(),"string"==typeof e.container){if(this.container=document.querySelector(e.container),!this.container)throw new Error("Container element not found: ".concat(e.container))}else this.container=e.container}return e.prototype.mount=function(){if(!this.container)throw new Error("Container element not found");this.unmount(),this.iframe=document.createElement("iframe"),this.iframe.style.width="100%",this.iframe.style.height=this.options.height||"550px",this.iframe.style.border="none",this.iframe.style.borderRadius="10px",this.iframe.style.overflow="hidden";var e=this.buildIframeUrl();this.iframe.src=e,this.container.appendChild(this.iframe),this.messageListener=this.handleMessage.bind(this),window.addEventListener("message",this.messageListener)},e.prototype.unmount=function(){this.iframe&&this.iframe.parentNode&&(this.iframe.parentNode.removeChild(this.iframe),this.iframe=null),this.messageListener&&(window.removeEventListener("message",this.messageListener),this.messageListener=null)},e.prototype.handleMessage=function(e){if(this.getAllowedOrigins().some((function(t){return e.origin===t}))){var t=e.data;if(t&&"object"==typeof t&&t.type)switch(t.type){case"PAYOS_LINK_CLOSE":this.options.onClose&&this.options.onClose({success:!!t.success,walletUserId:t.walletUserId||void 0}),this.unmount();break;case"PAYOS_LINK_ERROR":this.options.onError&&this.options.onError(new Error(t.error||"Unknown error"))}}},e.prototype.buildIframeUrl=function(){var e=this.getBaseUrl(),t=new URLSearchParams;return t.set("token",this.options.token),t.set("domain",window.location.origin),t.set("env",this.options.environment||"sandbox"),this.options.merchantName&&t.set("merchantName",this.options.merchantName),this.options.walletUserId&&t.set("walletUserId",this.options.walletUserId),"".concat(e,"?").concat(t.toString())},e.prototype.getBaseUrl=function(){return this.options.customBaseUrl?this.options.customBaseUrl:"production"===this.options.environment?"https://wallet-onboard.payos.ai":"localhost"===window.location.hostname||"127.0.0.1"===window.location.hostname?"http://localhost:3000/link":"https://wallet-onboard.payos.ai"},e.prototype.getAllowedOrigins=function(){var e=new Set;try{var t=new URL(this.getBaseUrl());e.add(t.origin)}catch(e){}return e.add("https://wallet-onboard.payos.ai"),e.add("http://localhost:3000"),Array.from(e)},e.prototype.validateOptions=function(){if(!this.options.token)throw new Error("Token is required");if(this.options.environment&&!["sandbox","production"].includes(this.options.environment))throw new Error('Environment must be either "sandbox" or "production"')},e}();var r=function(){return r=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},r.apply(this,arguments)};function i(e,t){var n;if("string"!=typeof e||t)if("object"!=typeof e||e instanceof HTMLElement){l=void 0;if("string"==typeof e)l=e;else{if(!(e instanceof HTMLElement))throw new Error("First argument must be a container selector, HTMLElement, token string, or options object");l=e}if(!t)throw new Error("When providing a container, options must be provided as second argument");if("token"in t&&!("container"in t)){var i=t;n=new o({container:l,token:i.token,environment:i.environment,merchantName:i.merchantName,customBaseUrl:i.customBaseUrl,onClose:function(e){e.success&&i.onSuccess?i.onSuccess(e.walletUserId):!e.success&&i.onClose&&i.onClose()},onError:i.onError})}else{n=new o(r(r({},t),{container:l}))}}else{var s=e;if(!s.token)throw new Error("Token is required");var a=s.containerId||"payos-wallet-onboard-container";(l=document.getElementById(a))||((l=document.createElement("div")).id=a,l.style.width="100%",l.style.minHeight="500px",document.body.appendChild(l)),n=new o({container:l,token:s.token,environment:s.environment,merchantName:s.merchantName,customBaseUrl:s.customBaseUrl,onClose:function(e){e.success&&s.onSuccess?s.onSuccess(e.walletUserId):!e.success&&s.onClose&&s.onClose()},onError:s.onError})}else{if(e.startsWith("#")||e.startsWith("."))throw new Error("When providing a selector as first argument, options must be provided as second argument");var c=e,l=function(){var e=document.createElement("div");return e.id="payos-wallet-onboard-container",e.style.width="100%",e.style.minHeight="500px",e.style.margin="0 auto",document.body.appendChild(e),e}();n=new o({container:l,token:c})}return n.mount(),{mount:function(){return n.mount()},unmount:function(){return n.unmount()}}}var s={init:i,PayOSWalletOnboard:o};"undefined"!=typeof window&&(window.PayOSWalletOnboard=s,window.initPayOS=function(e,t){return i(r({token:e},t))});const a=s;return t=t.default})()));
//# sourceMappingURL=payos-wallet-onboard.min.js.map