@ic-pay/icpay-widget 1.2.54 → 1.2.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1410,7 +1410,7 @@ import{a as s}from"./chunk-LTEQ7OUJ.js";import{css as Xe}from"lit";var H=Xe`
|
|
|
1410
1410
|
border-color: rgba(239, 68, 68, 0.3);
|
|
1411
1411
|
color: #ef4444;
|
|
1412
1412
|
}
|
|
1413
|
-
`],s([Ct({type:Object})],b.prototype,"config",2),s([M()],b.prototype,"selectedAmount",2),s([M()],b.prototype,"selectedSymbol",2),s([M()],b.prototype,"raised",2),s([M()],b.prototype,"processing",2),s([M()],b.prototype,"succeeded",2),s([M()],b.prototype,"errorMessage",2),s([M()],b.prototype,"errorSeverity",2),s([M()],b.prototype,"errorAction",2),s([M()],b.prototype,"walletConnected",2),s([M()],b.prototype,"pendingAction",2),s([M()],b.prototype,"showWalletModal",2),s([M()],b.prototype,"oisyReadyToPay",2),s([M()],b.prototype,"lastWalletId",2),s([M()],b.prototype,"showOnrampModal",2),s([M()],b.prototype,"onrampSessionId",2),s([M()],b.prototype,"onrampPaymentIntentId",2),s([M()],b.prototype,"onrampErrorMessage",2),s([M()],b.prototype,"showBalanceModal",2),s([M()],b.prototype,"balancesLoading",2),s([M()],b.prototype,"balancesError",2),s([M()],b.prototype,"walletBalances",2),b=s([Wt("icpay-donation-thermometer")],b);import{LitElement as Tt,html as ke,css as At}from"lit";import{customElement as Ot,property as Lt,state as S}from"lit/decorators.js";var qe=typeof window<"u",ue=null;function $(i,l,e){i&&(e!==void 0?console.log(`[ICPay Widget] ${l}`,e):console.log(`[ICPay Widget] ${l}`))}var y=class extends Tt{constructor(){super(...arguments);this.selectedSymbol=null;this.processing=!1;this.succeeded=!1;this.errorMessage=null;this.errorSeverity=null;this.errorAction=null;this.walletConnected=!1;this.pendingAction=null;this.showWalletModal=!1;this.walletModalStep="connect";this.showOnrampModal=!1;this.onrampSessionId=null;this.onrampPaymentIntentId=null;this.onrampErrorMessage=null;this.oisyReadyToPay=!1;this.oisySignerPreopened=!1;this.skipDisconnectOnce=!1;this.lastWalletId=null;this.showBalanceModal=!1;this.balancesLoading=!1;this.balancesError=null;this.walletBalances=[];this.onrampPollTimer=null;this.transakMessageHandlerBound=null;this.pnp=null;this.oisyConnectRetriedNewTab=!1;this.sdk=null;this.onrampPollingActive=!1;this.onrampNotifyController=null;this.onSwitchAccount=async e=>{try{if(this.pnp)try{await this.pnp.disconnect()}catch{}this.walletConnected=!1,this.config={...this.config,actorProvider:void 0,connectedWallet:void 0},this.pendingAction="pay",this.walletModalStep="connect",this.showWalletModal=!0,this.oisyReadyToPay=!1,this.lastWalletId=null,this.requestUpdate();try{let t=Number(this.config?.amountUsd||0),n=this.selectedSymbol||"ICP";window.dispatchEvent(new CustomEvent("icpay-sdk-method-start",{detail:{name:"pay",type:"sendUsd",amount:t,currency:n}}))}catch{}}catch{}};this.onSelectBalanceSymbol=async e=>{let t=(this.walletBalances||[]).find(a=>a?.tokenShortcode===e);if(t?.ledgerSymbol&&(this.selectedSymbol=t.ledgerSymbol),this.showBalanceModal=!1,this.showWalletModal=!1,R(this.lastWalletId)){$(this.config?.debug||!1,"EVM selection made",{selectedSymbol:this.selectedSymbol,selPresent:!!t,selSnapshot:t?{ledgerId:t?.ledgerId,ledgerSymbol:t?.ledgerSymbol,ledgerName:t?.ledgerName,chainUuid:t?.chainUuid,chainId:t?.chainId,chainName:t?.chainName,x402Accepts:t?.x402Accepts,requiredAmount:t?.requiredAmount,hasSufficientBalance:t?.hasSufficientBalance}:null});let a=t?.chainId;V(a,{provider:this.pnp?.getEvmProvider?.()||this.config?.evmProvider,chainName:t?.chainName,rpcUrlPublic:t?.rpcUrlPublic,nativeSymbol:t?.ledgerSymbol,decimals:t?.decimals}).then(async()=>{try{let r=this.getSdk(),o=Number(this.config?.amountUsd??0),c=t?.ledgerSymbol,d=!!(t&&t.x402Accepts);if($(this.config?.debug||!1,"EVM post-ensure chain snapshot",{targetChain:a,amountUsd:o,symbolNow:c,tryX402:d,x402Accepts:t?.x402Accepts}),d)try{let h={...this.config?.metadata,icpay_network:"evm",icpay_ledger_id:t?.ledgerId,icpay_context:"pay-button:x402"};$(this.config?.debug||!1,"Attempting X402 flow (EVM selection)",{amountUsd:o,tokenShortcode:t?.tokenShortcode,x402Accepts:t?.x402Accepts}),await r.client.createPaymentX402Usd({usdAmount:o,tokenShortcode:t?.tokenShortcode,metadata:h});return}catch(h){$(this.config?.debug||!1,"X402 payment failed (EVM selection), falling back",{message:h?.message,code:h?.code,data:h?.details||h?.data})}else $(this.config?.debug||!1,"Skipping X402 path",{reason:t?t.x402Accepts?"unknown":"x402Accepts false":"no selection",selPresent:!!t,x402Accepts:t?.x402Accepts});$(this.config?.debug||!1,"Falling back to normal EVM wallet flow",{amountUsd:o,tokenShortcode:t?.tokenShortcode}),await r.client.createPaymentUsd({usdAmount:o,tokenShortcode:t?.tokenShortcode,metadata:{...this.config?.metadata,icpay_network:"evm",icpay_ledger_id:t?.ledgerId}})}catch{}});return}let n=this.pendingAction;if(this.pendingAction=null,n==="pay")try{let a=(this.walletBalances||[]).find(c=>c?.tokenShortcode===e),r=this.getSdk(),o=Number(this.config?.amountUsd??0);$(this.config?.debug||!1,"IC selection made",{selectedSymbol:this.selectedSymbol,selPresent:!!a,selSnapshot:a?{ledgerId:a?.ledgerId,ledgerSymbol:a?.ledgerSymbol,ledgerName:a?.ledgerName,requiredAmount:a?.requiredAmount,hasSufficientBalance:a?.hasSufficientBalance,tokenShortcode:a?.tokenShortcode}:null}),await r.client.createPaymentUsd({usdAmount:o,tokenShortcode:a?.tokenShortcode,metadata:{...this.config?.metadata,icpay_network:"ic",icpay_ledger_id:a?.ledgerId}})}catch{}}}getSdk(){return this.sdk||(this.sdk=g(this.config)),this.sdk}connectedCallback(){if(super.connectedCallback(),!!qe){$(this.config?.debug||!1,"Pay button connected",{config:this.config});try{window.addEventListener("icpay-switch-account",this.onSwitchAccount)}catch{}try{window.addEventListener("icpay-sdk-transaction-created",(e=>{$(this.config?.debug||!1,"SDK transaction created",{detail:e?.detail}),this.showWalletModal=!1,this.showBalanceModal=!1,this.requestUpdate()}))}catch{}}}updated(e){if(e.has("config")&&this.pendingAction&&this.config?.actorProvider){let t=this.pendingAction;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-connected",{detail:{walletType:"external"}}))}catch{}this.walletModalStep!=="balances"&&!this.oisyReadyToPay&&(this.pendingAction=null,setTimeout(()=>{t==="pay"&&this.pay()},0))}e.has("config")&&(this.sdk=null)}selectSymbol(e){this.selectedSymbol=e}async ensureWallet(){if(this.config.useOwnWallet)return this.config.actorProvider?!0:(this.pendingAction="pay",this.dispatchEvent(new CustomEvent("icpay-connect-wallet",{bubbles:!0})),!1);if(this.walletConnected)return!0;try{ue||(ue=(await import("./wallet-select-NQ7XPOZX.js")).WalletSelect);let e=!!(this.config?.openOisyInNewTab||this.config?.plugNPlay?.openOisyInNewTab),t={...this.config?.plugNPlay||{}};this.config?.chainTypes&&(t.chainTypes=this.config.chainTypes);let n=e?U(t):t;try{if(typeof window<"u"){let{resolveDerivationOrigin:r}=await import("./origin-44ZEXSX4.js");n.derivationOrigin=this.config?.derivationOrigin||r()}}catch{}this.pnp=new ue(n);let a=this.pnp.getEnabledWallets();if($(this.config?.debug||!1,"Available wallets",a),!a?.length)throw new Error("No wallets available");return this.pendingAction="pay",this.showWalletModal=!0,!1}catch(e){return this.errorMessage=e instanceof Error?e.message:"Wallet connection failed",this.errorSeverity="error",!1}}getWalletId(e){return e&&(e.id||e.provider||e.key)||""}getWalletLabel(e){return e&&(e.label||e.name||e.title||e.id)||"Wallet"}getWalletIcon(e){return e&&(e.icon||e.logo||e.image)||null}connectWithWallet(e){if(this.pnp)try{if(!e)throw new Error("No wallet ID provided");this.lastWalletId=(e||"").toLowerCase(),this.lastWalletId==="oisy"&&(this.oisyConnectRetriedNewTab=!1),this.pnp.connect(e).then(n=>{if($(this.config?.debug||!1,"Wallet connect result",n),!!!(n&&(n.connected===!0||n.principal||n.owner||this.pnp?.account)))throw new Error("Wallet connection was rejected");this.walletConnected=!0;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-connected",{detail:{walletType:e}}))}catch{}let r=T(this.pnp,n),o=this.pnp?.getEvmProvider?.();this.config={...this.config,connectedWallet:r,actorProvider:(d,h)=>this.pnp.getActor({canisterId:d,idl:h,requiresSigning:!0,anon:!1}),...o?{evmProvider:o}:{}},this.sdk=null,(e||"").toLowerCase()==="oisy"?(this.oisyReadyToPay=!0,this.pendingAction=null):(this.walletModalStep="balances",this.fetchAndShowBalances("pay"))}).catch(n=>{$(this.config?.debug||!1,"Wallet connection error",n);let a=(e||"").toLowerCase()==="oisy",r=n&&(n.message||String(n))||"";if(a&&!this.oisyConnectRetriedNewTab&&(r.includes("Signer window could not be opened")||r.includes("Communication channel could not be established"))){this.oisyConnectRetriedNewTab=!0,(async()=>{try{ue||(ue=(await import("./wallet-select-NQ7XPOZX.js")).WalletSelect);let c={...this.config?.plugNPlay||{}};this.config?.chainTypes&&(c.chainTypes=this.config.chainTypes);let d=U(c);try{if(typeof window<"u"){let{resolveDerivationOrigin:p}=await import("./origin-44ZEXSX4.js");d.derivationOrigin=this.config?.derivationOrigin||p()}}catch{}this.pnp=new ue(d),this.pnp.connect("oisy").then(p=>{if(!!!(p&&(p.connected===!0||p.principal||p.owner||this.pnp?.account)))throw new Error("Wallet connection was rejected");this.walletConnected=!0;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-connected",{detail:{walletType:"oisy"}}))}catch{}let K=T(this.pnp,p),ee=this.pnp?.getEvmProvider?.();this.config={...this.config,connectedWallet:K,actorProvider:(C,B)=>this.pnp.getActor({canisterId:C,idl:B,requiresSigning:!0,anon:!1}),...ee?{evmProvider:ee}:{}},this.sdk=null,this.oisyReadyToPay=!0,this.pendingAction=null}).catch(p=>{$(this.config?.debug||!1,"Oisy retry connect (new tab) failed",p),this.errorMessage=p instanceof Error?p.message:"Wallet connection failed",this.errorSeverity="error",this.showWalletModal=!1;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-error",{detail:{message:this.errorMessage,code:"WALLET_CONNECT_ERROR"}}))}catch{}})}catch(c){$(this.config?.debug||!1,"Oisy new-tab init failed",c),this.errorMessage=c instanceof Error?c.message:"Wallet connection failed",this.errorSeverity="error",this.showWalletModal=!1;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-error",{detail:{message:this.errorMessage,code:"WALLET_CONNECT_ERROR"}}))}catch{}}})();return}this.errorMessage=n instanceof Error?n.message:"Wallet connection failed",this.errorSeverity="error",this.showWalletModal=!1;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-error",{detail:{message:this.errorMessage,code:"WALLET_CONNECT_ERROR"}}))}catch{}})}catch(t){$(this.config?.debug||!1,"Wallet connection error (sync)",t),this.errorMessage=t instanceof Error?t.message:"Wallet connection failed",this.errorSeverity="error",this.showWalletModal=!1;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-error",{detail:{message:this.errorMessage,code:"WALLET_CONNECT_ERROR"}}))}catch{}}}async fetchAndShowBalances(e){try{this.balancesLoading=!0,this.balancesError=null,this.walletModalStep="balances",this.showBalanceModal=!1;let t=this.getSdk(),{balances:n}=await Y({sdk:t,lastWalletId:this.lastWalletId,connectedWallet:this.config?.connectedWallet,amountUsd:Number(this.config?.amountUsd??0),chainShortcodes:this.config?.chainShortcodes,tokenShortcodes:this.config?.tokenShortcodes});this.walletBalances=n,this.pendingAction=e}catch(t){this.walletBalances=[],this.balancesError=t&&(t.message||String(t))||"Failed to load balances"}finally{this.balancesLoading=!1}}renderWalletModal(){if(!this.showWalletModal||!this.pnp)return null;let e=this.pnp.getEnabledWallets()||[],t=G(e),n=this.config?.onramp?.enabled!==!1&&this.config?.onrampDisabled!==!0,a=5,r=Number(this.config?.amountUsd??0),o=n&&r>0&&r<a,c=Math.max(0,a-r),d=o?`Note: Minimum card amount is $${a}. You will pay about $${c.toFixed(2)} more.`:null;return Q({visible:this.showWalletModal,wallets:t,isConnecting:!1,onSwitchAccount:()=>this.onSwitchAccount(null),step:this.walletModalStep,balances:this.walletModalStep==="balances"?this.walletBalances||[]:[],balancesLoading:this.walletModalStep==="balances"?this.balancesLoading:!1,balancesError:this.walletModalStep==="balances"?this.balancesError:null,onSelectBalance:h=>this.onSelectBalanceSymbol(h),onBack:()=>{this.walletModalStep="connect"},onSelect:h=>{this.connectWithWallet(h)},onClose:()=>{this.showWalletModal=!1,this.oisyReadyToPay=!1;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-cancelled",{detail:{reason:"user_cancelled"}}))}catch{}},onCreditCard:n?()=>this.startOnramp():void 0,creditCardLabel:this.config?.onramp?.creditCardLabel||"Pay with credit card",showCreditCard:n,creditCardTooltip:d,oisyReadyToPay:this.oisyReadyToPay,onOisyPay:()=>{this.showWalletModal=!1,this.skipDisconnectOnce=!0,this.oisyReadyToPay=!1,this.pay()}})}startOnramp(){try{window.dispatchEvent(new CustomEvent("icpay-sdk-method-start",{detail:{name:"createPaymentUsd",type:"onramp"}}))}catch{}this.showWalletModal=!1,setTimeout(()=>this.createOnrampIntent(),0)}async createOnrampIntent(){try{let e=Number(this.config?.amountUsd??0),t=this.getSdk();this.selectedSymbol||(this.selectedSymbol="ICP");let n=this.selectedSymbol||"ICP",a=await t.startOnrampUsd(e,n,{context:"pay-button:onramp"}),r=a?.metadata?.icpay_onramp?.sessionId||a?.metadata?.icpay_onramp?.session_id||a?.metadata?.onramp?.sessionId||a?.metadata?.onramp?.session_id||null,o=a?.metadata?.icpay_onramp?.errorMessage||a?.metadata?.onramp?.errorMessage||null;this.onrampErrorMessage=o||null;let c=a?.metadata?.icpay_payment_intent_id||a?.metadata?.paymentIntentId||a?.paymentIntentId||null;this.onrampPaymentIntentId=c,r?(this.onrampSessionId=r,this.showOnrampModal=!0,this.attachTransakMessageListener()):(this.onrampSessionId=null,this.showOnrampModal=!0)}catch(e){this.onrampSessionId=null,this.onrampErrorMessage=e?.message||null,this.showOnrampModal=!0,P(e,{onError:t=>{this.dispatchEvent(new CustomEvent("icpay-error",{detail:t,bubbles:!0})),D(t)&&(this.errorMessage=_(t),this.errorSeverity=F(t),this.errorAction=z(t))}})}}attachTransakMessageListener(){if(!this.transakMessageHandlerBound){this.transakMessageHandlerBound=e=>this.onTransakMessage(e);try{window.addEventListener("message",this.transakMessageHandlerBound)}catch{}}}detachTransakMessageListener(){if(this.transakMessageHandlerBound){try{window.removeEventListener("message",this.transakMessageHandlerBound)}catch{}this.transakMessageHandlerBound=null}}onTransakMessage(e){let t=e?.data,n=t?.event_id||t?.eventId||t?.id;if(!(!n||typeof n!="string")&&n==="TRANSAK_ORDER_SUCCESSFUL"){if(this.detachTransakMessageListener(),this.onrampPollingActive)return;let a=t?.data?.id||t?.id||t?.webhookData?.id||null;try{window.dispatchEvent(new CustomEvent("icpay-sdk-method-success",{detail:{name:"getLedgerBalance"}}))}catch{}try{window.dispatchEvent(new CustomEvent("icpay-sdk-method-success",{detail:{name:"sendFundsToLedger"}}))}catch{}try{window.dispatchEvent(new CustomEvent("icpay-sdk-method-success",{detail:{name:"notifyLedgerTransaction"}}))}catch{}this.showOnrampModal=!1,this.startOnrampPolling(a||void 0)}}startOnrampPolling(e){if(this.onrampPollTimer){try{clearInterval(this.onrampPollTimer)}catch{}this.onrampPollTimer=null}if(this.onrampNotifyController){try{this.onrampNotifyController.stop()}catch{}this.onrampNotifyController=null}let t=this.onrampPaymentIntentId;if(!t)return;let n=this.getSdk(),a=()=>{if(this.detachTransakMessageListener(),this.onrampNotifyController)try{this.onrampNotifyController.stop()}catch{}this.onrampNotifyController=null,this.onrampPollingActive=!1},r=o=>{a()};try{window.addEventListener("icpay-sdk-transaction-completed",r,{once:!0})}catch{}this.onrampPollingActive=!0,this.onrampNotifyController=n.notifyIntentUntilComplete(t,5e3,e),this.onrampPollTimer=1}async pay(){if(!(!qe||this.processing)){this.errorMessage=null,this.errorSeverity=null,this.errorAction=null;try{window.dispatchEvent(new CustomEvent("icpay-sdk-method-start",{detail:{name:"pay",type:"sendUsd",amount:this.config?.amountUsd,currency:this.selectedSymbol||"ICP"}}))}catch{}this.processing=!0;try{if(this.skipDisconnectOnce)this.skipDisconnectOnce=!1;else try{!this.config.useOwnWallet&&this.pnp&&(await this.pnp.disconnect?.(),this.walletConnected=!1,this.config={...this.config,actorProvider:void 0,connectedWallet:void 0})}catch{}if(!await this.ensureWallet())return;let t=this.getSdk();try{let n=this.config?.connectedWallet,a=this?.pnp?.account;$(this.config?.debug||!1,"Wallet state before payment",{connectedWallet:n,pnpAccount:a,principal:n?.owner||n?.principal||a?.owner||a?.principal||null})}catch{}this.walletModalStep="balances",this.showWalletModal=!0,await this.fetchAndShowBalances("pay");return}catch(e){$(this.config?.debug||!1,"Payment error",{message:e?.message,code:e?.code,details:e?.details,stack:e?.stack}),P(e,{onError:t=>{this.dispatchEvent(new CustomEvent("icpay-error",{detail:t,bubbles:!0})),D(t)&&(this.errorMessage=_(t),this.errorSeverity=F(t),this.errorAction=z(t))}});try{!this.config.useOwnWallet&&this.pnp&&(await this.pnp.disconnect?.(),this.walletConnected=!1,this.config={...this.config,actorProvider:void 0,connectedWallet:void 0})}catch{}}finally{this.processing=!1}}}render(){if(!this.config)return ke`<div class="icpay-card icpay-section">Loading...</div>`;let e=this.selectedSymbol||"ICP",t=typeof this.config?.amountUsd=="number"?`${Number(this.config.amountUsd).toFixed(2)}`:"",a=(this.config?.buttonLabel||(typeof this.config?.amountUsd=="number"?"Pay ${amount} with crypto":"Pay with {symbol}")).replace("{amount}",t||"$0.00").replace("{symbol}",e),o=this.config?.progressBar?.enabled!==!1,c=this.showWalletModal||this.showBalanceModal;return ke`
|
|
1413
|
+
`],s([Ct({type:Object})],b.prototype,"config",2),s([M()],b.prototype,"selectedAmount",2),s([M()],b.prototype,"selectedSymbol",2),s([M()],b.prototype,"raised",2),s([M()],b.prototype,"processing",2),s([M()],b.prototype,"succeeded",2),s([M()],b.prototype,"errorMessage",2),s([M()],b.prototype,"errorSeverity",2),s([M()],b.prototype,"errorAction",2),s([M()],b.prototype,"walletConnected",2),s([M()],b.prototype,"pendingAction",2),s([M()],b.prototype,"showWalletModal",2),s([M()],b.prototype,"oisyReadyToPay",2),s([M()],b.prototype,"lastWalletId",2),s([M()],b.prototype,"showOnrampModal",2),s([M()],b.prototype,"onrampSessionId",2),s([M()],b.prototype,"onrampPaymentIntentId",2),s([M()],b.prototype,"onrampErrorMessage",2),s([M()],b.prototype,"showBalanceModal",2),s([M()],b.prototype,"balancesLoading",2),s([M()],b.prototype,"balancesError",2),s([M()],b.prototype,"walletBalances",2),b=s([Wt("icpay-donation-thermometer")],b);import{LitElement as Tt,html as ke,css as At}from"lit";import{customElement as Ot,property as Lt,state as S}from"lit/decorators.js";var qe=typeof window<"u",ue=null;function $(i,l,e){i&&(e!==void 0?console.log(`[ICPay Widget] ${l}`,e):console.log(`[ICPay Widget] ${l}`))}var y=class extends Tt{constructor(){super(...arguments);this.selectedSymbol=null;this.processing=!1;this.succeeded=!1;this.errorMessage=null;this.errorSeverity=null;this.errorAction=null;this.walletConnected=!1;this.pendingAction=null;this.showWalletModal=!1;this.walletModalStep="connect";this.showOnrampModal=!1;this.onrampSessionId=null;this.onrampPaymentIntentId=null;this.onrampErrorMessage=null;this.oisyReadyToPay=!1;this.oisySignerPreopened=!1;this.skipDisconnectOnce=!1;this.lastWalletId=null;this.showBalanceModal=!1;this.balancesLoading=!1;this.balancesError=null;this.walletBalances=[];this.onrampPollTimer=null;this.transakMessageHandlerBound=null;this.pnp=null;this.oisyConnectRetriedNewTab=!1;this.sdk=null;this.onrampPollingActive=!1;this.onrampNotifyController=null;this.onSwitchAccount=async e=>{try{if(this.pnp)try{await this.pnp.disconnect()}catch{}this.walletConnected=!1,this.config={...this.config,actorProvider:void 0,connectedWallet:void 0},this.pendingAction="pay",this.walletModalStep="connect",this.showWalletModal=!0,this.oisyReadyToPay=!1,this.lastWalletId=null,this.requestUpdate();try{let t=Number(this.config?.amountUsd||0),n=this.selectedSymbol||"ICP";window.dispatchEvent(new CustomEvent("icpay-sdk-method-start",{detail:{name:"pay",type:"sendUsd",amount:t,currency:n}}))}catch{}}catch{}};this.onSelectBalanceSymbol=async e=>{let t=(this.walletBalances||[]).find(a=>a?.tokenShortcode===e);if(t?.ledgerSymbol&&(this.selectedSymbol=t.ledgerSymbol),this.showBalanceModal=!1,this.showWalletModal=!1,R(this.lastWalletId)){$(this.config?.debug||!1,"EVM selection made",{selectedSymbol:this.selectedSymbol,selPresent:!!t,selSnapshot:t?{ledgerId:t?.ledgerId,ledgerSymbol:t?.ledgerSymbol,ledgerName:t?.ledgerName,chainUuid:t?.chainUuid,chainId:t?.chainId,chainName:t?.chainName,x402Accepts:t?.x402Accepts,requiredAmount:t?.requiredAmount,hasSufficientBalance:t?.hasSufficientBalance}:null});let a=t?.chainId;V(a,{provider:this.pnp?.getEvmProvider?.()||this.config?.evmProvider,chainName:t?.chainName,rpcUrlPublic:t?.rpcUrlPublic,nativeSymbol:t?.ledgerSymbol,decimals:t?.decimals}).then(async()=>{try{let r=this.getSdk(),o=Number(this.config?.amountUsd??0),c=t?.ledgerSymbol,d=!!(t&&t.x402Accepts);if($(this.config?.debug||!1,"EVM post-ensure chain snapshot",{targetChain:a,amountUsd:o,symbolNow:c,tryX402:d,x402Accepts:t?.x402Accepts}),d)try{let h={...this.config?.metadata,icpay_network:"evm",icpay_ledger_id:t?.ledgerId,icpay_context:"pay-button:x402"};$(this.config?.debug||!1,"Attempting X402 flow (EVM selection)",{amountUsd:o,tokenShortcode:t?.tokenShortcode,x402Accepts:t?.x402Accepts}),await r.client.createPaymentX402Usd({usdAmount:o,tokenShortcode:t?.tokenShortcode,metadata:h});return}catch(h){$(this.config?.debug||!1,"X402 payment failed (EVM selection), falling back",{message:h?.message,code:h?.code,data:h?.details||h?.data})}else $(this.config?.debug||!1,"Skipping X402 path",{reason:t?t.x402Accepts?"unknown":"x402Accepts false":"no selection",selPresent:!!t,x402Accepts:t?.x402Accepts});$(this.config?.debug||!1,"Falling back to normal EVM wallet flow",{amountUsd:o,tokenShortcode:t?.tokenShortcode}),await r.client.createPaymentUsd({usdAmount:o,tokenShortcode:t?.tokenShortcode,metadata:{...this.config?.metadata,icpay_network:"evm",icpay_ledger_id:t?.ledgerId}})}catch{}});return}let n=this.pendingAction;if(this.pendingAction=null,n==="pay")try{let a=(this.walletBalances||[]).find(c=>c?.tokenShortcode===e),r=this.getSdk(),o=Number(this.config?.amountUsd??0);$(this.config?.debug||!1,"IC selection made",{selectedSymbol:this.selectedSymbol,selPresent:!!a,selSnapshot:a?{ledgerId:a?.ledgerId,ledgerSymbol:a?.ledgerSymbol,ledgerName:a?.ledgerName,requiredAmount:a?.requiredAmount,hasSufficientBalance:a?.hasSufficientBalance,tokenShortcode:a?.tokenShortcode}:null}),await r.client.createPaymentUsd({usdAmount:o,tokenShortcode:a?.tokenShortcode,metadata:{...this.config?.metadata,icpay_network:"ic",icpay_ledger_id:a?.ledgerId}})}catch{}}}getSdk(){return this.sdk||(this.sdk=g(this.config)),this.sdk}connectedCallback(){if(super.connectedCallback(),!!qe){$(this.config?.debug||!1,"Pay button connected",{config:this.config});try{window.addEventListener("icpay-switch-account",this.onSwitchAccount)}catch{}try{window.addEventListener("icpay-sdk-transaction-created",(e=>{$(this.config?.debug||!1,"SDK transaction created",{detail:e?.detail}),this.showWalletModal=!1,this.showBalanceModal=!1,this.requestUpdate()}))}catch{}}}updated(e){if(e.has("config")&&this.pendingAction&&this.config?.actorProvider){let t=this.pendingAction;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-connected",{detail:{walletType:"external"}}))}catch{}this.walletModalStep!=="balances"&&!this.oisyReadyToPay&&(this.pendingAction=null,setTimeout(()=>{t==="pay"&&this.pay()},0))}e.has("config")&&(this.sdk=null)}selectSymbol(e){this.selectedSymbol=e}async ensureWallet(){if(this.config.useOwnWallet)return this.config.actorProvider?!0:(this.pendingAction="pay",this.dispatchEvent(new CustomEvent("icpay-connect-wallet",{bubbles:!0})),!1);if(this.walletConnected)return!0;try{ue||(ue=(await import("./wallet-select-NQ7XPOZX.js")).WalletSelect);let e=!!(this.config?.openOisyInNewTab||this.config?.plugNPlay?.openOisyInNewTab),t={...this.config?.plugNPlay||{}};this.config?.chainTypes&&(t.chainTypes=this.config.chainTypes);let n=e?U(t):t;try{if(typeof window<"u"){let{resolveDerivationOrigin:r}=await import("./origin-44ZEXSX4.js");n.derivationOrigin=this.config?.derivationOrigin||r()}}catch{}this.pnp=new ue(n);let a=this.pnp.getEnabledWallets();if($(this.config?.debug||!1,"Available wallets",a),!a?.length)throw new Error("No wallets available");return this.pendingAction="pay",this.showWalletModal=!0,!1}catch(e){return this.errorMessage=e instanceof Error?e.message:"Wallet connection failed",this.errorSeverity="error",!1}}getWalletId(e){return e&&(e.id||e.provider||e.key)||""}getWalletLabel(e){return e&&(e.label||e.name||e.title||e.id)||"Wallet"}getWalletIcon(e){return e&&(e.icon||e.logo||e.image)||null}connectWithWallet(e){if(this.pnp)try{if(!e)throw new Error("No wallet ID provided");this.lastWalletId=(e||"").toLowerCase(),this.lastWalletId==="oisy"&&(this.oisyConnectRetriedNewTab=!1),this.pnp.connect(e).then(n=>{if($(this.config?.debug||!1,"Wallet connect result",n),!!!(n&&(n.connected===!0||n.principal||n.owner||this.pnp?.account)))throw new Error("Wallet connection was rejected");this.walletConnected=!0;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-connected",{detail:{walletType:e}}))}catch{}let r=T(this.pnp,n),o=this.pnp?.getEvmProvider?.();this.config={...this.config,connectedWallet:r,actorProvider:(c,d)=>this.pnp.getActor({canisterId:c,idl:d,requiresSigning:!0,anon:!1}),...o?{evmProvider:o}:{}},this.sdk=null,this.walletModalStep="balances",this.fetchAndShowBalances("pay")}).catch(n=>{$(this.config?.debug||!1,"Wallet connection error",n);let a=(e||"").toLowerCase()==="oisy",r=n&&(n.message||String(n))||"";if(a&&!this.oisyConnectRetriedNewTab&&(r.includes("Signer window could not be opened")||r.includes("Communication channel could not be established"))){this.oisyConnectRetriedNewTab=!0,(async()=>{try{ue||(ue=(await import("./wallet-select-NQ7XPOZX.js")).WalletSelect);let c={...this.config?.plugNPlay||{}};this.config?.chainTypes&&(c.chainTypes=this.config.chainTypes);let d=U(c);try{if(typeof window<"u"){let{resolveDerivationOrigin:p}=await import("./origin-44ZEXSX4.js");d.derivationOrigin=this.config?.derivationOrigin||p()}}catch{}this.pnp=new ue(d),this.pnp.connect("oisy").then(p=>{if(!!!(p&&(p.connected===!0||p.principal||p.owner||this.pnp?.account)))throw new Error("Wallet connection was rejected");this.walletConnected=!0;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-connected",{detail:{walletType:"oisy"}}))}catch{}let K=T(this.pnp,p),ee=this.pnp?.getEvmProvider?.();this.config={...this.config,connectedWallet:K,actorProvider:(C,B)=>this.pnp.getActor({canisterId:C,idl:B,requiresSigning:!0,anon:!1}),...ee?{evmProvider:ee}:{}},this.sdk=null,this.walletModalStep="balances",this.fetchAndShowBalances("pay")}).catch(p=>{$(this.config?.debug||!1,"Oisy retry connect (new tab) failed",p),this.errorMessage=p instanceof Error?p.message:"Wallet connection failed",this.errorSeverity="error",this.showWalletModal=!1;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-error",{detail:{message:this.errorMessage,code:"WALLET_CONNECT_ERROR"}}))}catch{}})}catch(c){$(this.config?.debug||!1,"Oisy new-tab init failed",c),this.errorMessage=c instanceof Error?c.message:"Wallet connection failed",this.errorSeverity="error",this.showWalletModal=!1;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-error",{detail:{message:this.errorMessage,code:"WALLET_CONNECT_ERROR"}}))}catch{}}})();return}this.errorMessage=n instanceof Error?n.message:"Wallet connection failed",this.errorSeverity="error",this.showWalletModal=!1;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-error",{detail:{message:this.errorMessage,code:"WALLET_CONNECT_ERROR"}}))}catch{}})}catch(t){$(this.config?.debug||!1,"Wallet connection error (sync)",t),this.errorMessage=t instanceof Error?t.message:"Wallet connection failed",this.errorSeverity="error",this.showWalletModal=!1;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-error",{detail:{message:this.errorMessage,code:"WALLET_CONNECT_ERROR"}}))}catch{}}}async fetchAndShowBalances(e){try{this.balancesLoading=!0,this.balancesError=null,this.walletModalStep="balances",this.showBalanceModal=!1;let t=this.getSdk(),{balances:n}=await Y({sdk:t,lastWalletId:this.lastWalletId,connectedWallet:this.config?.connectedWallet,amountUsd:Number(this.config?.amountUsd??0),chainShortcodes:this.config?.chainShortcodes,tokenShortcodes:this.config?.tokenShortcodes});this.walletBalances=n,this.pendingAction=e}catch(t){this.walletBalances=[],this.balancesError=t&&(t.message||String(t))||"Failed to load balances"}finally{this.balancesLoading=!1}}renderWalletModal(){if(!this.showWalletModal||!this.pnp)return null;let e=this.pnp.getEnabledWallets()||[],t=G(e),n=this.config?.onramp?.enabled!==!1&&this.config?.onrampDisabled!==!0,a=5,r=Number(this.config?.amountUsd??0),o=n&&r>0&&r<a,c=Math.max(0,a-r),d=o?`Note: Minimum card amount is $${a}. You will pay about $${c.toFixed(2)} more.`:null;return Q({visible:this.showWalletModal,wallets:t,isConnecting:!1,onSwitchAccount:()=>this.onSwitchAccount(null),step:this.walletModalStep,balances:this.walletModalStep==="balances"?this.walletBalances||[]:[],balancesLoading:this.walletModalStep==="balances"?this.balancesLoading:!1,balancesError:this.walletModalStep==="balances"?this.balancesError:null,onSelectBalance:h=>this.onSelectBalanceSymbol(h),onBack:()=>{this.walletModalStep="connect"},onSelect:h=>{this.connectWithWallet(h)},onClose:()=>{this.showWalletModal=!1,this.oisyReadyToPay=!1;try{window.dispatchEvent(new CustomEvent("icpay-sdk-wallet-cancelled",{detail:{reason:"user_cancelled"}}))}catch{}},onCreditCard:n?()=>this.startOnramp():void 0,creditCardLabel:this.config?.onramp?.creditCardLabel||"Pay with credit card",showCreditCard:n,creditCardTooltip:d,oisyReadyToPay:this.oisyReadyToPay,onOisyPay:()=>{this.showWalletModal=!1,this.skipDisconnectOnce=!0,this.oisyReadyToPay=!1,this.pay()}})}startOnramp(){try{window.dispatchEvent(new CustomEvent("icpay-sdk-method-start",{detail:{name:"createPaymentUsd",type:"onramp"}}))}catch{}this.showWalletModal=!1,setTimeout(()=>this.createOnrampIntent(),0)}async createOnrampIntent(){try{let e=Number(this.config?.amountUsd??0),t=this.getSdk();this.selectedSymbol||(this.selectedSymbol="ICP");let n=this.selectedSymbol||"ICP",a=await t.startOnrampUsd(e,n,{context:"pay-button:onramp"}),r=a?.metadata?.icpay_onramp?.sessionId||a?.metadata?.icpay_onramp?.session_id||a?.metadata?.onramp?.sessionId||a?.metadata?.onramp?.session_id||null,o=a?.metadata?.icpay_onramp?.errorMessage||a?.metadata?.onramp?.errorMessage||null;this.onrampErrorMessage=o||null;let c=a?.metadata?.icpay_payment_intent_id||a?.metadata?.paymentIntentId||a?.paymentIntentId||null;this.onrampPaymentIntentId=c,r?(this.onrampSessionId=r,this.showOnrampModal=!0,this.attachTransakMessageListener()):(this.onrampSessionId=null,this.showOnrampModal=!0)}catch(e){this.onrampSessionId=null,this.onrampErrorMessage=e?.message||null,this.showOnrampModal=!0,P(e,{onError:t=>{this.dispatchEvent(new CustomEvent("icpay-error",{detail:t,bubbles:!0})),D(t)&&(this.errorMessage=_(t),this.errorSeverity=F(t),this.errorAction=z(t))}})}}attachTransakMessageListener(){if(!this.transakMessageHandlerBound){this.transakMessageHandlerBound=e=>this.onTransakMessage(e);try{window.addEventListener("message",this.transakMessageHandlerBound)}catch{}}}detachTransakMessageListener(){if(this.transakMessageHandlerBound){try{window.removeEventListener("message",this.transakMessageHandlerBound)}catch{}this.transakMessageHandlerBound=null}}onTransakMessage(e){let t=e?.data,n=t?.event_id||t?.eventId||t?.id;if(!(!n||typeof n!="string")&&n==="TRANSAK_ORDER_SUCCESSFUL"){if(this.detachTransakMessageListener(),this.onrampPollingActive)return;let a=t?.data?.id||t?.id||t?.webhookData?.id||null;try{window.dispatchEvent(new CustomEvent("icpay-sdk-method-success",{detail:{name:"getLedgerBalance"}}))}catch{}try{window.dispatchEvent(new CustomEvent("icpay-sdk-method-success",{detail:{name:"sendFundsToLedger"}}))}catch{}try{window.dispatchEvent(new CustomEvent("icpay-sdk-method-success",{detail:{name:"notifyLedgerTransaction"}}))}catch{}this.showOnrampModal=!1,this.startOnrampPolling(a||void 0)}}startOnrampPolling(e){if(this.onrampPollTimer){try{clearInterval(this.onrampPollTimer)}catch{}this.onrampPollTimer=null}if(this.onrampNotifyController){try{this.onrampNotifyController.stop()}catch{}this.onrampNotifyController=null}let t=this.onrampPaymentIntentId;if(!t)return;let n=this.getSdk(),a=()=>{if(this.detachTransakMessageListener(),this.onrampNotifyController)try{this.onrampNotifyController.stop()}catch{}this.onrampNotifyController=null,this.onrampPollingActive=!1},r=o=>{a()};try{window.addEventListener("icpay-sdk-transaction-completed",r,{once:!0})}catch{}this.onrampPollingActive=!0,this.onrampNotifyController=n.notifyIntentUntilComplete(t,5e3,e),this.onrampPollTimer=1}async pay(){if(!(!qe||this.processing)){this.errorMessage=null,this.errorSeverity=null,this.errorAction=null;try{window.dispatchEvent(new CustomEvent("icpay-sdk-method-start",{detail:{name:"pay",type:"sendUsd",amount:this.config?.amountUsd,currency:this.selectedSymbol||"ICP"}}))}catch{}this.processing=!0;try{if(this.skipDisconnectOnce)this.skipDisconnectOnce=!1;else try{!this.config.useOwnWallet&&this.pnp&&(await this.pnp.disconnect?.(),this.walletConnected=!1,this.config={...this.config,actorProvider:void 0,connectedWallet:void 0})}catch{}if(!await this.ensureWallet())return;let t=this.getSdk();try{let n=this.config?.connectedWallet,a=this?.pnp?.account;$(this.config?.debug||!1,"Wallet state before payment",{connectedWallet:n,pnpAccount:a,principal:n?.owner||n?.principal||a?.owner||a?.principal||null})}catch{}this.walletModalStep="balances",this.showWalletModal=!0,await this.fetchAndShowBalances("pay");return}catch(e){$(this.config?.debug||!1,"Payment error",{message:e?.message,code:e?.code,details:e?.details,stack:e?.stack}),P(e,{onError:t=>{this.dispatchEvent(new CustomEvent("icpay-error",{detail:t,bubbles:!0})),D(t)&&(this.errorMessage=_(t),this.errorSeverity=F(t),this.errorAction=z(t))}});try{!this.config.useOwnWallet&&this.pnp&&(await this.pnp.disconnect?.(),this.walletConnected=!1,this.config={...this.config,actorProvider:void 0,connectedWallet:void 0})}catch{}}finally{this.processing=!1}}}render(){if(!this.config)return ke`<div class="icpay-card icpay-section">Loading...</div>`;let e=this.selectedSymbol||"ICP",t=typeof this.config?.amountUsd=="number"?`${Number(this.config.amountUsd).toFixed(2)}`:"",a=(this.config?.buttonLabel||(typeof this.config?.amountUsd=="number"?"Pay ${amount} with crypto":"Pay with {symbol}")).replace("{amount}",t||"$0.00").replace("{symbol}",e),o=this.config?.progressBar?.enabled!==!1,c=this.showWalletModal||this.showBalanceModal;return ke`
|
|
1414
1414
|
<div class="icpay-card icpay-section icpay-widget-base">
|
|
1415
1415
|
${o?ke`
|
|
1416
1416
|
<icpay-progress-bar
|
|
@@ -1508,4 +1508,4 @@ import{a as s}from"./chunk-LTEQ7OUJ.js";import{css as Xe}from"lit";var H=Xe`
|
|
|
1508
1508
|
.top-row { grid-template-columns: 1fr; }
|
|
1509
1509
|
}
|
|
1510
1510
|
`],s([$t({type:Object})],w.prototype,"config",2),s([W()],w.prototype,"amountUsd",2),s([W()],w.prototype,"hasUserAmount",2),s([W()],w.prototype,"selectedSymbol",2),s([W()],w.prototype,"processing",2),s([W()],w.prototype,"succeeded",2),s([W()],w.prototype,"errorMessage",2),s([W()],w.prototype,"errorSeverity",2),s([W()],w.prototype,"errorAction",2),s([W()],w.prototype,"walletConnected",2),s([W()],w.prototype,"pendingAction",2),s([W()],w.prototype,"showWalletModal",2),s([W()],w.prototype,"showOnrampModal",2),s([W()],w.prototype,"onrampSessionId",2),s([W()],w.prototype,"onrampPaymentIntentId",2),s([W()],w.prototype,"onrampErrorMessage",2),s([W()],w.prototype,"oisyReadyToPay",2),s([W()],w.prototype,"lastWalletId",2),s([W()],w.prototype,"showBalanceModal",2),s([W()],w.prototype,"balancesLoading",2),s([W()],w.prototype,"balancesError",2),s([W()],w.prototype,"walletBalances",2),w=s([Ut("icpay-amount-input")],w);export{H as a,Oe as b,g as c,q as d,E as e,v as f,u as g,x as h,b as i,y as j,w as k};
|
|
1511
|
-
//# sourceMappingURL=chunk-
|
|
1511
|
+
//# sourceMappingURL=chunk-GILUWNAP.js.map
|