File size: 4,412 Bytes
062a991 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | "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<HTMLFormElement>) {
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 (
<form className="stripe-card-form" onSubmit={submit}>
<div className="stripe-card-heading">
<CreditCard size={19} />
<div><b>Add a card</b><small>Stripe Test Mode</small></div>
</div>
<label className="stripe-card-label">
<span>Cardholder name</span>
<input
autoComplete="cc-name"
maxLength={100}
value={cardholderName}
onChange={(event) => setCardholderName(event.target.value)}
placeholder="Name on card"
required
/>
</label>
<label className="stripe-card-label">
<span>Card number</span>
<input
autoComplete="off"
inputMode="numeric"
maxLength={23}
value={cardNumber}
onChange={(event) => setCardNumber(event.target.value.replace(/[^\d ]/gu, ""))}
placeholder="1234 5678 9012 3456"
required
/>
</label>
<div className="stripe-card-row">
<label className="stripe-card-label">
<span>Expiration date</span>
<input
autoComplete="off"
inputMode="numeric"
maxLength={7}
value={expiration}
onChange={(event) => setExpiration(event.target.value.replace(/[^\d /]/gu, ""))}
placeholder="MM / YY"
required
/>
</label>
<label className="stripe-card-label">
<span>Security code</span>
<input
autoComplete="off"
inputMode="numeric"
maxLength={4}
value={securityCode}
onChange={(event) => setSecurityCode(event.target.value.replace(/\D/gu, ""))}
placeholder="CVC"
required
/>
</label>
</div>
<p className="stripe-card-security"><LockKeyhole size={15} /> Test values stay in this browser and are never stored. Stripe receives its standard sandbox card token.</p>
{error && <div className="form-error" role="alert">{error}</div>}
<button className="button button-primary button-full" disabled={submitting} type="submit">
<CreditCard size={18} /> {submitting ? "Verifying with Stripe…" : "Add card and subscribe"}
</button>
<button className="stripe-card-cancel" disabled={submitting} onClick={onCancel} type="button">Cancel</button>
</form>
);
}
|