|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + BuyWithFiatQuote, |
| 5 | + getBuyWithFiatQuote, |
| 6 | + getBuyWithFiatStatus, |
| 7 | +} from "thirdweb/pay"; |
| 8 | +import { NATIVE_TOKEN_ADDRESS, ThirdwebClient } from "thirdweb"; |
| 9 | +import { base } from "thirdweb/chains"; |
| 10 | +import { isSwapRequiredPostOnramp } from "thirdweb/pay"; |
| 11 | +import { useEffect, useState } from "react"; |
| 12 | + |
| 13 | +export default function DirectPayment({ |
| 14 | + client, |
| 15 | + fromAddress, |
| 16 | +}: { |
| 17 | + client: ThirdwebClient; |
| 18 | + fromAddress: string; |
| 19 | +}) { |
| 20 | + const [quote, setQuote] = useState<BuyWithFiatQuote | null>(null); |
| 21 | + const [polling, setPolling] = useState<NodeJS.Timeout | null>(null); |
| 22 | + const [isLoading, setIsLoading] = useState(false); |
| 23 | + const [isSuccess, setIsSuccess] = useState(false); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + if (quote) { |
| 27 | + startPolling(); |
| 28 | + } |
| 29 | + }, [quote]); |
| 30 | + |
| 31 | + const startPolling = () => { |
| 32 | + stopPolling(); |
| 33 | + const interval = setInterval(pollStatus, 10000); |
| 34 | + setPolling(interval); |
| 35 | + }; |
| 36 | + |
| 37 | + const stopPolling = () => { |
| 38 | + if (polling) { |
| 39 | + clearInterval(polling); |
| 40 | + setPolling(null); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + const pollStatus = async () => { |
| 45 | + setIsLoading(true); |
| 46 | + |
| 47 | + if (quote && quote.intentId) { |
| 48 | + try { |
| 49 | + const fiatStatus = await getBuyWithFiatStatus({ |
| 50 | + client, |
| 51 | + intentId: quote.intentId, |
| 52 | + }); |
| 53 | + const currentStatus = fiatStatus.status as string; |
| 54 | + const hasTwoSteps = isSwapRequiredPostOnramp(quote); |
| 55 | + |
| 56 | + if (currentStatus === "NOT_FOUND") { |
| 57 | + alert("Error: Intent ID not found"); |
| 58 | + } |
| 59 | + |
| 60 | + if ( |
| 61 | + currentStatus === "NONE" || |
| 62 | + currentStatus === "PENDING_PAYMENT" || |
| 63 | + currentStatus === "PENDING_ON_RAMP_TRANSFER" || |
| 64 | + currentStatus === "ON_RAMP_TRANSFER_IN_PROGRESS" |
| 65 | + ) { |
| 66 | + console.log("Current status: ", currentStatus); |
| 67 | + } |
| 68 | + |
| 69 | + if (currentStatus === "ON_RAMP_TRANSFER_FAILED") { |
| 70 | + alert("Error: Onramp transfer failed"); |
| 71 | + stopPolling(); |
| 72 | + } |
| 73 | + |
| 74 | + if (currentStatus === "ON_RAMP_TRANSFER_COMPLETED") { |
| 75 | + // if only on-ramp is required - process is done! |
| 76 | + if (!hasTwoSteps) { |
| 77 | + alert("Payment successful"); |
| 78 | + setIsSuccess(true); |
| 79 | + stopPolling(); |
| 80 | + } else { |
| 81 | + alert("Crypto Swap Required"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (currentStatus === "CRYPTO_SWAP_REQUIRED") { |
| 86 | + alert("This is where you swap the crypto for fiat"); |
| 87 | + } |
| 88 | + } catch (error) { |
| 89 | + console.log("Failed to get status", error); |
| 90 | + stopPolling(); |
| 91 | + } |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + const sendFiatTransaction = async () => { |
| 96 | + // Get a quote for buying 0.01 Base ETH with USD |
| 97 | + try { |
| 98 | + const quote = await getBuyWithFiatQuote({ |
| 99 | + client, // thirdweb client |
| 100 | + fromCurrencySymbol: "USD", // fiat currency symbol |
| 101 | + toChainId: base.id, // base chain id |
| 102 | + toAmount: "0.01", // amount of token to buy |
| 103 | + toTokenAddress: NATIVE_TOKEN_ADDRESS, // native token |
| 104 | + toAddress: `${process.env.NEXT_PUBLIC_WALLET_ADDRESS}`, |
| 105 | + isTestMode: true, |
| 106 | + fromAddress, |
| 107 | + purchaseData: { nftId: 1 }, |
| 108 | + }); |
| 109 | + |
| 110 | + setQuote(quote); |
| 111 | + window.open(quote.onRampLink, "_blank"); |
| 112 | + } catch (error) { |
| 113 | + console.error("Error sending transaction:", error); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + return ( |
| 118 | + <button |
| 119 | + className="flex w-full justify-center rounded-lg border border-gray-400 px-4 py-2 hover:bg-gray-300 hover:border-gray-300 hover:text-black active:opacity-80" |
| 120 | + onClick={() => sendFiatTransaction()} |
| 121 | + > |
| 122 | + {isSuccess ? "Success" : isLoading ? "Loading..." : "Initiate Payment"} |
| 123 | + </button> |
| 124 | + ); |
| 125 | +} |
0 commit comments