|
| 1 | +import type { Metadata } from "next"; |
| 2 | +import "@workspace/ui/global.css"; |
| 3 | +import { InlineCode } from "@workspace/ui/components/code/inline-code"; |
| 4 | +import { AlertTriangleIcon } from "lucide-react"; |
| 5 | +import type { SupportedFiatCurrency } from "thirdweb/react"; |
| 6 | +import { NEXT_PUBLIC_CHECKOUT_IFRAME_CLIENT_ID } from "@/constants/public-envs"; |
| 7 | +import { isValidCurrency } from "../_common/isValidCurrency"; |
| 8 | +import { |
| 9 | + onlyAddress, |
| 10 | + onlyNumber, |
| 11 | + parseQueryParams, |
| 12 | +} from "../_common/parseQueryParams"; |
| 13 | +import { BridgeProviders } from "../(general)/components/client/Providers.client"; |
| 14 | +import { CheckoutWidgetEmbed } from "./CheckoutWidgetEmbed.client"; |
| 15 | + |
| 16 | +const title = "thirdweb Checkout: Accept Crypto & Fiat Payments"; |
| 17 | +const description = |
| 18 | + "Accept fiat or crypto payments on any chain—direct to your wallet. Instant checkout, webhook support, and full control over post-sale actions."; |
| 19 | + |
| 20 | +export const metadata: Metadata = { |
| 21 | + description, |
| 22 | + openGraph: { |
| 23 | + description, |
| 24 | + title, |
| 25 | + }, |
| 26 | + title, |
| 27 | +}; |
| 28 | + |
| 29 | +type SearchParams = { |
| 30 | + [key: string]: string | string[] | undefined; |
| 31 | +}; |
| 32 | + |
| 33 | +export default async function Page(props: { |
| 34 | + searchParams: Promise<SearchParams>; |
| 35 | +}) { |
| 36 | + const searchParams = await props.searchParams; |
| 37 | + |
| 38 | + // Required params |
| 39 | + const chainId = parseQueryParams(searchParams.chain, onlyNumber); |
| 40 | + const amount = parseQueryParams(searchParams.amount, (v) => v); |
| 41 | + const seller = parseQueryParams(searchParams.seller, onlyAddress); |
| 42 | + |
| 43 | + // Optional params |
| 44 | + const tokenAddress = parseQueryParams(searchParams.tokenAddress, onlyAddress); |
| 45 | + const title = parseQueryParams(searchParams.title, (v) => v); |
| 46 | + const productDescription = parseQueryParams( |
| 47 | + searchParams.description, |
| 48 | + (v) => v, |
| 49 | + ); |
| 50 | + const image = parseQueryParams(searchParams.image, (v) => v); |
| 51 | + const buttonLabel = parseQueryParams(searchParams.buttonLabel, (v) => v); |
| 52 | + const feePayer = parseQueryParams(searchParams.feePayer, (v) => |
| 53 | + v === "seller" || v === "user" ? v : undefined, |
| 54 | + ); |
| 55 | + const country = parseQueryParams(searchParams.country, (v) => v); |
| 56 | + |
| 57 | + const showThirdwebBranding = parseQueryParams( |
| 58 | + searchParams.showThirdwebBranding, |
| 59 | + (v) => v !== "false", |
| 60 | + ); |
| 61 | + |
| 62 | + const theme = |
| 63 | + parseQueryParams(searchParams.theme, (v) => |
| 64 | + v === "light" ? "light" : "dark", |
| 65 | + ) || "dark"; |
| 66 | + |
| 67 | + const currency = parseQueryParams(searchParams.currency, (v) => |
| 68 | + isValidCurrency(v) ? (v as SupportedFiatCurrency) : undefined, |
| 69 | + ); |
| 70 | + |
| 71 | + // Validate required params |
| 72 | + if (!chainId || !amount || !seller) { |
| 73 | + return ( |
| 74 | + <Providers theme={theme}> |
| 75 | + <div className="flex min-h-screen items-center justify-center bg-background px-4 py-8"> |
| 76 | + <div className="w-full max-w-lg rounded-xl border bg-card p-6 shadow-xl"> |
| 77 | + <div className="p-2.5 inline-flex rounded-full bg-background mb-4 border"> |
| 78 | + <AlertTriangleIcon className="size-5 text-destructive-text" /> |
| 79 | + </div> |
| 80 | + <h2 className="mb-2 font-semibold text-destructive-text text-lg"> |
| 81 | + Invalid Configuration |
| 82 | + </h2> |
| 83 | + <p className="text-muted-foreground text-sm mb-4"> |
| 84 | + The following query parameters are required but are missing: |
| 85 | + </p> |
| 86 | + <ul className="mt-2 text-left text-muted-foreground text-sm space-y-2"> |
| 87 | + {!chainId && ( |
| 88 | + <li> |
| 89 | + • <InlineCode code="chain" /> - Chain ID (e.g., 1, 8453, |
| 90 | + 42161) |
| 91 | + </li> |
| 92 | + )} |
| 93 | + {!amount && ( |
| 94 | + <li> |
| 95 | + • <InlineCode code="amount" /> - Amount to charge (e.g., |
| 96 | + "0.01") |
| 97 | + </li> |
| 98 | + )} |
| 99 | + {!seller && ( |
| 100 | + <li> |
| 101 | + • <InlineCode code="seller" /> - Seller wallet address |
| 102 | + </li> |
| 103 | + )} |
| 104 | + </ul> |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + </Providers> |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + return ( |
| 112 | + <Providers theme={theme}> |
| 113 | + <div className="flex min-h-screen items-center justify-center bg-background px-4 py-8"> |
| 114 | + <CheckoutWidgetEmbed |
| 115 | + chainId={chainId} |
| 116 | + amount={amount} |
| 117 | + seller={seller} |
| 118 | + tokenAddress={tokenAddress} |
| 119 | + name={title} |
| 120 | + description={productDescription} |
| 121 | + image={image} |
| 122 | + buttonLabel={buttonLabel} |
| 123 | + feePayer={feePayer} |
| 124 | + country={country} |
| 125 | + showThirdwebBranding={showThirdwebBranding} |
| 126 | + theme={theme} |
| 127 | + currency={currency} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + </Providers> |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +function Providers({ |
| 135 | + children, |
| 136 | + theme, |
| 137 | +}: { |
| 138 | + children: React.ReactNode; |
| 139 | + theme: string; |
| 140 | +}) { |
| 141 | + if (!NEXT_PUBLIC_CHECKOUT_IFRAME_CLIENT_ID) { |
| 142 | + throw new Error("NEXT_PUBLIC_CHECKOUT_IFRAME_CLIENT_ID is not set"); |
| 143 | + } |
| 144 | + return ( |
| 145 | + <BridgeProviders |
| 146 | + clientId={NEXT_PUBLIC_CHECKOUT_IFRAME_CLIENT_ID} |
| 147 | + forcedTheme={theme} |
| 148 | + > |
| 149 | + {children} |
| 150 | + </BridgeProviders> |
| 151 | + ); |
| 152 | +} |
0 commit comments