cppay-sdk 0.0.2-beta.2 → 0.0.2-beta.20

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.js ADDED
@@ -0,0 +1,327 @@
1
+ import { i as __toESM, n as injectStyle, r as payment_dialog_default, t as require_browser } from "./browser-Dm8HHcNy.js";
2
+ import { t as cppay_default } from "./cppay-LnYTnh4d.js";
3
+ import { EMPTY, defer, timer } from "rxjs";
4
+ import { expand, retry, switchMap, tap, timeout } from "rxjs/operators";
5
+ import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
6
+ import { jsx, jsxs } from "react/jsx-runtime";
7
+ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialog_default = React.memo(({ open: t, onClose: d, apikey: f, plain: p, orderId: b, amount: x, intervalDays: S, onExpired: C, onSuccess: w, onFailed: T, onError: E }) => {
8
+ let D = useMemo(() => new cppay_default(f), [f]), [O, k] = useState("select"), [A, j] = useState(!1), [M, N] = useState([]), [P, F] = useState(""), [I, L] = useState(""), [R, z] = useState(""), B = useRef(null), V = useMemo(() => M.find((t) => t.chain === P), [M, P]), H = useMemo(() => V?.tokens || [], [V]);
9
+ useMemo(() => H.find((t) => t.symbol === I), [H, I]);
10
+ let U = (t) => [
11
+ "USDT",
12
+ "USDC",
13
+ "BUSD",
14
+ "DAI",
15
+ "TUSD",
16
+ "USDD",
17
+ "FDUSD"
18
+ ].includes(t.toUpperCase()) ? 2 : 6, W = (t, d) => {
19
+ let f = parseFloat(t);
20
+ return isNaN(f) ? "0" : f.toFixed(d).replace(/\.?0+$/, "");
21
+ }, G = useMemo(() => {
22
+ if (!I) return "0";
23
+ let t = U(I);
24
+ return W((parseFloat(x) / 1).toFixed(t), t);
25
+ }, [
26
+ x,
27
+ I,
28
+ 1
29
+ ]), K = async () => {
30
+ try {
31
+ j(!0);
32
+ let t = await D.getSupportedChains();
33
+ N(t), t.length > 0 && F(t[0].chain);
34
+ } catch (t) {
35
+ E?.(t);
36
+ } finally {
37
+ j(!1);
38
+ }
39
+ }, q = useRef(null), J = async () => {
40
+ if (!(!P || !I)) try {
41
+ j(!0);
42
+ let t = "";
43
+ p === "one-time" ? (q.current = await D.createOnetimePayment({
44
+ paymentChain: P,
45
+ paymentToken: I,
46
+ orderId: b,
47
+ amount: G
48
+ }), t = `${P.toLowerCase()}:${q.current.receiveAddress}?amount=${q.current.paymentAmount}`) : p === "subscription" && (q.current = await D.createSubscriptionPayment({
49
+ paymentChain: P,
50
+ paymentToken: I,
51
+ orderId: b,
52
+ amountOfUsd: G,
53
+ intervalDays: S || 30
54
+ }), t = `${P.toLowerCase()}:${q.current.spenderAddress}?amount=${q.current.approveAmount}`), k("payment"), z(await import_browser.toDataURL(t, {
55
+ width: 200,
56
+ margin: 2,
57
+ errorCorrectionLevel: "H"
58
+ }));
59
+ } catch (t) {
60
+ E?.(t);
61
+ } finally {
62
+ j(!1);
63
+ }
64
+ }, Y = async () => {
65
+ if (q.current) try {
66
+ j(!0), p === "one-time" && X(q.current.subscriptionId), p === "subscription" && X(q.current.paymentId);
67
+ } catch (t) {
68
+ console.error("支付状态检查错误:", t), E?.(t);
69
+ } finally {
70
+ j(!1);
71
+ }
72
+ }, X = (t) => {
73
+ let d = () => defer(() => p === "subscription" ? D.checkSubscriptionPaymentStatus({ subscriptionId: t }) : D.checkOnetimePaymentStatus({ paymentId: t })).pipe(timeout(15e3), retry({
74
+ count: 3,
75
+ delay: 2e3
76
+ }));
77
+ B.current?.unsubscribe(), B.current = d().pipe(expand((t) => t.status === "pending" ? timer(2e3).pipe(switchMap(() => d())) : EMPTY), tap((t) => {
78
+ t.status === "expired" && C?.(t), t.status === "paid" && (w?.(t), Q()), t.status === "failed" && T?.(t), t.status === "approved" && (w?.(t), Q());
79
+ })).subscribe({ error: (t) => {
80
+ console.error("支付状态检查错误:", t), E?.(t);
81
+ } });
82
+ }, Z = async () => {
83
+ if (!q.current) return;
84
+ let t = p === "subscription" ? q.current.spenderAddress : q.current.receiveAddress;
85
+ t && await navigator.clipboard.writeText(t);
86
+ }, Q = () => {
87
+ d(), setTimeout(() => {
88
+ k("select"), q.current = null;
89
+ }, 300);
90
+ };
91
+ return useEffect(() => {
92
+ t && M.length === 0 && K();
93
+ }, [t]), useEffect(() => {
94
+ H.length > 0 && L(H[0].symbol);
95
+ }, [H]), useEffect(() => () => {
96
+ B.current?.unsubscribe();
97
+ }, []), t ? /* @__PURE__ */ jsx("div", {
98
+ className: "_cppay-overlay",
99
+ onClick: (t) => t.target === t.currentTarget && Q(),
100
+ children: /* @__PURE__ */ jsxs("div", {
101
+ className: "_cppay-dialog",
102
+ children: [/* @__PURE__ */ jsxs("div", {
103
+ className: "_cppay-header",
104
+ children: [/* @__PURE__ */ jsx("h2", {
105
+ className: "_cppay-title",
106
+ children: O === "select" ? "选择支付方式" : "完成支付"
107
+ }), /* @__PURE__ */ jsx("button", {
108
+ onClick: Q,
109
+ disabled: A,
110
+ className: "_cppay-close-btn",
111
+ children: /* @__PURE__ */ jsx("svg", {
112
+ fill: "none",
113
+ stroke: "currentColor",
114
+ viewBox: "0 0 24 24",
115
+ children: /* @__PURE__ */ jsx("path", {
116
+ strokeLinecap: "round",
117
+ strokeLinejoin: "round",
118
+ strokeWidth: 2,
119
+ d: "M6 18L18 6M6 6l12 12"
120
+ })
121
+ })
122
+ })]
123
+ }), /* @__PURE__ */ jsx("div", {
124
+ className: "_cppay-content",
125
+ children: O === "select" ? /* @__PURE__ */ jsxs("div", { children: [
126
+ /* @__PURE__ */ jsxs("div", {
127
+ className: "_cppay-section",
128
+ children: [/* @__PURE__ */ jsx("label", {
129
+ className: "_cppay-label",
130
+ children: "支付网络"
131
+ }), /* @__PURE__ */ jsx("div", {
132
+ className: "_cppay-grid",
133
+ children: M.map((t) => /* @__PURE__ */ jsxs("button", {
134
+ onClick: () => F(t.chain),
135
+ className: `_cppay-select-btn ${P === t.chain ? "_cppay-selected" : ""}`,
136
+ children: [t.icon && /* @__PURE__ */ jsx("img", {
137
+ src: t.icon,
138
+ alt: t.chain
139
+ }), /* @__PURE__ */ jsx("span", { children: t.chain })]
140
+ }, t.chain))
141
+ })]
142
+ }),
143
+ /* @__PURE__ */ jsxs("div", {
144
+ className: "_cppay-section",
145
+ children: [/* @__PURE__ */ jsx("label", {
146
+ className: "_cppay-label",
147
+ children: "支付代币"
148
+ }), /* @__PURE__ */ jsx("div", {
149
+ className: "_cppay-grid",
150
+ children: H.map((t) => /* @__PURE__ */ jsxs("button", {
151
+ onClick: () => L(t.symbol),
152
+ className: `_cppay-select-btn ${I === t.symbol ? "_cppay-selected" : ""}`,
153
+ children: [t.icon && /* @__PURE__ */ jsx("img", {
154
+ src: t.icon,
155
+ alt: t.symbol
156
+ }), /* @__PURE__ */ jsx("span", { children: t.symbol })]
157
+ }, t.symbol))
158
+ })]
159
+ }),
160
+ /* @__PURE__ */ jsx("div", {
161
+ className: "_cppay-section",
162
+ children: /* @__PURE__ */ jsx("div", {
163
+ className: "_cppay-price-box",
164
+ children: /* @__PURE__ */ jsxs("div", {
165
+ className: "_cppay-price-row",
166
+ children: [/* @__PURE__ */ jsx("span", {
167
+ className: "_cppay-price-label",
168
+ children: "支付金额"
169
+ }), /* @__PURE__ */ jsxs("div", {
170
+ className: "_cppay-price-amount",
171
+ children: [/* @__PURE__ */ jsxs("div", {
172
+ className: "_cppay-price-main",
173
+ children: [
174
+ G,
175
+ " ",
176
+ I
177
+ ]
178
+ }), /* @__PURE__ */ jsxs("div", {
179
+ className: "_cppay-price-sub",
180
+ children: ["≈ $", x]
181
+ })]
182
+ })]
183
+ })
184
+ })
185
+ }),
186
+ /* @__PURE__ */ jsx("div", {
187
+ className: "_cppay-section",
188
+ children: /* @__PURE__ */ jsx("button", {
189
+ onClick: J,
190
+ disabled: !P || !I || A,
191
+ className: "_cppay-btn _cppay-btn-primary",
192
+ children: A ? "处理中..." : "继续支付"
193
+ })
194
+ })
195
+ ] }) : /* @__PURE__ */ jsxs("div", { children: [
196
+ /* @__PURE__ */ jsx("div", {
197
+ className: "_cppay-qr-container",
198
+ children: /* @__PURE__ */ jsx("div", {
199
+ className: "_cppay-qr-code",
200
+ style: {
201
+ background: "white",
202
+ padding: "16px",
203
+ borderRadius: "16px"
204
+ },
205
+ children: R && /* @__PURE__ */ jsx("img", {
206
+ src: R,
207
+ alt: "Payment QR Code",
208
+ style: {
209
+ width: "200px",
210
+ height: "200px"
211
+ }
212
+ })
213
+ })
214
+ }),
215
+ /* @__PURE__ */ jsxs("div", {
216
+ className: "_cppay-section",
217
+ children: [
218
+ /* @__PURE__ */ jsxs("div", {
219
+ className: "_cppay-info-box",
220
+ children: [/* @__PURE__ */ jsx("div", {
221
+ className: "_cppay-info-label",
222
+ children: p === "subscription" ? "授权金额" : "支付金额"
223
+ }), /* @__PURE__ */ jsx("div", {
224
+ className: "_cppay-info-value",
225
+ children: p === "subscription" ? `${W(q.current.approveAmount, U(I))} ${I}` : `${W(q.current.paymentAmount, U(I))} ${I}`
226
+ })]
227
+ }),
228
+ /* @__PURE__ */ jsxs("div", {
229
+ className: "_cppay-info-box",
230
+ children: [/* @__PURE__ */ jsx("div", {
231
+ className: "_cppay-info-label",
232
+ children: p === "subscription" ? "授权合约地址" : "支付地址"
233
+ }), /* @__PURE__ */ jsxs("div", {
234
+ className: "_cppay-address-row",
235
+ children: [/* @__PURE__ */ jsx("code", { children: p === "subscription" ? q.current.spenderAddress : q.current.receiveAddress }), /* @__PURE__ */ jsx("button", {
236
+ onClick: Z,
237
+ className: "_cppay-copy-btn",
238
+ title: "复制地址",
239
+ children: /* @__PURE__ */ jsx("svg", {
240
+ fill: "none",
241
+ stroke: "currentColor",
242
+ viewBox: "0 0 24 24",
243
+ children: /* @__PURE__ */ jsx("path", {
244
+ strokeLinecap: "round",
245
+ strokeLinejoin: "round",
246
+ strokeWidth: 2,
247
+ 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"
248
+ })
249
+ })
250
+ })]
251
+ })]
252
+ }),
253
+ p === "subscription" && /* @__PURE__ */ jsxs("div", {
254
+ className: "_cppay-info-box",
255
+ style: {
256
+ background: "#fff3cd",
257
+ borderColor: "#ffc107"
258
+ },
259
+ children: [/* @__PURE__ */ jsx("div", {
260
+ className: "_cppay-info-label",
261
+ style: { color: "#856404" },
262
+ children: "📌 订阅说明"
263
+ }), /* @__PURE__ */ jsx("div", {
264
+ className: "_cppay-info-value",
265
+ style: {
266
+ fontSize: "12px",
267
+ color: "#856404"
268
+ },
269
+ children: "订阅支付需要授权代币给合约地址,系统将按周期自动扣款。授权后无需每次手动支付。"
270
+ })]
271
+ })
272
+ ]
273
+ }),
274
+ /* @__PURE__ */ jsx("div", {
275
+ className: "_cppay-section",
276
+ children: /* @__PURE__ */ jsx("button", {
277
+ onClick: Y,
278
+ disabled: A,
279
+ className: "_cppay-btn _cppay-btn-primary",
280
+ children: A ? "检查中..." : "我已完成支付"
281
+ })
282
+ }),
283
+ /* @__PURE__ */ jsx("div", {
284
+ className: "_cppay-section",
285
+ children: /* @__PURE__ */ jsx("button", {
286
+ onClick: () => k("select"),
287
+ disabled: A,
288
+ className: "_cppay-btn _cppay-btn-secondary",
289
+ children: "更改支付方式"
290
+ })
291
+ })
292
+ ] })
293
+ })]
294
+ })
295
+ }) : null;
296
+ }), CppayContext = createContext(null);
297
+ const CppayProvider = ({ apikey: t, children: d }) => {
298
+ let [f, p] = useState(!1), [m, h] = useState(null), g = useCallback((t) => {
299
+ h(t), p(!0);
300
+ }, []), _ = useCallback(() => {
301
+ p(!1);
302
+ }, []), v = useCallback((t) => {
303
+ m?.onSuccess?.(t), p(!1);
304
+ }, [m]), y = useCallback((t) => {
305
+ m?.onError?.(t);
306
+ }, [m]);
307
+ return /* @__PURE__ */ jsxs(CppayContext.Provider, {
308
+ value: { showPayment: g },
309
+ children: [d, m && /* @__PURE__ */ jsx(PaymentDialog_default, {
310
+ open: f,
311
+ onClose: _,
312
+ apikey: t,
313
+ plain: m.plain,
314
+ orderId: m.orderId,
315
+ amount: m.amount,
316
+ intervalDays: m.intervalDays,
317
+ onSuccess: v,
318
+ onError: y
319
+ })]
320
+ });
321
+ }, useCppayPayment = () => {
322
+ let t = useContext(CppayContext);
323
+ if (!t) throw Error("useCppayPayment must be used within CppayProvider");
324
+ return t;
325
+ };
326
+ injectStyle(payment_dialog_default);
327
+ export { CppayProvider, PaymentDialog_default as PaymentDialog, useCppayPayment };
package/dist/vue.cjs ADDED
@@ -0,0 +1 @@
1
+ const e=require(`./browser-B4fiRjvC.cjs`),t=require(`./cppay-ClXaSwp8.cjs`);let n=require(`vue`),r=require(`rxjs`),i=require(`rxjs/operators`);var a=e.i(e.t(),1),o={class:`_cppay-dialog`},s={class:`_cppay-header`},c={class:`_cppay-title`},l=[`disabled`],u={class:`_cppay-content`},d={key:0},f={class:`_cppay-section`},p={class:`_cppay-grid`},m=[`onClick`],ee=[`src`,`alt`],te={class:`_cppay-section`},ne={class:`_cppay-grid`},re=[`onClick`],ie=[`src`,`alt`],h={class:`_cppay-section`},g={class:`_cppay-price-box`},_={class:`_cppay-price-row`},v={class:`_cppay-price-amount`},y={class:`_cppay-price-main`},b={class:`_cppay-price-sub`},x={class:`_cppay-section`},S=[`disabled`],C={key:1},w={class:`_cppay-qr-container`},T={class:`_cppay-qr-code`,style:{background:`white`,padding:`16px`,"border-radius":`16px`,display:`inline-block`}},E=[`src`],D={class:`_cppay-section`},O={class:`_cppay-info-box`},k={class:`_cppay-info-label`},A={class:`_cppay-info-value`},j={class:`_cppay-info-box`},M={class:`_cppay-info-label`},N={class:`_cppay-address-row`},P={key:0,class:`_cppay-info-box`,style:{background:`#fff3cd`,"border-color":`#ffc107`}},F={class:`_cppay-section`},I=[`disabled`],ae={class:`_cppay-section`},L=[`disabled`],R=(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:R}){let z=e,B=R,V=new t.t(z.apikey),H=(0,n.ref)(`select`),U=(0,n.ref)(!1),W=(0,n.ref)([]),G=(0,n.ref)(``),K=(0,n.ref)(``),q=(0,n.ref)(null),J=(0,n.ref)(``),Y=(0,n.ref)(null),oe=(0,n.computed)(()=>W.value.find(e=>e.chain===G.value)),X=(0,n.computed)(()=>oe.value?.tokens||[]);(0,n.computed)(()=>X.value.find(e=>e.symbol===K.value));let se=(0,n.ref)(1),Z=(0,n.computed)(()=>K.value?(parseFloat(z.amount)/se.value).toFixed(6):`0`),Q=()=>{B(`update:modelValue`,!1),setTimeout(()=>{H.value=`select`,q.value=null},300)},ce=async()=>{try{U.value=!0,W.value=await V.getSupportedChains(),W.value.length>0&&(G.value=W.value[0].chain)}catch(e){B(`error`,e)}finally{U.value=!1}},le=async()=>{if(!(!G.value||!K.value))try{U.value=!0;let e;z.plain===`one-time`?e=await V.createPayment(`one-time`,{paymentChain:G.value,paymentToken:K.value,orderId:z.orderId,amount:Z.value}):z.plain===`subscription`&&(e=await V.createPayment(`subscription`,{paymentChain:G.value,paymentToken:K.value,orderId:z.orderId,amountOfUsd:Z.value,intervalDays:z.intervalDays||30})),q.value={paymentId:e.paymentId||e.subscriptionId,paymentAmount:e.paymentAmount||e.approveAmount,receiveAddress:e.receiveAddress||e.spenderAddress||``};let t=`${G.value.toLowerCase()}:${q.value.receiveAddress}?amount=${q.value.paymentAmount}`;J.value=await a.toDataURL(t,{width:200,margin:2,errorCorrectionLevel:`H`}),H.value=`payment`}catch(e){B(`error`,e)}finally{U.value=!1}},$=e=>{let t=()=>(0,r.defer)(()=>z.plain===`subscription`?V.checkSubscriptionPaymentStatus({subscriptionId:e}):V.checkOnetimePaymentStatus({paymentId:e})).pipe((0,i.timeout)(15e3),(0,i.retry)({count:3,delay:2e3}));Y.value?.unsubscribe(),Y.value=t().pipe((0,i.expand)(e=>e.status===`pending`?(0,r.timer)(2e3).pipe((0,i.switchMap)(()=>t())):r.EMPTY),(0,i.tap)(t=>{t.status===`expired`&&alert(`支付已过期`),t.status===`paid`&&(alert(`支付成功!`),B(`success`,e),Q()),t.status===`failed`&&alert(`支付失败`),t.status===`approved`&&(alert(`订阅成功!`),B(`success`,e),Q())})).subscribe({error:e=>{console.error(`支付状态检查错误:`,e),B(`error`,e)}})},ue=async()=>{if(q.value)try{U.value=!0,$(q.value.paymentId)}catch(e){B(`error`,e)}finally{U.value=!1}},de=async()=>{q.value?.receiveAddress&&(await navigator.clipboard.writeText(q.value.receiveAddress),alert(`地址已复制`))};return(0,n.watch)(()=>z.modelValue,e=>{e&&W.value.length===0&&ce()}),(0,n.watch)(G,()=>{X.value.length>0&&(K.value=X.value[0].symbol)}),(0,n.watch)(H,e=>{e===`payment`&&q.value&&$(q.value.paymentId)}),(0,n.onUnmounted)(()=>{Y.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:(0,n.withModifiers)(Q,[`self`])},[(0,n.createElementVNode)(`div`,o,[(0,n.createElementVNode)(`div`,s,[(0,n.createElementVNode)(`h2`,c,(0,n.toDisplayString)(H.value===`select`?`选择支付方式`:`完成支付`),1),(0,n.createElementVNode)(`button`,{onClick:Q,class:`_cppay-close-btn`,disabled:U.value},[...r[1]||=[(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,l)]),(0,n.createElementVNode)(`div`,u,[H.value===`select`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,d,[(0,n.createElementVNode)(`div`,f,[r[2]||=(0,n.createElementVNode)(`label`,{class:`_cppay-label`},`支付网络`,-1),(0,n.createElementVNode)(`div`,p,[((0,n.openBlock)(!0),(0,n.createElementBlock)(n.Fragment,null,(0,n.renderList)(W.value,e=>((0,n.openBlock)(),(0,n.createElementBlock)(`button`,{key:e.chain,onClick:t=>G.value=e.chain,class:(0,n.normalizeClass)([`_cppay-select-btn`,G.value===e.chain?`_cppay-selected`:``])},[e.icon?((0,n.openBlock)(),(0,n.createElementBlock)(`img`,{key:0,src:e.icon,alt:e.chain},null,8,ee)):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(e.chain),1)],10,m))),128))])]),(0,n.createElementVNode)(`div`,te,[r[3]||=(0,n.createElementVNode)(`label`,{class:`_cppay-label`},`支付代币`,-1),(0,n.createElementVNode)(`div`,ne,[((0,n.openBlock)(!0),(0,n.createElementBlock)(n.Fragment,null,(0,n.renderList)(X.value,e=>((0,n.openBlock)(),(0,n.createElementBlock)(`button`,{key:e.symbol,onClick:t=>K.value=e.symbol,class:(0,n.normalizeClass)([`_cppay-select-btn`,K.value===e.symbol?`_cppay-selected`:``])},[e.icon?((0,n.openBlock)(),(0,n.createElementBlock)(`img`,{key:0,src:e.icon,alt:e.symbol},null,8,ie)):(0,n.createCommentVNode)(``,!0),(0,n.createElementVNode)(`span`,null,(0,n.toDisplayString)(e.symbol),1)],10,re))),128))])]),(0,n.createElementVNode)(`div`,h,[(0,n.createElementVNode)(`div`,g,[(0,n.createElementVNode)(`div`,_,[r[4]||=(0,n.createElementVNode)(`span`,{class:`_cppay-price-label`},`支付金额`,-1),(0,n.createElementVNode)(`div`,v,[(0,n.createElementVNode)(`div`,y,(0,n.toDisplayString)(Z.value)+` `+(0,n.toDisplayString)(K.value),1),(0,n.createElementVNode)(`div`,b,`≈ $`+(0,n.toDisplayString)(e.amount),1)])])])]),(0,n.createElementVNode)(`div`,x,[(0,n.createElementVNode)(`button`,{onClick:le,disabled:!G.value||!K.value||U.value,class:`_cppay-btn _cppay-btn-primary`},(0,n.toDisplayString)(U.value?`处理中...`:`继续支付`),9,S)])])):H.value===`payment`&&q.value?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,C,[(0,n.createElementVNode)(`div`,w,[(0,n.createElementVNode)(`div`,T,[J.value?((0,n.openBlock)(),(0,n.createElementBlock)(`img`,{key:0,src:J.value,alt:`QR Code`,style:{width:`200px`,height:`200px`}},null,8,E)):(0,n.createCommentVNode)(``,!0)])]),(0,n.createElementVNode)(`div`,D,[(0,n.createElementVNode)(`div`,O,[(0,n.createElementVNode)(`div`,k,(0,n.toDisplayString)(z.plain===`subscription`?`授权金额`:`支付金额`),1),(0,n.createElementVNode)(`div`,A,(0,n.toDisplayString)(q.value.paymentAmount)+` `+(0,n.toDisplayString)(K.value),1)]),(0,n.createElementVNode)(`div`,j,[(0,n.createElementVNode)(`div`,M,(0,n.toDisplayString)(z.plain===`subscription`?`授权合约地址`:`支付地址`),1),(0,n.createElementVNode)(`div`,N,[(0,n.createElementVNode)(`code`,null,(0,n.toDisplayString)(q.value.receiveAddress),1),(0,n.createElementVNode)(`button`,{onClick:de,class:`_cppay-copy-btn`,title:`复制地址`},[...r[5]||=[(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)]])])]),z.plain===`subscription`?((0,n.openBlock)(),(0,n.createElementBlock)(`div`,P,[...r[6]||=[(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)]),(0,n.createElementVNode)(`div`,F,[(0,n.createElementVNode)(`button`,{onClick:ue,disabled:U.value,class:`_cppay-btn _cppay-btn-primary`},(0,n.toDisplayString)(U.value?`检查支付状态...`:`我已完成支付`),9,I)]),(0,n.createElementVNode)(`div`,ae,[(0,n.createElementVNode)(`button`,{onClick:r[0]||=e=>H.value=`select`,disabled:U.value,class:`_cppay-btn _cppay-btn-secondary`},` 更改支付方式 `,8,L)])])):(0,n.createCommentVNode)(``,!0)])])])):(0,n.createCommentVNode)(``,!0)]),_:1})]))}}),z=(e,t)=>{let n=e.__vccOpts||e;for(let[e,r]of t)n[e]=r;return n},B=z(R,[[`__scopeId`,`data-v-681ced40`]]);e.n(e.r),exports.PaymentDialog=B;
package/dist/vue.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { ComponentOptionsMixin } from 'vue';
2
+ import { ComponentProvideOptions } from 'vue';
3
+ import { DefineComponent } from 'vue';
4
+ import { PublicProps } from 'vue';
5
+
6
+ export declare const PaymentDialog: DefineComponent<Props, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {} & {
7
+ error: (error: Error) => any;
8
+ "update:modelValue": (value: boolean) => any;
9
+ success: (paymentId: string) => any;
10
+ }, string, PublicProps, Readonly<Props> & Readonly<{
11
+ onError?: ((error: Error) => any) | undefined;
12
+ "onUpdate:modelValue"?: ((value: boolean) => any) | undefined;
13
+ onSuccess?: ((paymentId: string) => any) | undefined;
14
+ }>, {
15
+ intervalDays: number;
16
+ plain: "one-time" | "subscription";
17
+ }, {}, {}, {}, string, ComponentProvideOptions, false, {}, any>;
18
+
19
+ declare interface Props {
20
+ modelValue: boolean;
21
+ apikey: string;
22
+ orderId: string;
23
+ amount: string;
24
+ plain?: "one-time" | "subscription";
25
+ intervalDays?: number;
26
+ }
27
+
28
+ export { }
package/dist/vue.js ADDED
@@ -0,0 +1,211 @@
1
+ import { i as __toESM, n as injectStyle, r as payment_dialog_default, t as require_browser } from "./browser-Dm8HHcNy.js";
2
+ import { t as cppay_default } from "./cppay-LnYTnh4d.js";
3
+ import { Fragment, Teleport, Transition, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, normalizeClass, onUnmounted, openBlock, ref, renderList, toDisplayString, watch, withCtx, withModifiers } from "vue";
4
+ import { EMPTY, defer, timer } from "rxjs";
5
+ import { expand, retry, switchMap, tap, timeout } from "rxjs/operators";
6
+ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), _hoisted_1 = { class: "_cppay-dialog" }, _hoisted_2 = { class: "_cppay-header" }, _hoisted_3 = { class: "_cppay-title" }, _hoisted_4 = ["disabled"], _hoisted_5 = { class: "_cppay-content" }, _hoisted_6 = { key: 0 }, _hoisted_7 = { class: "_cppay-section" }, _hoisted_8 = { class: "_cppay-grid" }, _hoisted_9 = ["onClick"], _hoisted_10 = ["src", "alt"], _hoisted_11 = { class: "_cppay-section" }, _hoisted_12 = { class: "_cppay-grid" }, _hoisted_13 = ["onClick"], _hoisted_14 = ["src", "alt"], _hoisted_15 = { class: "_cppay-section" }, _hoisted_16 = { class: "_cppay-price-box" }, _hoisted_17 = { class: "_cppay-price-row" }, _hoisted_18 = { class: "_cppay-price-amount" }, _hoisted_19 = { class: "_cppay-price-main" }, _hoisted_20 = { class: "_cppay-price-sub" }, _hoisted_21 = { class: "_cppay-section" }, _hoisted_22 = ["disabled"], _hoisted_23 = { key: 1 }, _hoisted_24 = { class: "_cppay-qr-container" }, _hoisted_25 = {
7
+ class: "_cppay-qr-code",
8
+ style: {
9
+ background: "white",
10
+ padding: "16px",
11
+ "border-radius": "16px",
12
+ display: "inline-block"
13
+ }
14
+ }, _hoisted_26 = ["src"], _hoisted_27 = { class: "_cppay-section" }, _hoisted_28 = { class: "_cppay-info-box" }, _hoisted_29 = { class: "_cppay-info-label" }, _hoisted_30 = { class: "_cppay-info-value" }, _hoisted_31 = { class: "_cppay-info-box" }, _hoisted_32 = { class: "_cppay-info-label" }, _hoisted_33 = { class: "_cppay-address-row" }, _hoisted_34 = {
15
+ key: 0,
16
+ class: "_cppay-info-box",
17
+ style: {
18
+ background: "#fff3cd",
19
+ "border-color": "#ffc107"
20
+ }
21
+ }, _hoisted_35 = { class: "_cppay-section" }, _hoisted_36 = ["disabled"], _hoisted_37 = { class: "_cppay-section" }, _hoisted_38 = ["disabled"], PaymentDialog_default = /* @__PURE__ */ ((r, M) => {
22
+ let N = r.__vccOpts || r;
23
+ for (let [r, P] of M) N[r] = P;
24
+ return N;
25
+ })(/* @__PURE__ */ defineComponent({
26
+ __name: "PaymentDialog",
27
+ props: {
28
+ modelValue: { type: Boolean },
29
+ apikey: {},
30
+ orderId: {},
31
+ amount: {},
32
+ plain: { default: "one-time" },
33
+ intervalDays: { default: 30 }
34
+ },
35
+ emits: [
36
+ "update:modelValue",
37
+ "success",
38
+ "error"
39
+ ],
40
+ setup(r, { emit: M }) {
41
+ let N = r, P = M, F = new cppay_default(N.apikey), I = ref("select"), L = ref(!1), R = ref([]), z = ref(""), B = ref(""), V = ref(null), H = ref(""), U = ref(null), W = computed(() => R.value.find((r) => r.chain === z.value)), G = computed(() => W.value?.tokens || []);
42
+ computed(() => G.value.find((r) => r.symbol === B.value));
43
+ let K = ref(1), q = computed(() => B.value ? (parseFloat(N.amount) / K.value).toFixed(6) : "0"), J = () => {
44
+ P("update:modelValue", !1), setTimeout(() => {
45
+ I.value = "select", V.value = null;
46
+ }, 300);
47
+ }, Y = async () => {
48
+ try {
49
+ L.value = !0, R.value = await F.getSupportedChains(), R.value.length > 0 && (z.value = R.value[0].chain);
50
+ } catch (r) {
51
+ P("error", r);
52
+ } finally {
53
+ L.value = !1;
54
+ }
55
+ }, X = async () => {
56
+ if (!(!z.value || !B.value)) try {
57
+ L.value = !0;
58
+ let r;
59
+ N.plain === "one-time" ? r = await F.createPayment("one-time", {
60
+ paymentChain: z.value,
61
+ paymentToken: B.value,
62
+ orderId: N.orderId,
63
+ amount: q.value
64
+ }) : N.plain === "subscription" && (r = await F.createPayment("subscription", {
65
+ paymentChain: z.value,
66
+ paymentToken: B.value,
67
+ orderId: N.orderId,
68
+ amountOfUsd: q.value,
69
+ intervalDays: N.intervalDays || 30
70
+ })), V.value = {
71
+ paymentId: r.paymentId || r.subscriptionId,
72
+ paymentAmount: r.paymentAmount || r.approveAmount,
73
+ receiveAddress: r.receiveAddress || r.spenderAddress || ""
74
+ };
75
+ let M = `${z.value.toLowerCase()}:${V.value.receiveAddress}?amount=${V.value.paymentAmount}`;
76
+ H.value = await import_browser.toDataURL(M, {
77
+ width: 200,
78
+ margin: 2,
79
+ errorCorrectionLevel: "H"
80
+ }), I.value = "payment";
81
+ } catch (r) {
82
+ P("error", r);
83
+ } finally {
84
+ L.value = !1;
85
+ }
86
+ }, Z = (r) => {
87
+ let M = () => defer(() => N.plain === "subscription" ? F.checkSubscriptionPaymentStatus({ subscriptionId: r }) : F.checkOnetimePaymentStatus({ paymentId: r })).pipe(timeout(15e3), retry({
88
+ count: 3,
89
+ delay: 2e3
90
+ }));
91
+ U.value?.unsubscribe(), U.value = M().pipe(expand((r) => r.status === "pending" ? timer(2e3).pipe(switchMap(() => M())) : EMPTY), tap((M) => {
92
+ M.status === "expired" && alert("支付已过期"), M.status === "paid" && (alert("支付成功!"), P("success", r), J()), M.status === "failed" && alert("支付失败"), M.status === "approved" && (alert("订阅成功!"), P("success", r), J());
93
+ })).subscribe({ error: (r) => {
94
+ console.error("支付状态检查错误:", r), P("error", r);
95
+ } });
96
+ }, Q = async () => {
97
+ if (V.value) try {
98
+ L.value = !0, Z(V.value.paymentId);
99
+ } catch (r) {
100
+ P("error", r);
101
+ } finally {
102
+ L.value = !1;
103
+ }
104
+ }, $ = async () => {
105
+ V.value?.receiveAddress && (await navigator.clipboard.writeText(V.value.receiveAddress), alert("地址已复制"));
106
+ };
107
+ return watch(() => N.modelValue, (r) => {
108
+ r && R.value.length === 0 && Y();
109
+ }), watch(z, () => {
110
+ G.value.length > 0 && (B.value = G.value[0].symbol);
111
+ }), watch(I, (r) => {
112
+ r === "payment" && V.value && Z(V.value.paymentId);
113
+ }), onUnmounted(() => {
114
+ U.value?.unsubscribe();
115
+ }), (M, P) => (openBlock(), createBlock(Teleport, { to: "body" }, [createVNode(Transition, { name: "_cppay-fade" }, {
116
+ default: withCtx(() => [r.modelValue ? (openBlock(), createElementBlock("div", {
117
+ key: 0,
118
+ class: "_cppay-overlay",
119
+ onClick: withModifiers(J, ["self"])
120
+ }, [createElementVNode("div", _hoisted_1, [createElementVNode("div", _hoisted_2, [createElementVNode("h2", _hoisted_3, toDisplayString(I.value === "select" ? "选择支付方式" : "完成支付"), 1), createElementVNode("button", {
121
+ onClick: J,
122
+ class: "_cppay-close-btn",
123
+ disabled: L.value
124
+ }, [...P[1] ||= [createElementVNode("svg", {
125
+ fill: "none",
126
+ stroke: "currentColor",
127
+ viewBox: "0 0 24 24"
128
+ }, [createElementVNode("path", {
129
+ "stroke-linecap": "round",
130
+ "stroke-linejoin": "round",
131
+ "stroke-width": "2",
132
+ d: "M6 18L18 6M6 6l12 12"
133
+ })], -1)]], 8, _hoisted_4)]), createElementVNode("div", _hoisted_5, [I.value === "select" ? (openBlock(), createElementBlock("div", _hoisted_6, [
134
+ createElementVNode("div", _hoisted_7, [P[2] ||= createElementVNode("label", { class: "_cppay-label" }, "支付网络", -1), createElementVNode("div", _hoisted_8, [(openBlock(!0), createElementBlock(Fragment, null, renderList(R.value, (r) => (openBlock(), createElementBlock("button", {
135
+ key: r.chain,
136
+ onClick: (M) => z.value = r.chain,
137
+ class: normalizeClass(["_cppay-select-btn", z.value === r.chain ? "_cppay-selected" : ""])
138
+ }, [r.icon ? (openBlock(), createElementBlock("img", {
139
+ key: 0,
140
+ src: r.icon,
141
+ alt: r.chain
142
+ }, null, 8, _hoisted_10)) : createCommentVNode("", !0), createElementVNode("span", null, toDisplayString(r.chain), 1)], 10, _hoisted_9))), 128))])]),
143
+ createElementVNode("div", _hoisted_11, [P[3] ||= createElementVNode("label", { class: "_cppay-label" }, "支付代币", -1), createElementVNode("div", _hoisted_12, [(openBlock(!0), createElementBlock(Fragment, null, renderList(G.value, (r) => (openBlock(), createElementBlock("button", {
144
+ key: r.symbol,
145
+ onClick: (M) => B.value = r.symbol,
146
+ class: normalizeClass(["_cppay-select-btn", B.value === r.symbol ? "_cppay-selected" : ""])
147
+ }, [r.icon ? (openBlock(), createElementBlock("img", {
148
+ key: 0,
149
+ src: r.icon,
150
+ alt: r.symbol
151
+ }, null, 8, _hoisted_14)) : createCommentVNode("", !0), createElementVNode("span", null, toDisplayString(r.symbol), 1)], 10, _hoisted_13))), 128))])]),
152
+ createElementVNode("div", _hoisted_15, [createElementVNode("div", _hoisted_16, [createElementVNode("div", _hoisted_17, [P[4] ||= createElementVNode("span", { class: "_cppay-price-label" }, "支付金额", -1), createElementVNode("div", _hoisted_18, [createElementVNode("div", _hoisted_19, toDisplayString(q.value) + " " + toDisplayString(B.value), 1), createElementVNode("div", _hoisted_20, "≈ $" + toDisplayString(r.amount), 1)])])])]),
153
+ createElementVNode("div", _hoisted_21, [createElementVNode("button", {
154
+ onClick: X,
155
+ disabled: !z.value || !B.value || L.value,
156
+ class: "_cppay-btn _cppay-btn-primary"
157
+ }, toDisplayString(L.value ? "处理中..." : "继续支付"), 9, _hoisted_22)])
158
+ ])) : I.value === "payment" && V.value ? (openBlock(), createElementBlock("div", _hoisted_23, [
159
+ createElementVNode("div", _hoisted_24, [createElementVNode("div", _hoisted_25, [H.value ? (openBlock(), createElementBlock("img", {
160
+ key: 0,
161
+ src: H.value,
162
+ alt: "QR Code",
163
+ style: {
164
+ width: "200px",
165
+ height: "200px"
166
+ }
167
+ }, null, 8, _hoisted_26)) : createCommentVNode("", !0)])]),
168
+ createElementVNode("div", _hoisted_27, [
169
+ createElementVNode("div", _hoisted_28, [createElementVNode("div", _hoisted_29, toDisplayString(N.plain === "subscription" ? "授权金额" : "支付金额"), 1), createElementVNode("div", _hoisted_30, toDisplayString(V.value.paymentAmount) + " " + toDisplayString(B.value), 1)]),
170
+ createElementVNode("div", _hoisted_31, [createElementVNode("div", _hoisted_32, toDisplayString(N.plain === "subscription" ? "授权合约地址" : "支付地址"), 1), createElementVNode("div", _hoisted_33, [createElementVNode("code", null, toDisplayString(V.value.receiveAddress), 1), createElementVNode("button", {
171
+ onClick: $,
172
+ class: "_cppay-copy-btn",
173
+ title: "复制地址"
174
+ }, [...P[5] ||= [createElementVNode("svg", {
175
+ fill: "none",
176
+ stroke: "currentColor",
177
+ viewBox: "0 0 24 24"
178
+ }, [createElementVNode("path", {
179
+ "stroke-linecap": "round",
180
+ "stroke-linejoin": "round",
181
+ "stroke-width": "2",
182
+ 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"
183
+ })], -1)]])])]),
184
+ N.plain === "subscription" ? (openBlock(), createElementBlock("div", _hoisted_34, [...P[6] ||= [createElementVNode("div", {
185
+ class: "_cppay-info-label",
186
+ style: { color: "#856404" }
187
+ }, "📌 订阅说明", -1), createElementVNode("div", {
188
+ class: "_cppay-info-value",
189
+ style: {
190
+ "font-size": "12px",
191
+ color: "#856404"
192
+ }
193
+ }, " 订阅支付需要授权代币给合约地址,系统将按周期自动扣款。授权后无需每次手动支付。 ", -1)]])) : createCommentVNode("", !0)
194
+ ]),
195
+ createElementVNode("div", _hoisted_35, [createElementVNode("button", {
196
+ onClick: Q,
197
+ disabled: L.value,
198
+ class: "_cppay-btn _cppay-btn-primary"
199
+ }, toDisplayString(L.value ? "检查支付状态..." : "我已完成支付"), 9, _hoisted_36)]),
200
+ createElementVNode("div", _hoisted_37, [createElementVNode("button", {
201
+ onClick: P[0] ||= (r) => I.value = "select",
202
+ disabled: L.value,
203
+ class: "_cppay-btn _cppay-btn-secondary"
204
+ }, " 更改支付方式 ", 8, _hoisted_38)])
205
+ ])) : createCommentVNode("", !0)])])])) : createCommentVNode("", !0)]),
206
+ _: 1
207
+ })]));
208
+ }
209
+ }), [["__scopeId", "data-v-681ced40"]]);
210
+ injectStyle(payment_dialog_default);
211
+ export { PaymentDialog_default as PaymentDialog };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "cppay-sdk",
3
- "version": "0.0.2-beta.2",
3
+ "version": "0.0.2-beta.20",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite",
7
- "build": "vite build",
7
+ "build": "rm -rf dist && vite build",
8
8
  "test": "vitest",
