UNPKG

@uphold/topper-web-sdk

Version:

Seamless integration of Topper on Web applications

2 lines 4.86 kB
var d=(i=>(i.PRODUCTION="production",i.SANDBOX="sandbox",i))(d||{});var l=(r=>(r.ALL="*",r.ORDER_CONTINUE_BUTTON_CLICKED="orderContinueButtonClicked",r.ORDER_PLACED="orderPlaced",r.ORDER_WALLET_SEND_BUTTON_CLICKED="orderWalletSendButtonClicked",r.WIDGET_CONTINUE_BUTTON_CLICKED="widgetContinueButtonClicked",r))(l||{});var c=(e=>(e.AUTHENTICATION="authentication",e))(c||{});var h=(a=>(a.EN="en",a.EN_US="en-US",a.PT="pt",a.PT_BR="pt-BR",a.ES="es",a.ES_ES="es-ES",a))(h||{});var g=(i=>(i.DARK="dark",i.LIGHT="light",i))(g||{});var _=(e=>(e.SIGN_AND_BROADCAST_TRANSACTION="signAndBroadcastTransaction",e))(_||{});var p=(t=>(t.PRODUCTION="https://app.topperpay.com",t.SANDBOX="https://app.sandbox.topperpay.com",t.WEBSITE="https://www.topperpay.com/",t))(p||{});var E=(t=>(t.IFRAME="iframe",t.NEW_TAB="new-tab",t.SAME_TAB="same-tab",t))(E||{});import w from"query-string";var f=class{eventHandlers={};handlers={};handleMessage=null;isInitialized=!1;targetWindow=null;config;constructor(e={}){this.config={active_flow:null,environment:"production",initial_screen:null,is_android_app:!1,is_android_webview:!1,is_ios_app:!1,is_ios_webview:!1,variant:"new-tab",...e}}static getMainWindow(){return window.self!==window.top?window.parent:window.opener}static handle(e,i){return this.getMainWindow().postMessage({name:"resolveHandler",payload:{data:i,handlerName:e},source:"@topper-web-sdk-handler"},"*"),new Promise((n,r)=>{let a=o=>{if(o.data.name==="resolveHandler"&&o.data.payload&&o.data.source==="@topper-web-sdk-handler"){if(window.removeEventListener("message",a),o.data.payload.error){r(o.data.payload.error);return}n(o.data.payload)}};window.addEventListener("message",a)})}static triggerEvent(e,i){let t=this.getMainWindow();t&&t.postMessage({name:e,payload:i,source:"@topper-web-sdk-event"},"*")}triggerEvent(e,i){this.eventHandlers["*"]?.forEach(t=>t({data:i,name:e})),this.eventHandlers[e]?.forEach(t=>t({data:i}))}setConfig(e){this.config={...this.config,...e}}initializeMessageListener(){this.handleMessage=e=>{!["https://app.topperpay.com","https://app.sandbox.topperpay.com"].includes(e.origin)||e.source!==this.targetWindow||(e.data.name&&e.data.source==="@topper-web-sdk-event"&&this.triggerEvent(e.data.name,e.data.payload),e.data.name==="resolveHandler"&&e.data.payload&&e.data.source==="@topper-web-sdk-handler"&&this.resolveHandler(e.data.payload.handlerName,e.data.payload.data))},window.addEventListener("message",this.handleMessage)}dispose(){window.removeEventListener("message",this.handleMessage,!1),this.eventHandlers={}}initialize({bootstrapToken:e,bootstrapTokens:i,config:t,iframeElement:n}){if(this.isInitialized)throw new Error("This TopperWebSdk instance was already initialized.");this.isInitialized=!0,t&&this.setConfig(t);let r=this.config.environment==="sandbox"?"https://app.sandbox.topperpay.com":"https://app.topperpay.com",a=i?i.join(";"):e,o=window.location.href.includes("https://www.topperpay.com/"),m={bt:a,...this.config.active_flow&&{active_flow:this.config.active_flow},...o&&{embed:1},...this.eventHandlers&&Object.keys(this.eventHandlers).length&&{events:Object.keys(this.eventHandlers).join(",")},...this.handlers&&Object.keys(this.handlers).length&&{handlers:Object.keys(this.handlers).join(",")},...this.config.is_android_app&&{is_android_app:1},...this.config.is_android_webview&&{is_android_webview:1},...this.config.is_ios_app&&{is_ios_app:1},...this.config.is_ios_webview&&{is_ios_webview:1},...this.config.initial_screen&&{initial_screen:this.config.initial_screen},...this.config.locale&&{locale:this.config.locale},...this.config.theme&&{theme:this.config.theme}},s=w.stringifyUrl({query:m,url:`${r}/`});if(this.config.variant==="iframe"){if(!n)throw new Error("iframeElement is required for iframe variant");n.src=s,this.targetWindow=n.contentWindow}else if(this.config.variant==="new-tab")this.targetWindow=window.open(s,"_blank");else if(this.config.variant==="same-tab"){window.location.href=s;return}this.initializeMessageListener()}on(e,i){this.eventHandlers[e]||(this.eventHandlers[e]=[]),this.eventHandlers[e]?.push(i)}registerHandler(e,i){if(typeof i!="function")throw new Error("Handler must be a function.");this.handlers[e]=i}async resolveHandler(e,i){let t=this.handlers[e];if(!t)throw new Error(`Handler "${e}" is not registered.`);try{let n=await t(i);this.targetWindow?.postMessage({name:"resolveHandler",payload:n,source:"@topper-web-sdk-handler"},"*")}catch(n){this.targetWindow?.postMessage({name:"resolveHandler",payload:{error:{message:n instanceof Error?n.message:String(n),name:n instanceof Error?n.name:"Error"}},source:"@topper-web-sdk-handler"},"*")}}};export{d as TOPPER_ENVIRONMENTS,l as TOPPER_EVENTS,_ as TOPPER_HANDLERS,c as TOPPER_INITIAL_SCREENS,h as TOPPER_LOCALES,g as TOPPER_THEMES,p as TOPPER_URLS,E as TOPPER_VARIANTS,f as TopperWebSdk}; //# sourceMappingURL=index.js.map