"use client"; import { FormEvent, useState } from "react"; import { CreditCard, LockKeyhole } from "lucide-react"; type Subscription = { status: string; currentPeriodEnd: string | null; cancelAtPeriodEnd: boolean; }; type Props = { setupIntentId: string; onComplete: (subscription: Subscription) => void; onCancel: () => void; }; export function StripeCardForm({ setupIntentId, onComplete, onCancel }: Props) { const [cardholderName, setCardholderName] = useState(""); const [cardNumber, setCardNumber] = useState(""); const [expiration, setExpiration] = useState(""); const [securityCode, setSecurityCode] = useState(""); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(""); async function submit(event: FormEvent) { event.preventDefault(); const name = cardholderName.trim(); if (!name) { setError("Enter the cardholder name."); return; } if (!/^\d{12,19}$/u.test(cardNumber.replace(/\s/gu, ""))) { setError("Enter a card number for the test simulation."); return; } if (!/^(0[1-9]|1[0-2])\s*\/\s*\d{2}$/u.test(expiration)) { setError("Enter the expiration date as MM / YY."); return; } if (!/^\d{3,4}$/u.test(securityCode)) { setError("Enter a 3 or 4 digit security code."); return; } setSubmitting(true); setError(""); try { const response = await fetch("/api/billing/complete", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ setupIntentId }), }); const result = await response.json() as { subscription?: Subscription; error?: string }; if (!response.ok || !result.subscription) throw new Error(result.error ?? "Unable to activate Premium."); onComplete(result.subscription); } catch (caught) { setError(caught instanceof Error ? caught.message : "Unable to verify this card."); setSubmitting(false); } } return (
Add a cardStripe Test Mode

Test values stay in this browser and are never stored. Stripe receives its standard sandbox card token.

{error &&
{error}
}
); }