cppay-sdk 0.0.2-beta.31 → 0.0.2-beta.33
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/{cppay-DXZVBIDi.cjs → cppay-BYRjTzRF.cjs} +1 -1
- package/dist/{cppay-BPtRBPjB.js → cppay-C_gp2ZZ3.js} +4 -7
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +18 -8
- package/dist/index.js +1 -1
- package/dist/locales-DAKREZkH.cjs +1 -0
- package/dist/locales-DGvDecya.js +112 -0
- package/dist/react.cjs +1 -1
- package/dist/react.d.ts +141 -2
- package/dist/react.js +540 -434
- package/dist/vue.cjs +1 -1
- package/dist/vue.d.ts +131 -12
- package/dist/vue.js +454 -351
- package/package.json +1 -1
- package/dist/cppay-sdk.css +0 -2
- package/dist/inject-style-Boo8CMsU.cjs +0 -1
- package/dist/inject-style-CWza6SKp.js +0 -7
package/dist/vue.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=require(`./inject-style-Boo8CMsU.cjs`),t=require(`./cppay-DXZVBIDi.cjs`);let n=require(`vue`),r=require(`qrcode`);r=e.r(r);let i=require(`rxjs`),a=require(`rxjs/operators`),o=require(`viem`),s=require(`viem/chains`),ee=require(`@reown/appkit`),te=require(`@reown/appkit-adapter-ethers`);var ne={class:`_cppay-dialog`},re={class:`_cppay-header`},ie={class:`_cppay-title`},ae=[`disabled`],c={class:`_cppay-content`},oe={key:0,style:{"text-align":`center`,padding:`2rem 0`}},se={style:{"font-size":`1.5rem`,"font-weight":`600`,color:`#10b981`,margin:`0 0 0.5rem 0`}},ce={style:{color:`#6b7280`,margin:`0`}},le={key:1},ue={class:`_cppay-section`},de={class:`_cppay-grid`},fe=[`onClick`],pe=[`src`,`alt`],me={class:`_cppay-section`},he={class:`_cppay-grid`},ge=[`onClick`],_e=[`src`,`alt`],ve={class:`_cppay-section`},ye={class:`_cppay-price-box`},be={class:`_cppay-price-row`},xe={class:`_cppay-price-amount`},Se={class:`_cppay-price-main`},Ce={class:`_cppay-price-sub`},we={class:`_cppay-section`},Te={class:`_cppay-error-tooltip`},Ee={key:0,class:`_cppay-error-tooltip-full`},De=[`disabled`],l={key:2},u={class:`_cppay-qr-container`},d={class:`_cppay-qr-code`},f=[`src`],p={class:`_cppay-section`},m={key:0,class:`_cppay-info-box`},h={class:`_cppay-info-label`},g={class:`_cppay-info-value`},_={class:`_cppay-info-box`},v={class:`_cppay-info-label`},y={class:`_cppay-address-row`},b={key:1,class:`_cppay-info-box`,style:{background:`#fff3cd`,"border-color":`#ffc107`}},x={class:`_cppay-error-tooltip`},S={key:0,class:`_cppay-error-tooltip-full`},C={key:1,class:`_cppay-section`},w=[`disabled`],T={key:1},E={class:`_cppay-btn-container`},Oe=[`disabled`],ke=[`disabled`],Ae={style:{"text-align":`center`,"margin-top":`8px`,"font-size":`12px`,color:`#666`}},je={class:`_cppay-section`},Me=[`disabled`],Ne={class:`_cppay-section`},Pe=[`disabled`],D=(0,n.defineComponent)({__name:`PaymentDialog`,props:{modelValue:{type:Boolean},apikey:{},orderId:{},amount:{},plain:{default:`one-time`},intervalDays:{default:30}},emits:[`update:modelValue`,`success`,`error`],setup(e,{emit:D}){let O=e,k=D,A=new t.t(O.apikey),j=(0,n.ref)(`select`),M=(0,n.ref)(!1),N=(0,n.ref)(!1),P=(0,n.ref)(),F=(0,n.ref)(!1),I=(0,n.ref)([]),L=(0,n.ref)(),R=(0,n.ref)(),z=(0,n.ref)(null),B=(0,n.ref)(),V=(0,n.ref)(),H=(0,n.ref)(!1),U=(0,n.ref)(),W=(0,n.ref)(null),G,K=(0,n.computed)(()=>I.value.find(e=>e.chain===L.value)),q=(0,n.computed)(()=>K.value?.tokens||[]),J=(0,n.computed)(()=>q.value.find(e=>e.symbol===R.value)),Fe=e=>[`USDT`,`USDC`,`BUSD`,`DAI`,`TUSD`,`USDD`,`FDUSD`].includes(e.toUpperCase())?2:6,Ie=(e,t)=>{let n=parseFloat(e);return isNaN(n)?`0`:n.toFixed(t).replace(/\.?0+$/,``)},Y=(0,n.computed)(()=>{if(!R.value||!J.value)return`0`;let e=parseFloat(J.value.price);if(isNaN(e)||e===0)return`0`;let t=Fe(R.value);return Ie((parseFloat(O.amount)/e).toFixed(t),t)}),Le=()=>{z.value&&(P.value=void 0,N.value=!0,O.plain===`one-time`?$(z.value.paymentId):O.plain===`subscription`&&$(z.value.subscriptionId))},X=()=>{M.value||N.value||(k(`update:modelValue`,!1),setTimeout(()=>{j.value=`select`,P.value=void 0,z.value=null},300))},Re=async()=>{try{M.value=!0,P.value=void 0,I.value=await A.getSupportedChains(),I.value.length>0&&(L.value=I.value[0].chain)}catch(e){P.value=e instanceof Error?e.message:`加载支付网络失败`,k(`error`,e)}finally{M.value=!1}};(0,n.onMounted)(()=>{typeof window<`u`&&!G&&(G=(0,ee.createAppKit)({debug:!1,enableNetworkSwitch:!1,adapters:[new te.EthersAdapter],projectId:`8d2e1854d3f1782e45aa15fbd8938894`,allowUnsupportedChain:!0,networks:[s.mainnet,s.bsc,s.polygon,s.arbitrum,s.optimism,s.base],metadata:{name:`Cppay`,description:`Cppay Payment Gateway`,url:window.location.origin,icons:[`https://cppay.com/icon.png`]},features:{analytics:!1}}),U.value=G.getProvider(`eip155`),V.value=G.getAddress(),G.subscribeAccount(e=>{e.isConnected?V.value=e.address:(V.value=void 0,U.value=void 0)}),G.subscribeProviders(e=>{U.value=e?.eip155}))});let Z=async()=>{try{H.value=!0,P.value=void 0,G&&await G.open()}catch(e){console.error(`钱包连接失败:`,e),P.value=e instanceof Error?e.message:`钱包连接失败`,k(`error`,e)}finally{H.value=!1}},Q=async e=>{if(U.value)try{await U.value.request({method:`wallet_switchEthereumChain`,params:[{chainId:`0x${e.toString(16)}`}]})}catch(e){throw e.code===4902?Error(`请在钱包中添加该网络`):e}},ze=async()=>{if(!(!V.value||!U.value||!z.value||!J.value||!K.value))try{M.value=!0;let e=z.value;await Q(K.value.chainId);let t=(0,o.createWalletClient)({account:V.value,transport:(0,o.custom)(U.value)});if(J.value.address){let n=await t.writeContract({address:J.value.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,o.parseUnits)(e.paymentAmount,J.value.decimals)],chain:null});console.log(`转账交易哈希:`,n)}else{let n=await t.sendTransaction({to:e.receiveAddress,value:(0,o.parseUnits)(e.paymentAmount,J.value.decimals),chain:null});console.log(`转账交易哈希:`,n)}M.value=!1,N.value=!0,$(e.paymentId)}catch(e){console.error(`钱包支付失败:`,e),P.value=e instanceof Error?e.message:`钱包支付失败`,k(`error`,e),M.value=!1}},Be=async()=>{if(!(!V.value||!U.value||!z.value||!J.value||!K.value))try{M.value=!0;let e=z.value;await Q(K.value.chainId);let t=(0,o.createWalletClient)({account:V.value,transport:(0,o.custom)(U.value)});if(!J.value.address)throw Error(`订阅支付不支持原生代币`);let n=await t.writeContract({address:J.value.address,abi:[{name:`approve`,type:`function`,stateMutability:`nonpayable`,inputs:[{name:`spender`,type:`address`},{name:`amount`,type:`uint256`}],outputs:[{type:`bool`}]}],functionName:`approve`,args:[e.receiveAddress,(0,o.parseUnits)(e.paymentAmount,J.value.decimals)],chain:null});console.log(`授权交易哈希:`,n),M.value=!1,N.value=!0,$(e.paymentId)}catch(e){console.error(`钱包授权失败:`,e),P.value=e instanceof Error?e.message:`钱包授权失败`,k(`error`,e),M.value=!1}},Ve=async()=>{if(!V.value){await Z();return}O.plain===`one-time`?await ze():O.plain===`subscription`&&await Be()},He=async()=>{if(!(!L.value||!R.value))try{M.value=!0,P.value=void 0;let e;O.plain===`one-time`?e=await A.createPayment(`one-time`,{paymentChain:L.value,paymentToken:R.value,orderId:O.orderId,amount:Y.value}):O.plain===`subscription`&&(e=await A.createPayment(`subscription`,{paymentChain:L.value,paymentToken:R.value,orderId:O.orderId,amountOfUsd:Y.value,intervalDays:O.intervalDays||30})),z.value={paymentId:e.paymentId||e.subscriptionId,paymentAmount:e.paymentAmount||e.approveAmount,receiveAddress:e.receiveAddress||e.spenderAddress||``};let t=`${L.value.toLowerCase()}:${z.value.receiveAddress}?amount=${z.value.paymentAmount}`;B.value=await r.toDataURL(t,{width:200,margin:2,errorCorrectionLevel:`H`}),j.value=`payment`}catch(e){P.value=e instanceof Error?e.message:`创建支付失败`,k(`error`,e)}finally{M.value=!1}},$=e=>{let t=()=>(0,i.defer)(()=>O.plain===`subscription`?A.checkSubscriptionPaymentStatus({subscriptionId:e}):A.checkOnetimePaymentStatus({paymentId:e})).pipe((0,a.timeout)(15e3),(0,a.retry)({count:3,delay:2e3}));W.value?.unsubscribe(),W.value=t().pipe((0,a.expand)(e=>e.status===`pending`?(0,i.timer)(2e3).pipe((0,a.switchMap)(()=>t())):i.EMPTY),(0,a.tap)(e=>{e.status===`expired`&&(N.value=!1),e.status===`paid`&&(N.value=!1,j.value=`success`,k(`success`,e)),e.status===`failed`&&(N.value=!1),e.status===`approved`&&(N.value=!1,j.value=`success`,k(`success`,e))})).subscribe({error:e=>{console.error(`支付状态检查错误:`,e),P.value=e instanceof Error?e.message:`检查支付状态失败`,N.value=!1,k(`error`,e)}})},Ue=async()=>{z.value?.receiveAddress&&await navigator.clipboard.writeText(z.value.receiveAddress)};return(0,n.watch)(()=>O.modelValue,e=>{e&&I.value.length===0&&Re()}),(0,n.watch)(L,()=>{q.value.length>0&&(R.value=q.value[0].symbol)}),(0,n.watch)(j,e=>{e===`payment`&&z.value&&$(z.value.paymentId)}),(0,n.onUnmounted)(()=>{W.value?.unsubscribe()}),(t,r)=>((0,n.openBlock)(),(0,n.createBlock)(n.Teleport,{to:`body`},[(0,n.createVNode)(n.Transition,{name:`_cppay-fade`},{default:(0,n.withCtx)(()=>[e.modelValue?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,{key:0,class:`_cppay-overlay`,onClick:r[5]||=(0,n.withModifiers)(e=>!M.value&&!N.value&&X(),[`self`])},[(0,n.createElementVNode)(`div`,ne,[(0,n.createElementVNode)(`div`,re,[(0,n.createElementVNode)(`h2`,ie,(0,n.toDisplayString)(j.value===`select`?`选择支付方式`:j.value===`success`?`支付结果`:`完成支付`),1),(0,n.createElementVNode)(`button`,{onClick:X,class:`_cppay-close-btn`,disabled:M.value||N.value},[...r[6]||=[(0,n.createElementVNode)(`svg`,{fill:`none`,stroke:`currentColor`,viewBox:`0 0 24 24`},[(0,n.createElementVNode)(`path`,{"stroke-linecap":`round`,"stroke-linejoin":`round`,"stroke-width":`2`,d:`M6 18L18 6M6 6l12 12`})],-1)]],8,ae)]),(0,n.createElementVNode)(`div`,c,[j.value===`success`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,oe,[r[7]||=(0,n.createElementVNode)(`div`,{style:{"font-size":`64px`,"margin-bottom":`1rem`}},`✅`,-1),(0,n.createElementVNode)(`h3`,se,(0,n.toDisplayString)(O.plain===`subscription`?`授权成功!`:`支付成功!`),1),(0,n.createElementVNode)(`p`,ce,(0,n.toDisplayString)(O.plain===`subscription`?`订阅已激活`:`交易已完成`),1)])):(0,n.createCommentVNode)(``,!0),j.value===`select`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,le,[(0,n.createElementVNode)(`div`,ue,[r[8]||=(0,n.createElementVNode)(`label`,{class:`_cppay-label`},`支付网络`,-1),(0,n.createElementVNode)(`div`,de,[((0,n.openBlock)(!0),(0,n.createElementBlock)(n.Fragment,null,(0,n.renderList)(I.value,e=>((0,n.openBlock)(),(0,n.createElementBlock)(`button`,{key:e.chain,onClick:t=>L.value=e.chain,class:(0,n.normalizeClass)([`_cppay-select-btn`,L.value===e.chain?`_cppay-selected`:``])},[e.icon?((0,n.openBlock)(),(0,n.createElementBlock)(`img`,{key:0,src:e.icon,alt:e.chain},null,8,pe)):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(e.chain),1)],10,fe))),128))])]),(0,n.createElementVNode)(`div`,me,[r[9]||=(0,n.createElementVNode)(`label`,{class:`_cppay-label`},`支付代币`,-1),(0,n.createElementVNode)(`div`,he,[((0,n.openBlock)(!0),(0,n.createElementBlock)(n.Fragment,null,(0,n.renderList)(q.value,e=>((0,n.openBlock)(),(0,n.createElementBlock)(`button`,{key:e.symbol,onClick:t=>R.value=e.symbol,class:(0,n.normalizeClass)([`_cppay-select-btn`,R.value===e.symbol?`_cppay-selected`:``])},[e.icon?((0,n.openBlock)(),(0,n.createElementBlock)(`img`,{key:0,src:e.icon,alt:e.symbol},null,8,_e)):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(e.symbol),1)],10,ge))),128))])]),(0,n.createElementVNode)(`div`,ve,[(0,n.createElementVNode)(`div`,ye,[(0,n.createElementVNode)(`div`,be,[r[10]||=(0,n.createElementVNode)(`span`,{class:`_cppay-price-label`},`支付金额`,-1),(0,n.createElementVNode)(`div`,xe,[(0,n.createElementVNode)(`div`,Se,(0,n.toDisplayString)(Y.value??`-`)+` `+(0,n.toDisplayString)(R.value??``),1),(0,n.createElementVNode)(`div`,Ce,`≈ $`+(0,n.toDisplayString)(e.amount),1)])])])]),(0,n.createElementVNode)(`div`,we,[P.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,{key:0,class:`_cppay-error-tooltip-wrapper`,onMouseenter:r[0]||=e=>F.value=!0,onMouseleave:r[1]||=e=>F.value=!1},[(0,n.createElementVNode)(`div`,Te,[r[11]||=(0,n.createElementVNode)(`svg`,{viewBox:`0 0 20 20`,fill:`currentColor`},[(0,n.createElementVNode)(`path`,{"fill-rule":`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`,"clip-rule":`evenodd`})],-1),(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(P.value),1)]),F.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,Ee,(0,n.toDisplayString)(P.value),1)):(0,n.createCommentVNode)(``,!0)],32)):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`button`,{onClick:He,disabled:!L.value||!R.value||M.value,class:`_cppay-btn _cppay-btn-primary`},(0,n.toDisplayString)(M.value?`处理中...`:`继续支付`),9,De)])])):j.value===`payment`&&z.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,l,[(0,n.createElementVNode)(`div`,u,[(0,n.createElementVNode)(`div`,d,[B.value?((0,n.openBlock)(),(0,n.createElementBlock)(`img`,{key:0,src:B.value,alt:`QR Code`,style:{width:`160px`,height:`160px`,display:`block`}},null,8,f)):(0,n.createCommentVNode)(``,!0)])]),(0,n.createElementVNode)(`div`,p,[R.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,m,[(0,n.createElementVNode)(`div`,h,(0,n.toDisplayString)(O.plain===`subscription`?`授权金额`:`支付金额`),1),(0,n.createElementVNode)(`div`,g,(0,n.toDisplayString)(z.value.paymentAmount)+` `+(0,n.toDisplayString)(R.value),1)])):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`div`,_,[(0,n.createElementVNode)(`div`,v,(0,n.toDisplayString)(O.plain===`subscription`?`授权合约地址`:`支付地址`),1),(0,n.createElementVNode)(`div`,y,[(0,n.createElementVNode)(`code`,null,(0,n.toDisplayString)(z.value.receiveAddress),1),(0,n.createElementVNode)(`button`,{onClick:Ue,class:`_cppay-copy-btn`,title:`复制地址`},[...r[12]||=[(0,n.createElementVNode)(`svg`,{fill:`none`,stroke:`currentColor`,viewBox:`0 0 24 24`},[(0,n.createElementVNode)(`path`,{"stroke-linecap":`round`,"stroke-linejoin":`round`,"stroke-width":`2`,d:`M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z`})],-1)]])])]),O.plain===`subscription`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,b,[...r[13]||=[(0,n.createElementVNode)(`div`,{class:`_cppay-info-label`,style:{color:`#856404`}},`📌 订阅说明`,-1),(0,n.createElementVNode)(`div`,{class:`_cppay-info-value`,style:{"font-size":`12px`,color:`#856404`}},` 订阅支付需要授权代币给合约地址,系统将按周期自动扣款。授权后无需每次手动支付。 `,-1)]])):(0,n.createCommentVNode)(``,!0)]),P.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,{key:0,class:`_cppay-error-tooltip-wrapper`,onMouseenter:r[2]||=e=>F.value=!0,onMouseleave:r[3]||=e=>F.value=!1},[(0,n.createElementVNode)(`div`,x,[r[14]||=(0,n.createElementVNode)(`svg`,{viewBox:`0 0 20 20`,fill:`currentColor`},[(0,n.createElementVNode)(`path`,{"fill-rule":`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`,"clip-rule":`evenodd`})],-1),(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(P.value),1)]),F.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,S,(0,n.toDisplayString)(P.value),1)):(0,n.createCommentVNode)(``,!0)],32)):(0,n.createCommentVNode)(``,!0),N.value?(0,n.createCommentVNode)(``,!0):((0,n.openBlock)(),(0,n.createElementBlock)(`div`,C,[V.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,T,[(0,n.createElementVNode)(`div`,E,[(0,n.createElementVNode)(`button`,{onClick:Ve,disabled:M.value,class:`_cppay-btn _cppay-btn-primary`},(0,n.toDisplayString)(M.value?e.plain===`subscription`?`授权中...`:`支付中...`:e.plain===`subscription`?`💳 钱包授权`:`💳 钱包支付`),9,Oe),(0,n.createElementVNode)(`button`,{onClick:Z,disabled:M.value,class:`_cppay-btn _cppay-btn-primary`},` 💰钱包状态 `,8,ke)]),(0,n.createElementVNode)(`div`,Ae,` 已连接: `+(0,n.toDisplayString)(V.value.slice(0,6))+`...`+(0,n.toDisplayString)(V.value.slice(-4)),1)])):((0,n.openBlock)(),(0,n.createElementBlock)(`button`,{key:0,onClick:Z,disabled:H.value,class:`_cppay-btn _cppay-btn-secondary`,style:{display:`flex`,"align-items":`center`,"justify-content":`center`,gap:`0.5rem`,padding:`0.75rem 1rem`,width:`100%`}},[(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(H.value?`连接中...`:`💳 连接钱包`),1)],8,w))])),(0,n.createElementVNode)(`div`,je,[(0,n.createElementVNode)(`button`,{onClick:Le,disabled:N.value,class:`_cppay-btn _cppay-btn-primary`},(0,n.toDisplayString)(N.value?`检查中...`:`我已完成支付`),9,Me)]),(0,n.createElementVNode)(`div`,Ne,[(0,n.createElementVNode)(`button`,{onClick:r[4]||=e=>j.value=`select`,disabled:M.value||N.value,class:`_cppay-btn _cppay-btn-text`},` 更改支付方式 `,8,Pe)])])):(0,n.createCommentVNode)(``,!0)])])])):(0,n.createCommentVNode)(``,!0)]),_:1})]))}}),O=(e,t)=>{let n=e.__vccOpts||e;for(let[e,r]of t)n[e]=r;return n},k=O(D,[[`__scopeId`,`data-v-9361860d`]]),A=(0,n.ref)({apikey:``,open:!1,options:null});const j=e=>{A.value.options=e,A.value.open=!0};var M=()=>{A.value.open=!1},N=e=>{A.value.options?.onSuccess?.(e),A.value.open=!1},P=e=>{A.value.options?.onError?.(e)};const F={install(e,t){if(!t?.apikey)throw Error(`Cppay plugin requires an apikey`);A.value.apikey=t.apikey,e.config.globalProperties.$showPayment=j;let r=document.createElement(`div`);document.body.appendChild(r),(0,n.createApp)({setup(){return()=>A.value.options?(0,n.h)(k,{modelValue:A.value.open,"onUpdate:modelValue":e=>{A.value.open=e,e||M()},apikey:A.value.apikey,plain:A.value.options.plain,orderId:A.value.options.orderId,amount:A.value.options.amount,intervalDays:A.value.options.intervalDays,onSuccess:N,onError:P}):null}}).mount(r)}},I=()=>{if(!A.value.apikey)throw Error(`useCppayPayment must be used after installing CppayPlugin`);return{showPayment:j}};e.t(e.n),exports.CppayPlugin=F,exports.PaymentDialog=k,exports.showPayment=j,exports.useCppayPayment=I;
|
|
1
|
+
const e=require(`./locales-DAKREZkH.cjs`),t=require(`./cppay-BYRjTzRF.cjs`);let n=require(`vue`),r=require(`qrcode`);r=e.i(r);let i=require(`rxjs`),a=require(`rxjs/operators`),o=require(`viem`),s=require(`viem/chains`),c=require(`@reown/appkit`),l=require(`@reown/appkit-adapter-ethers`);var u={class:`_cppay-content`},d={key:0,class:`_cppay-state-container`},f={class:`_cppay-state-title`},ee={class:`_cppay-state-message`},te={key:1,class:`_cppay-state-container`},p={class:`_cppay-state-title`},ne=[`disabled`],re={key:2,class:`_cppay-state-container`},ie={class:`_cppay-state-title`},ae={key:3,class:`_cppay-state-container`},oe={class:`_cppay-state-title`},se={key:4},ce={class:`_cppay-section`},le={class:`_cppay-label`},ue={class:`_cppay-grid`},de=[`onClick`],fe=[`src`,`alt`],pe={class:`_cppay-section`},me={class:`_cppay-label`},he={class:`_cppay-grid`},ge=[`onClick`],_e=[`src`,`alt`],ve={class:`_cppay-section`},ye={class:`_cppay-price-box`},be={class:`_cppay-price-row`},xe={class:`_cppay-price-label`},Se={class:`_cppay-price-amount`},Ce={class:`_cppay-price-main`},we={class:`_cppay-price-sub`},Te={class:`_cppay-section`},Ee={class:`_cppay-error-tooltip`},De={key:0,class:`_cppay-error-tooltip-full`},Oe=[`disabled`],ke={key:5},Ae={class:`_cppay-qr-container`},je={class:`_cppay-qr-code`},m=[`src`],h={class:`_cppay-section`},g={key:0,class:`_cppay-info-box`},_={class:`_cppay-info-flex-container`},v={class:`_cppay-info-flex-child`},y={class:`_cppay-info-label`},b={class:`_cppay-info-value _cppay-info-value-flex`},x={key:0},S={class:`_cppay-info-box`},Me={class:`_cppay-info-flex-child`},Ne={class:`_cppay-info-label _cppay-info-label-flex`},Pe={key:0,class:`_cppay-countdown`},Fe={class:`_cppay-address-row`},Ie={key:0},Le=[`title`],Re={key:0,class:`_cppay-copy-icon`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,"stroke-width":`2`},ze={key:1,class:`_cppay-copy-icon`,viewBox:`0 0 24 24`,fill:`currentColor`},Be={key:1,class:`_cppay-info-box _cppay-subscription-box`},Ve={class:`_cppay-info-label _cppay-subscription-label`},He={class:`_cppay-info-value _cppay-subscription-message`},Ue={class:`_cppay-error-tooltip`},We={key:0,class:`_cppay-error-tooltip-full`},Ge={key:1,class:`_cppay-section`},Ke=[`disabled`],qe=[`disabled`],Je=[`disabled`,`title`],Ye={class:`_cppay-wallet-address-text`},Xe={class:`_cppay-section`},Ze=[`disabled`],Qe={class:`_cppay-section`},$e=[`disabled`],C=(0,n.defineComponent)({__name:`PaymentContent`,props:{apikey:{},orderId:{},amount:{},plain:{default:`one-time`},intervalDays:{},paymentStep:{},loading:{type:Boolean,default:!1},isCheckingPayment:{type:Boolean,default:!1},error:{},slots:{},locale:{}},emits:[`update:paymentStep`,`success`,`expired`,`failed`,`error`,`update:error`],setup(C,{emit:w}){let T=C,E=w,D=(0,n.computed)(()=>e.t(T.locale||`zh-CN`)),O=new t.t(T.apikey),k=(0,n.ref)([]),A=(0,n.ref)(),j=(0,n.ref)(),M=(0,n.ref)(),N=(0,n.ref)(),P=(0,n.ref)(!1),F=(0,n.ref)(),I=(0,n.ref)(``),L=(0,n.ref)(!1),R=(0,n.ref)(!1),z=(0,n.ref)(null),B=(0,n.ref)(null),V=(0,n.ref)(null),H=(0,n.ref)(null),et=()=>{typeof window<`u`&&!z.value&&(z.value=(0,c.createAppKit)({debug:!1,enableNetworkSwitch:!1,adapters:[new l.EthersAdapter],projectId:`8d2e1854d3f1782e45aa15fbd8938894`,allowUnsupportedChain:!0,networks:[s.mainnet,s.bsc,s.polygon,s.arbitrum,s.optimism,s.base],metadata:{name:`Cppay`,description:`Cppay Payment Gateway`,url:window.location.origin,icons:[`https://cppay.com/icon.png`]},features:{analytics:!1}}),N.value=z.value.getAddress(),F.value=z.value.getProvider(`eip155`),z.value.subscribeAccount(e=>{e.isConnected?N.value=e.address:(N.value=void 0,F.value=void 0)}),z.value.subscribeProviders(e=>{F.value=e?.eip155}))},U=async()=>{try{P.value=!0,E(`update:error`,void 0),z.value&&await z.value.open()}catch(e){E(`update:error`,e instanceof Error?e.message:D.value.walletConnectionFailed),E(`error`,e)}finally{P.value=!1}},W=(0,n.computed)(()=>k.value.find(e=>e.chain===A.value)),G=(0,n.computed)(()=>W.value?.tokens||[]),K=(0,n.computed)(()=>G.value.find(e=>e.symbol===j.value)),q=e=>[`USDT`,`USDC`,`BUSD`,`DAI`,`TUSD`,`USDD`,`FDUSD`].includes(e.toUpperCase())?2:6,J=(e,t)=>{let n=parseFloat(e);return isNaN(n)?`0`:n.toFixed(t).replace(/\.?0+$/,``)},tt=e=>{if(e<=0)return D.value.expired;let t=Math.floor(e/3600),n=Math.floor(e%3600/60),r=e%60;return t>0?`${t}${D.value.hours}${n}${D.value.minutes}${r}${D.value.seconds}`:`${n}${D.value.minutes}${r}${D.value.seconds}`},Y=e=>{V.value&&clearInterval(V.value);let t=()=>{let t=e-Math.floor(Date.now()/1e3);I.value=tt(t),t<=0&&(E(`update:paymentStep`,`expired`),X())};t(),V.value=setInterval(t,1e3)},X=()=>{V.value&&=(clearInterval(V.value),null)},Z=(0,n.computed)(()=>{if(!j.value||!K.value)return`0`;let e=parseFloat(K.value.price);if(isNaN(e)||e===0)return`0`;let t=q(j.value);return J((parseFloat(T.amount)/e).toFixed(t),t)}),nt=async()=>{try{E(`update:error`,void 0);let e=await O.getSupportedChains();k.value=e,e.length>0&&(A.value=e[0].chain)}catch(e){E(`update:error`,e instanceof Error?e.message:D.value.loadPaymentNetworkFailed),E(`error`,e)}},Q=async e=>{if(F.value)try{await F.value.request({method:`wallet_switchEthereumChain`,params:[{chainId:`0x${e.toString(16)}`}]})}catch(e){throw e.code===4902?Error(D.value.pleaseAddNetwork):e}},rt=async()=>{if(!(!N.value||!F.value||!H.value||!K.value||!W.value))try{let e=H.value;await Q(W.value.chainId);let t=(0,o.createWalletClient)({account:N.value,transport:(0,o.custom)(F.value)});K.value.address?await t.writeContract({address:K.value.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,o.parseUnits)(e.paymentAmount,K.value.decimals)],chain:null}):await t.sendTransaction({to:e.receiveAddress,value:(0,o.parseUnits)(e.paymentAmount,K.value.decimals),chain:null}),M.value=void 0}catch(e){E(`update:error`,e instanceof Error?e.message:D.value.walletPaymentFailed),E(`error`,e)}},it=async()=>{if(!(!N.value||!F.value||!H.value||!K.value||!W.value))try{let e=H.value;await Q(W.value.chainId);let t=(0,o.createWalletClient)({account:N.value,transport:(0,o.custom)(F.value)});if(!K.value.address)throw Error(D.value.subscriptionDoesNotSupportNative);await t.writeContract({address:K.value.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,o.parseUnits)(e.approveAmount,K.value.decimals)],chain:null}),M.value=void 0}catch(e){E(`update:error`,e instanceof Error?e.message:D.value.walletPaymentFailed),E(`error`,e)}},at=async()=>{if(!N.value){E(`update:error`,D.value.pleaseConnectWallet),E(`error`,Error(D.value.pleaseConnectWallet));return}T.plain===`one-time`?await rt():T.plain===`subscription`&&await it()},ot=async()=>{if(!(!A.value||!j.value))try{E(`update:error`,void 0);let e=``;T.plain===`one-time`?(H.value=await O.createOnetimePayment({paymentChain:A.value,paymentToken:j.value,orderId:T.orderId,amount:Z.value}),e=`${A.value.toLowerCase()}:${H.value.receiveAddress}?amount=${H.value.paymentAmount}`,Y(H.value.expireAt),$({paymentId:H.value.paymentId})):T.plain===`subscription`&&(H.value=await O.createSubscriptionPayment({paymentChain:A.value,paymentToken:j.value,orderId:T.orderId,amountOfUsd:Z.value,intervalDays:T.intervalDays||30}),e=`${A.value.toLowerCase()}:${H.value.spenderAddress}?amount=${H.value.approveAmount}`,Y(H.value.expireAt),$({subscriptionId:H.value.subscriptionId})),E(`update:paymentStep`,`payment`),M.value=await r.toDataURL(e,{width:200,margin:2,errorCorrectionLevel:`H`})}catch(e){E(`update:error`,e instanceof Error?e.message:D.value.createPaymentFailed),E(`error`,e)}},$=e=>{B.value?.unsubscribe();let t=()=>(0,i.defer)(()=>T.plain===`subscription`?O.checkSubscriptionPaymentStatus(e):O.checkOnetimePaymentStatus(e)).pipe((0,a.timeout)(15e3),(0,a.retry)({delay:2e3}));B.value=t().pipe((0,a.expand)(e=>e.status===`pending`?(0,i.timer)(2e3).pipe((0,a.switchMap)(()=>t())):i.EMPTY),(0,a.tap)(e=>{(e.status===`paid`||e.status===`approved`)&&(E(`update:paymentStep`,`success`),E(`success`,e)),e.status===`expired`&&(E(`update:paymentStep`,`expired`),E(`expired`,e)),e.status===`failed`&&(E(`update:paymentStep`,`failed`),E(`failed`,e))})).subscribe({error:e=>{if(H.value){let t=e instanceof Error?e.message:D.value.checkPaymentStatusFailed;E(`update:paymentStep`,`error`),E(`update:error`,t),E(`error`,e)}},complete:()=>{B.value?.unsubscribe()}})},st=async()=>{if(!H.value)return;let e=T.plain===`subscription`?H.value.spenderAddress:H.value.receiveAddress;if(e)try{await navigator.clipboard.writeText(e),L.value=!0,setTimeout(()=>{L.value=!1},2e3)}catch(e){console.error(`复制失败:`,e)}};return(0,n.watch)(()=>T.paymentStep,e=>{e===`select`&&k.value.length===0&&nt()},{immediate:!0}),(0,n.watch)(G,e=>{e.length>0&&!j.value&&(j.value=e[0].symbol)}),(0,n.watch)(()=>T.orderId,e=>{e&&$({orderId:e})}),(0,n.onMounted)(()=>{et()}),(0,n.onUnmounted)(()=>{B.value?.unsubscribe(),X()}),(e,t)=>((0,n.openBlock)(),(0,n.createElementBlock)(`div`,u,[C.paymentStep===`success`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,d,[t[7]||=(0,n.createElementVNode)(`div`,{class:`_cppay-state-icon`},`✅`,-1),(0,n.createElementVNode)(`h3`,f,(0,n.toDisplayString)(C.plain===`subscription`?D.value.authorizationSuccess:D.value.paymentSuccess),1),(0,n.createElementVNode)(`p`,ee,(0,n.toDisplayString)(C.plain===`subscription`?D.value.subscriptionActivated:D.value.transactionCompleted),1)])):C.paymentStep===`expired`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,te,[t[8]||=(0,n.createElementVNode)(`div`,{class:`_cppay-state-icon`},`⏰`,-1),(0,n.createElementVNode)(`h3`,p,(0,n.toDisplayString)(D.value.paymentExpired),1),(0,n.createElementVNode)(`button`,{onClick:t[0]||=t=>e.$emit(`update:paymentStep`,`select`),disabled:C.loading||C.isCheckingPayment,class:`_cppay-btn _cppay-btn-text`},(0,n.toDisplayString)(D.value.returnButton),9,ne)])):C.paymentStep===`error`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,re,[t[9]||=(0,n.createElementVNode)(`div`,{class:`_cppay-state-icon`},`❌`,-1),(0,n.createElementVNode)(`h3`,ie,(0,n.toDisplayString)(D.value.checking),1)])):C.paymentStep===`failed`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,ae,[t[10]||=(0,n.createElementVNode)(`div`,{class:`_cppay-state-icon`},`❌`,-1),(0,n.createElementVNode)(`h3`,oe,(0,n.toDisplayString)(D.value.paymentFailed),1)])):C.paymentStep===`select`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,se,[(0,n.renderSlot)(e.$slots,`choose-top`),(0,n.createElementVNode)(`div`,ce,[(0,n.createElementVNode)(`label`,le,(0,n.toDisplayString)(D.value.paymentNetwork),1),(0,n.createElementVNode)(`div`,ue,[((0,n.openBlock)(!0),(0,n.createElementBlock)(n.Fragment,null,(0,n.renderList)(k.value,e=>((0,n.openBlock)(),(0,n.createElementBlock)(`button`,{key:e.chain,onClick:t=>A.value=e.chain,class:(0,n.normalizeClass)([`_cppay-select-btn`,{"_cppay-selected":A.value===e.chain}])},[e.icon?((0,n.openBlock)(),(0,n.createElementBlock)(`img`,{key:0,src:e.icon,alt:e.chain},null,8,fe)):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(e.chain),1)],10,de))),128))])]),(0,n.createElementVNode)(`div`,pe,[(0,n.createElementVNode)(`label`,me,(0,n.toDisplayString)(D.value.paymentToken),1),(0,n.createElementVNode)(`div`,he,[((0,n.openBlock)(!0),(0,n.createElementBlock)(n.Fragment,null,(0,n.renderList)(G.value,e=>((0,n.openBlock)(),(0,n.createElementBlock)(`button`,{key:e.symbol,onClick:t=>j.value=e.symbol,class:(0,n.normalizeClass)([`_cppay-select-btn`,{"_cppay-selected":j.value===e.symbol}])},[e.icon?((0,n.openBlock)(),(0,n.createElementBlock)(`img`,{key:0,src:e.icon,alt:e.symbol},null,8,_e)):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(e.symbol),1)],10,ge))),128))])]),(0,n.renderSlot)(e.$slots,`choose-bottom`),(0,n.createElementVNode)(`div`,ve,[(0,n.createElementVNode)(`div`,ye,[(0,n.createElementVNode)(`div`,be,[(0,n.createElementVNode)(`span`,xe,(0,n.toDisplayString)(D.value.paymentAmount),1),(0,n.createElementVNode)(`div`,Se,[(0,n.createElementVNode)(`div`,Ce,(0,n.toDisplayString)(Z.value??`-`)+` `+(0,n.toDisplayString)(j.value??``),1),(0,n.createElementVNode)(`div`,we,`≈ $`+(0,n.toDisplayString)(C.amount),1)])])])]),(0,n.createElementVNode)(`div`,Te,[C.error?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,{key:0,class:`_cppay-error-tooltip-wrapper`,onMouseenter:t[1]||=e=>R.value=!0,onMouseleave:t[2]||=e=>R.value=!1},[(0,n.createElementVNode)(`div`,Ee,[t[11]||=(0,n.createElementVNode)(`svg`,{viewBox:`0 0 20 20`,fill:`currentColor`},[(0,n.createElementVNode)(`path`,{"fill-rule":`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`,"clip-rule":`evenodd`})],-1),(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(C.error),1)]),R.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,De,(0,n.toDisplayString)(C.error),1)):(0,n.createCommentVNode)(``,!0)],32)):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`button`,{onClick:ot,disabled:!A.value||!j.value||C.loading,class:`_cppay-btn _cppay-btn-primary`},(0,n.toDisplayString)(C.loading?D.value.processing:D.value.continuePayment),9,Oe)])])):((0,n.openBlock)(),(0,n.createElementBlock)(`div`,ke,[(0,n.createElementVNode)(`div`,Ae,[(0,n.createElementVNode)(`div`,je,[M.value?((0,n.openBlock)(),(0,n.createElementBlock)(`img`,{key:0,src:M.value,alt:`Payment QR Code`,class:`_cppay-qr-image`},null,8,m)):(0,n.createCommentVNode)(``,!0)])]),(0,n.createElementVNode)(`div`,h,[K.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,g,[(0,n.createElementVNode)(`div`,_,[(0,n.createElementVNode)(`div`,v,[(0,n.createElementVNode)(`div`,y,(0,n.toDisplayString)(C.plain===`subscription`?D.value.authorizationAmount:D.value.paymentAmount),1),(0,n.createElementVNode)(`div`,b,[H.value?((0,n.openBlock)(),(0,n.createElementBlock)(`span`,x,(0,n.toDisplayString)(C.plain===`subscription`?`${J(H.value.approveAmount,q(K.value.symbol))} ${K.value.symbol}`:`${J(H.value.paymentAmount,q(K.value.symbol))} ${K.value.symbol}`),1)):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`span`,null,`≈ $`+(0,n.toDisplayString)(C.amount),1)])])])])):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`div`,S,[(0,n.createElementVNode)(`div`,Me,[(0,n.createElementVNode)(`div`,Ne,[(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(C.plain===`subscription`?D.value.authorizationContractAddress:D.value.paymentAddress),1),I.value?((0,n.openBlock)(),(0,n.createElementBlock)(`span`,Pe,`⏰ `+(0,n.toDisplayString)(I.value),1)):(0,n.createCommentVNode)(``,!0)]),(0,n.createElementVNode)(`div`,Fe,[H.value?((0,n.openBlock)(),(0,n.createElementBlock)(`code`,Ie,(0,n.toDisplayString)(C.plain===`subscription`?H.value.spenderAddress:H.value.receiveAddress),1)):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`button`,{onClick:st,class:(0,n.normalizeClass)([`_cppay-copy-btn`,{"_cppay-copy-success":L.value}]),title:L.value?D.value.copied:D.value.copyAddress},[L.value?((0,n.openBlock)(),(0,n.createElementBlock)(`svg`,ze,[...t[13]||=[(0,n.createElementVNode)(`path`,{d:`M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z`},null,-1)]])):((0,n.openBlock)(),(0,n.createElementBlock)(`svg`,Re,[...t[12]||=[(0,n.createElementVNode)(`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`},null,-1),(0,n.createElementVNode)(`rect`,{x:`8`,y:`2`,width:`8`,height:`4`,rx:`1`,ry:`1`},null,-1)]]))],10,Le)])])]),C.plain===`subscription`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,Be,[(0,n.createElementVNode)(`div`,Ve,(0,n.toDisplayString)(D.value.subscriptionNotice),1),(0,n.createElementVNode)(`div`,He,(0,n.toDisplayString)(D.value.subscriptionNoticeMessage),1)])):(0,n.createCommentVNode)(``,!0)]),C.error?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,{key:0,class:`_cppay-error-tooltip-wrapper`,onMouseenter:t[3]||=e=>R.value=!0,onMouseleave:t[4]||=e=>R.value=!1},[(0,n.createElementVNode)(`div`,Ue,[t[14]||=(0,n.createElementVNode)(`svg`,{viewBox:`0 0 20 20`,fill:`currentColor`},[(0,n.createElementVNode)(`path`,{"fill-rule":`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`,"clip-rule":`evenodd`})],-1),(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(C.error),1)]),R.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,We,(0,n.toDisplayString)(C.error),1)):(0,n.createCommentVNode)(``,!0)],32)):(0,n.createCommentVNode)(``,!0),C.isCheckingPayment?(0,n.createCommentVNode)(``,!0):((0,n.openBlock)(),(0,n.createElementBlock)(`div`,Ge,[N.value?((0,n.openBlock)(),(0,n.createElementBlock)(n.Fragment,{key:1},[(0,n.createElementVNode)(`button`,{onClick:at,disabled:C.loading,class:`_cppay-btn _cppay-btn-primary`},(0,n.toDisplayString)(C.loading?C.plain===`subscription`?D.value.authorizing:D.value.processing:(C.plain,D.value.walletPay)),9,qe),(0,n.createElementVNode)(`button`,{onClick:U,disabled:C.loading,class:`_cppay-btn _cppay-wallet-address-btn`,title:D.value.clickToModifyWallet},[(0,n.createElementVNode)(`span`,Ye,(0,n.toDisplayString)(N.value.slice(0,10))+`...`+(0,n.toDisplayString)(N.value.slice(-4)),1),t[15]||=(0,n.createElementVNode)(`svg`,{class:`_cppay-wallet-arrow`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,"stroke-width":`2`},[(0,n.createElementVNode)(`polyline`,{points:`6 9 12 15 18 9`})],-1)],8,Je)],64)):((0,n.openBlock)(),(0,n.createElementBlock)(`button`,{key:0,onClick:U,disabled:P.value,class:`_cppay-btn _cppay-btn-secondary _cppay-connect-wallet-btn`},[(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(P.value?D.value.processing:D.value.connectWallet),1)],8,Ke))])),(0,n.createElementVNode)(`div`,Xe,[(0,n.createElementVNode)(`button`,{onClick:t[5]||=t=>e.$emit(`update:paymentStep`,`payment`),disabled:C.isCheckingPayment,class:`_cppay-btn _cppay-btn-primary`},(0,n.toDisplayString)(C.isCheckingPayment?D.value.checking:D.value.completedPayment),9,Ze)]),t[16]||=(0,n.createElementVNode)(`hr`,{class:`_cppay-divider`},null,-1),(0,n.createElementVNode)(`div`,Qe,[(0,n.createElementVNode)(`button`,{onClick:t[6]||=t=>e.$emit(`update:paymentStep`,`select`),disabled:C.loading||C.isCheckingPayment,class:`_cppay-btn _cppay-btn-text`},(0,n.toDisplayString)(D.value.changePaymentMethod),9,$e)])]))]))}}),w=C,T={class:`_cppay-dialog`},E={class:`_cppay-header`},D={class:`_cppay-title`},O=[`disabled`],k=(0,n.defineComponent)({__name:`PaymentDialog`,props:{modelValue:{type:Boolean},apikey:{},orderId:{},amount:{},plain:{default:`one-time`},intervalDays:{default:30},locale:{default:`zh-CN`}},emits:[`update:modelValue`,`success`,`expired`,`failed`,`error`],setup(t,{emit:r}){let i=t,a=r,o=(0,n.computed)(()=>e.t(i.locale)),s=(0,n.ref)(`select`),c=(0,n.ref)(!1),l=(0,n.ref)(!1),u=(0,n.ref)();(0,n.watch)(()=>s.value,e=>{e===`success`&&setTimeout(()=>{d()},1e3)}),(0,n.watch)(()=>i.modelValue,e=>{e||(s.value=`select`,u.value=void 0,c.value=!1,l.value=!1)}),(0,n.onMounted)(()=>{let e=e=>{e.key===`Escape`&&i.modelValue&&!c.value&&!l.value&&d()};return window.addEventListener(`keydown`,e),()=>{window.removeEventListener(`keydown`,e)}});let d=()=>{c.value||l.value||a(`update:modelValue`,!1)},f=()=>{switch(s.value){case`success`:return o.value.paymentResult;case`expired`:return o.value.paymentExpired;case`failed`:return o.value.paymentFailed;case`error`:return o.value.error;case`payment`:return o.value.completePayment;default:return o.value.selectPaymentMethod}};return(e,r)=>t.modelValue?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,{key:0,class:`_cppay-overlay`,onClick:r[6]||=e=>e.target===e.currentTarget&&!c.value&&!l.value&&d()},[(0,n.createElementVNode)(`div`,T,[(0,n.createElementVNode)(`div`,E,[(0,n.createElementVNode)(`h2`,D,(0,n.toDisplayString)(f()),1),(0,n.createElementVNode)(`button`,{onClick:d,disabled:c.value||l.value,class:`_cppay-close-btn`},[...r[7]||=[(0,n.createElementVNode)(`svg`,{fill:`none`,stroke:`currentColor`,viewBox:`0 0 24 24`},[(0,n.createElementVNode)(`path`,{"stroke-linecap":`round`,"stroke-linejoin":`round`,"stroke-width":`2`,d:`M6 18L18 6M6 6l12 12`})],-1)]],8,O)]),(0,n.createVNode)(w,{apikey:t.apikey,orderId:t.orderId,amount:t.amount,plain:t.plain,intervalDays:t.intervalDays,locale:t.locale,paymentStep:s.value,loading:c.value,isCheckingPayment:l.value,error:u.value,"onUpdate:paymentStep":r[0]||=e=>s.value=e,"onUpdate:error":r[1]||=e=>u.value=e,onSuccess:r[2]||=e=>a(`success`,e),onExpired:r[3]||=e=>a(`expired`,e),onFailed:r[4]||=e=>a(`failed`,e),onError:r[5]||=e=>a(`error`,e)},{"choose-top":(0,n.withCtx)(()=>[(0,n.renderSlot)(e.$slots,`choose-top`)]),"choose-bottom":(0,n.withCtx)(()=>[(0,n.renderSlot)(e.$slots,`choose-bottom`)]),_:3},8,[`apikey`,`orderId`,`amount`,`plain`,`intervalDays`,`locale`,`paymentStep`,`loading`,`isCheckingPayment`,`error`])])])):(0,n.createCommentVNode)(``,!0)}}),A=k,j=(0,n.ref)({apikey:``,open:!1,options:null});const M=e=>{j.value.options=e,j.value.open=!0},N=()=>{j.value.open=!1};var P=()=>{j.value.open=!1},F=e=>{j.value.options?.onSuccess?.(e),setTimeout(()=>{j.value.open=!1},1e3)},I=e=>{j.value.options?.onExpired?.(e)},L=e=>{j.value.options?.onFailed?.(e)},R=e=>{j.value.options?.onError?.(e)};const z={install(e,t){if(!t?.apikey)throw Error(`Cppay plugin requires an apikey`);j.value.apikey=t.apikey,e.config.globalProperties.$showPayment=M,e.config.globalProperties.$closePayment=N;let r=e=>{e.key===`Escape`&&j.value.open&&P()},i=document.createElement(`div`);document.body.appendChild(i),(0,n.createApp)({setup(){return()=>j.value.options?(0,n.h)(A,{modelValue:j.value.open,"onUpdate:modelValue":e=>{j.value.open=e,e||P()},apikey:j.value.apikey,plain:j.value.options.plain,orderId:j.value.options.orderId,amount:j.value.options.amount,intervalDays:j.value.options.intervalDays,onSuccess:F,onExpired:I,onFailed:L,onError:R}):null}}).mount(i),j.value.open&&document.addEventListener(`keydown`,r),e.config.globalProperties.$watchEffect?.(()=>{j.value.open?document.addEventListener(`keydown`,r):document.removeEventListener(`keydown`,r)})}},B=()=>{if(!j.value.apikey)throw Error(`useCppayPayment must be used after installing CppayPlugin`);return{showPayment:M,closePayment:N}};e.n(e.r),exports.CppayPlugin=z,exports.PaymentContent=w,exports.PaymentDialog=A,exports.showPayment=M,exports.useCppayPayment=B;
|
package/dist/vue.d.ts
CHANGED
|
@@ -4,10 +4,86 @@ import { DefineComponent } from '../../vue/dist/vue.esm-bundler.js';
|
|
|
4
4
|
import { Plugin as Plugin_2 } from '../../vue/dist/vue.esm-bundler.js';
|
|
5
5
|
import { PublicProps } from '../../vue/dist/vue.esm-bundler.js';
|
|
6
6
|
|
|
7
|
+
declare const __VLS_component: DefineComponent<Props, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {} & {
|
|
8
|
+
expired: (order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any;
|
|
9
|
+
failed: (order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any;
|
|
10
|
+
error: (error: Error) => any;
|
|
11
|
+
success: (order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any;
|
|
12
|
+
"update:modelValue": (value: boolean) => any;
|
|
13
|
+
}, string, PublicProps, Readonly<Props> & Readonly<{
|
|
14
|
+
onExpired?: ((order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any) | undefined;
|
|
15
|
+
onFailed?: ((order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any) | undefined;
|
|
16
|
+
onError?: ((error: Error) => any) | undefined;
|
|
17
|
+
onSuccess?: ((order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any) | undefined;
|
|
18
|
+
"onUpdate:modelValue"?: ((value: boolean) => any) | undefined;
|
|
19
|
+
}>, {
|
|
20
|
+
intervalDays: number;
|
|
21
|
+
plain: PaymentPlain;
|
|
22
|
+
locale: Locale;
|
|
23
|
+
}, {}, {}, {}, string, ComponentProvideOptions, false, {}, any>;
|
|
24
|
+
|
|
25
|
+
declare const __VLS_component_2: DefineComponent<Props_2, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {} & {
|
|
26
|
+
expired: (order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any;
|
|
27
|
+
failed: (order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any;
|
|
28
|
+
error: (error: Error) => any;
|
|
29
|
+
success: (order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any;
|
|
30
|
+
"update:paymentStep": (value: "expired" | "failed" | "error" | "select" | "payment" | "success") => any;
|
|
31
|
+
"update:error": (value: string | undefined) => any;
|
|
32
|
+
}, string, PublicProps, Readonly<Props_2> & Readonly<{
|
|
33
|
+
onExpired?: ((order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any) | undefined;
|
|
34
|
+
onFailed?: ((order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any) | undefined;
|
|
35
|
+
onError?: ((error: Error) => any) | undefined;
|
|
36
|
+
onSuccess?: ((order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any) | undefined;
|
|
37
|
+
"onUpdate:paymentStep"?: ((value: "expired" | "failed" | "error" | "select" | "payment" | "success") => any) | undefined;
|
|
38
|
+
"onUpdate:error"?: ((value: string | undefined) => any) | undefined;
|
|
39
|
+
}>, {
|
|
40
|
+
plain: PaymentPlain;
|
|
41
|
+
loading: boolean;
|
|
42
|
+
isCheckingPayment: boolean;
|
|
43
|
+
}, {}, {}, {}, string, ComponentProvideOptions, false, {}, HTMLDivElement>;
|
|
44
|
+
|
|
45
|
+
declare function __VLS_template(): {
|
|
46
|
+
attrs: Partial<{}>;
|
|
47
|
+
slots: {
|
|
48
|
+
'choose-top'?(_: {}): any;
|
|
49
|
+
'choose-bottom'?(_: {}): any;
|
|
50
|
+
};
|
|
51
|
+
refs: {};
|
|
52
|
+
rootEl: any;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
declare function __VLS_template_2(): {
|
|
56
|
+
attrs: Partial<{}>;
|
|
57
|
+
slots: {
|
|
58
|
+
'choose-top'?(_: {}): any;
|
|
59
|
+
'choose-bottom'?(_: {}): any;
|
|
60
|
+
};
|
|
61
|
+
refs: {};
|
|
62
|
+
rootEl: HTMLDivElement;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
declare type __VLS_TemplateResult = ReturnType<typeof __VLS_template>;
|
|
66
|
+
|
|
67
|
+
declare type __VLS_TemplateResult_2 = ReturnType<typeof __VLS_template_2>;
|
|
68
|
+
|
|
69
|
+
declare type __VLS_WithTemplateSlots<T, S> = T & {
|
|
70
|
+
new (): {
|
|
71
|
+
$slots: S;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
declare type __VLS_WithTemplateSlots_2<T, S> = T & {
|
|
76
|
+
new (): {
|
|
77
|
+
$slots: S;
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
7
81
|
declare type Address = `0x${string}`;
|
|
8
82
|
|
|
9
83
|
export declare const CppayPlugin: Plugin_2;
|
|
10
84
|
|
|
85
|
+
export declare type Locale = "zh-CN" | "en-US";
|
|
86
|
+
|
|
11
87
|
declare type Numberic = `${number}.${number}` | `${number}`;
|
|
12
88
|
|
|
13
89
|
declare interface OnetimePaymentOrderStatus {
|
|
@@ -22,26 +98,30 @@ declare interface OnetimePaymentOrderStatus {
|
|
|
22
98
|
status: PaymentStatus;
|
|
23
99
|
}
|
|
24
100
|
|
|
25
|
-
export declare const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
success: (order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any;
|
|
29
|
-
}, string, PublicProps, Readonly<Props> & Readonly<{
|
|
30
|
-
onError?: ((error: Error) => any) | undefined;
|
|
31
|
-
"onUpdate:modelValue"?: ((value: boolean) => any) | undefined;
|
|
32
|
-
onSuccess?: ((order: OnetimePaymentOrderStatus | SubscriptionPaymentOrderStatus) => any) | undefined;
|
|
33
|
-
}>, {
|
|
34
|
-
intervalDays: number;
|
|
35
|
-
plain: PaymentPlain;
|
|
36
|
-
}, {}, {}, {}, string, ComponentProvideOptions, false, {}, any>;
|
|
101
|
+
export declare const PaymentContent: __VLS_WithTemplateSlots_2<typeof __VLS_component_2, __VLS_TemplateResult_2["slots"]>;
|
|
102
|
+
|
|
103
|
+
export declare const PaymentDialog: __VLS_WithTemplateSlots<typeof __VLS_component, __VLS_TemplateResult["slots"]>;
|
|
37
104
|
|
|
105
|
+
/**
|
|
106
|
+
* 支付选项配置
|
|
107
|
+
*/
|
|
38
108
|
declare interface PaymentOptions_2 {
|
|
109
|
+
/** 支付类型:一次性支付或订阅支付 */
|
|
39
110
|
plain: PaymentPlain;
|
|
111
|
+
/** 订单 ID */
|
|
40
112
|
orderId: string;
|
|
113
|
+
/** 支付金额(USD) */
|
|
41
114
|
amount: string;
|
|
115
|
+
/** 订阅间隔天数(仅订阅支付需要) */
|
|
42
116
|
intervalDays?: number;
|
|
117
|
+
/** 支付成功回调 */
|
|
43
118
|
onSuccess?: (order: SubscriptionPaymentOrderStatus | OnetimePaymentOrderStatus) => void;
|
|
119
|
+
/** 支付错误回调 */
|
|
44
120
|
onError?: (error: Error) => void;
|
|
121
|
+
/** 支付过期回调 */
|
|
122
|
+
onExpired?: (order: SubscriptionPaymentOrderStatus | OnetimePaymentOrderStatus) => void;
|
|
123
|
+
/** 支付失败回调 */
|
|
124
|
+
onFailed?: (order: SubscriptionPaymentOrderStatus | OnetimePaymentOrderStatus) => void;
|
|
45
125
|
}
|
|
46
126
|
|
|
47
127
|
declare type PaymentPlain = "one-time" | "subscription" | "x402";
|
|
@@ -55,8 +135,37 @@ declare interface Props {
|
|
|
55
135
|
amount: string;
|
|
56
136
|
plain?: PaymentPlain;
|
|
57
137
|
intervalDays?: number;
|
|
138
|
+
locale?: Locale;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
declare interface Props_2 {
|
|
142
|
+
/** API Key */
|
|
143
|
+
apikey: string;
|
|
144
|
+
/** 订单 ID */
|
|
145
|
+
orderId: string;
|
|
146
|
+
/** 支付金额(USD) */
|
|
147
|
+
amount: string;
|
|
148
|
+
/** 支付类型 */
|
|
149
|
+
plain?: PaymentPlain;
|
|
150
|
+
/** 订阅间隔天数 */
|
|
151
|
+
intervalDays?: number;
|
|
152
|
+
/** 支付步骤 */
|
|
153
|
+
paymentStep: "select" | "payment" | "success" | "failed" | "expired" | "error";
|
|
154
|
+
/** 加载状态 */
|
|
155
|
+
loading?: boolean;
|
|
156
|
+
/** 检查支付中状态 */
|
|
157
|
+
isCheckingPayment?: boolean;
|
|
158
|
+
/** 错误信息 */
|
|
159
|
+
error?: string;
|
|
160
|
+
/** 插槽 */
|
|
161
|
+
slots?: any;
|
|
162
|
+
/** 语言设置 */
|
|
163
|
+
locale?: Locale;
|
|
58
164
|
}
|
|
59
165
|
|
|
166
|
+
/**
|
|
167
|
+
* 显示支付对话框
|
|
168
|
+
*/
|
|
60
169
|
export declare const showPayment: (options: PaymentOptions_2) => void;
|
|
61
170
|
|
|
62
171
|
declare interface SubscriptionPaymentOrderStatus {
|
|
@@ -74,8 +183,15 @@ declare interface SubscriptionPaymentOrderStatus {
|
|
|
74
183
|
|
|
75
184
|
declare type SubscriptionPaymentStatus = "pending" | "approved" | "expired" | "failed";
|
|
76
185
|
|
|
186
|
+
/**
|
|
187
|
+
* 使用 CPPay 支付钩子(Vue Composable)
|
|
188
|
+
* @throws {Error} 如果在 CppayPlugin 安装之前使用会抛出错误
|
|
189
|
+
*/
|
|
77
190
|
export declare const useCppayPayment: () => {
|
|
191
|
+
/** 显示支付对话框 */
|
|
78
192
|
showPayment: (options: PaymentOptions_2) => void;
|
|
193
|
+
/** 关闭支付对话框 */
|
|
194
|
+
closePayment: () => void;
|
|
79
195
|
};
|
|
80
196
|
|
|
81
197
|
export { }
|
|
@@ -83,6 +199,9 @@ export { }
|
|
|
83
199
|
|
|
84
200
|
declare module "@vue/runtime-core" {
|
|
85
201
|
interface ComponentCustomProperties {
|
|
202
|
+
/** 显示支付对话框的全局方法 */
|
|
86
203
|
$showPayment: typeof showPayment;
|
|
204
|
+
/** 关闭支付对话框的全局方法 */
|
|
205
|
+
$closePayment: typeof closePayment;
|
|
87
206
|
}
|
|
88
207
|
}
|