cppay-sdk 0.0.2-beta.41 → 0.0.2-beta.42

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.
package/dist/react.cjs CHANGED
@@ -1 +1 @@
1
- const e=require(`./locales-Cm9yIiVb.cjs`),t=require(`./cppay-DuOJqlpA.cjs`);let n=require(`qrcode`);n=e.i(n);let r=require(`rxjs`),i=require(`rxjs/operators`),a=require(`viem`),o=require(`viem/chains`),ee=require(`@reown/appkit`),s=require(`@reown/appkit-adapter-ethers`),c=require(`react`);c=e.i(c);let l=require(`react/jsx-runtime`);var u=({apikey:u,ott:d,plain:f,orderId:p,amount:m,intervalDays:h,remark:g,locale:_,onSuccess:v,onExpired:y,onFailed:b,onError:x,Slots:S,onPaymentStepChange:te})=>{let C=(0,c.useMemo)(()=>new t.t(u,d),[u,d]),w=(0,c.useMemo)(()=>e.t(_),[_]),[T,E]=(0,c.useState)(`select`),D=(0,c.useMemo)(()=>T===`checking`,[T]),[O,k]=(0,c.useState)(!1),[A,j]=(0,c.useState)(),[M,ne]=(0,c.useState)([]),[N,re]=(0,c.useState)(),[P,ie]=(0,c.useState)(),[ae,oe]=(0,c.useState)(),[F,se]=(0,c.useState)(),[ce,le]=(0,c.useState)(!1),[I,L]=(0,c.useState)(),[ue,de]=(0,c.useState)(``),[R,z]=(0,c.useState)(!1),[fe,B]=(0,c.useState)(!1),[V,H]=(0,c.useState)(``),U=(0,c.useRef)(null),W=(0,c.useRef)(null),G=(0,c.useRef)(null),K=(0,c.useRef)(null),q=(0,c.useRef)(``);(0,c.useEffect)(()=>{typeof window<`u`&&!U.current&&(U.current=(0,ee.createAppKit)({debug:!1,enableNetworkSwitch:!1,adapters:[new s.EthersAdapter],projectId:`8d2e1854d3f1782e45aa15fbd8938894`,allowUnsupportedChain:!0,networks:[o.mainnet,o.bsc,o.polygon,o.arbitrum,o.optimism,o.base],metadata:{name:`Cppay`,description:`Cppay Payment Gateway`,url:window.location.origin,icons:[`https://cppay.com/icon.png`]},features:{analytics:!1}}),se(U.current.getAddress()),L(U.current.getProvider(`eip155`)),U.current.subscribeAccount(e=>{e.isConnected?se(e.address):(se(void 0),L(void 0))}),U.current.subscribeProviders(e=>{let t=e?.eip155;L(t)}))},[]),(0,c.useEffect)(()=>{te?.(T)},[T]);let pe=async()=>{try{le(!0),j(void 0),U.current&&await U.current.open()}catch(e){console.error(`钱包连接失败:`,e),j(e instanceof Error?e.message:w.walletConnectionFailed),x?.(e)}finally{le(!1)}},J=(0,c.useMemo)(()=>M.find(e=>e.chain===N),[M,N]),Y=(0,c.useMemo)(()=>J?.tokens||[],[J]),X=(0,c.useMemo)(()=>Y.find(e=>e.symbol===P),[Y,P]),Z=e=>[`USDT`,`USDC`,`BUSD`,`DAI`,`TUSD`,`USDD`,`FDUSD`].includes(e.toUpperCase())?2:6,Q=(e,t)=>{let n=parseFloat(e);return isNaN(n)?`0`:n.toFixed(t).replace(/\.?0+$/,``)},me=e=>{if(e<=0)return w.expired;let t=Math.floor(e/3600),n=Math.floor(e%3600/60),r=e%60,i=e=>e.toString().padStart(2,`0`);return t>0?`${t}:${i(n)}:${i(r)}`:`${n}:${i(r)}`},he=e=>{G.current&&clearInterval(G.current);let t=()=>{if(T===`checking`){ge();return}let t=e-Math.floor(Date.now()/1e3);de(me(t)),t<=0&&(E(`expired`),ge())};t(),G.current=setInterval(t,1e3)},ge=()=>{G.current&&=(clearInterval(G.current),null)},$=(0,c.useMemo)(()=>{if(!P||!X)return`0`;let e=parseFloat(X.price);if(isNaN(e)||e===0)return`0`;let t=Z(P);return Q((parseFloat(m)/e).toFixed(t),t)},[m,P,X]),_e=async()=>{try{k(!0),j(void 0);let e=await C.getSupportedChains();ne(e),e.length>0&&re(e[0].chain)}catch(e){j(e instanceof Error?e.message:w.loadPaymentNetworkFailed),x?.(e)}finally{k(!1)}},ve=async e=>{if(I)try{await I.request({method:`wallet_switchEthereumChain`,params:[{chainId:`0x${e.toString(16)}`}]})}catch(e){throw e.code===4902?Error(w.pleaseAddNetwork):e}},ye=async()=>{if(!(!F||!I||!K.current||!X||!J))try{k(!0);let e=K.current;await ve(J.chainId);let t=(0,a.createWalletClient)({account:F,transport:(0,a.custom)(I)});if(X.address){let n=await t.writeContract({address:X.address,abi:[{name:`transfer`,type:`function`,stateMutability:`nonpayable`,inputs:[{name:`to`,type:`address`},{name:`amount`,type:`uint256`}],outputs:[{type:`bool`}]}],functionName:`transfer`,args:[e.receiveAddress,(0,a.parseUnits)(e.paymentAmount,X.decimals)],chain:null});console.log(`转账交易哈希:`,n),H(n),q.current=n}else{let n=await t.sendTransaction({to:e.receiveAddress,value:(0,a.parseUnits)(e.paymentAmount,X.decimals),chain:null});console.log(`转账交易哈希:`,n),H(n),q.current=n}k(!1),E(`checking`)}catch(e){console.error(`钱包支付失败:`,e),j(e instanceof Error?e.message:w.walletPaymentFailed),x?.(e),k(!1)}},be=async()=>{if(!(!F||!I||!K.current||!X||!J))try{k(!0);let e=K.current;await ve(J.chainId);let t=(0,a.createWalletClient)({account:F,transport:(0,a.custom)(I)});if(!X.address)throw Error(w.subscriptionDoesNotSupportNative);let n=await t.writeContract({address:X.address,abi:[{name:`approve`,type:`function`,stateMutability:`nonpayable`,inputs:[{name:`spender`,type:`address`},{name:`amount`,type:`uint256`}],outputs:[{type:`bool`}]}],functionName:`approve`,args:[e.spenderAddress,(0,a.parseUnits)(e.approveAmount,X.decimals)],chain:null});console.log(`授权交易哈希:`,n),H(n),q.current=n,k(!1),E(`checking`)}catch(e){console.error(`钱包授权失败:`,e),j(e instanceof Error?e.message:w.walletPaymentFailed),x?.(e),k(!1)}},xe=async()=>{if(!F){let e=w.pleaseConnectWallet;j(e),x?.(Error(e));return}f===`instant`?await ye():f===`subscription`&&await be()},Se=async()=>{if(!(!N||!P))try{k(!0),j(void 0);let e=``;if(f===`instant`)K.current=await C.createOnetimePayment({paymentChain:N,paymentToken:P,orderId:p,amount:$,remark:g}),e=`${N.toLowerCase()}:${K.current.receiveAddress}?amount=${K.current.paymentAmount}`,he(K.current.expireAt),we({paymentId:K.current.paymentId});else if(f===`subscription`){if(!h)throw Error(`Invalid Renewal Days: ${h??``}`);K.current=await C.createSubscriptionPayment({paymentChain:N,paymentToken:P,orderId:p,amountOfUsd:$,intervalDays:h}),e=`${N.toLowerCase()}:${K.current.spenderAddress}?amount=${K.current.approveAmount}`,he(K.current.expireAt),we({subscriptionId:K.current.subscriptionId})}E(`payment`),oe(await n.toDataURL(e,{width:200,margin:2,errorCorrectionLevel:`H`}))}catch(e){j(e instanceof Error?e.message:w.createPaymentFailed),x?.(e)}finally{k(!1)}},Ce=()=>{K.current&&(E(`checking`),j(void 0))},we=e=>{W.current?.unsubscribe();let t=()=>(0,r.defer)(()=>f===`subscription`?C.checkSubscriptionPaymentStatus(e):C.checkOnetimePaymentStatus(e)).pipe((0,i.timeout)(15e3),(0,i.retry)({delay:2e3}));W.current=t().pipe((0,i.expand)(e=>e.status===`pending`?(0,r.timer)(2e3).pipe((0,i.switchMap)(()=>t())):r.EMPTY),(0,i.tap)(e=>{(e.status===`paid`||e.status===`approved`)&&(E(`success`),v?.(e)),e.status===`expired`&&(E(`expired`),y?.(e)),e.status===`failed`&&(E(`failed`),b?.(e))})).subscribe({error:e=>{if(K.current){let t=e instanceof Error?e.message:w.checkPaymentStatusFailed;E(`error`),j(t),x?.(e)}},complete:()=>{W.current?.unsubscribe()}})};return(0,c.useEffect)(()=>{M.length===0&&_e()},[]),(0,c.useEffect)(()=>{Y.length>0&&ie(Y[0].symbol)},[Y]),(0,c.useEffect)(()=>(p&&we({orderId:p}),()=>{W.current?.unsubscribe(),ge()}),[p]),(0,l.jsx)(`div`,{className:`_cppay-content`,children:T===`success`?S?.Success?(0,l.jsx)(S.Success,{paymentInfo:K.current}):(0,l.jsxs)(`div`,{className:`_cppay-state-container`,children:[(0,l.jsx)(`div`,{className:`_cppay-state-success-bg`,children:(0,l.jsx)(`div`,{className:`_cppay-state-success-icon`,children:`✓`})}),(0,l.jsx)(`h3`,{className:`_cppay-state-title _cppay-state-title-success`,children:f===`subscription`?w.authorizationSuccess:w.paymentSuccess}),V&&(0,l.jsxs)(`div`,{className:`_cppay-state-hash-container`,children:[(0,l.jsx)(`div`,{className:`_cppay-state-label`,children:w.transactionHash}),(0,l.jsxs)(`div`,{className:`_cppay-hash-row`,children:[(0,l.jsxs)(`code`,{className:`_cppay-state-hash`,children:[V.slice(0,18),`...`,V.slice(-18)]}),(0,l.jsx)(`button`,{onClick:async()=>{if(V)try{await navigator.clipboard.writeText(V),z(!0),setTimeout(()=>{z(!1)},2e3)}catch(e){console.error(`复制失败:`,e)}},className:`_cppay-state-copy-btn`,title:w.copyAddress,children:(0,l.jsxs)(`svg`,{viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,children:[(0,l.jsx)(`path`,{d:`M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2`}),(0,l.jsx)(`rect`,{x:`8`,y:`2`,width:`8`,height:`4`,rx:`1`,ry:`1`})]})})]})]}),K.current&&(0,l.jsxs)(`div`,{className:`_cppay-success-details`,children:[(0,l.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,l.jsx)(`span`,{className:`_cppay-detail-label`,children:w.orderId}),(0,l.jsx)(`span`,{className:`_cppay-detail-value`,children:K.current.orderId})]}),(0,l.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,l.jsx)(`span`,{className:`_cppay-detail-label`,children:f===`subscription`?w.authorizationAmount:w.paymentAmount}),(0,l.jsx)(`span`,{className:`_cppay-detail-value`,children:f===`subscription`?`${Q(K.current.approveAmount,Z(P))} ${P}`:`${Q(K.current.paymentAmount,Z(P))} ${P}`})]}),(0,l.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,l.jsx)(`span`,{className:`_cppay-detail-label`,children:w.paymentNetwork}),(0,l.jsx)(`span`,{className:`_cppay-detail-value`,children:K.current.paymentChain})]}),(0,l.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,l.jsx)(`span`,{className:`_cppay-detail-label`,children:w.paymentToken}),(0,l.jsx)(`span`,{className:`_cppay-detail-value`,children:K.current.paymentToken})]}),f===`subscription`&&(0,l.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,l.jsx)(`span`,{className:`_cppay-detail-label`,children:w.renewalInterval}),(0,l.jsxs)(`span`,{className:`_cppay-detail-value`,children:[K.current.intervalDays,` `,w.days]})]}),(0,l.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,l.jsx)(`span`,{className:`_cppay-detail-label`,children:f===`subscription`?w.subscriptionId:w.paymentId}),(0,l.jsx)(`span`,{className:`_cppay-detail-value _cppay-detail-id`,children:f===`subscription`?K.current.subscriptionId:K.current.paymentId})]})]})]}):T===`expired`?S?.Expired?(0,l.jsx)(S.Expired,{paymentInfo:K.current}):(0,l.jsxs)(`div`,{className:`_cppay-state-container`,children:[(0,l.jsx)(`div`,{className:`_cppay-state-expired-bg`,children:(0,l.jsx)(`div`,{className:`_cppay-state-expired-icon`,children:`⏰`})}),(0,l.jsx)(`h3`,{className:`_cppay-state-title _cppay-state-title-expired`,children:w.paymentExpired}),(0,l.jsx)(`p`,{className:`_cppay-state-message`,children:w.pleaseInitiatePaymentAgain}),(0,l.jsx)(`button`,{onClick:()=>E(`select`),disabled:O||D,className:`_cppay-btn _cppay-btn-primary _cppay-state-btn`,children:w.returnButton})]}):T===`error`?S?.Error?(0,l.jsx)(S.Error,{paymentInfo:K.current}):(0,l.jsxs)(`div`,{className:`_cppay-state-container`,children:[(0,l.jsx)(`div`,{className:`_cppay-state-error-bg`,children:(0,l.jsx)(`div`,{className:`_cppay-state-error-icon`,children:`!`})}),(0,l.jsx)(`h3`,{className:`_cppay-state-title _cppay-state-title-error`,children:A||w.error}),(0,l.jsx)(`p`,{className:`_cppay-state-message`,children:w.checkStatusFailed}),(0,l.jsx)(`button`,{onClick:()=>E(`select`),disabled:O||D,className:`_cppay-btn _cppay-btn-primary _cppay-state-btn`,children:w.returnButton})]}):T===`failed`?S?.Failed?(0,l.jsx)(S.Failed,{paymentInfo:K.current}):(0,l.jsxs)(`div`,{className:`_cppay-state-container`,children:[(0,l.jsx)(`div`,{className:`_cppay-state-failed-bg`,children:(0,l.jsx)(`div`,{className:`_cppay-state-failed-icon`,children:`✕`})}),(0,l.jsx)(`h3`,{className:`_cppay-state-title _cppay-state-title-failed`,children:w.paymentFailed}),(0,l.jsx)(`p`,{className:`_cppay-state-message`,children:w.pleaseInitiatePaymentAgain}),(0,l.jsx)(`button`,{onClick:()=>E(`select`),disabled:O||D,className:`_cppay-btn _cppay-btn-primary _cppay-state-btn`,children:w.returnButton})]}):T===`select`?(0,l.jsxs)(`div`,{children:[S?.ChooseTop,(0,l.jsxs)(`div`,{className:`_cppay-section`,children:[(0,l.jsx)(`label`,{className:`_cppay-label`,children:w.paymentNetwork}),(0,l.jsx)(`div`,{className:`_cppay-grid`,children:M.map(e=>(0,l.jsxs)(`button`,{onClick:()=>re(e.chain),className:`_cppay-select-btn ${N===e.chain?`_cppay-selected`:``}`,children:[e.icon&&(0,l.jsx)(`img`,{src:e.icon,alt:e.chain}),(0,l.jsx)(`span`,{children:e.chain})]},e.chain))})]}),(0,l.jsxs)(`div`,{className:`_cppay-section`,children:[(0,l.jsx)(`label`,{className:`_cppay-label`,children:w.paymentToken}),(0,l.jsx)(`div`,{className:`_cppay-grid`,children:Y.map(e=>(0,l.jsxs)(`button`,{onClick:()=>ie(e.symbol),className:`_cppay-select-btn ${P===e.symbol?`_cppay-selected`:``}`,children:[e.icon&&(0,l.jsx)(`img`,{src:e.icon,alt:e.symbol}),(0,l.jsx)(`span`,{children:e.symbol})]},e.symbol))})]}),S?.ChooseBottom,(0,l.jsx)(`div`,{className:`_cppay-section`,children:(0,l.jsx)(`div`,{className:`_cppay-price-box`,children:(0,l.jsxs)(`div`,{className:`_cppay-price-row`,children:[(0,l.jsx)(`span`,{className:`_cppay-price-label`,children:w.paymentAmount}),(0,l.jsxs)(`div`,{className:`_cppay-price-amount`,children:[(0,l.jsxs)(`div`,{className:`_cppay-price-main`,children:[$??`-`,` `,P??``]}),(0,l.jsxs)(`div`,{className:`_cppay-price-sub`,children:[`≈ $`,m]})]})]})})}),(0,l.jsxs)(`div`,{className:`_cppay-section`,children:[A&&(0,l.jsxs)(`div`,{className:`_cppay-error-tooltip-wrapper`,onMouseEnter:()=>B(!0),onMouseLeave:()=>B(!1),children:[(0,l.jsxs)(`div`,{className:`_cppay-error-tooltip`,children:[(0,l.jsx)(`svg`,{viewBox:`0 0 20 20`,fill:`currentColor`,children:(0,l.jsx)(`path`,{fillRule:`evenodd`,d:`M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z`,clipRule:`evenodd`})}),(0,l.jsx)(`span`,{children:A})]}),fe&&(0,l.jsx)(`div`,{className:`_cppay-error-tooltip-full`,children:A})]}),(0,l.jsx)(`button`,{onClick:Se,disabled:!N||!P||O,className:`_cppay-btn _cppay-btn-primary`,children:O?w.processing:w.continuePayment})]})]}):(0,l.jsxs)(`div`,{children:[(0,l.jsx)(`div`,{className:`_cppay-qr-container`,children:(0,l.jsx)(`div`,{className:`_cppay-qr-code`,children:ae&&(0,l.jsx)(`img`,{src:ae,alt:`Payment QR Code`,className:`_cppay-qr-image`})})}),(0,l.jsxs)(`div`,{className:`_cppay-section`,children:[X&&(0,l.jsx)(`div`,{className:`_cppay-info-box`,children:(0,l.jsx)(`div`,{className:`_cppay-info-flex-container`,children:(0,l.jsxs)(`div`,{className:`_cppay-info-flex-child`,children:[(0,l.jsx)(`div`,{className:`_cppay-info-label`,children:f===`subscription`?w.authorizationAmount:w.paymentAmount}),(0,l.jsxs)(`div`,{className:`_cppay-info-value _cppay-info-value-flex`,children:[(0,l.jsx)(`span`,{children:f===`subscription`?`${Q(K.current.approveAmount,Z(X.symbol))} ${X.symbol}`:`${Q(K.current.paymentAmount,Z(X.symbol))} ${X.symbol}`}),(0,l.jsxs)(`span`,{children:[`≈ $`,m]})]})]})})}),(0,l.jsx)(`div`,{className:`_cppay-info-box`,children:(0,l.jsxs)(`div`,{className:`_cppay-info-flex-child`,children:[(0,l.jsxs)(`div`,{className:`_cppay-info-label _cppay-info-label-flex`,children:[(0,l.jsx)(`span`,{children:f===`subscription`?w.authorizationContractAddress:w.paymentAddress}),ue&&(0,l.jsxs)(`span`,{className:`_cppay-countdown`,children:[`⏰ `,ue]})]}),(0,l.jsxs)(`div`,{className:`_cppay-address-row`,children:[(0,l.jsx)(`code`,{children:f===`subscription`?K.current.spenderAddress:K.current.receiveAddress}),(0,l.jsx)(`button`,{onClick:async()=>{if(!K.current)return;let e=f===`subscription`?K.current.spenderAddress:K.current.receiveAddress;if(e)try{await navigator.clipboard.writeText(e),z(!0),setTimeout(()=>{z(!1)},2e3)}catch(e){console.error(`复制失败:`,e)}},className:`_cppay-copy-btn ${R?`_cppay-copy-success`:``}`,title:R?w.copied:w.copyAddress,children:R?(0,l.jsx)(`svg`,{className:`_cppay-copy-icon`,viewBox:`0 0 24 24`,fill:`currentColor`,children:(0,l.jsx)(`path`,{d:`M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z`})}):(0,l.jsxs)(`svg`,{className:`_cppay-copy-icon`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,children:[(0,l.jsx)(`path`,{d:`M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2`}),(0,l.jsx)(`rect`,{x:`8`,y:`2`,width:`8`,height:`4`,rx:`1`,ry:`1`})]})})]})]})}),f===`subscription`&&(0,l.jsxs)(`div`,{className:`_cppay-info-box _cppay-subscription-box`,children:[(0,l.jsx)(`div`,{className:`_cppay-info-label _cppay-subscription-label`,children:w.subscriptionNotice}),(0,l.jsx)(`div`,{className:`_cppay-info-value _cppay-subscription-message`,children:w.subscriptionNoticeMessage})]})]}),A&&(0,l.jsxs)(`div`,{className:`_cppay-error-tooltip-wrapper`,onMouseEnter:()=>B(!0),onMouseLeave:()=>B(!1),children:[(0,l.jsxs)(`div`,{className:`_cppay-error-tooltip`,children:[(0,l.jsx)(`svg`,{viewBox:`0 0 20 20`,fill:`currentColor`,children:(0,l.jsx)(`path`,{fillRule:`evenodd`,d:`M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z`,clipRule:`evenodd`})}),(0,l.jsx)(`span`,{children:A})]}),fe&&(0,l.jsx)(`div`,{className:`_cppay-error-tooltip-full`,children:A})]}),!D&&(0,l.jsx)(`div`,{className:`_cppay-section`,children:F?(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(`button`,{onClick:xe,disabled:O,className:`_cppay-btn _cppay-btn-primary`,children:O?f===`subscription`?w.authorizing:w.processing:f===`subscription`?`💳 ${w.connectWallet}`:`💳 ${w.walletPay}`}),(0,l.jsxs)(`button`,{onClick:pe,disabled:O,className:`_cppay-btn _cppay-wallet-address-btn`,title:w.clickToModifyWallet,children:[(0,l.jsxs)(`span`,{className:`_cppay-wallet-address-text`,children:[F.slice(0,10),`...`,F.slice(-4)]}),(0,l.jsx)(`svg`,{className:`_cppay-wallet-arrow`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,children:(0,l.jsx)(`polyline`,{points:`6 9 12 15 18 9`})})]})]}):(0,l.jsx)(`button`,{onClick:pe,disabled:ce,className:`_cppay-btn _cppay-btn-secondary _cppay-connect-wallet-btn`,children:(0,l.jsx)(`span`,{children:ce?w.processing:`💳 ${w.connectWallet}`})})}),(0,l.jsx)(`div`,{className:`_cppay-section`,children:(0,l.jsx)(`button`,{onClick:Ce,disabled:D,className:`_cppay-btn _cppay-btn-primary`,children:D?(0,l.jsxs)(l.Fragment,{children:[(0,l.jsxs)(`svg`,{className:`_cppay-spinner`,viewBox:`0 0 24 24`,fill:`none`,xmlns:`http://www.w3.org/2000/svg`,children:[(0,l.jsx)(`circle`,{cx:`12`,cy:`12`,r:`10`,stroke:`currentColor`,strokeWidth:`3`,strokeOpacity:`0.25`}),(0,l.jsx)(`path`,{d:`M12 2a10 10 0 0 1 10 10`,stroke:`currentColor`,strokeWidth:`3`,strokeLinecap:`round`})]}),w.checking]}):w.completedPayment})}),(0,l.jsx)(`hr`,{className:`_cppay-divider`}),(0,l.jsx)(`div`,{className:`_cppay-section`,children:(0,l.jsx)(`button`,{onClick:()=>E(`select`),disabled:O||D,className:`_cppay-btn _cppay-btn-text`,children:w.changePaymentMethod})})]})})},d=u,f=({open:t,onClose:n,ott:r,apikey:i,plain:a,orderId:o,amount:ee,intervalDays:s,remark:u,locale:f,onExpired:p,onSuccess:m,onFailed:h,onError:g,Slots:_})=>{let[v,y]=(0,c.useState)(`select`),b=(0,c.useMemo)(()=>v!==`checking`,[v]);(0,c.useEffect)(()=>{t||y(`select`)},[t]),(0,c.useEffect)(()=>{if(!t)return;let e=e=>{e.key===`Escape`&&b&&x()};return window.addEventListener(`keydown`,e),()=>window.removeEventListener(`keydown`,e)},[t,b]);let x=()=>{b&&n()},S=(0,c.useMemo)(()=>e.t(f),[f]);return t?(0,l.jsx)(`div`,{className:`_cppay-overlay`,onClick:e=>e.target===e.currentTarget&&x(),children:(0,l.jsxs)(`div`,{className:`_cppay-dialog`,children:[(0,l.jsxs)(`div`,{className:`_cppay-header`,children:[(0,l.jsx)(`h2`,{className:`_cppay-title`,children:(()=>{switch(v){case`success`:return S.paymentResult;case`expired`:return S.paymentExpired;case`failed`:return S.paymentFailed;case`error`:return S.error;case`payment`:case`checking`:return S.completePayment;default:return S.selectPaymentMethod}})()}),(0,l.jsx)(`button`,{onClick:x,disabled:!b,className:`_cppay-close-btn`,children:(0,l.jsx)(`svg`,{fill:`none`,stroke:`currentColor`,viewBox:`0 0 24 24`,children:(0,l.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:2,d:`M6 18L18 6M6 6l12 12`})})})]}),(0,l.jsx)(d,{apikey:i,ott:r,plain:a,orderId:o,amount:ee,intervalDays:s,remark:u,locale:f,onSuccess:m,onExpired:p,onFailed:h,onError:g,Slots:_,onPaymentStepChange:y})]})}):null},p=f,m=(0,c.createContext)(null);const h=({children:e})=>{let[t,n]=(0,c.useState)(!1),[r,i]=(0,c.useState)(null),a=(0,c.useCallback)(e=>{i(e),n(!0)},[]),o=(0,c.useCallback)(()=>{n(!1)},[]),ee=(0,c.useCallback)(()=>{n(!1)},[]),s=(0,c.useCallback)(e=>{r?.onSuccess?.(e)},[r]),u=(0,c.useCallback)(e=>{r?.onExpired?.(e)},[r]),d=(0,c.useCallback)(e=>{r?.onFailed?.(e)},[r]),f=(0,c.useCallback)(e=>{r?.onError?.(e)},[r]);return(0,l.jsxs)(m.Provider,{value:{showPayment:a,closePayment:o},children:[e,r&&(0,l.jsx)(p,{open:t,onClose:ee,...r,onSuccess:s,onExpired:u,onFailed:d,onError:f})]})},g=()=>{let e=(0,c.useContext)(m);if(!e)throw Error(`useCppayPayment must be used within CppayProvider`);return e};e.n(e.r),exports.CppayProvider=h,exports.PaymentContent=d,exports.PaymentDialog=p,exports.useCppayPayment=g;
1
+ const e=require(`./locales-Cm9yIiVb.cjs`),t=require(`./cppay-DuOJqlpA.cjs`);let n=require(`qrcode`);n=e.i(n);let r=require(`rxjs`),i=require(`rxjs/operators`),a=require(`viem`),o=require(`viem/chains`),ee=require(`@reown/appkit`),te=require(`@reown/appkit-adapter-ethers`),s=require(`react`);s=e.i(s);let c=require(`react/jsx-runtime`);var l=({apikey:l,ott:u,plain:d,orderId:f,amount:p,intervalDays:m,remark:ne,locale:h,onSuccess:g,onExpired:re,onFailed:_,onError:v,Slots:y,onPaymentStepChange:ie})=>{let b=(0,s.useMemo)(()=>new t.t(l,u),[l,u]),x=(0,s.useMemo)(()=>e.t(h),[h]),[S,C]=(0,s.useState)(`select`),w=(0,s.useMemo)(()=>S===`checking`,[S]),[T,E]=(0,s.useState)(!1),[D,O]=(0,s.useState)(),[k,ae]=(0,s.useState)([]),[A,j]=(0,s.useState)(),[M,N]=(0,s.useState)(),[oe,se]=(0,s.useState)(),[P,F]=(0,s.useState)(),[ce,le]=(0,s.useState)(!1),[I,ue]=(0,s.useState)(),[de,fe]=(0,s.useState)(``),[L,R]=(0,s.useState)(!1),[pe,z]=(0,s.useState)(!1),[B,me]=(0,s.useState)(``),V=(0,s.useRef)(null),H=(0,s.useRef)(null),U=(0,s.useRef)(null),W=(0,s.useRef)(null),G=(0,s.useRef)(``);(0,s.useEffect)(()=>{typeof window<`u`&&!V.current&&(V.current=(0,ee.createAppKit)({debug:!1,enableNetworkSwitch:!1,adapters:[new te.EthersAdapter],projectId:`8d2e1854d3f1782e45aa15fbd8938894`,allowUnsupportedChain:!0,networks:[o.mainnet,o.bsc,o.polygon,o.arbitrum,o.optimism,o.base],metadata:{name:`Cppay`,description:`Cppay Payment Gateway`,url:window.location.origin,icons:[`https://cppay.com/icon.png`]},features:{analytics:!1}}),F(V.current.getAddress()),ue(V.current.getProvider(`eip155`)),V.current.subscribeAccount(e=>{e.isConnected?F(e.address):(F(void 0),ue(void 0))}),V.current.subscribeProviders(e=>{let t=e?.eip155;ue(t)}))},[]),(0,s.useEffect)(()=>{ie?.(S)},[S]);let he=async()=>{try{le(!0),O(void 0),V.current&&await V.current.open()}catch(e){console.error(`钱包连接失败:`,e),O(e instanceof Error?e.message:x.walletConnectionFailed),v?.(e)}finally{le(!1)}},K=(0,s.useMemo)(()=>k.find(e=>e.chain===A),[k,A]),q=(0,s.useMemo)(()=>K?.tokens||[],[K]),J=(0,s.useMemo)(()=>q.find(e=>e.symbol===M),[q,M]),Y=e=>[`USDT`,`USDC`,`BUSD`,`DAI`,`TUSD`,`USDD`,`FDUSD`].includes(e.toUpperCase())?2:6,X=(e,t)=>{let n=parseFloat(e);return isNaN(n)?`0`:n.toFixed(t).replace(/\.?0+$/,``)},ge=e=>{if(e<=0)return x.expired;let t=Math.floor(e/3600),n=Math.floor(e%3600/60),r=e%60,i=e=>e.toString().padStart(2,`0`);return t>0?`${t}:${i(n)}:${i(r)}`:`${n}:${i(r)}`},Z=e=>{U.current&&clearInterval(U.current);let t=()=>{if(S===`checking`){_e();return}let t=e-Math.floor(Date.now()/1e3);fe(ge(t)),t<=0&&_e()};t(),U.current=setInterval(t,1e3)},_e=()=>{U.current&&=(clearInterval(U.current),null)},ve=(0,s.useMemo)(()=>{if(!M||!J)return`0`;let e=parseFloat(J.price);if(isNaN(e)||e===0)return`0`;let t=Y(M);return X((parseFloat(p)/e).toFixed(t),t)},[p,M,J]),ye=async()=>{try{E(!0),O(void 0);let e=await b.getSupportedChains();ae(e),e.length>0&&j(e[0].chain)}catch(e){O(e instanceof Error?e.message:x.loadPaymentNetworkFailed),v?.(e)}finally{E(!1)}},be=async e=>{if(I)try{await I.request({method:`wallet_switchEthereumChain`,params:[{chainId:`0x${e.toString(16)}`}]})}catch(e){throw e.code===4902?Error(x.pleaseAddNetwork):e}},xe=async()=>{if(!(!P||!I||!W.current||!J||!K))try{E(!0);let e=W.current;await be(K.chainId);let t=(0,a.createWalletClient)({account:P,transport:(0,a.custom)(I)});if(J.address){let n=await t.writeContract({address:J.address,abi:[{name:`transfer`,type:`function`,stateMutability:`nonpayable`,inputs:[{name:`to`,type:`address`},{name:`amount`,type:`uint256`}],outputs:[{type:`bool`}]}],functionName:`transfer`,args:[e.receiveAddress,(0,a.parseUnits)(e.paymentAmount,J.decimals)],chain:null});console.log(`转账交易哈希:`,n),me(n),G.current=n}else{let n=await t.sendTransaction({to:e.receiveAddress,value:(0,a.parseUnits)(e.paymentAmount,J.decimals),chain:null});console.log(`转账交易哈希:`,n),me(n),G.current=n}E(!1),C(`checking`),Q({paymentId:e.paymentId})}catch(e){console.error(`钱包支付失败:`,e),O(e instanceof Error?e.message:x.walletPaymentFailed),v?.(e),E(!1)}},Se=async()=>{if(!(!P||!I||!W.current||!J||!K))try{E(!0);let e=W.current;await be(K.chainId);let t=(0,a.createWalletClient)({account:P,transport:(0,a.custom)(I)});if(!J.address)throw Error(x.subscriptionDoesNotSupportNative);let n=await t.writeContract({address:J.address,abi:[{name:`approve`,type:`function`,stateMutability:`nonpayable`,inputs:[{name:`spender`,type:`address`},{name:`amount`,type:`uint256`}],outputs:[{type:`bool`}]}],functionName:`approve`,args:[e.spenderAddress,(0,a.parseUnits)(e.approveAmount,J.decimals)],chain:null});console.log(`授权交易哈希:`,n),me(n),G.current=n,E(!1),C(`checking`),Q({subscriptionId:e.subscriptionId})}catch(e){console.error(`钱包授权失败:`,e),O(e instanceof Error?e.message:x.walletPaymentFailed),v?.(e),E(!1)}},Ce=async()=>{if(!P){let e=x.pleaseConnectWallet;O(e),v?.(Error(e));return}d===`instant`?await xe():d===`subscription`&&await Se()},we=async()=>{if(!(!A||!M))try{E(!0),O(void 0);let e=``;if(d===`instant`)W.current=await b.createOnetimePayment({paymentChain:A,paymentToken:M,orderId:f,amount:ve,remark:ne}),e=`${A.toLowerCase()}:${W.current.receiveAddress}?amount=${W.current.paymentAmount}`,Z(W.current.expireAt),Oe(W.current.paymentId,`instant`);else if(d===`subscription`){if(!m)throw Error(`Invalid Renewal Days: ${m??``}`);W.current=await b.createSubscriptionPayment({paymentChain:A,paymentToken:M,orderId:f,amountOfUsd:ve,intervalDays:m}),e=`${A.toLowerCase()}:${W.current.spenderAddress}?amount=${W.current.approveAmount}`,Z(W.current.expireAt),Oe(W.current.subscriptionId,`subscription`)}C(`payment`),se(await n.toDataURL(e,{width:200,margin:2,errorCorrectionLevel:`H`}))}catch(e){O(e instanceof Error?e.message:x.createPaymentFailed),v?.(e)}finally{E(!1)}},Te=()=>{W.current&&(C(`checking`),O(void 0),d===`instant`?Q({paymentId:W.current.paymentId}):d===`subscription`&&Q({subscriptionId:W.current.subscriptionId}))},Q=e=>{H.current?.unsubscribe();let t=()=>(0,r.defer)(()=>d===`subscription`?b.checkSubscriptionPaymentStatus(e):b.checkOnetimePaymentStatus(e)).pipe((0,i.timeout)(15e3),(0,i.retry)({delay:2e3}));H.current=t().pipe((0,i.expand)(e=>e.status===`pending`?(0,r.timer)(2e3).pipe((0,i.switchMap)(()=>t())):r.EMPTY),(0,i.tap)(e=>{if(!W.current&&d===`instant`){let t=e;W.current={paymentId:t.paymentId,orderId:t.orderId,paymentChain:t.chain,paymentToken:t.token,paymentAmount:t.payAmount,receiveAddress:t.receiveAddress,expireAt:t.expireAt},j(t.chain),N(t.token),Z(t.expireAt);let r=`${t.chain.toLowerCase()}:${t.receiveAddress}?amount=${t.payAmount}`;n.toDataURL(r,{width:200,margin:2,errorCorrectionLevel:`H`}).then(e=>{se(e),C(`payment`)}).catch(e=>{console.error(`生成二维码失败:`,e),C(`payment`)})}if(!W.current&&d===`subscription`){let t=e;W.current={subscriptionId:t.subscriptionId,orderId:t.orderId,paymentChain:t.chain,paymentToken:t.token,approveAmount:t.approveAmount,spenderAddress:t.approvedAddress,expireAt:t.expireAt,intervalDays:0,amountOfUsd:t.amountOfUsd},j(t.chain),N(t.token),Z(t.expireAt);let r=`${t.chain.toLowerCase()}:${t.approvedAddress}?amount=${t.approveAmount}`;n.toDataURL(r,{width:200,margin:2,errorCorrectionLevel:`H`}).then(e=>{se(e),C(`payment`)}).catch(e=>{console.error(`生成二维码失败:`,e),C(`payment`)})}(e.status===`paid`||e.status===`approved`)&&(C(`success`),$(),g?.(e)),e.status===`expired`&&(C(`expired`),$(),re?.(e)),e.status===`failed`&&(C(`failed`),$(),_?.(e))})).subscribe({error:e=>{if(W.current){let t=e instanceof Error?e.message:x.checkPaymentStatusFailed;C(`error`),O(t),v?.(e)}},complete:()=>{H.current?.unsubscribe()}})},Ee=async()=>{if(!W.current)return;let e=d===`subscription`?W.current.spenderAddress:W.current.receiveAddress;if(e)try{await navigator.clipboard.writeText(e),R(!0),setTimeout(()=>{R(!1)},2e3)}catch(e){console.error(`复制失败:`,e)}},De=async()=>{if(B)try{await navigator.clipboard.writeText(B),R(!0),setTimeout(()=>{R(!1)},2e3)}catch(e){console.error(`复制失败:`,e)}},Oe=(e,t)=>{if(typeof window>`u`)return;let n=new URL(window.location.href),r=t===`subscription`?`subscriptionId`:`paymentId`;n.searchParams.set(r,e),window.history.replaceState({},``,n.toString())},$=()=>{if(typeof window>`u`)return;let e=new URL(window.location.href);e.searchParams.delete(`paymentId`),e.searchParams.delete(`subscriptionId`),window.history.replaceState({},``,e.toString())};return(0,s.useEffect)(()=>{k.length===0&&ye()},[]),(0,s.useEffect)(()=>{q.length>0&&N(q[0].symbol)},[q]),(0,s.useEffect)(()=>{if(typeof window>`u`)return;let e=new URL(window.location.href);if(d===`instant`){let t=e.searchParams.get(`paymentId`);t&&(C(`payment`),Q({paymentId:t}))}if(d===`subscription`){let t=e.searchParams.get(`subscriptionId`);t&&(C(`payment`),Q({subscriptionId:t}))}},[]),(0,s.useEffect)(()=>()=>{H.current?.unsubscribe(),_e()},[]),(0,c.jsx)(`div`,{className:`_cppay-content`,children:S===`success`?y?.Success?(0,c.jsx)(y.Success,{paymentInfo:W.current}):(0,c.jsxs)(`div`,{className:`_cppay-state-container`,children:[(0,c.jsx)(`div`,{className:`_cppay-state-success-bg`,children:(0,c.jsx)(`div`,{className:`_cppay-state-success-icon`,children:`✓`})}),(0,c.jsx)(`h3`,{className:`_cppay-state-title _cppay-state-title-success`,children:d===`subscription`?x.authorizationSuccess:x.paymentSuccess}),B&&(0,c.jsxs)(`div`,{className:`_cppay-state-hash-container`,children:[(0,c.jsx)(`div`,{className:`_cppay-state-label`,children:x.transactionHash}),(0,c.jsxs)(`div`,{className:`_cppay-hash-row`,children:[(0,c.jsxs)(`code`,{className:`_cppay-state-hash`,children:[B.slice(0,18),`...`,B.slice(-18)]}),(0,c.jsx)(`button`,{onClick:De,className:`_cppay-state-copy-btn`,title:x.copyAddress,children:(0,c.jsxs)(`svg`,{viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,children:[(0,c.jsx)(`path`,{d:`M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2`}),(0,c.jsx)(`rect`,{x:`8`,y:`2`,width:`8`,height:`4`,rx:`1`,ry:`1`})]})})]})]}),W.current&&(0,c.jsxs)(`div`,{className:`_cppay-success-details`,children:[(0,c.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,c.jsx)(`span`,{className:`_cppay-detail-label`,children:x.orderId}),(0,c.jsx)(`span`,{className:`_cppay-detail-value`,children:W.current.orderId})]}),(0,c.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,c.jsx)(`span`,{className:`_cppay-detail-label`,children:d===`subscription`?x.authorizationAmount:x.paymentAmount}),(0,c.jsx)(`span`,{className:`_cppay-detail-value`,children:d===`subscription`?`${X(W.current.approveAmount,Y(M))} ${M}`:`${X(W.current.paymentAmount,Y(M))} ${M}`})]}),(0,c.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,c.jsx)(`span`,{className:`_cppay-detail-label`,children:x.paymentNetwork}),(0,c.jsx)(`span`,{className:`_cppay-detail-value`,children:W.current.paymentChain})]}),(0,c.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,c.jsx)(`span`,{className:`_cppay-detail-label`,children:x.paymentToken}),(0,c.jsx)(`span`,{className:`_cppay-detail-value`,children:W.current.paymentToken})]}),d===`subscription`&&(0,c.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,c.jsx)(`span`,{className:`_cppay-detail-label`,children:x.renewalInterval}),(0,c.jsxs)(`span`,{className:`_cppay-detail-value`,children:[W.current.intervalDays,` `,x.days]})]}),(0,c.jsxs)(`div`,{className:`_cppay-detail-item`,children:[(0,c.jsx)(`span`,{className:`_cppay-detail-label`,children:d===`subscription`?x.subscriptionId:x.paymentId}),(0,c.jsx)(`span`,{className:`_cppay-detail-value _cppay-detail-id`,children:d===`subscription`?W.current.subscriptionId:W.current.paymentId})]})]})]}):S===`expired`?y?.Expired?(0,c.jsx)(y.Expired,{paymentInfo:W.current}):(0,c.jsxs)(`div`,{className:`_cppay-state-container`,children:[(0,c.jsx)(`div`,{className:`_cppay-state-expired-bg`,children:(0,c.jsx)(`div`,{className:`_cppay-state-expired-icon`,children:`⏰`})}),(0,c.jsx)(`h3`,{className:`_cppay-state-title _cppay-state-title-expired`,children:x.paymentExpired}),(0,c.jsx)(`p`,{className:`_cppay-state-message`,children:x.pleaseInitiatePaymentAgain}),(0,c.jsx)(`button`,{onClick:()=>{C(`select`),$()},disabled:T||w,className:`_cppay-btn _cppay-btn-primary _cppay-state-btn`,children:x.returnButton})]}):S===`error`?y?.Error?(0,c.jsx)(y.Error,{paymentInfo:W.current}):(0,c.jsxs)(`div`,{className:`_cppay-state-container`,children:[(0,c.jsx)(`div`,{className:`_cppay-state-error-bg`,children:(0,c.jsx)(`div`,{className:`_cppay-state-error-icon`,children:`!`})}),(0,c.jsx)(`h3`,{className:`_cppay-state-title _cppay-state-title-error`,children:D||x.error}),(0,c.jsx)(`p`,{className:`_cppay-state-message`,children:x.checkStatusFailed}),(0,c.jsx)(`button`,{onClick:()=>{C(`select`),$()},disabled:T||w,className:`_cppay-btn _cppay-btn-primary _cppay-state-btn`,children:x.returnButton})]}):S===`failed`?y?.Failed?(0,c.jsx)(y.Failed,{paymentInfo:W.current}):(0,c.jsxs)(`div`,{className:`_cppay-state-container`,children:[(0,c.jsx)(`div`,{className:`_cppay-state-failed-bg`,children:(0,c.jsx)(`div`,{className:`_cppay-state-failed-icon`,children:`✕`})}),(0,c.jsx)(`h3`,{className:`_cppay-state-title _cppay-state-title-failed`,children:x.paymentFailed}),(0,c.jsx)(`p`,{className:`_cppay-state-message`,children:x.pleaseInitiatePaymentAgain}),(0,c.jsx)(`button`,{onClick:()=>{C(`select`),$()},disabled:T||w,className:`_cppay-btn _cppay-btn-primary _cppay-state-btn`,children:x.returnButton})]}):S===`select`?(0,c.jsxs)(`div`,{children:[y?.ChooseTop,(0,c.jsxs)(`div`,{className:`_cppay-section`,children:[(0,c.jsx)(`label`,{className:`_cppay-label`,children:x.paymentNetwork}),(0,c.jsx)(`div`,{className:`_cppay-grid`,children:k.map(e=>(0,c.jsxs)(`button`,{onClick:()=>j(e.chain),className:`_cppay-select-btn ${A===e.chain?`_cppay-selected`:``}`,children:[e.icon&&(0,c.jsx)(`img`,{src:e.icon,alt:e.chain}),(0,c.jsx)(`span`,{children:e.chain})]},e.chain))})]}),(0,c.jsxs)(`div`,{className:`_cppay-section`,children:[(0,c.jsx)(`label`,{className:`_cppay-label`,children:x.paymentToken}),(0,c.jsx)(`div`,{className:`_cppay-grid`,children:q.map(e=>(0,c.jsxs)(`button`,{onClick:()=>N(e.symbol),className:`_cppay-select-btn ${M===e.symbol?`_cppay-selected`:``}`,children:[e.icon&&(0,c.jsx)(`img`,{src:e.icon,alt:e.symbol}),(0,c.jsx)(`span`,{children:e.symbol})]},e.symbol))})]}),y?.ChooseBottom,(0,c.jsx)(`div`,{className:`_cppay-section`,children:(0,c.jsx)(`div`,{className:`_cppay-price-box`,children:(0,c.jsxs)(`div`,{className:`_cppay-price-row`,children:[(0,c.jsx)(`span`,{className:`_cppay-price-label`,children:x.paymentAmount}),(0,c.jsxs)(`div`,{className:`_cppay-price-amount`,children:[(0,c.jsxs)(`div`,{className:`_cppay-price-main`,children:[ve??`-`,` `,M??``]}),(0,c.jsxs)(`div`,{className:`_cppay-price-sub`,children:[`≈ $`,p]})]})]})})}),(0,c.jsxs)(`div`,{className:`_cppay-section`,children:[D&&(0,c.jsxs)(`div`,{className:`_cppay-error-tooltip-wrapper`,onMouseEnter:()=>z(!0),onMouseLeave:()=>z(!1),children:[(0,c.jsxs)(`div`,{className:`_cppay-error-tooltip`,children:[(0,c.jsx)(`svg`,{viewBox:`0 0 20 20`,fill:`currentColor`,children:(0,c.jsx)(`path`,{fillRule:`evenodd`,d:`M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z`,clipRule:`evenodd`})}),(0,c.jsx)(`span`,{children:D})]}),pe&&(0,c.jsx)(`div`,{className:`_cppay-error-tooltip-full`,children:D})]}),(0,c.jsx)(`button`,{onClick:we,disabled:!A||!M||T,className:`_cppay-btn _cppay-btn-primary`,children:T?x.processing:x.continuePayment})]})]}):(0,c.jsxs)(`div`,{children:[(0,c.jsx)(`div`,{className:`_cppay-qr-container`,children:(0,c.jsx)(`div`,{className:`_cppay-qr-code`,children:oe&&(0,c.jsx)(`img`,{src:oe,alt:`Payment QR Code`,className:`_cppay-qr-image`})})}),(0,c.jsxs)(`div`,{className:`_cppay-section`,children:[J&&(0,c.jsx)(`div`,{className:`_cppay-info-box`,children:(0,c.jsx)(`div`,{className:`_cppay-info-flex-container`,children:(0,c.jsxs)(`div`,{className:`_cppay-info-flex-child`,children:[(0,c.jsx)(`div`,{className:`_cppay-info-label`,children:d===`subscription`?x.authorizationAmount:x.paymentAmount}),(0,c.jsxs)(`div`,{className:`_cppay-info-value _cppay-info-value-flex`,children:[W.current&&(0,c.jsx)(`span`,{children:d===`subscription`?`${X(W.current.approveAmount,Y(J.symbol))} ${J.symbol}`:`${X(W.current.paymentAmount,Y(J.symbol))} ${J.symbol}`}),(0,c.jsxs)(`span`,{children:[`≈ $`,p]})]})]})})}),(0,c.jsx)(`div`,{className:`_cppay-info-box`,children:(0,c.jsxs)(`div`,{className:`_cppay-info-flex-child`,children:[(0,c.jsxs)(`div`,{className:`_cppay-info-label _cppay-info-label-flex`,children:[(0,c.jsx)(`span`,{children:d===`subscription`?x.authorizationContractAddress:x.paymentAddress}),de&&(0,c.jsxs)(`span`,{className:`_cppay-countdown`,children:[`⏰ `,de]})]}),(0,c.jsxs)(`div`,{className:`_cppay-address-row`,children:[W.current&&(0,c.jsx)(`code`,{children:d===`subscription`?W.current.spenderAddress:W.current.receiveAddress}),(0,c.jsx)(`button`,{onClick:Ee,className:`_cppay-copy-btn ${L?`_cppay-copy-success`:``}`,title:L?x.copied:x.copyAddress,children:L?(0,c.jsx)(`svg`,{className:`_cppay-copy-icon`,viewBox:`0 0 24 24`,fill:`currentColor`,children:(0,c.jsx)(`path`,{d:`M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z`})}):(0,c.jsxs)(`svg`,{className:`_cppay-copy-icon`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,children:[(0,c.jsx)(`path`,{d:`M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2`}),(0,c.jsx)(`rect`,{x:`8`,y:`2`,width:`8`,height:`4`,rx:`1`,ry:`1`})]})})]})]})}),d===`subscription`&&(0,c.jsxs)(`div`,{className:`_cppay-info-box _cppay-subscription-box`,children:[(0,c.jsx)(`div`,{className:`_cppay-info-label _cppay-subscription-label`,children:x.subscriptionNotice}),(0,c.jsx)(`div`,{className:`_cppay-info-value _cppay-subscription-message`,children:x.subscriptionNoticeMessage})]})]}),D&&(0,c.jsxs)(`div`,{className:`_cppay-error-tooltip-wrapper`,onMouseEnter:()=>z(!0),onMouseLeave:()=>z(!1),children:[(0,c.jsxs)(`div`,{className:`_cppay-error-tooltip`,children:[(0,c.jsx)(`svg`,{viewBox:`0 0 20 20`,fill:`currentColor`,children:(0,c.jsx)(`path`,{fillRule:`evenodd`,d:`M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z`,clipRule:`evenodd`})}),(0,c.jsx)(`span`,{children:D})]}),pe&&(0,c.jsx)(`div`,{className:`_cppay-error-tooltip-full`,children:D})]}),!w&&(0,c.jsx)(`div`,{className:`_cppay-section`,children:P?(0,c.jsxs)(c.Fragment,{children:[(0,c.jsx)(`button`,{onClick:Ce,disabled:T,className:`_cppay-btn _cppay-btn-primary`,children:T?d===`subscription`?x.authorizing:x.processing:d===`subscription`?`💳 ${x.connectWallet}`:`💳 ${x.walletPay}`}),(0,c.jsxs)(`button`,{onClick:he,disabled:T,className:`_cppay-btn _cppay-wallet-address-btn`,title:x.clickToModifyWallet,children:[(0,c.jsxs)(`span`,{className:`_cppay-wallet-address-text`,children:[P.slice(0,10),`...`,P.slice(-4)]}),(0,c.jsx)(`svg`,{className:`_cppay-wallet-arrow`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,children:(0,c.jsx)(`polyline`,{points:`6 9 12 15 18 9`})})]})]}):(0,c.jsx)(`button`,{onClick:he,disabled:ce,className:`_cppay-btn _cppay-btn-secondary _cppay-connect-wallet-btn`,children:(0,c.jsx)(`span`,{children:ce?x.processing:`💳 ${x.connectWallet}`})})}),(0,c.jsx)(`div`,{className:`_cppay-section`,children:(0,c.jsx)(`button`,{onClick:Te,disabled:w,className:`_cppay-btn _cppay-btn-primary`,children:w?(0,c.jsxs)(c.Fragment,{children:[(0,c.jsxs)(`svg`,{className:`_cppay-spinner`,viewBox:`0 0 24 24`,fill:`none`,xmlns:`http://www.w3.org/2000/svg`,children:[(0,c.jsx)(`circle`,{cx:`12`,cy:`12`,r:`10`,stroke:`currentColor`,strokeWidth:`3`,strokeOpacity:`0.25`}),(0,c.jsx)(`path`,{d:`M12 2a10 10 0 0 1 10 10`,stroke:`currentColor`,strokeWidth:`3`,strokeLinecap:`round`})]}),x.checking]}):x.completedPayment})}),(0,c.jsx)(`hr`,{className:`_cppay-divider`}),(0,c.jsx)(`div`,{className:`_cppay-section`,children:(0,c.jsx)(`button`,{onClick:()=>{C(`select`),$()},disabled:T||w,className:`_cppay-btn _cppay-btn-text`,children:x.changePaymentMethod})})]})})},u=l,d=({open:t,onClose:n,ott:r,apikey:i,plain:a,orderId:o,amount:ee,intervalDays:te,remark:l,locale:d,onExpired:f,onSuccess:p,onFailed:m,onError:ne,Slots:h})=>{let[g,re]=(0,s.useState)(`select`),_=(0,s.useMemo)(()=>g!==`checking`,[g]);(0,s.useEffect)(()=>{t||re(`select`)},[t]),(0,s.useEffect)(()=>{if(!t)return;let e=e=>{e.key===`Escape`&&_&&v()};return window.addEventListener(`keydown`,e),()=>window.removeEventListener(`keydown`,e)},[t,_]);let v=()=>{_&&n()},y=(0,s.useMemo)(()=>e.t(d),[d]);return t?(0,c.jsx)(`div`,{className:`_cppay-overlay`,onClick:e=>e.target===e.currentTarget&&v(),children:(0,c.jsxs)(`div`,{className:`_cppay-dialog`,children:[(0,c.jsxs)(`div`,{className:`_cppay-header`,children:[(0,c.jsx)(`h2`,{className:`_cppay-title`,children:(()=>{switch(g){case`success`:return y.paymentResult;case`expired`:return y.paymentExpired;case`failed`:return y.paymentFailed;case`error`:return y.error;case`payment`:case`checking`:return y.completePayment;default:return y.selectPaymentMethod}})()}),(0,c.jsx)(`button`,{onClick:v,disabled:!_,className:`_cppay-close-btn`,children:(0,c.jsx)(`svg`,{fill:`none`,stroke:`currentColor`,viewBox:`0 0 24 24`,children:(0,c.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:2,d:`M6 18L18 6M6 6l12 12`})})})]}),(0,c.jsx)(u,{apikey:i,ott:r,plain:a,orderId:o,amount:ee,intervalDays:te,remark:l,locale:d,onSuccess:p,onExpired:f,onFailed:m,onError:ne,Slots:h,onPaymentStepChange:re})]})}):null},f=d,p=(0,s.createContext)(null);const m=({children:e})=>{let[t,n]=(0,s.useState)(!1),[r,i]=(0,s.useState)(null),a=(0,s.useCallback)(e=>{i(e),n(!0)},[]),o=(0,s.useCallback)(()=>{n(!1)},[]),ee=(0,s.useCallback)(()=>{n(!1)},[]),te=(0,s.useCallback)(e=>{r?.onSuccess?.(e)},[r]),l=(0,s.useCallback)(e=>{r?.onExpired?.(e)},[r]),u=(0,s.useCallback)(e=>{r?.onFailed?.(e)},[r]),d=(0,s.useCallback)(e=>{r?.onError?.(e)},[r]);return(0,c.jsxs)(p.Provider,{value:{showPayment:a,closePayment:o},children:[e,r&&(0,c.jsx)(f,{open:t,onClose:ee,...r,onSuccess:te,onExpired:l,onFailed:u,onError:d})]})},ne=()=>{let e=(0,s.useContext)(p);if(!e)throw Error(`useCppayPayment must be used within CppayProvider`);return e};e.n(e.r),exports.CppayProvider=m,exports.PaymentContent=u,exports.PaymentDialog=f,exports.useCppayPayment=ne;