9
9
  "preview": "vite preview"
10
10
  },
@@ -12,15 +12,56 @@
12
12
  "dist",
13
13
  "README.md"
14
14
  ],
15
- "main": "./dist/index.js",
15
+ "main": "./dist/index.cjs",
16
16
  "module": "./dist/index.js",
17
17
  "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js",
21
+ "require": "./dist/index.cjs",
22
+ "types": "./dist/index.d.ts"
23
+ },
24
+ "./vue": {
25
+ "import": "./dist/vue.js",
26
+ "require": "./dist/vue.cjs",
27
+ "types": "./dist/vue.d.ts"
28
+ },
29
+ "./react": {
30
+ "import": "./dist/react.js",
31
+ "require": "./dist/react.cjs",
32
+ "types": "./dist/react.d.ts"
33
+ }
34
+ },
35
+ "peerDependencies": {
36
+ "react": ">=16.8.0",
37
+ "react-dom": ">=16.8.0",
38
+ "vue": "^3.0.0"
39
+ },
40
+ "peerDependenciesMeta": {
41
+ "react": {
42
+ "optional": true
43
+ },
44
+ "react-dom": {
45
+ "optional": true
46
+ },
47
+ "vue": {
48
+ "optional": true
49
+ }
50
+ },
18
51
  "devDependencies": {
19
52
  "@types/node": "^25.0.8",
53
+ "@types/qrcode": "^1",
54
+ "@types/react": "^19",
55
+ "@types/react-dom": "^19",
56
+ "@vitejs/plugin-react": "^5.1.2",
57
+ "@vitejs/plugin-vue": "^6.0.3",
58
+ "react": "^19.2.3",
59
+ "react-dom": "^19.2.3",
20
60
  "typescript": "~5.9.3",
21
61
  "vite": "npm:rolldown-vite@7.2.5",
22
62
  "vite-plugin-dts": "^4.5.4",
23
- "vitest": "^4.0.17"
63
+ "vitest": "^4.0.17",
64
+ "vue": "^3.5.26"
24
65
  },
25
66
  "resolutions": {
26
67
  "vite": "npm:rolldown-vite@7.2.5"
@@ -30,5 +71,9 @@
30
71
  "author": "",
31
72
  "license": "MIT",
32
73
  "repository": "",
33
- "keywords": []
74
+ "keywords": [],
75
+ "dependencies": {
76
+ "qrcode": "^1.5.4",
77
+ "rxjs": "^7.8.2"
78
+ }
34
79
  }