UNPKG

@payos-inc/payos-js

Version:

PayOS JavaScript SDK for browser-based checkout and wallet onboarding

2 lines 7.53 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.PayOS=t():e.PayOS=t()}(this,()=>(()=>{"use strict";var e={149:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.WalletOnboardClient=void 0;t.WalletOnboardClient=class{constructor(e){this.popup=null,this.defaultUrl=e?.baseUrl?e.baseUrl:"https://wallet-onboard.payos.ai",this.defaultParams=e?.defaultParams}open(e){const{mode:t="popup"}=e;switch(this.close(),t){case"popup":return this.openPopup(e);case"redirect":return this.openRedirect(e);default:throw new Error(`Invalid mode: ${t}`)}}openPopup(e){const{token:t,baseUrl:o,customParams:r,environment:s="sandbox",returnUrl:n,merchantName:a,walletUserId:i,onComplete:l,onError:p,onCancel:c}=e,u=o||this.defaultUrl,d=crypto?.randomUUID?.()||`${Date.now()}-${Math.random().toString(36).slice(2)}`,h=new URLSearchParams({token:t,env:s,state:d,...this.defaultParams,...r});a&&h.set("merchantName",a),i&&h.set("walletUserId",i);const w=`${u}?${h.toString()}`;if(this.popup=window.open(w,"payos-wallet-onboard","width=500,height=700,left=100,top=100"),!this.popup||this.popup.closed)return n?(window.location.href=w,{close:()=>{}}):(p?.(new Error("Popup blocked")),{close:()=>{}});const m=e=>{if(!["https://payos.app","https://wallet-onboard.payos.ai","https://staging.wallet-onboard.payos.ai","https://purple-grass-0eb0a1b1e.1.azurestaticapps.net","http://localhost:3000","http://localhost:3001","http://localhost:5173"].some(t=>e.origin===t))return;const t=e.data;if(t?.status){if(t.state&&t.state!==d)return;return"success"===t.status?l?.({walletUserId:t.data?.walletUserId,linkedCardIds:t.data?.linkedCardIds||[]}):"cancel"===t.status?c?.():"error"===t.status&&p?.(new Error(t.error||"Unknown error")),window.removeEventListener("message",m),this.popup?.close(),void(this.popup=null)}const{type:o,success:r,walletUserId:s}=t;"PAYOS_LINK_CLOSE"===o&&(window.removeEventListener("message",m),r&&l?l({walletUserId:s,linkedCardIds:[]}):!r&&c&&c(),this.popup=null)};window.addEventListener("message",m);const f=setTimeout(()=>{window.removeEventListener("message",m),this.popup?.close(),this.popup=null},3e5),v=setInterval(()=>{this.popup&&this.popup.closed&&(clearInterval(v),clearTimeout(f),window.removeEventListener("message",m),c&&c(),this.popup=null)},500);return{close:()=>{this.popup&&!this.popup.closed&&(this.popup.close(),this.popup=null)}}}openRedirect(e){const{token:t,baseUrl:o,customParams:r,environment:s="sandbox",merchantName:n,walletUserId:a,returnUrl:i=window.location.href}=e,l=o||this.defaultUrl,p=new URLSearchParams({token:t,env:s,returnUrl:i,...this.defaultParams,...r});n&&p.set("merchantName",n),a&&p.set("walletUserId",a);const c=`${l}?${p.toString()}`;return window.location.href=c,{close:()=>{}}}close(){this.popup&&!this.popup.closed&&(this.popup.close(),this.popup=null)}}},156:function(e,t,o){var r=this&&this.__createBinding||(Object.create?function(e,t,o,r){void 0===r&&(r=o);var s=Object.getOwnPropertyDescriptor(t,o);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[o]}}),Object.defineProperty(e,r,s)}:function(e,t,o,r){void 0===r&&(r=o),e[r]=t[o]}),s=this&&this.__exportStar||function(e,t){for(var o in e)"default"===o||Object.prototype.hasOwnProperty.call(t,o)||r(t,e,o)};Object.defineProperty(t,"__esModule",{value:!0}),t.WalletOnboardClient=t.CheckoutClient=t.PayOS=void 0;const n=o(566);Object.defineProperty(t,"CheckoutClient",{enumerable:!0,get:function(){return n.CheckoutClient}});const a=o(149);Object.defineProperty(t,"WalletOnboardClient",{enumerable:!0,get:function(){return a.WalletOnboardClient}}),s(o(613),t);class i{constructor(e){this._checkout=new n.CheckoutClient(e),this._walletOnboard=new a.WalletOnboardClient(e)}get checkout(){return this._checkout}get walletOnboard(){return this._walletOnboard}static isLoaded(){return!0}static get version(){return"1.0.0"}}function l(e){const t=new a.WalletOnboardClient;let o;if("string"==typeof e)o={token:e};else if(o=e,!o.token)throw new Error("Token is required");return t.open({token:o.token,mode:o.mode||"popup",environment:o.environment,merchantName:o.merchantName,returnUrl:o.returnUrl,walletUserId:void 0,onComplete:e=>{o.onSuccess&&o.onSuccess(e.walletUserId)},onError:o.onError,onCancel:o.onClose})}t.PayOS=i,i.walletOnboard={init:l};const p={init:l};"undefined"!=typeof window&&(window.PayOS=i,window.PayOSWalletOnboard=p,window.initPayOS=(e,t)=>l({token:e,...t})),t.default=i},566:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.CheckoutClient=void 0;t.CheckoutClient=class{constructor(e){this.popup=null,this.messageHandler=null,this.currentState=null,this.popupCheckInterval=null,this.defaultUrl=e?.baseUrl?`${e.baseUrl}/checkout`:"https://wallet-onboard.payos.ai/checkout",this.defaultParams=e?.defaultParams}open(e){const{token:t,mode:o="popup",environment:r="sandbox",returnUrl:s,baseUrl:n,customParams:a,onReady:i,onComplete:l,onError:p,onCancel:c}=e;if(!t){const e=new Error("Token is required");throw p?.(e),e}const u=n?`${n}/checkout`:this.defaultUrl;this.currentState="undefined"!=typeof crypto&&crypto.randomUUID?crypto.randomUUID():Math.random().toString(36).substring(2,15)+Math.random().toString(36).substring(2,15);const d=new URLSearchParams({token:t,mode:o,env:r,state:this.currentState,...this.defaultParams,...a});s&&d.set("returnUrl",s);const h=`${u}?${d.toString()}`;if("redirect"===o)this.openRedirect(h);else this.openPopup(h,s,i,l,p,c)}close(){this.popupCheckInterval&&(clearInterval(this.popupCheckInterval),this.popupCheckInterval=null),this.popup&&(this.popup.close(),this.popup=null),this.messageHandler&&(window.removeEventListener("message",this.messageHandler),this.messageHandler=null)}openRedirect(e){window.location.href=e}openPopup(e,t,o,r,s,n){const a=(window.innerWidth-500)/2+window.screenLeft,i=(window.innerHeight-700)/2+window.screenTop;if(this.popup=window.open(e,"payos-checkout",`width=500,height=700,left=${a},top=${i},resizable=yes,scrollbars=yes`),!this.popup||this.popup.closed)return t?void(window.location.href=e):void s?.(new Error("Popup blocked"));this.setupMessageListener(o,r,s,n),this.popupCheckInterval=setInterval(()=>{this.popup?.closed&&(this.popupCheckInterval&&(clearInterval(this.popupCheckInterval),this.popupCheckInterval=null),n?.(),this.close())},500)}setupMessageListener(e,t,o,r){this.messageHandler=s=>{if(!["https://payos.app","https://checkout.payos.ai","https://calm-moss-06c4a301e.2.azurestaticapps.net","http://localhost:3000","http://localhost:3001","http://localhost:5173"].some(e=>s.origin===e))return;const n=s.data;if(n?.status){if(n.state&&n.state!==this.currentState)return;return"success"===n.status?t?.(n.data||{}):"cancel"===n.status?r?.():"error"===n.status&&o?.(new Error(n.error||"Unknown error")),void this.close()}if("payos-checkout"===n?.source)switch(n.type){case"CHECKOUT_READY":e?.();break;case"CHECKOUT_COMPLETE":t?.(n.payload),this.close();break;case"CHECKOUT_ERROR":o?.(new Error(n.error||"Checkout error")),this.close();break;case"CHECKOUT_CANCELLED":r?.(),this.close()}},window.addEventListener("message",this.messageHandler),setTimeout(()=>this.close(),3e5)}}},613:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})}},t={};var o=function o(r){var s=t[r];if(void 0!==s)return s.exports;var n=t[r]={exports:{}};return e[r].call(n.exports,n,n.exports,o),n.exports}(156);return o=o.default})()); //# sourceMappingURL=payos-js.min.js.map