WebsiteBench / tasks /linkedin /solution /site /implementation /clone /components /StripeCardForm.tsx
Download tasks/linkedin/solution/site/implementation/clone/components/StripeCardForm.tsx from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 4.41 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/solution/site/implementation/clone/components/StripeCardForm.tsx
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/linkedin/solution/site/implementation/clone/components/StripeCardForm.tsx
-
curl -L -o StripeCardForm.tsx https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/solution/site/implementation/clone/components/StripeCardForm.tsx
4.41 kB
| "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> | |
| ); | |
| } | |