@b3dotfun/sdk 0.1.66-alpha.1 → 0.1.66-alpha.2
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/cjs/anyspend/react/components/checkout/AnySpendCheckout.d.ts +50 -0
- package/dist/cjs/anyspend/react/components/checkout/AnySpendCheckout.js +30 -0
- package/dist/cjs/anyspend/react/components/checkout/AnySpendCheckoutTrigger.d.ts +47 -0
- package/dist/cjs/anyspend/react/components/checkout/AnySpendCheckoutTrigger.js +45 -0
- package/dist/cjs/anyspend/react/components/checkout/CartItemRow.d.ts +8 -0
- package/dist/cjs/anyspend/react/components/checkout/CartItemRow.js +9 -0
- package/dist/cjs/anyspend/react/components/checkout/CartSummary.d.ts +8 -0
- package/dist/cjs/anyspend/react/components/checkout/CartSummary.js +9 -0
- package/dist/cjs/anyspend/react/components/checkout/CheckoutCartPanel.d.ts +12 -0
- package/dist/cjs/anyspend/react/components/checkout/CheckoutCartPanel.js +19 -0
- package/dist/cjs/anyspend/react/components/checkout/CheckoutLayout.d.ts +10 -0
- package/dist/cjs/anyspend/react/components/checkout/CheckoutLayout.js +25 -0
- package/dist/cjs/anyspend/react/components/checkout/CheckoutPaymentPanel.d.ts +20 -0
- package/dist/cjs/anyspend/react/components/checkout/CheckoutPaymentPanel.js +45 -0
- package/dist/cjs/anyspend/react/components/checkout/CheckoutSuccess.d.ts +10 -0
- package/dist/cjs/anyspend/react/components/checkout/CheckoutSuccess.js +11 -0
- package/dist/cjs/anyspend/react/components/checkout/CoinbaseCheckoutPanel.d.ts +16 -0
- package/dist/cjs/anyspend/react/components/checkout/CoinbaseCheckoutPanel.js +27 -0
- package/dist/cjs/anyspend/react/components/checkout/CryptoCheckoutPanel.d.ts +33 -0
- package/dist/cjs/anyspend/react/components/checkout/CryptoCheckoutPanel.js +317 -0
- package/dist/cjs/anyspend/react/components/checkout/FiatCheckoutPanel.d.ts +16 -0
- package/dist/cjs/anyspend/react/components/checkout/FiatCheckoutPanel.js +233 -0
- package/dist/cjs/anyspend/react/components/checkout/PoweredByBranding.d.ts +8 -0
- package/dist/cjs/anyspend/react/components/checkout/PoweredByBranding.js +9 -0
- package/dist/cjs/anyspend/react/components/checkout/QRCheckoutPanel.d.ts +17 -0
- package/dist/cjs/anyspend/react/components/checkout/QRCheckoutPanel.js +148 -0
- package/dist/cjs/anyspend/react/components/index.d.ts +5 -1
- package/dist/cjs/anyspend/react/components/index.js +6 -1
- package/dist/cjs/anyspend/react/components/types/classes.d.ts +32 -0
- package/dist/cjs/global-account/react/components/B3DynamicModal.js +5 -1
- package/dist/cjs/global-account/react/components/WalletImage/WalletImage.d.ts +1 -1
- package/dist/cjs/global-account/react/components/ui/command.d.ts +7 -7
- package/dist/cjs/global-account/react/hooks/useAuth.d.ts +1 -1
- package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +1 -1
- package/dist/cjs/global-account/react/hooks/useUser.d.ts +1 -1
- package/dist/cjs/global-account/react/hooks/useUserQuery.d.ts +1 -1
- package/dist/cjs/global-account/react/stores/useModalStore.d.ts +53 -1
- package/dist/cjs/shared/constants/chains/b3Chain.d.ts +2 -2
- package/dist/cjs/shared/constants/chains/supported.d.ts +3 -3
- package/dist/esm/anyspend/react/components/checkout/AnySpendCheckout.d.ts +50 -0
- package/dist/esm/anyspend/react/components/checkout/AnySpendCheckout.js +27 -0
- package/dist/esm/anyspend/react/components/checkout/AnySpendCheckoutTrigger.d.ts +47 -0
- package/dist/esm/anyspend/react/components/checkout/AnySpendCheckoutTrigger.js +42 -0
- package/dist/esm/anyspend/react/components/checkout/CartItemRow.d.ts +8 -0
- package/dist/esm/anyspend/react/components/checkout/CartItemRow.js +6 -0
- package/dist/esm/anyspend/react/components/checkout/CartSummary.d.ts +8 -0
- package/dist/esm/anyspend/react/components/checkout/CartSummary.js +6 -0
- package/dist/esm/anyspend/react/components/checkout/CheckoutCartPanel.d.ts +12 -0
- package/dist/esm/anyspend/react/components/checkout/CheckoutCartPanel.js +16 -0
- package/dist/esm/anyspend/react/components/checkout/CheckoutLayout.d.ts +10 -0
- package/dist/esm/anyspend/react/components/checkout/CheckoutLayout.js +22 -0
- package/dist/esm/anyspend/react/components/checkout/CheckoutPaymentPanel.d.ts +20 -0
- package/dist/esm/anyspend/react/components/checkout/CheckoutPaymentPanel.js +42 -0
- package/dist/esm/anyspend/react/components/checkout/CheckoutSuccess.d.ts +10 -0
- package/dist/esm/anyspend/react/components/checkout/CheckoutSuccess.js +8 -0
- package/dist/esm/anyspend/react/components/checkout/CoinbaseCheckoutPanel.d.ts +16 -0
- package/dist/esm/anyspend/react/components/checkout/CoinbaseCheckoutPanel.js +24 -0
- package/dist/esm/anyspend/react/components/checkout/CryptoCheckoutPanel.d.ts +33 -0
- package/dist/esm/anyspend/react/components/checkout/CryptoCheckoutPanel.js +313 -0
- package/dist/esm/anyspend/react/components/checkout/FiatCheckoutPanel.d.ts +16 -0
- package/dist/esm/anyspend/react/components/checkout/FiatCheckoutPanel.js +230 -0
- package/dist/esm/anyspend/react/components/checkout/PoweredByBranding.d.ts +8 -0
- package/dist/esm/anyspend/react/components/checkout/PoweredByBranding.js +6 -0
- package/dist/esm/anyspend/react/components/checkout/QRCheckoutPanel.d.ts +17 -0
- package/dist/esm/anyspend/react/components/checkout/QRCheckoutPanel.js +145 -0
- package/dist/esm/anyspend/react/components/index.d.ts +5 -1
- package/dist/esm/anyspend/react/components/index.js +3 -0
- package/dist/esm/anyspend/react/components/types/classes.d.ts +32 -0
- package/dist/esm/global-account/react/components/B3DynamicModal.js +5 -1
- package/dist/esm/global-account/react/components/WalletImage/WalletImage.d.ts +1 -1
- package/dist/esm/global-account/react/components/ui/command.d.ts +7 -7
- package/dist/esm/global-account/react/hooks/useAuth.d.ts +1 -1
- package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +1 -1
- package/dist/esm/global-account/react/hooks/useUser.d.ts +1 -1
- package/dist/esm/global-account/react/hooks/useUserQuery.d.ts +1 -1
- package/dist/esm/global-account/react/stores/useModalStore.d.ts +53 -1
- package/dist/esm/shared/constants/chains/b3Chain.d.ts +2 -2
- package/dist/esm/shared/constants/chains/supported.d.ts +3 -3
- package/dist/styles/index.css +1 -1
- package/dist/types/anyspend/react/components/checkout/AnySpendCheckout.d.ts +50 -0
- package/dist/types/anyspend/react/components/checkout/AnySpendCheckoutTrigger.d.ts +47 -0
- package/dist/types/anyspend/react/components/checkout/CartItemRow.d.ts +8 -0
- package/dist/types/anyspend/react/components/checkout/CartSummary.d.ts +8 -0
- package/dist/types/anyspend/react/components/checkout/CheckoutCartPanel.d.ts +12 -0
- package/dist/types/anyspend/react/components/checkout/CheckoutLayout.d.ts +10 -0
- package/dist/types/anyspend/react/components/checkout/CheckoutPaymentPanel.d.ts +20 -0
- package/dist/types/anyspend/react/components/checkout/CheckoutSuccess.d.ts +10 -0
- package/dist/types/anyspend/react/components/checkout/CoinbaseCheckoutPanel.d.ts +16 -0
- package/dist/types/anyspend/react/components/checkout/CryptoCheckoutPanel.d.ts +33 -0
- package/dist/types/anyspend/react/components/checkout/FiatCheckoutPanel.d.ts +16 -0
- package/dist/types/anyspend/react/components/checkout/PoweredByBranding.d.ts +8 -0
- package/dist/types/anyspend/react/components/checkout/QRCheckoutPanel.d.ts +17 -0
- package/dist/types/anyspend/react/components/index.d.ts +5 -1
- package/dist/types/anyspend/react/components/types/classes.d.ts +32 -0
- package/dist/types/global-account/react/components/WalletImage/WalletImage.d.ts +1 -1
- package/dist/types/global-account/react/components/ui/command.d.ts +7 -7
- package/dist/types/global-account/react/hooks/useAuth.d.ts +1 -1
- package/dist/types/global-account/react/hooks/useAuthentication.d.ts +1 -1
- package/dist/types/global-account/react/hooks/useUser.d.ts +1 -1
- package/dist/types/global-account/react/hooks/useUserQuery.d.ts +1 -1
- package/dist/types/global-account/react/stores/useModalStore.d.ts +53 -1
- package/dist/types/shared/constants/chains/b3Chain.d.ts +2 -2
- package/dist/types/shared/constants/chains/supported.d.ts +3 -3
- package/package.json +1 -1
- package/src/anyspend/react/components/checkout/AnySpendCheckout.tsx +127 -0
- package/src/anyspend/react/components/checkout/AnySpendCheckoutTrigger.tsx +166 -0
- package/src/anyspend/react/components/checkout/CartItemRow.tsx +43 -0
- package/src/anyspend/react/components/checkout/CartSummary.tsx +23 -0
- package/src/anyspend/react/components/checkout/CheckoutCartPanel.tsx +60 -0
- package/src/anyspend/react/components/checkout/CheckoutLayout.tsx +72 -0
- package/src/anyspend/react/components/checkout/CheckoutPaymentPanel.tsx +320 -0
- package/src/anyspend/react/components/checkout/CheckoutSuccess.tsx +91 -0
- package/src/anyspend/react/components/checkout/CoinbaseCheckoutPanel.tsx +90 -0
- package/src/anyspend/react/components/checkout/CryptoCheckoutPanel.tsx +643 -0
- package/src/anyspend/react/components/checkout/FiatCheckoutPanel.tsx +568 -0
- package/src/anyspend/react/components/checkout/PoweredByBranding.tsx +32 -0
- package/src/anyspend/react/components/checkout/QRCheckoutPanel.tsx +320 -0
- package/src/anyspend/react/components/index.ts +7 -0
- package/src/anyspend/react/components/types/classes.ts +48 -0
- package/src/global-account/react/components/B3DynamicModal.tsx +5 -0
- package/src/global-account/react/stores/useModalStore.ts +52 -1
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useAnyspendQuote } from "../../../../anyspend/react/hooks/useAnyspendQuote.js";
|
|
4
|
+
import { useAnyspendCreateOrder } from "../../../../anyspend/react/hooks/useAnyspendCreateOrder.js";
|
|
5
|
+
import { useAnyspendOrderAndTransactions } from "../../../../anyspend/react/hooks/useAnyspendOrderAndTransactions.js";
|
|
6
|
+
import { useAnyspendTokenList } from "../../../../anyspend/react/hooks/useAnyspendTokens.js";
|
|
7
|
+
import { useOnOrderSuccess } from "../../../../anyspend/react/hooks/useOnOrderSuccess.js";
|
|
8
|
+
import { ALL_CHAINS } from "../../../../anyspend/index.js";
|
|
9
|
+
import { EVM_MAINNET } from "../../../../anyspend/utils/chain.js";
|
|
10
|
+
import { useAccountWallet, useB3Config, useModalStore, useSimBalance, useSimTokenBalance, useTokenData, useUnifiedChainSwitchAndExecute, } from "../../../../global-account/react/index.js";
|
|
11
|
+
import { thirdwebB3Chain } from "../../../../shared/constants/chains/b3Chain.js";
|
|
12
|
+
import { formatTokenAmount } from "../../../../shared/utils/number.js";
|
|
13
|
+
import { isNativeToken } from "../../../../anyspend/utils/token.js";
|
|
14
|
+
import { cn } from "../../../../shared/utils/cn.js";
|
|
15
|
+
import { TextShimmer } from "../../../../global-account/react/index.js";
|
|
16
|
+
import { useIsMobile } from "../../../../global-account/react/index.js";
|
|
17
|
+
import { Dialog, DialogContent, DialogDescription, DialogTitle, } from "../../../../global-account/react/components/ui/dialog.js";
|
|
18
|
+
import { Drawer, DrawerContent, DrawerDescription, DrawerTitle, } from "../../../../global-account/react/components/ui/drawer.js";
|
|
19
|
+
import { ChevronDown, Loader2, Search } from "lucide-react";
|
|
20
|
+
import { encodeFunctionData, erc20Abi } from "viem";
|
|
21
|
+
import { AnimatePresence, motion } from "motion/react";
|
|
22
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
23
|
+
import { ChainTokenIcon } from "../common/ChainTokenIcon.js";
|
|
24
|
+
export function CryptoCheckoutPanel({ recipientAddress, destinationTokenAddress, destinationTokenChainId, totalAmount, buttonText = "Pay", themeColor, onSuccess, onError, callbackMetadata, classes, }) {
|
|
25
|
+
const [selectedSrcChainId, setSelectedSrcChainId] = useState(destinationTokenChainId);
|
|
26
|
+
const [selectedSrcToken, setSelectedSrcToken] = useState(null);
|
|
27
|
+
const [showTokenSelector, setShowTokenSelector] = useState(false);
|
|
28
|
+
const [tokenSearchQuery, setTokenSearchQuery] = useState("");
|
|
29
|
+
// Get wallet & modal
|
|
30
|
+
const { address: walletAddress } = useAccountWallet();
|
|
31
|
+
const { partnerId } = useB3Config();
|
|
32
|
+
const setB3ModalOpen = useModalStore(state => state.setB3ModalOpen);
|
|
33
|
+
const setB3ModalContentType = useModalStore(state => state.setB3ModalContentType);
|
|
34
|
+
// Get destination token data
|
|
35
|
+
const { data: dstTokenData } = useTokenData(destinationTokenChainId, destinationTokenAddress);
|
|
36
|
+
// Get token list for source chain
|
|
37
|
+
const { data: tokenList, isLoading: isLoadingTokens } = useAnyspendTokenList(selectedSrcChainId, tokenSearchQuery);
|
|
38
|
+
// Set default source token to destination token (same-chain, no swap needed)
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (!selectedSrcToken && tokenList && tokenList.length > 0) {
|
|
41
|
+
// Try to find the destination token in the list
|
|
42
|
+
const dstToken = tokenList.find((t) => t.address.toLowerCase() === destinationTokenAddress.toLowerCase() && t.chainId === destinationTokenChainId);
|
|
43
|
+
if (dstToken) {
|
|
44
|
+
setSelectedSrcToken(dstToken);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
// Default to first token
|
|
48
|
+
setSelectedSrcToken(tokenList[0]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}, [tokenList, selectedSrcToken, destinationTokenAddress, destinationTokenChainId]);
|
|
52
|
+
// Compute source amount from destination amount using quote
|
|
53
|
+
const isSameToken = selectedSrcToken &&
|
|
54
|
+
selectedSrcToken.address.toLowerCase() === destinationTokenAddress.toLowerCase() &&
|
|
55
|
+
selectedSrcToken.chainId === destinationTokenChainId;
|
|
56
|
+
const { anyspendQuote, isLoadingAnyspendQuote } = useAnyspendQuote({
|
|
57
|
+
type: "swap",
|
|
58
|
+
srcChain: selectedSrcChainId,
|
|
59
|
+
dstChain: destinationTokenChainId,
|
|
60
|
+
srcTokenAddress: selectedSrcToken?.address || "",
|
|
61
|
+
dstTokenAddress: destinationTokenAddress,
|
|
62
|
+
tradeType: "EXACT_OUTPUT",
|
|
63
|
+
amount: totalAmount,
|
|
64
|
+
});
|
|
65
|
+
// Get balance
|
|
66
|
+
const tokenAddress = selectedSrcToken
|
|
67
|
+
? isNativeToken(selectedSrcToken.address)
|
|
68
|
+
? "native"
|
|
69
|
+
: selectedSrcToken.address
|
|
70
|
+
: undefined;
|
|
71
|
+
const { data: balanceData } = useSimTokenBalance(walletAddress, tokenAddress, selectedSrcChainId);
|
|
72
|
+
const balance = useMemo(() => {
|
|
73
|
+
const b = balanceData?.balances?.[0];
|
|
74
|
+
if (!b?.amount)
|
|
75
|
+
return { raw: BigInt(0), formatted: "0", decimals: 18 };
|
|
76
|
+
return {
|
|
77
|
+
raw: BigInt(b.amount),
|
|
78
|
+
formatted: formatTokenAmount(BigInt(b.amount), b.decimals),
|
|
79
|
+
decimals: b.decimals,
|
|
80
|
+
};
|
|
81
|
+
}, [balanceData]);
|
|
82
|
+
// Determine the amount to pay in source token
|
|
83
|
+
const srcAmount = useMemo(() => {
|
|
84
|
+
if (isSameToken)
|
|
85
|
+
return totalAmount;
|
|
86
|
+
return anyspendQuote?.data?.currencyIn?.amount || "0";
|
|
87
|
+
}, [isSameToken, totalAmount, anyspendQuote]);
|
|
88
|
+
const srcAmountFormatted = useMemo(() => {
|
|
89
|
+
if (!selectedSrcToken)
|
|
90
|
+
return "0";
|
|
91
|
+
const decimals = selectedSrcToken.decimals || 18;
|
|
92
|
+
return formatTokenAmount(BigInt(srcAmount || "0"), decimals);
|
|
93
|
+
}, [srcAmount, selectedSrcToken]);
|
|
94
|
+
// Check if user has enough balance
|
|
95
|
+
const hasEnoughBalance = balance.raw >= BigInt(srcAmount || "0");
|
|
96
|
+
// Order tracking state
|
|
97
|
+
const [orderId, setOrderId] = useState();
|
|
98
|
+
const [isSendingDeposit, setIsSendingDeposit] = useState(false);
|
|
99
|
+
const depositSentRef = useRef(false);
|
|
100
|
+
// Wallet transaction execution
|
|
101
|
+
const { switchChainAndExecute } = useUnifiedChainSwitchAndExecute();
|
|
102
|
+
// Create order
|
|
103
|
+
const { createOrder, isCreatingOrder } = useAnyspendCreateOrder({
|
|
104
|
+
onSuccess: (data) => {
|
|
105
|
+
const id = data?.data?.id;
|
|
106
|
+
if (id) {
|
|
107
|
+
setOrderId(id);
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
onError: (error) => {
|
|
111
|
+
setIsSendingDeposit(false);
|
|
112
|
+
onError?.(error);
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
// Poll order status until executed
|
|
116
|
+
const { orderAndTransactions: oat } = useAnyspendOrderAndTransactions(orderId);
|
|
117
|
+
// Send deposit transaction once order is created and ready
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
if (!oat?.data?.order || depositSentRef.current)
|
|
120
|
+
return;
|
|
121
|
+
const order = oat.data.order;
|
|
122
|
+
if (order.status !== "scanning_deposit_transaction")
|
|
123
|
+
return;
|
|
124
|
+
if (oat.data.depositTxs?.length)
|
|
125
|
+
return; // Already deposited
|
|
126
|
+
depositSentRef.current = true;
|
|
127
|
+
const sendDeposit = async () => {
|
|
128
|
+
try {
|
|
129
|
+
setIsSendingDeposit(true);
|
|
130
|
+
const amount = BigInt(order.srcAmount);
|
|
131
|
+
if (isNativeToken(order.srcTokenAddress)) {
|
|
132
|
+
await switchChainAndExecute(order.srcChain, {
|
|
133
|
+
to: order.globalAddress,
|
|
134
|
+
value: amount,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
const data = encodeFunctionData({
|
|
139
|
+
abi: erc20Abi,
|
|
140
|
+
functionName: "transfer",
|
|
141
|
+
args: [order.globalAddress, amount],
|
|
142
|
+
});
|
|
143
|
+
await switchChainAndExecute(order.srcChain, {
|
|
144
|
+
to: order.srcTokenAddress,
|
|
145
|
+
data,
|
|
146
|
+
value: BigInt(0),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
depositSentRef.current = false;
|
|
152
|
+
onError?.(error instanceof Error ? error : new Error(error?.message || "Transaction rejected"));
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
setIsSendingDeposit(false);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
sendDeposit();
|
|
159
|
+
}, [oat, switchChainAndExecute, onError]);
|
|
160
|
+
// Only call onSuccess when order is actually executed with a real txHash
|
|
161
|
+
useOnOrderSuccess({
|
|
162
|
+
orderData: oat,
|
|
163
|
+
orderId,
|
|
164
|
+
onSuccess: (txHash) => {
|
|
165
|
+
onSuccess?.({ orderId, txHash });
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
const isWaitingForExecution = !!orderId && oat?.data?.order.status !== "executed";
|
|
169
|
+
const handlePay = useCallback(() => {
|
|
170
|
+
if (!selectedSrcToken || !walletAddress)
|
|
171
|
+
return;
|
|
172
|
+
depositSentRef.current = false;
|
|
173
|
+
const dstToken = {
|
|
174
|
+
address: destinationTokenAddress,
|
|
175
|
+
chainId: destinationTokenChainId,
|
|
176
|
+
decimals: dstTokenData?.decimals || 18,
|
|
177
|
+
symbol: dstTokenData?.symbol || "",
|
|
178
|
+
name: dstTokenData?.name || "",
|
|
179
|
+
metadata: {
|
|
180
|
+
logoURI: dstTokenData?.logoURI || "",
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
createOrder({
|
|
184
|
+
recipientAddress,
|
|
185
|
+
orderType: "swap",
|
|
186
|
+
srcChain: selectedSrcChainId,
|
|
187
|
+
dstChain: destinationTokenChainId,
|
|
188
|
+
srcToken: selectedSrcToken,
|
|
189
|
+
dstToken,
|
|
190
|
+
srcAmount,
|
|
191
|
+
expectedDstAmount: totalAmount,
|
|
192
|
+
callbackMetadata,
|
|
193
|
+
});
|
|
194
|
+
}, [
|
|
195
|
+
selectedSrcToken,
|
|
196
|
+
walletAddress,
|
|
197
|
+
recipientAddress,
|
|
198
|
+
selectedSrcChainId,
|
|
199
|
+
destinationTokenChainId,
|
|
200
|
+
destinationTokenAddress,
|
|
201
|
+
dstTokenData,
|
|
202
|
+
srcAmount,
|
|
203
|
+
totalAmount,
|
|
204
|
+
callbackMetadata,
|
|
205
|
+
createOrder,
|
|
206
|
+
]);
|
|
207
|
+
const handleSelectToken = (token) => {
|
|
208
|
+
setSelectedSrcToken(token);
|
|
209
|
+
setSelectedSrcChainId(token.chainId);
|
|
210
|
+
setShowTokenSelector(false);
|
|
211
|
+
setTokenSearchQuery("");
|
|
212
|
+
};
|
|
213
|
+
const isLoading = isLoadingAnyspendQuote || isLoadingTokens;
|
|
214
|
+
const isPending = isCreatingOrder || isSendingDeposit || isWaitingForExecution;
|
|
215
|
+
const canPay = walletAddress && selectedSrcToken && hasEnoughBalance && !isLoading && !isPending;
|
|
216
|
+
return (_jsxs("div", { className: cn("anyspend-crypto-panel flex flex-col gap-4", classes?.cryptoPanel), children: [_jsxs("div", { className: "anyspend-token-selector", children: [_jsx("label", { className: "anyspend-token-label mb-1.5 block text-sm font-medium text-gray-700 dark:text-gray-300", children: "Pay with" }), _jsxs("button", { onClick: () => setShowTokenSelector(true), className: cn("anyspend-token-btn flex w-full items-center justify-between rounded-xl border border-gray-200 bg-white px-4 py-3 transition-colors hover:border-gray-300 dark:border-gray-700 dark:bg-gray-800 dark:hover:border-gray-600", classes?.tokenSelector), children: [selectedSrcToken ? (_jsxs("div", { className: "flex items-center gap-3", children: [_jsx(ChainTokenIcon, { chainUrl: ALL_CHAINS[selectedSrcToken.chainId]?.logoUrl || "", tokenUrl: selectedSrcToken.metadata?.logoURI, className: "h-8 w-8" }), _jsxs("div", { className: "text-left", children: [_jsx("p", { className: "text-sm font-medium text-gray-900 dark:text-gray-100", children: selectedSrcToken.symbol }), _jsxs("p", { className: "text-xs text-gray-500 dark:text-gray-400", children: ["Balance: ", balance.formatted] })] })] })) : (_jsx("span", { className: "text-sm text-gray-400", children: "Select token" })), _jsx(ChevronDown, { className: "h-4 w-4 text-gray-400" })] })] }), _jsx(TokenSelectorModal, { open: showTokenSelector, onClose: () => {
|
|
217
|
+
setShowTokenSelector(false);
|
|
218
|
+
setTokenSearchQuery("");
|
|
219
|
+
}, tokenList: tokenList, isLoadingTokens: isLoadingTokens, tokenSearchQuery: tokenSearchQuery, onSearchChange: setTokenSearchQuery, onSelectToken: handleSelectToken, selectedToken: selectedSrcToken, walletAddress: walletAddress, chainId: selectedSrcChainId, onChainChange: chainId => {
|
|
220
|
+
setSelectedSrcChainId(chainId);
|
|
221
|
+
setSelectedSrcToken(null);
|
|
222
|
+
setTokenSearchQuery("");
|
|
223
|
+
} }), _jsx(motion.div, { initial: { opacity: 0, y: 6 }, animate: { opacity: 1, y: 0 }, transition: { duration: 0.25, ease: "easeOut" }, className: cn("anyspend-quote-display rounded-xl border border-gray-200 bg-gray-50 px-4 py-3 dark:border-gray-700 dark:bg-gray-800/50", classes?.quoteDisplay), children: _jsxs("div", { className: "flex items-center justify-between", children: [_jsx("span", { className: "text-sm text-gray-500 dark:text-gray-400", children: "You pay" }), _jsx(AnimatePresence, { mode: "wait", children: isLoadingAnyspendQuote ? (_jsx(motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: 0.15 }, children: _jsx(TextShimmer, { duration: 1, className: "text-sm", children: "Fetching quote..." }) }, "quote-loading")) : (_jsxs(motion.span, { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: 0.15 }, className: "text-sm font-medium text-gray-900 dark:text-gray-100", children: [srcAmountFormatted, " ", selectedSrcToken?.symbol || ""] }, "quote-amount")) })] }) }), _jsx(AnimatePresence, { children: walletAddress && selectedSrcToken && !hasEnoughBalance && !isLoading && (_jsxs(motion.p, { initial: { opacity: 0, height: 0 }, animate: { opacity: 1, height: "auto" }, exit: { opacity: 0, height: 0 }, transition: { duration: 0.2, ease: "easeOut" }, className: "anyspend-balance-warning text-center text-sm text-red-500", children: ["Insufficient ", selectedSrcToken.symbol, " balance"] }, "balance-warning")) }), !walletAddress ? (_jsx("button", { onClick: () => {
|
|
224
|
+
setB3ModalContentType({ type: "signInWithB3", showBackButton: false, chain: thirdwebB3Chain, partnerId });
|
|
225
|
+
setB3ModalOpen(true);
|
|
226
|
+
}, className: cn("anyspend-crypto-pay-btn w-full rounded-xl px-4 py-3.5 text-sm font-semibold text-white transition-all", "bg-blue-600 hover:bg-blue-700 active:scale-[0.98]", classes?.payButton), style: themeColor ? { backgroundColor: themeColor } : undefined, children: "Connect Wallet to Pay" })) : (_jsx("button", { onClick: handlePay, disabled: !canPay, className: cn("anyspend-crypto-pay-btn w-full rounded-xl px-4 py-3.5 text-sm font-semibold text-white transition-all", canPay ? "bg-blue-600 hover:bg-blue-700 active:scale-[0.98]" : "cursor-not-allowed bg-blue-600 opacity-50", classes?.payButton), style: !canPay ? undefined : themeColor ? { backgroundColor: themeColor } : undefined, children: isPending ? (_jsxs("span", { className: "flex items-center justify-center gap-2", children: [_jsx(Loader2, { className: "h-4 w-4 animate-spin" }), isCreatingOrder
|
|
227
|
+
? "Creating order..."
|
|
228
|
+
: isSendingDeposit
|
|
229
|
+
? "Confirm in wallet..."
|
|
230
|
+
: "Confirming transaction..."] })) : (buttonText) }))] }));
|
|
231
|
+
}
|
|
232
|
+
const SOURCE_CHAINS = Object.values(EVM_MAINNET).map(c => ({ id: c.id, name: c.name, logoUrl: c.logoUrl }));
|
|
233
|
+
export function TokenSelectorModal({ open, onClose, tokenList, isLoadingTokens, tokenSearchQuery, onSearchChange, onSelectToken, selectedToken, walletAddress, chainId, onChainChange, }) {
|
|
234
|
+
const isMobile = useIsMobile();
|
|
235
|
+
const searchInputRef = useRef(null);
|
|
236
|
+
// Fetch all balances for the wallet on this chain
|
|
237
|
+
const { data: balanceData } = useSimBalance(walletAddress, [chainId]);
|
|
238
|
+
// Build a lookup map: lowercase token address -> balance info
|
|
239
|
+
const balanceMap = useMemo(() => {
|
|
240
|
+
const map = new Map();
|
|
241
|
+
if (!balanceData?.balances)
|
|
242
|
+
return map;
|
|
243
|
+
for (const b of balanceData.balances) {
|
|
244
|
+
if (b.amount && BigInt(b.amount) > BigInt(0)) {
|
|
245
|
+
map.set(b.address.toLowerCase(), {
|
|
246
|
+
raw: BigInt(b.amount),
|
|
247
|
+
formatted: formatTokenAmount(BigInt(b.amount), b.decimals),
|
|
248
|
+
decimals: b.decimals,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return map;
|
|
253
|
+
}, [balanceData]);
|
|
254
|
+
// Sort tokens: tokens with balance first (sorted by balance desc), then the rest
|
|
255
|
+
const sortedTokenList = useMemo(() => {
|
|
256
|
+
if (!tokenList)
|
|
257
|
+
return undefined;
|
|
258
|
+
const withBalance = [];
|
|
259
|
+
const withoutBalance = [];
|
|
260
|
+
for (const token of tokenList) {
|
|
261
|
+
const bal = balanceMap.get(token.address.toLowerCase());
|
|
262
|
+
if (bal) {
|
|
263
|
+
withBalance.push(token);
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
withoutBalance.push(token);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
withBalance.sort((a, b) => {
|
|
270
|
+
const balA = balanceMap.get(a.address.toLowerCase())?.raw || BigInt(0);
|
|
271
|
+
const balB = balanceMap.get(b.address.toLowerCase())?.raw || BigInt(0);
|
|
272
|
+
if (balB > balA)
|
|
273
|
+
return 1;
|
|
274
|
+
if (balB < balA)
|
|
275
|
+
return -1;
|
|
276
|
+
return 0;
|
|
277
|
+
});
|
|
278
|
+
return [...withBalance, ...withoutBalance];
|
|
279
|
+
}, [tokenList, balanceMap]);
|
|
280
|
+
// Keep showing the previous list while new chain tokens are loading
|
|
281
|
+
const prevListRef = useRef(undefined);
|
|
282
|
+
if (sortedTokenList && sortedTokenList.length > 0) {
|
|
283
|
+
prevListRef.current = sortedTokenList;
|
|
284
|
+
}
|
|
285
|
+
const displayList = sortedTokenList && sortedTokenList.length > 0
|
|
286
|
+
? sortedTokenList
|
|
287
|
+
: isLoadingTokens
|
|
288
|
+
? prevListRef.current
|
|
289
|
+
: sortedTokenList;
|
|
290
|
+
// Focus search input when modal opens
|
|
291
|
+
useEffect(() => {
|
|
292
|
+
if (open) {
|
|
293
|
+
const timer = setTimeout(() => searchInputRef.current?.focus(), 100);
|
|
294
|
+
return () => clearTimeout(timer);
|
|
295
|
+
}
|
|
296
|
+
}, [open]);
|
|
297
|
+
const ModalComponent = isMobile ? Drawer : Dialog;
|
|
298
|
+
const ModalContent = isMobile ? DrawerContent : DialogContent;
|
|
299
|
+
const ModalTitle = isMobile ? DrawerTitle : DialogTitle;
|
|
300
|
+
const ModalDescription = isMobile ? DrawerDescription : DialogDescription;
|
|
301
|
+
return (_jsxs(ModalComponent, { open: open, onOpenChange: (v) => {
|
|
302
|
+
if (!v)
|
|
303
|
+
onClose();
|
|
304
|
+
}, children: [_jsx("style", { dangerouslySetInnerHTML: {
|
|
305
|
+
__html: `.anyspend-token-modal .b3-modal-ga-branding { display: none; } .anyspend-token-modal .modal-inner-content { margin-bottom: 0; }`,
|
|
306
|
+
} }), _jsxs(ModalContent, { className: "anyspend-token-modal flex max-h-[80dvh] flex-col overflow-hidden rounded-2xl bg-white p-0 shadow-xl sm:max-h-[70dvh] dark:bg-gray-900", children: [_jsx(ModalTitle, { className: "sr-only", children: "Select token" }), _jsx(ModalDescription, { className: "sr-only", children: "Choose a token to pay with" }), _jsxs("div", { className: "flex min-h-0 flex-1 flex-col", children: [_jsx("div", { className: "flex items-center justify-between px-5 py-4", children: _jsx("h3", { className: "text-base font-semibold text-gray-900 dark:text-gray-100", children: "Select token" }) }), _jsx("div", { className: "anyspend-chain-selector flex items-center gap-2 px-5 pb-3", children: SOURCE_CHAINS.map(chain => (_jsxs("button", { onClick: () => onChainChange(chain.id), title: chain.name, className: "relative shrink-0 rounded-full transition-opacity", style: { opacity: chain.id === chainId ? 1 : 0.4 }, children: [_jsx("img", { src: chain.logoUrl, alt: chain.name, className: "h-7 w-7 rounded-full" }), chain.id === chainId && (_jsx("div", { className: "absolute inset-0 rounded-full", style: { boxShadow: "0 0 0 2px #3b82f6" } }))] }, chain.id))) }), _jsxs("div", { className: "anyspend-token-search flex items-center gap-2 border-b border-gray-100 px-5 py-2.5 dark:border-gray-800", children: [_jsx(Search, { className: "h-4 w-4 shrink-0 text-gray-400" }), _jsx("input", { ref: searchInputRef, type: "text", value: tokenSearchQuery, onChange: e => onSearchChange(e.target.value), placeholder: "Search tokens...", className: "anyspend-token-search-input w-full bg-transparent text-sm outline-none placeholder:text-gray-400 dark:text-gray-100" })] }), _jsxs("div", { className: "anyspend-token-list relative flex-1 overflow-y-auto", style: { minHeight: 300 }, children: [displayList?.map((token) => {
|
|
307
|
+
const isSelected = selectedToken &&
|
|
308
|
+
selectedToken.address.toLowerCase() === token.address.toLowerCase() &&
|
|
309
|
+
selectedToken.chainId === token.chainId;
|
|
310
|
+
const tokenBalance = balanceMap.get(token.address.toLowerCase());
|
|
311
|
+
return (_jsxs("button", { onClick: () => onSelectToken(token), className: cn("anyspend-token-option flex w-full items-center gap-3 px-5 py-3 text-left transition-colors hover:bg-gray-50 dark:hover:bg-gray-800", isSelected && "bg-blue-50 dark:bg-blue-900/20"), children: [_jsx(ChainTokenIcon, { chainUrl: ALL_CHAINS[token.chainId]?.logoUrl || "", tokenUrl: token.metadata?.logoURI, className: "h-8 w-8" }), _jsxs("div", { className: "min-w-0 flex-1", children: [_jsx("p", { className: "text-sm font-medium text-gray-900 dark:text-gray-100", children: token.symbol }), _jsx("p", { className: "truncate text-xs text-gray-500 dark:text-gray-400", children: token.name })] }), _jsxs("div", { className: "flex items-center gap-2", children: [tokenBalance && (_jsx("span", { className: "text-xs font-medium text-gray-600 dark:text-gray-300", children: tokenBalance.formatted })), isSelected && _jsx("div", { className: "h-2 w-2 rounded-full bg-blue-600" })] })] }, `${token.chainId}-${token.address}`));
|
|
312
|
+
}), !isLoadingTokens && displayList && displayList.length === 0 && (_jsx("div", { className: "px-5 py-8 text-center text-sm text-gray-400", children: "No tokens found" }))] })] })] })] }));
|
|
313
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AnySpendCheckoutClasses } from "./AnySpendCheckout";
|
|
2
|
+
interface FiatCheckoutPanelProps {
|
|
3
|
+
recipientAddress: string;
|
|
4
|
+
destinationTokenAddress: string;
|
|
5
|
+
destinationTokenChainId: number;
|
|
6
|
+
totalAmount: string;
|
|
7
|
+
themeColor?: string;
|
|
8
|
+
onSuccess?: (result: {
|
|
9
|
+
txHash?: string;
|
|
10
|
+
orderId?: string;
|
|
11
|
+
}) => void;
|
|
12
|
+
onError?: (error: Error) => void;
|
|
13
|
+
classes?: AnySpendCheckoutClasses;
|
|
14
|
+
}
|
|
15
|
+
export declare function FiatCheckoutPanel({ recipientAddress, destinationTokenAddress, destinationTokenChainId, totalAmount, themeColor, onSuccess, onError, classes, }: FiatCheckoutPanelProps): import("react/jsx-runtime").JSX.Element;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
import { useAnyspendCreateOnrampOrder, useGeoOnrampOptions, useStripeClientSecret } from "../../../../anyspend/react/index.js";
|
|
4
|
+
import { USDC_BASE } from "../../../../anyspend/constants/index.js";
|
|
5
|
+
import { cn } from "../../../../shared/utils/cn.js";
|
|
6
|
+
import { formatTokenAmount } from "../../../../shared/utils/number.js";
|
|
7
|
+
import { getStripePromise } from "../../../../shared/utils/payment.utils.js";
|
|
8
|
+
import { TextShimmer, useB3Config, useTokenData } from "../../../../global-account/react/index.js";
|
|
9
|
+
import { AddressElement, Elements, PaymentElement, useElements, useStripe } from "@stripe/react-stripe-js";
|
|
10
|
+
import { Loader2, Lock } from "lucide-react";
|
|
11
|
+
import { AnimatePresence, motion } from "motion/react";
|
|
12
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
13
|
+
export function FiatCheckoutPanel({ recipientAddress, destinationTokenAddress, destinationTokenChainId, totalAmount, themeColor, onSuccess, onError, classes, }) {
|
|
14
|
+
const { data: tokenData } = useTokenData(destinationTokenChainId, destinationTokenAddress);
|
|
15
|
+
const { theme, stripePublishableKey } = useB3Config();
|
|
16
|
+
const formattedAmount = useMemo(() => {
|
|
17
|
+
const decimals = tokenData?.decimals || 18;
|
|
18
|
+
return formatTokenAmount(BigInt(totalAmount), decimals);
|
|
19
|
+
}, [totalAmount, tokenData]);
|
|
20
|
+
const { geoData, stripeOnrampSupport, stripeWeb2Support, isLoading: isLoadingGeo, } = useGeoOnrampOptions(formattedAmount);
|
|
21
|
+
// Detect if destination token is the same as the onramp source (USDC on Base)
|
|
22
|
+
// In this case, onramp order creation would fail with "Cannot swap same token on same chain"
|
|
23
|
+
const isSameAsOnrampSource = useMemo(() => {
|
|
24
|
+
return (destinationTokenChainId === USDC_BASE.chainId &&
|
|
25
|
+
destinationTokenAddress.toLowerCase() === USDC_BASE.address.toLowerCase());
|
|
26
|
+
}, [destinationTokenAddress, destinationTokenChainId]);
|
|
27
|
+
// Order state
|
|
28
|
+
const [orderId, setOrderId] = useState(null);
|
|
29
|
+
const [stripePaymentIntentId, setStripePaymentIntentId] = useState(null);
|
|
30
|
+
const [orderError, setOrderError] = useState(null);
|
|
31
|
+
const orderCreatedRef = useRef(false);
|
|
32
|
+
const { createOrder, isCreatingOrder } = useAnyspendCreateOnrampOrder({
|
|
33
|
+
onSuccess: (data) => {
|
|
34
|
+
const id = data?.data?.id;
|
|
35
|
+
const intentId = data?.data?.stripePaymentIntentId;
|
|
36
|
+
if (id && intentId) {
|
|
37
|
+
setOrderId(id);
|
|
38
|
+
setStripePaymentIntentId(intentId);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
setOrderError("Failed to initialize payment. Please try again.");
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
onError: (error) => {
|
|
45
|
+
setOrderError(error.message || "Failed to create payment order.");
|
|
46
|
+
onError?.(error);
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
// Auto-create onramp order when Stripe Web2 is supported and all data is ready
|
|
50
|
+
// Skip auto-creation when destination is same as source (USDC on Base)
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
if (!isLoadingGeo &&
|
|
53
|
+
stripeWeb2Support?.isSupport &&
|
|
54
|
+
!isSameAsOnrampSource &&
|
|
55
|
+
!orderCreatedRef.current &&
|
|
56
|
+
!orderId &&
|
|
57
|
+
!isCreatingOrder &&
|
|
58
|
+
!orderError &&
|
|
59
|
+
tokenData &&
|
|
60
|
+
recipientAddress) {
|
|
61
|
+
orderCreatedRef.current = true;
|
|
62
|
+
const dstToken = {
|
|
63
|
+
address: destinationTokenAddress,
|
|
64
|
+
chainId: destinationTokenChainId,
|
|
65
|
+
decimals: tokenData.decimals || 18,
|
|
66
|
+
symbol: tokenData.symbol || "",
|
|
67
|
+
name: tokenData.name || "",
|
|
68
|
+
metadata: {
|
|
69
|
+
logoURI: tokenData.logoURI || "",
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
createOrder({
|
|
73
|
+
recipientAddress,
|
|
74
|
+
orderType: "swap",
|
|
75
|
+
dstChain: destinationTokenChainId,
|
|
76
|
+
dstToken,
|
|
77
|
+
srcFiatAmount: formattedAmount,
|
|
78
|
+
onramp: {
|
|
79
|
+
vendor: "stripe-web2",
|
|
80
|
+
paymentMethod: "",
|
|
81
|
+
country: geoData?.country || "US",
|
|
82
|
+
redirectUrl: window.location.origin,
|
|
83
|
+
},
|
|
84
|
+
expectedDstAmount: totalAmount,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}, [
|
|
88
|
+
isLoadingGeo,
|
|
89
|
+
stripeWeb2Support,
|
|
90
|
+
isSameAsOnrampSource,
|
|
91
|
+
orderId,
|
|
92
|
+
isCreatingOrder,
|
|
93
|
+
orderError,
|
|
94
|
+
tokenData,
|
|
95
|
+
recipientAddress,
|
|
96
|
+
destinationTokenAddress,
|
|
97
|
+
destinationTokenChainId,
|
|
98
|
+
formattedAmount,
|
|
99
|
+
totalAmount,
|
|
100
|
+
geoData,
|
|
101
|
+
createOrder,
|
|
102
|
+
]);
|
|
103
|
+
// Loading geo/stripe support check
|
|
104
|
+
if (isLoadingGeo) {
|
|
105
|
+
return (_jsxs(motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, transition: { duration: 0.2, ease: "easeOut" }, className: cn("anyspend-fiat-loading flex flex-col items-center gap-3 py-6", classes?.fiatPanel), children: [_jsx(Loader2, { className: "h-5 w-5 animate-spin text-gray-400" }), _jsx(TextShimmer, { duration: 1.5, className: "text-sm", children: "Loading payment form..." })] }));
|
|
106
|
+
}
|
|
107
|
+
const hasStripeWeb2 = stripeWeb2Support && stripeWeb2Support.isSupport;
|
|
108
|
+
const hasStripeRedirect = !!stripeOnrampSupport;
|
|
109
|
+
// Not available in region
|
|
110
|
+
if (!hasStripeWeb2 && !hasStripeRedirect) {
|
|
111
|
+
return (_jsx(motion.div, { initial: { opacity: 0, y: 8 }, animate: { opacity: 1, y: 0 }, transition: { duration: 0.25, ease: "easeOut" }, className: cn("anyspend-fiat-unavailable py-4 text-center", classes?.fiatPanel), children: _jsx("p", { className: "text-sm text-gray-500 dark:text-gray-400", children: "Card payments are not available in your region for this amount." }) }));
|
|
112
|
+
}
|
|
113
|
+
// Order creation error - show with retry
|
|
114
|
+
if (orderError) {
|
|
115
|
+
return (_jsxs(motion.div, { initial: { opacity: 0, y: 8 }, animate: { opacity: 1, y: 0 }, transition: { duration: 0.25, ease: "easeOut" }, className: cn("anyspend-fiat-error flex flex-col items-center gap-3 py-4", classes?.fiatPanel), children: [_jsx("p", { className: "text-sm text-red-500", children: orderError }), _jsx("button", { onClick: () => {
|
|
116
|
+
setOrderError(null);
|
|
117
|
+
orderCreatedRef.current = false;
|
|
118
|
+
}, className: "anyspend-fiat-retry text-sm font-medium text-blue-600 hover:text-blue-700 dark:text-blue-400", children: "Try again" })] }));
|
|
119
|
+
}
|
|
120
|
+
// Same-token fallback: USDC on Base uses Stripe redirect instead of embedded
|
|
121
|
+
if (isSameAsOnrampSource && hasStripeRedirect) {
|
|
122
|
+
return (_jsxs("div", { className: cn("anyspend-fiat-redirect flex flex-col gap-3 py-2", classes?.fiatPanel), children: [_jsx("p", { className: "text-sm text-gray-600 dark:text-gray-400", children: "You'll be redirected to Stripe to complete your payment securely." }), _jsxs("button", { className: cn("anyspend-fiat-redirect-btn flex w-full items-center justify-center gap-2 rounded-xl px-4 py-3.5 text-sm font-semibold text-white transition-all active:scale-[0.98]", "bg-blue-600 hover:bg-blue-700"), style: themeColor ? { backgroundColor: themeColor } : undefined, children: [_jsx(Lock, { className: "h-3.5 w-3.5" }), "Pay with Card"] }), _jsxs("p", { className: "anyspend-fiat-secured flex items-center justify-center gap-1 text-xs text-gray-400", children: [_jsx(Lock, { className: "h-3 w-3" }), "Secured by Stripe"] })] }));
|
|
123
|
+
}
|
|
124
|
+
// Same-token without redirect support
|
|
125
|
+
if (isSameAsOnrampSource) {
|
|
126
|
+
return (_jsx("div", { className: cn("anyspend-fiat-unavailable py-4 text-center", classes?.fiatPanel), children: _jsx("p", { className: "text-sm text-gray-500 dark:text-gray-400", children: "Card payments are not available for this token configuration." }) }));
|
|
127
|
+
}
|
|
128
|
+
// Creating order / waiting for PaymentIntent
|
|
129
|
+
if (hasStripeWeb2 && (isCreatingOrder || !stripePaymentIntentId)) {
|
|
130
|
+
return (_jsxs(motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, transition: { duration: 0.2, ease: "easeOut" }, className: cn("anyspend-fiat-initializing flex flex-col items-center gap-3 py-6", classes?.fiatPanel), children: [_jsx(Loader2, { className: "h-5 w-5 animate-spin text-gray-400" }), _jsx(TextShimmer, { duration: 1.5, className: "text-sm", children: "Initializing secure payment..." })] }));
|
|
131
|
+
}
|
|
132
|
+
// Stripe Web2 embedded form
|
|
133
|
+
if (hasStripeWeb2 && stripePaymentIntentId && orderId) {
|
|
134
|
+
return (_jsx(motion.div, { initial: { opacity: 0, y: 10 }, animate: { opacity: 1, y: 0 }, transition: { duration: 0.3, ease: "easeOut" }, className: cn("anyspend-fiat-stripe", classes?.fiatPanel), children: _jsx(StripeCheckout, { stripePaymentIntentId: stripePaymentIntentId, stripePublishableKey: stripePublishableKey, theme: theme, themeColor: themeColor, orderId: orderId, onSuccess: onSuccess, onError: onError, classes: classes }) }));
|
|
135
|
+
}
|
|
136
|
+
// Fallback: Stripe redirect flow (only if web2 not available but redirect is)
|
|
137
|
+
return (_jsxs("div", { className: cn("anyspend-fiat-redirect flex flex-col gap-3 py-2", classes?.fiatPanel), children: [_jsx("p", { className: "text-sm text-gray-600 dark:text-gray-400", children: "You'll be redirected to Stripe to complete your payment securely." }), _jsxs("button", { className: cn("anyspend-fiat-redirect-btn flex w-full items-center justify-center gap-2 rounded-xl px-4 py-3.5 text-sm font-semibold text-white transition-all active:scale-[0.98]", "bg-blue-600 hover:bg-blue-700"), style: themeColor ? { backgroundColor: themeColor } : undefined, children: [_jsx(Lock, { className: "h-3.5 w-3.5" }), "Pay with Card"] }), _jsxs("p", { className: "anyspend-fiat-secured flex items-center justify-center gap-1 text-xs text-gray-400", children: [_jsx(Lock, { className: "h-3 w-3" }), "Secured by Stripe"] })] }));
|
|
138
|
+
}
|
|
139
|
+
function StripeCheckout({ stripePaymentIntentId, stripePublishableKey, theme, themeColor, orderId, onSuccess, onError, classes, }) {
|
|
140
|
+
const { clientSecret, isLoadingStripeClientSecret, stripeClientSecretError } = useStripeClientSecret(stripePaymentIntentId);
|
|
141
|
+
if (isLoadingStripeClientSecret) {
|
|
142
|
+
return (_jsxs(motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, transition: { duration: 0.2, ease: "easeOut" }, className: "anyspend-stripe-loading flex flex-col items-center gap-3 py-6", children: [_jsx(Loader2, { className: "h-5 w-5 animate-spin text-gray-400" }), _jsx(TextShimmer, { duration: 1.5, className: "text-sm", children: "Loading payment form..." })] }));
|
|
143
|
+
}
|
|
144
|
+
if (stripeClientSecretError || !clientSecret) {
|
|
145
|
+
return (_jsx(motion.div, { initial: { opacity: 0, y: 8 }, animate: { opacity: 1, y: 0 }, transition: { duration: 0.25, ease: "easeOut" }, className: "anyspend-stripe-error py-4 text-center", children: _jsx("p", { className: "text-sm text-red-500", children: stripeClientSecretError?.message || "Failed to load payment form. Please try again." }) }));
|
|
146
|
+
}
|
|
147
|
+
return (_jsx(Elements, { stripe: getStripePromise(stripePublishableKey), options: {
|
|
148
|
+
clientSecret,
|
|
149
|
+
appearance: {
|
|
150
|
+
theme: theme === "light" ? "stripe" : "night",
|
|
151
|
+
variables: {
|
|
152
|
+
borderRadius: "8px",
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
}, children: _jsx(StripeCheckoutForm, { themeColor: themeColor, orderId: orderId, onSuccess: onSuccess, onError: onError, classes: classes }) }));
|
|
156
|
+
}
|
|
157
|
+
function StripeCheckoutForm({ themeColor, orderId, onSuccess, onError, classes }) {
|
|
158
|
+
const stripe = useStripe();
|
|
159
|
+
const elements = useElements();
|
|
160
|
+
const [loading, setLoading] = useState(false);
|
|
161
|
+
const [message, setMessage] = useState(null);
|
|
162
|
+
const [stripeReady, setStripeReady] = useState(false);
|
|
163
|
+
const [showAddressElement, setShowAddressElement] = useState(false);
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
if (stripe && elements) {
|
|
166
|
+
setStripeReady(true);
|
|
167
|
+
}
|
|
168
|
+
}, [stripe, elements]);
|
|
169
|
+
const handlePaymentElementChange = useCallback((event) => {
|
|
170
|
+
setShowAddressElement(event.value?.type === "card");
|
|
171
|
+
}, []);
|
|
172
|
+
const handleSubmit = async (e) => {
|
|
173
|
+
e.preventDefault();
|
|
174
|
+
if (!stripe || !elements) {
|
|
175
|
+
setMessage("Payment system is not ready. Please wait.");
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
setLoading(true);
|
|
179
|
+
setMessage(null);
|
|
180
|
+
try {
|
|
181
|
+
const result = (await stripe.confirmPayment({
|
|
182
|
+
elements,
|
|
183
|
+
redirect: "if_required",
|
|
184
|
+
}));
|
|
185
|
+
if (result.error) {
|
|
186
|
+
setMessage(result.error.message || "Payment failed. Please try again.");
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
// Payment succeeded
|
|
190
|
+
onSuccess?.({ orderId, txHash: undefined });
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
const errorMessage = error?.message || "Payment failed. Please try again.";
|
|
194
|
+
setMessage(errorMessage);
|
|
195
|
+
onError?.(error instanceof Error ? error : new Error(errorMessage));
|
|
196
|
+
}
|
|
197
|
+
finally {
|
|
198
|
+
setLoading(false);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
const stripeElementOptions = {
|
|
202
|
+
layout: "tabs",
|
|
203
|
+
fields: {
|
|
204
|
+
billingDetails: "auto",
|
|
205
|
+
},
|
|
206
|
+
wallets: {
|
|
207
|
+
applePay: "auto",
|
|
208
|
+
googlePay: "auto",
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
if (!stripeReady) {
|
|
212
|
+
return (_jsxs(motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, transition: { duration: 0.2, ease: "easeOut" }, className: "anyspend-stripe-loading flex flex-col items-center gap-3 py-6", children: [_jsx(Loader2, { className: "h-5 w-5 animate-spin text-gray-400" }), _jsx(TextShimmer, { duration: 1.5, className: "text-sm", children: "Loading payment form..." })] }));
|
|
213
|
+
}
|
|
214
|
+
return (_jsxs(motion.form, { initial: { opacity: 0, y: 10 }, animate: { opacity: 1, y: 0 }, transition: { duration: 0.3, ease: "easeOut" }, onSubmit: handleSubmit, className: "anyspend-stripe-form flex flex-col gap-4", children: [_jsx("div", { className: "anyspend-stripe-payment-element", children: _jsx(PaymentElement, { onChange: handlePaymentElementChange, options: stripeElementOptions }) }), _jsx(AnimatePresence, { initial: false, children: showAddressElement && (_jsx(motion.div, { initial: { height: 0, opacity: 0 }, animate: { height: "auto", opacity: 1 }, exit: { height: 0, opacity: 0 }, transition: { duration: 0.25, ease: "easeOut" }, style: { overflow: "hidden" }, className: "anyspend-stripe-address-element", children: _jsx(AddressElement, { options: {
|
|
215
|
+
mode: "billing",
|
|
216
|
+
fields: {
|
|
217
|
+
phone: "always",
|
|
218
|
+
},
|
|
219
|
+
display: {
|
|
220
|
+
name: "split",
|
|
221
|
+
},
|
|
222
|
+
validation: {
|
|
223
|
+
phone: {
|
|
224
|
+
required: "always",
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
} }) }, "address-element")) }), _jsx(AnimatePresence, { initial: false, children: message && (_jsx(motion.div, { initial: { opacity: 0, height: 0 }, animate: { opacity: 1, height: "auto" }, exit: { opacity: 0, height: 0 }, transition: { duration: 0.2, ease: "easeOut" }, style: { overflow: "hidden" }, className: "anyspend-stripe-error rounded-lg border border-red-200 bg-red-50 px-3 py-2 dark:border-red-800 dark:bg-red-900/20", children: _jsx("p", { className: "text-sm text-red-600 dark:text-red-400", children: message }) }, "stripe-error")) }), _jsx("button", { type: "submit", disabled: !stripe || !elements || loading, className: cn("anyspend-stripe-submit flex w-full items-center justify-center gap-2 rounded-xl px-4 py-3.5 text-sm font-semibold text-white transition-all", !stripe || !elements || loading
|
|
228
|
+
? "cursor-not-allowed bg-gray-300 dark:bg-gray-600"
|
|
229
|
+
: "bg-blue-600 hover:bg-blue-700 active:scale-[0.98]", classes?.payButton), style: themeColor && !loading ? { backgroundColor: themeColor } : undefined, children: loading ? (_jsxs(_Fragment, { children: [_jsx(Loader2, { className: "h-4 w-4 animate-spin" }), "Processing..."] })) : (_jsxs(_Fragment, { children: [_jsx(Lock, { className: "h-3.5 w-3.5" }), "Complete Payment"] })) }), _jsxs("p", { className: "anyspend-fiat-secured flex items-center justify-center gap-1 text-xs text-gray-400", children: [_jsx(Lock, { className: "h-3 w-3" }), "Secured by Stripe"] })] }));
|
|
230
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AnySpendCheckoutClasses } from "./AnySpendCheckout";
|
|
2
|
+
interface PoweredByBrandingProps {
|
|
3
|
+
organizationName?: string;
|
|
4
|
+
organizationLogo?: string;
|
|
5
|
+
classes?: AnySpendCheckoutClasses;
|
|
6
|
+
}
|
|
7
|
+
export declare function PoweredByBranding({ organizationName, organizationLogo, classes }: PoweredByBrandingProps): import("react/jsx-runtime").JSX.Element;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { cn } from "../../../../shared/utils/cn.js";
|
|
4
|
+
export function PoweredByBranding({ organizationName, organizationLogo, classes }) {
|
|
5
|
+
return (_jsxs("div", { className: cn("flex items-center justify-between pt-4", classes?.poweredBy), children: [organizationLogo || organizationName ? (_jsxs("div", { className: "flex items-center gap-2", children: [organizationLogo && (_jsx("img", { src: organizationLogo, alt: organizationName || "Organization", className: "h-5 w-5 rounded-full" })), organizationName && (_jsx("span", { className: "text-xs font-medium text-gray-600 dark:text-gray-400", children: organizationName }))] })) : (_jsx("div", {})), _jsxs("span", { className: "text-xs text-gray-400 dark:text-gray-500", children: ["powered by ", _jsx("span", { className: "font-medium text-gray-500 dark:text-gray-400", children: "anyspend" })] })] }));
|
|
6
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AnySpendCheckoutClasses } from "./AnySpendCheckout";
|
|
2
|
+
interface QRCheckoutPanelProps {
|
|
3
|
+
recipientAddress: string;
|
|
4
|
+
destinationTokenAddress: string;
|
|
5
|
+
destinationTokenChainId: number;
|
|
6
|
+
totalAmount: string;
|
|
7
|
+
themeColor?: string;
|
|
8
|
+
onSuccess?: (result: {
|
|
9
|
+
txHash?: string;
|
|
10
|
+
orderId?: string;
|
|
11
|
+
}) => void;
|
|
12
|
+
onError?: (error: Error) => void;
|
|
13
|
+
callbackMetadata?: Record<string, unknown>;
|
|
14
|
+
classes?: AnySpendCheckoutClasses;
|
|
15
|
+
}
|
|
16
|
+
export declare function QRCheckoutPanel({ recipientAddress, destinationTokenAddress, destinationTokenChainId, totalAmount, themeColor, onSuccess, onError, callbackMetadata: _callbackMetadata, classes, }: QRCheckoutPanelProps): import("react/jsx-runtime").JSX.Element;
|
|
17
|
+
export {};
|