Payment Failed Alert
Billing alert that retries a declined payment through to a paid state, with attempt history.
"use client"
import { useState } from "react"
import {
CheckCircleIcon,
CreditCardIcon,
WarningCircleIcon,
} from "@phosphor-icons/react"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
import { Button } from "@/components/ui/button"
import { Field, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import { Spinner } from "@/components/ui/spinner"
const COPY = {
failed: {
title: "Your payment of $148.00 was declined",
body: "The Visa ending 4417 was declined by the bank. Your workspace stays active until Nov 14, then moves to read-only.",
},
retrying: {
title: "Retrying payment",
body: "Contacting your bank. This takes a few seconds.",
},
paid: {
title: "Payment received",
body: "$148.00 was charged to Visa 4417. Your receipt is in your inbox.",
},
cardUpdated: (card: string) =>
`Card updated to ${card}. Retry the payment to charge it.`,
paidWith: (card: string) =>
`$148.00 was charged to ${card}. Your receipt is in your inbox.`,
cardLabel: "New card number",
cardPlaceholder: "Visa ending 1234",
save: "Save card",
cancel: "Cancel",
retry: "Retry payment",
update: "Update card",
declined: "Declined",
next: "Next automatic retry in 2 days",
} as const
const ATTEMPTS = [
{ date: "Nov 10", note: "Insufficient funds", state: "Declined" },
{ date: "Nov 7", note: "Card expired", state: "Declined" },
] as const
type Phase = "failed" | "retrying" | "paid"
/** Billing alert that retries a failed payment through to a paid state. */
export default function AlertPaymentFailed01() {
const [phase, setPhase] = useState<Phase>("failed")
const [editing, setEditing] = useState(false)
const [card, setCard] = useState("")
const [draft, setDraft] = useState("")
function saveCard() {
const next = draft.trim()
if (!next) return
setCard(next)
setEditing(false)
}
// Your payment retry goes here; this one succeeds after a moment.
function retry() {
setPhase("retrying")
setTimeout(() => setPhase("paid"), 1600)
}
const c = {
...COPY[phase],
body:
phase === "paid" && card
? COPY.paidWith(card)
: phase === "failed" && card
? COPY.cardUpdated(card)
: COPY[phase].body,
}
return (
<div className="mx-auto flex w-full max-w-2xl flex-col gap-4">
<Alert
variant={phase === "failed" ? "destructive" : "default"}
aria-live="polite"
>
{phase === "retrying" ? (
<Spinner />
) : phase === "paid" ? (
<CheckCircleIcon aria-hidden="true" />
) : (
<WarningCircleIcon aria-hidden="true" />
)}
<AlertTitle>{c.title}</AlertTitle>
<AlertDescription>{c.body}</AlertDescription>
{phase === "failed" && (
<div className="col-start-2 mt-3 flex gap-2">
<Button size="sm" onClick={retry}>
{COPY.retry}
</Button>
<Button
size="sm"
variant="outline"
aria-expanded={editing}
onClick={() => {
setDraft(card)
setEditing((open) => !open)
}}
>
<CreditCardIcon data-icon="inline-start" />
{COPY.update}
</Button>
</div>
)}
{phase === "failed" && editing && (
<form
className="col-start-2 mt-3 flex items-end gap-2"
onSubmit={(event) => {
event.preventDefault()
saveCard()
}}
>
<Field className="flex-1">
<FieldLabel htmlFor="alert-payment-failed-01-card">
{COPY.cardLabel}
</FieldLabel>
<Input
id="alert-payment-failed-01-card"
value={draft}
placeholder={COPY.cardPlaceholder}
onChange={(event) => setDraft(event.target.value)}
/>
</Field>
<Button type="submit" size="sm" disabled={!draft.trim()}>
{COPY.save}
</Button>
<Button
type="button"
size="sm"
variant="ghost"
onClick={() => setEditing(false)}
>
{COPY.cancel}
</Button>
</form>
)}
</Alert>
{phase === "failed" && (
<div className="rounded-2xl border bg-card p-4 text-sm">
<ul className="divide-y">
{ATTEMPTS.map((a) => (
<li
key={a.date}
className="flex items-center justify-between py-2 first:pt-0"
>
<span className="font-medium">{a.date}</span>
<span className="text-muted-foreground">{a.note}</span>
<span className="text-destructive">{a.state}</span>
</li>
))}
</ul>
<p className="mt-3 text-xs text-muted-foreground">{COPY.next}</p>
</div>
)}
</div>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/payment-failed-01npx shadcn@latest add @01orbital/payment-failed-01yarn dlx shadcn@latest add @01orbital/payment-failed-01bunx --bun shadcn@latest add @01orbital/payment-failed-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/alert/payment-failed-01/index.tsx: change them there, not in the markup. ATTEMPTS, also at the top of the file, holds its lists and settings.
Built from
npm: @phosphor-icons/react. The CLI installs it for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on