app/pay/checkout.tsx (Payment Element version)
"use client";
import { useRouter } from "next/navigation";
import { loadStripe, type Appearance } from "@stripe/stripe-js";
import { Elements, PaymentElement, useStripe, useElements } from "@stripe/react-stripe-js";
import CardCheckout from "@/components/CardCheckout";
const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY!);
// The free part: Stripe's Appearance API, set to the card's dark colours.
const appearance: Appearance = {
theme: "night",
labels: "above",
variables: {
colorPrimary: "#C6FF3D",
colorBackground: "#0d0e13",
colorText: "#F4F4F6",
colorDanger: "#FF5C6C",
fontFamily: "Space Grotesk, system-ui, sans-serif",
borderRadius: "14px",
},
rules: {
".Input": { border: "1px solid #2a2e3b", fontSize: "16px" },
".Input:focus": { borderColor: "#C6FF3D", boxShadow: "0 0 0 4px rgba(198, 255, 61, 0.14)" },
".Label": { color: "#b4b8c8", fontWeight: "500" },
".Tab--selected": { borderColor: "#C6FF3D" },
},
};
function PaymentForm({ amount }: { amount: number }) {
const stripe = useStripe();
const elements = useElements();
const router = useRouter();
return (
<CardCheckout
amount={amount}
currency="USD"
productName="Pro plan"
resetAfter={null}
renderCardFields={(slot) => (
<PaymentElement
options={{ layout: "tabs" }}
onChange={(e) => slot.onChange({ complete: e.complete })}
onReady={(element) => {
// React Stripe.js 7.0.0 has no prop for this event, so attach it here.
element.on("carddetailschange", (e) => {
slot.onChange({ brand: e.details?.brands?.[0] ?? null });
});
}}
/>
)}
onConfirm={async ({ name }) => {
if (!stripe || !elements) throw new Error("Stripe hasn't loaded yet.");
const { error, paymentIntent } = await stripe.confirmPayment({
elements,
redirect: "if_required", // cards stay on the page, redirect methods leave
confirmParams: {
return_url: `${window.location.origin}/thanks`,
payment_method_data: { billing_details: { name } },
},
});
if (error) throw error; // the card shows Stripe's message and Pay comes back
setTimeout(() => router.push(`/thanks?pi=${paymentIntent.id}`), 1200);
}}
/>
);
}
export default function Checkout({ clientSecret, amount }: { clientSecret: string; amount: number }) {
return (
<Elements stripe={stripePromise} options={{ clientSecret, appearance }}>
<PaymentForm amount={amount} />
</Elements>
);
}
app/pay/page.tsx (the changed call)
// app/pay/page.tsx: the call that changes from the card-only page above
const intent = await stripe.paymentIntents.create({
amount: 2900,
currency: "usd",
automatic_payment_methods: { enabled: true }, // was allowed_payment_method_types: ["card"]
metadata: { plan: "pro" },
});