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

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 CHANGED
@@ -1,13 +1,14 @@
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";
1
+ import { a as payment_dialog_default, i as injectStyle, n as metamask_default, o as __toESM, r as require_browser, t as walletconnect_default } from "./walletconnect-B0L70Mgc.js";
2
+ import { t as cppay_default } from "./cppay-BcCDwXlg.js";
3
3
  import { EMPTY, defer, timer } from "rxjs";
4
4
  import { expand, retry, switchMap, tap, timeout } from "rxjs/operators";
5
+ import { createWalletClient, custom, parseUnits } from "viem";
6
+ import { arbitrum, base, bsc, mainnet, optimism, polygon } from "viem/chains";
7
+ import EthereumProvider from "@walletconnect/ethereum-provider";
5
8
  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) => [
9
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
10
+ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialog_default = React.memo(({ open: e, onClose: t, apikey: r, plain: i, orderId: l, amount: u, intervalDays: d, onExpired: we, onSuccess: f, onFailed: p, onError: m }) => {
11
+ let h = useMemo(() => new cppay_default(r), [r]), [g, _] = useState("select"), [v, y] = useState(!1), [b, x] = useState([]), [S, C] = useState(""), [w, T] = useState(""), [E, D] = useState(""), [O, k] = useState(""), [A, j] = useState(!1), [M, N] = useState(null), P = useRef(null), F = useRef(null), I = useMemo(() => b.find((e) => e.chain === S), [b, S]), L = useMemo(() => I?.tokens || [], [I]), R = useMemo(() => L.find((e) => e.symbol === w), [L, w]), z = (e) => [
11
12
  "USDT",
12
13
  "USDC",
13
14
  "BUSD",
@@ -15,98 +16,258 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
15
16
  "TUSD",
16
17
  "USDD",
17
18
  "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);
19
+ ].includes(e.toUpperCase()) ? 2 : 6, B = (e) => ({
20
+ ETH: mainnet,
21
+ BSC: bsc,
22
+ Polygon: polygon,
23
+ Arbitrum: arbitrum,
24
+ Optimism: optimism,
25
+ Base: base
26
+ })[e] || mainnet, V = (e, t) => {
27
+ let n = parseFloat(e);
28
+ return isNaN(n) ? "0" : n.toFixed(t).replace(/\.?0+$/, "");
29
+ }, H = useMemo(() => {
30
+ if (!w || !R) return "0";
31
+ let e = parseFloat(R.price);
32
+ if (isNaN(e) || e === 0) return "0";
33
+ let t = z(w);
34
+ return V((parseFloat(u) / e).toFixed(t), t);
25
35
  }, [
26
- x,
27
- I,
28
- 1
29
- ]), K = async () => {
36
+ u,
37
+ w,
38
+ R
39
+ ]), U = async () => {
30
40
  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);
41
+ y(!0);
42
+ let e = await h.getSupportedChains();
43
+ x(e), e.length > 0 && C(e[0].chain);
44
+ } catch (e) {
45
+ m?.(e);
46
+ } finally {
47
+ y(!1);
48
+ }
49
+ }, W = async (e) => {
50
+ try {
51
+ if (j(!0), e === "metamask") {
52
+ if (typeof window < "u" && window.ethereum?.isMetaMask) {
53
+ let e = await window.ethereum.request({ method: "eth_requestAccounts" });
54
+ if (e && e.length > 0) {
55
+ k(e[0]), N("metamask");
56
+ return;
57
+ }
58
+ }
59
+ throw Error("请安装 MetaMask 扩展");
60
+ }
61
+ if (e === "walletconnect") {
62
+ let e = await EthereumProvider.init({
63
+ projectId: "8d2e1854d3f1782e45aa15fbd8938894",
64
+ chains: [1],
65
+ showQrModal: !0,
66
+ optionalChains: [
67
+ 56,
68
+ 137,
69
+ 42161,
70
+ 10,
71
+ 8453
72
+ ],
73
+ methods: [
74
+ "eth_sendTransaction",
75
+ "eth_signTransaction",
76
+ "eth_sign",
77
+ "personal_sign",
78
+ "eth_signTypedData"
79
+ ],
80
+ events: ["chainChanged", "accountsChanged"],
81
+ metadata: {
82
+ name: "Cppay",
83
+ description: "Cppay Payment Gateway",
84
+ url: typeof window < "u" ? window.location.origin : "https://cppay.com",
85
+ icons: ["https://cppay.com/icon.png"]
86
+ },
87
+ rpcMap: {
88
+ 1: "https://ethereum.publicnode.com",
89
+ 56: "https://bsc-dataseed.binance.org",
90
+ 137: "https://polygon-rpc.com",
91
+ 42161: "https://arb1.arbitrum.io/rpc",
92
+ 10: "https://mainnet.optimism.io",
93
+ 8453: "https://mainnet.base.org"
94
+ }
95
+ });
96
+ await e.enable();
97
+ let t = await e.request({ method: "eth_accounts" });
98
+ t && t.length > 0 && (k(t[0]), N("walletconnect"), P.current = e);
99
+ }
100
+ } catch (e) {
101
+ console.error("钱包连接失败:", e), m?.(e);
36
102
  } finally {
37
103
  j(!1);
38
104
  }
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, {
105
+ }, G = async (e) => {
106
+ let t;
107
+ if (t = M === "walletconnect" ? P.current : window.ethereum, t) try {
108
+ await t.request({
109
+ method: "wallet_switchEthereumChain",
110
+ params: [{ chainId: `0x${e.toString(16)}` }]
111
+ });
112
+ } catch (e) {
113
+ throw e.code === 4902 ? Error("请在钱包中添加该网络") : e;
114
+ }
115
+ }, K = async () => {
116
+ if (!(!O || !Y.current || !R)) try {
117
+ y(!0);
118
+ let e = Y.current, t = B(S);
119
+ await G(I.chainId);
120
+ let n = createWalletClient({
121
+ account: O,
122
+ chain: t,
123
+ transport: custom(M === "walletconnect" ? P.current : window.ethereum)
124
+ });
125
+ if (R.address) {
126
+ let t = await n.writeContract({
127
+ address: R.address,
128
+ abi: [{
129
+ name: "transfer",
130
+ type: "function",
131
+ stateMutability: "nonpayable",
132
+ inputs: [{
133
+ name: "to",
134
+ type: "address"
135
+ }, {
136
+ name: "amount",
137
+ type: "uint256"
138
+ }],
139
+ outputs: [{ type: "bool" }]
140
+ }],
141
+ functionName: "transfer",
142
+ args: [e.receiveAddress, parseUnits(e.paymentAmount, R.decimals)],
143
+ chain: null
144
+ });
145
+ console.log("转账交易哈希:", t);
146
+ } else {
147
+ let t = await n.sendTransaction({
148
+ to: e.receiveAddress,
149
+ value: parseUnits(e.paymentAmount, R.decimals),
150
+ chain: null
151
+ });
152
+ console.log("转账交易哈希:", t);
153
+ }
154
+ Z(e.paymentId);
155
+ } catch (e) {
156
+ console.error("钱包支付失败:", e), m?.(e);
157
+ } finally {
158
+ y(!1);
159
+ }
160
+ }, q = async () => {
161
+ if (!(!O || !Y.current || !R)) try {
162
+ y(!0);
163
+ let e = Y.current, t = B(S);
164
+ await G(I.chainId);
165
+ let n = createWalletClient({
166
+ account: O,
167
+ chain: t,
168
+ transport: custom(M === "walletconnect" ? P.current : window.ethereum)
169
+ });
170
+ if (!R.address) throw Error("订阅支付不支持原生代币");
171
+ let r = await n.writeContract({
172
+ address: R.address,
173
+ abi: [{
174
+ name: "approve",
175
+ type: "function",
176
+ stateMutability: "nonpayable",
177
+ inputs: [{
178
+ name: "spender",
179
+ type: "address"
180
+ }, {
181
+ name: "amount",
182
+ type: "uint256"
183
+ }],
184
+ outputs: [{ type: "bool" }]
185
+ }],
186
+ functionName: "approve",
187
+ args: [e.spenderAddress, parseUnits(e.approveAmount, R.decimals)],
188
+ chain: null
189
+ });
190
+ console.log("授权交易哈希:", r), Z(e.subscriptionId);
191
+ } catch (e) {
192
+ console.error("钱包授权失败:", e), m?.(e);
193
+ } finally {
194
+ y(!1);
195
+ }
196
+ }, J = async () => {
197
+ if (!O) {
198
+ m?.(/* @__PURE__ */ Error("请先连接钱包"));
199
+ return;
200
+ }
201
+ i === "one-time" ? await K() : i === "subscription" && await q();
202
+ }, Y = useRef(null), X = async () => {
203
+ if (!(!S || !w)) try {
204
+ y(!0);
205
+ let e = "";
206
+ i === "one-time" ? (Y.current = await h.createOnetimePayment({
207
+ paymentChain: S,
208
+ paymentToken: w,
209
+ orderId: l,
210
+ amount: H
211
+ }), e = `${S.toLowerCase()}:${Y.current.receiveAddress}?amount=${Y.current.paymentAmount}`) : i === "subscription" && (Y.current = await h.createSubscriptionPayment({
212
+ paymentChain: S,
213
+ paymentToken: w,
214
+ orderId: l,
215
+ amountOfUsd: H,
216
+ intervalDays: d || 30
217
+ }), e = `${S.toLowerCase()}:${Y.current.spenderAddress}?amount=${Y.current.approveAmount}`), _("payment"), D(await import_browser.toDataURL(e, {
55
218
  width: 200,
56
219
  margin: 2,
57
220
  errorCorrectionLevel: "H"
58
221
  }));
59
- } catch (t) {
60
- E?.(t);
222
+ } catch (e) {
223
+ m?.(e);
61
224
  } finally {
62
- j(!1);
225
+ y(!1);
63
226
  }
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);
227
+ }, Te = async () => {
228
+ if (Y.current) try {
229
+ y(!0), i === "one-time" ? Z(Y.current.paymentId) : i === "subscription" && Z(Y.current.subscriptionId);
230
+ } catch (e) {
231
+ console.error("支付状态检查错误:", e), m?.(e), y(!1);
71
232
  }
72
- }, X = (t) => {
73
- let d = () => defer(() => p === "subscription" ? D.checkSubscriptionPaymentStatus({ subscriptionId: t }) : D.checkOnetimePaymentStatus({ paymentId: t })).pipe(timeout(15e3), retry({
233
+ }, Z = (e) => {
234
+ let t = () => defer(() => i === "subscription" ? h.checkSubscriptionPaymentStatus({ subscriptionId: e }) : h.checkOnetimePaymentStatus({ paymentId: e })).pipe(timeout(15e3), retry({
74
235
  count: 3,
75
236
  delay: 2e3
76
237
  }));
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);
238
+ F.current?.unsubscribe(), F.current = t().pipe(expand((e) => e.status === "pending" ? timer(2e3).pipe(switchMap(() => t())) : EMPTY), tap((e) => {
239
+ e.status === "expired" && (y(!1), we?.(e)), e.status === "paid" && (y(!1), _("success"), f?.(e)), e.status === "failed" && (y(!1), p?.(e)), e.status === "approved" && (y(!1), _("success"), f?.(e));
240
+ })).subscribe({ error: (e) => {
241
+ console.error("支付状态检查错误:", e), y(!1), m?.(e);
81
242
  } });
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);
243
+ }, Q = async () => {
244
+ if (!Y.current) return;
245
+ let e = i === "subscription" ? Y.current.spenderAddress : Y.current.receiveAddress;
246
+ e && await navigator.clipboard.writeText(e);
247
+ }, $ = () => {
248
+ v || (t(), setTimeout(() => {
249
+ _("select"), Y.current = null;
250
+ }, 300));
90
251
  };
91
252
  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", {
253
+ e && b.length === 0 && U();
254
+ }, [e]), useEffect(() => {
255
+ L.length > 0 && T(L[0].symbol);
256
+ }, [L]), useEffect(() => () => {
257
+ F.current?.unsubscribe();
258
+ }, []), e ? /* @__PURE__ */ jsx("div", {
98
259
  className: "_cppay-overlay",
99
- onClick: (t) => t.target === t.currentTarget && Q(),
260
+ onClick: (e) => e.target === e.currentTarget && !v && $(),
100
261
  children: /* @__PURE__ */ jsxs("div", {
101
262
  className: "_cppay-dialog",
102
263
  children: [/* @__PURE__ */ jsxs("div", {
103
264
  className: "_cppay-header",
104
265
  children: [/* @__PURE__ */ jsx("h2", {
105
266
  className: "_cppay-title",
106
- children: O === "select" ? "选择支付方式" : "完成支付"
267
+ children: g === "select" ? "选择支付方式" : "完成支付"
107
268
  }), /* @__PURE__ */ jsx("button", {
108
- onClick: Q,
109
- disabled: A,
269
+ onClick: $,
270
+ disabled: v,
110
271
  className: "_cppay-close-btn",
111
272
  children: /* @__PURE__ */ jsx("svg", {
112
273
  fill: "none",
@@ -122,7 +283,37 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
122
283
  })]
123
284
  }), /* @__PURE__ */ jsx("div", {
124
285
  className: "_cppay-content",
125
- children: O === "select" ? /* @__PURE__ */ jsxs("div", { children: [
286
+ children: g === "success" ? /* @__PURE__ */ jsxs("div", {
287
+ style: {
288
+ textAlign: "center",
289
+ padding: "2rem 0"
290
+ },
291
+ children: [
292
+ /* @__PURE__ */ jsx("div", {
293
+ style: {
294
+ fontSize: "64px",
295
+ marginBottom: "1rem"
296
+ },
297
+ children: "✅"
298
+ }),
299
+ /* @__PURE__ */ jsx("h3", {
300
+ style: {
301
+ fontSize: "1.5rem",
302
+ fontWeight: 600,
303
+ color: "#10b981",
304
+ margin: "0 0 0.5rem 0"
305
+ },
306
+ children: i === "subscription" ? "授权成功!" : "支付成功!"
307
+ }),
308
+ /* @__PURE__ */ jsx("p", {
309
+ style: {
310
+ color: "#6b7280",
311
+ margin: 0
312
+ },
313
+ children: i === "subscription" ? "订阅已激活" : "交易已完成"
314
+ })
315
+ ]
316
+ }) : g === "select" ? /* @__PURE__ */ jsxs("div", { children: [
126
317
  /* @__PURE__ */ jsxs("div", {
127
318
  className: "_cppay-section",
128
319
  children: [/* @__PURE__ */ jsx("label", {
@@ -130,14 +321,14 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
130
321
  children: "支付网络"
131
322
  }), /* @__PURE__ */ jsx("div", {
132
323
  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))
324
+ children: b.map((e) => /* @__PURE__ */ jsxs("button", {
325
+ onClick: () => C(e.chain),
326
+ className: `_cppay-select-btn ${S === e.chain ? "_cppay-selected" : ""}`,
327
+ children: [e.icon && /* @__PURE__ */ jsx("img", {
328
+ src: e.icon,
329
+ alt: e.chain
330
+ }), /* @__PURE__ */ jsx("span", { children: e.chain })]
331
+ }, e.chain))
141
332
  })]
142
333
  }),
143
334
  /* @__PURE__ */ jsxs("div", {
@@ -147,14 +338,14 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
147
338
  children: "支付代币"
148
339
  }), /* @__PURE__ */ jsx("div", {
149
340
  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))
341
+ children: L.map((e) => /* @__PURE__ */ jsxs("button", {
342
+ onClick: () => T(e.symbol),
343
+ className: `_cppay-select-btn ${w === e.symbol ? "_cppay-selected" : ""}`,
344
+ children: [e.icon && /* @__PURE__ */ jsx("img", {
345
+ src: e.icon,
346
+ alt: e.symbol
347
+ }), /* @__PURE__ */ jsx("span", { children: e.symbol })]
348
+ }, e.symbol))
158
349
  })]
159
350
  }),
160
351
  /* @__PURE__ */ jsx("div", {
@@ -171,13 +362,13 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
171
362
  children: [/* @__PURE__ */ jsxs("div", {
172
363
  className: "_cppay-price-main",
173
364
  children: [
174
- G,
365
+ H,
175
366
  " ",
176
- I
367
+ w
177
368
  ]
178
369
  }), /* @__PURE__ */ jsxs("div", {
179
370
  className: "_cppay-price-sub",
180
- children: ["≈ $", x]
371
+ children: ["≈ $", u]
181
372
  })]
182
373
  })]
183
374
  })
@@ -186,10 +377,10 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
186
377
  /* @__PURE__ */ jsx("div", {
187
378
  className: "_cppay-section",
188
379
  children: /* @__PURE__ */ jsx("button", {
189
- onClick: J,
190
- disabled: !P || !I || A,
380
+ onClick: X,
381
+ disabled: !S || !w || v,
191
382
  className: "_cppay-btn _cppay-btn-primary",
192
- children: A ? "处理中..." : "继续支付"
383
+ children: v ? "处理中..." : "继续支付"
193
384
  })
194
385
  })
195
386
  ] }) : /* @__PURE__ */ jsxs("div", { children: [
@@ -197,17 +388,13 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
197
388
  className: "_cppay-qr-container",
198
389
  children: /* @__PURE__ */ jsx("div", {
199
390
  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,
391
+ children: E && /* @__PURE__ */ jsx("img", {
392
+ src: E,
207
393
  alt: "Payment QR Code",
208
394
  style: {
209
- width: "200px",
210
- height: "200px"
395
+ width: "160px",
396
+ height: "160px",
397
+ display: "block"
211
398
  }
212
399
  })
213
400
  })
@@ -219,21 +406,21 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
219
406
  className: "_cppay-info-box",
220
407
  children: [/* @__PURE__ */ jsx("div", {
221
408
  className: "_cppay-info-label",
222
- children: p === "subscription" ? "授权金额" : "支付金额"
409
+ children: i === "subscription" ? "授权金额" : "支付金额"
223
410
  }), /* @__PURE__ */ jsx("div", {
224
411
  className: "_cppay-info-value",
225
- children: p === "subscription" ? `${W(q.current.approveAmount, U(I))} ${I}` : `${W(q.current.paymentAmount, U(I))} ${I}`
412
+ children: i === "subscription" ? `${V(Y.current.approveAmount, z(w))} ${w}` : `${V(Y.current.paymentAmount, z(w))} ${w}`
226
413
  })]
227
414
  }),
228
415
  /* @__PURE__ */ jsxs("div", {
229
416
  className: "_cppay-info-box",
230
417
  children: [/* @__PURE__ */ jsx("div", {
231
418
  className: "_cppay-info-label",
232
- children: p === "subscription" ? "授权合约地址" : "支付地址"
419
+ children: i === "subscription" ? "授权合约地址" : "支付地址"
233
420
  }), /* @__PURE__ */ jsxs("div", {
234
421
  className: "_cppay-address-row",
235
- children: [/* @__PURE__ */ jsx("code", { children: p === "subscription" ? q.current.spenderAddress : q.current.receiveAddress }), /* @__PURE__ */ jsx("button", {
236
- onClick: Z,
422
+ children: [/* @__PURE__ */ jsx("code", { children: i === "subscription" ? Y.current.spenderAddress : Y.current.receiveAddress }), /* @__PURE__ */ jsx("button", {
423
+ onClick: Q,
237
424
  className: "_cppay-copy-btn",
238
425
  title: "复制地址",
239
426
  children: /* @__PURE__ */ jsx("svg", {
@@ -250,7 +437,7 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
250
437
  })]
251
438
  })]
252
439
  }),
253
- p === "subscription" && /* @__PURE__ */ jsxs("div", {
440
+ i === "subscription" && /* @__PURE__ */ jsxs("div", {
254
441
  className: "_cppay-info-box",
255
442
  style: {
256
443
  background: "#fff3cd",
@@ -271,21 +458,93 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
271
458
  })
272
459
  ]
273
460
  }),
461
+ /* @__PURE__ */ jsx("div", {
462
+ className: "_cppay-section",
463
+ children: O ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("button", {
464
+ onClick: J,
465
+ disabled: v,
466
+ className: "_cppay-btn _cppay-btn-primary",
467
+ children: v ? i === "subscription" ? "授权中..." : "支付中..." : i === "subscription" ? "💳 钱包授权" : "💳 钱包支付"
468
+ }), /* @__PURE__ */ jsxs("div", {
469
+ style: {
470
+ textAlign: "center",
471
+ marginTop: "8px",
472
+ fontSize: "12px",
473
+ color: "#666"
474
+ },
475
+ children: [
476
+ "已连接: ",
477
+ O.slice(0, 6),
478
+ "...",
479
+ O.slice(-4)
480
+ ]
481
+ })] }) : /* @__PURE__ */ jsxs("div", {
482
+ style: {
483
+ display: "flex",
484
+ flexDirection: "row",
485
+ gap: "0.5rem",
486
+ justifyContent: "center"
487
+ },
488
+ children: [/* @__PURE__ */ jsxs("button", {
489
+ onClick: () => W("metamask"),
490
+ disabled: A,
491
+ className: "_cppay-btn _cppay-btn-secondary",
492
+ style: {
493
+ display: "flex",
494
+ alignItems: "center",
495
+ justifyContent: "center",
496
+ gap: "0.5rem",
497
+ padding: "0.5rem 1rem",
498
+ fontSize: "14px",
499
+ flex: "1"
500
+ },
501
+ children: [/* @__PURE__ */ jsx("img", {
502
+ src: metamask_default,
503
+ alt: "MetaMask",
504
+ style: {
505
+ width: "20px",
506
+ height: "20px"
507
+ }
508
+ }), /* @__PURE__ */ jsx("span", { children: "MetaMask" })]
509
+ }), /* @__PURE__ */ jsxs("button", {
510
+ onClick: () => W("walletconnect"),
511
+ disabled: A,
512
+ className: "_cppay-btn _cppay-btn-secondary",
513
+ style: {
514
+ display: "flex",
515
+ alignItems: "center",
516
+ justifyContent: "center",
517
+ gap: "0.5rem",
518
+ padding: "0.5rem 1rem",
519
+ fontSize: "14px",
520
+ flex: "1"
521
+ },
522
+ children: [/* @__PURE__ */ jsx("img", {
523
+ src: walletconnect_default,
524
+ alt: "WalletConnect",
525
+ style: {
526
+ width: "20px",
527
+ height: "20px"
528
+ }
529
+ }), /* @__PURE__ */ jsx("span", { children: "WalletConnect" })]
530
+ })]
531
+ })
532
+ }),
274
533
  /* @__PURE__ */ jsx("div", {
275
534
  className: "_cppay-section",
276
535
  children: /* @__PURE__ */ jsx("button", {
277
- onClick: Y,
278
- disabled: A,
536
+ onClick: Te,
537
+ disabled: v,
279
538
  className: "_cppay-btn _cppay-btn-primary",
280
- children: A ? "检查中..." : "我已完成支付"
539
+ children: v ? "检查中..." : "我已完成支付"
281
540
  })
282
541
  }),
283
542
  /* @__PURE__ */ jsx("div", {
284
543
  className: "_cppay-section",
285
544
  children: /* @__PURE__ */ jsx("button", {
286
- onClick: () => k("select"),
287
- disabled: A,
288
- className: "_cppay-btn _cppay-btn-secondary",
545
+ onClick: () => _("select"),
546
+ disabled: v,
547
+ className: "_cppay-btn _cppay-btn-text",
289
548
  children: "更改支付方式"
290
549
  })
291
550
  })
@@ -294,34 +553,34 @@ var import_browser = /* @__PURE__ */ __toESM(require_browser(), 1), PaymentDialo
294
553
  })
295
554
  }) : null;
296
555
  }), 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]);
