@uphold/topper-web-sdk
Version:
Seamless integration of Topper on Web applications
2 lines • 3.28 kB
JavaScript
var a=(t=>(t.PRODUCTION="production",t.SANDBOX="sandbox",t))(a||{});var d=(o=>(o.ALL="*",o.ORDER_CONTINUE_BUTTON_CLICKED="orderContinueButtonClicked",o.ORDER_PLACED="orderPlaced",o.ORDER_WALLET_SEND_BUTTON_CLICKED="orderWalletSendButtonClicked",o.WIDGET_CONTINUE_BUTTON_CLICKED="widgetContinueButtonClicked",o))(d||{});var f=(e=>(e.AUTHENTICATION="authentication",e))(f||{});var c=(n=>(n.EN="en",n.EN_US="en-US",n.PT="pt",n.PT_BR="pt-BR",n.ES="es",n.ES_ES="es-ES",n))(c||{});var h=(t=>(t.DARK="dark",t.LIGHT="light",t))(h||{});var p=(i=>(i.PRODUCTION="https://app.topperpay.com",i.SANDBOX="https://app.sandbox.topperpay.com",i.WEBSITE="https://www.topperpay.com/",i))(p||{});var l=(i=>(i.IFRAME="iframe",i.NEW_TAB="new-tab",i.SAME_TAB="same-tab",i))(l||{});import m from"query-string";var g="@topper-web-sdk",E=class{eventHandlers={};handleMessage=null;isInitialized=!1;targetWindow=null;config;constructor(e={}){this.config={active_flow:null,environment:"production",initial_screen:null,is_android_app:!1,is_ios_app:!1,variant:"new-tab",...e}}static triggerEvent(e,t){let i=window.self!==window.top?window.parent:window.opener;i&&i.postMessage({name:e,payload:t,source:g},"*")}triggerEvent(e,t){this.eventHandlers["*"]?.forEach(i=>i({data:t,name:e})),this.eventHandlers[e]?.forEach(i=>i({data:t}))}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===g&&this.triggerEvent(e.data.name,e.data.payload)},window.addEventListener("message",this.handleMessage)}dispose(){window.removeEventListener("message",this.handleMessage,!1),this.eventHandlers={}}initialize({bootstrapToken:e,bootstrapTokens:t,config:i,iframeElement:r}){if(this.isInitialized)throw new Error("This TopperWebSdk instance was already initialized.");this.isInitialized=!0,i&&this.setConfig(i);let o=this.config.environment==="sandbox"?"https://app.sandbox.topperpay.com":"https://app.topperpay.com",n=t?t.join(";"):e,_=window.location.href.includes("https://www.topperpay.com/"),T={bt:n,...this.config.active_flow&&{active_flow:this.config.active_flow},..._&&{embed:1},...this.eventHandlers&&Object.keys(this.eventHandlers).length&&{events:Object.keys(this.eventHandlers).join(",")},...this.config.is_android_app&&{is_android_app:1},...this.config.is_ios_app&&{is_ios_app: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=m.stringifyUrl({query:T,url:`${o}/`});if(this.config.variant==="iframe"){if(!r)throw new Error("iframeElement is required for iframe variant");r.src=s,this.targetWindow=r.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,t){this.eventHandlers[e]||(this.eventHandlers[e]=[]),this.eventHandlers[e]?.push(t)}};export{a as TOPPER_ENVIRONMENTS,d as TOPPER_EVENTS,f as TOPPER_INITIAL_SCREENS,c as TOPPER_LOCALES,h as TOPPER_THEMES,p as TOPPER_URLS,l as TOPPER_VARIANTS,E as TopperWebSdk};
//# sourceMappingURL=index.js.map