556
+ const CppayProvider = ({ apikey: e, children: t }) => {
557
+ let [n, r] = useState(!1), [i, a] = useState(null), o = useCallback((e) => {
558
+ a(e), r(!0);
559
+ }, []), s = useCallback(() => {
560
+ r(!1);
561
+ }, []), Ce = useCallback((e) => {
562
+ i?.onSuccess?.(e), r(!1);
563
+ }, [i]), c = useCallback((e) => {
564
+ i?.onError?.(e);
565
+ }, [i]);
307
566
  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
567
+ value: { showPayment: o },
568
+ children: [t, i && /* @__PURE__ */ jsx(PaymentDialog_default, {
569
+ open: n,
570
+ onClose: s,
571
+ apikey: e,
572
+ plain: i.plain,
573
+ orderId: i.orderId,
574
+ amount: i.amount,
575
+ intervalDays: i.intervalDays,
576
+ onSuccess: Ce,
577
+ onError: c
319
578
  })]
320
579
  });
321
580
  }, useCppayPayment = () => {
322
- let t = useContext(CppayContext);
323
- if (!t) throw Error("useCppayPayment must be used within CppayProvider");
324
- return t;
581
+ let e = useContext(CppayContext);
582
+ if (!e) throw Error("useCppayPayment must be used within CppayProvider");
583
+ return e;
325
584
  };
326
585
  injectStyle(payment_dialog_default);
327
586
  export { CppayProvider, PaymentDialog_default as PaymentDialog, useCppayPayment };