|
| 1 | +import { PaymentElement, useCheckout } from "@stripe/react-stripe-js" |
| 2 | +import classNames from "classnames" |
| 3 | +import Link from "next/link" |
| 4 | +import { ChangeEvent, FormEvent, useCallback, useState } from "react" |
| 5 | +import { FormattedMessage } from "react-intl" |
| 6 | + |
| 7 | +import LoadingIcon from "../../public/icons/loading.svg?inline" |
| 8 | +import ArrowLeftIcon from "../../public/ui/arrow-left.svg?inline" |
| 9 | + |
| 10 | +import { Button } from "../Button" |
| 11 | +import { Input } from "../Input" |
| 12 | + |
| 13 | +interface DonateCheckoutProps { |
| 14 | + backUrl?: string |
| 15 | + className?: string |
| 16 | + onComplete: () => void |
| 17 | +} |
| 18 | + |
| 19 | +export function DonateCheckout({ |
| 20 | + className, |
| 21 | + backUrl, |
| 22 | + onComplete, |
| 23 | +}: DonateCheckoutProps) { |
| 24 | + const checkout = useCheckout() |
| 25 | + |
| 26 | + const [email, setEmail] = useState("") |
| 27 | + const [errorMessage, setErrorMessage] = useState<string | null>(null) |
| 28 | + const [isLoading, setIsLoading] = useState(false) |
| 29 | + |
| 30 | + const handleChange = useCallback((e: ChangeEvent<HTMLInputElement>) => { |
| 31 | + setErrorMessage(null) |
| 32 | + setEmail(e.target.value) |
| 33 | + }, []) |
| 34 | + |
| 35 | + const handleEmailBlur = useCallback(async () => { |
| 36 | + if (!email) { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + const result = await checkout.updateEmail(email) |
| 41 | + if (result.type === "error") { |
| 42 | + setErrorMessage(result.error.message) |
| 43 | + } else { |
| 44 | + setErrorMessage(null) |
| 45 | + } |
| 46 | + }, [checkout, email]) |
| 47 | + |
| 48 | + const handleCheckout = useCallback( |
| 49 | + async (e: FormEvent<HTMLFormElement>) => { |
| 50 | + e.preventDefault() |
| 51 | + |
| 52 | + setIsLoading(true) |
| 53 | + |
| 54 | + const result = await checkout.updateEmail(email) |
| 55 | + if (result.type === "error") { |
| 56 | + setErrorMessage(result.error.message) |
| 57 | + setIsLoading(false) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + const confirmResult = await checkout.confirm({ |
| 62 | + redirect: "if_required", // Only redirect if required |
| 63 | + }) |
| 64 | + |
| 65 | + if (confirmResult.type === "error") { |
| 66 | + setErrorMessage(confirmResult.error.message) |
| 67 | + setIsLoading(false) |
| 68 | + } else { |
| 69 | + onComplete() |
| 70 | + } |
| 71 | + }, |
| 72 | + [checkout, email, onComplete] |
| 73 | + ) |
| 74 | + |
| 75 | + return ( |
| 76 | + <form |
| 77 | + className={classNames("dark:text-white", className)} |
| 78 | + onSubmit={handleCheckout} |
| 79 | + > |
| 80 | + <header className="mb-4"> |
| 81 | + <h3 className="text-b1"> |
| 82 | + {checkout.recurring ? ( |
| 83 | + <FormattedMessage |
| 84 | + id="donate_widget.checkout.header.recurring" |
| 85 | + defaultMessage="You are donating {total} every {frequency}" |
| 86 | + values={{ |
| 87 | + total: checkout.total.total.amount, |
| 88 | + frequency: checkout.recurring.interval, |
| 89 | + }} |
| 90 | + /> |
| 91 | + ) : ( |
| 92 | + <FormattedMessage |
| 93 | + id="donate_widget.checkout.header.one_time" |
| 94 | + defaultMessage="You are donating {total} once" |
| 95 | + values={{ |
| 96 | + total: checkout.total.total.amount, |
| 97 | + }} |
| 98 | + /> |
| 99 | + )} |
| 100 | + </h3> |
| 101 | + {backUrl && ( |
| 102 | + <Link |
| 103 | + href={backUrl} |
| 104 | + className="text-gray-1 text-b3 mt-2 flex gap-1 items-center" |
| 105 | + > |
| 106 | + <ArrowLeftIcon className="size-4" /> |
| 107 | + <FormattedMessage |
| 108 | + id="donate_widget.checkout.header.back" |
| 109 | + defaultMessage="Edit your donation" |
| 110 | + /> |
| 111 | + </Link> |
| 112 | + )} |
| 113 | + </header> |
| 114 | + <hr className="my-4 border-t border-gray-2" /> |
| 115 | + <div className="flex max-sm:flex-col gap-4"> |
| 116 | + <label className="w-full"> |
| 117 | + <FormattedMessage |
| 118 | + id="donate_widget.checkout.email" |
| 119 | + defaultMessage="Email" |
| 120 | + > |
| 121 | + {(text) => <p className="mb-2">{text}</p>} |
| 122 | + </FormattedMessage> |
| 123 | + <Input |
| 124 | + type="email" |
| 125 | + value={email} |
| 126 | + onChange={handleChange} |
| 127 | + |
| 128 | + onBlur={handleEmailBlur} |
| 129 | + disabled={isLoading} |
| 130 | + fullWidth |
| 131 | + /> |
| 132 | + </label> |
| 133 | + |
| 134 | + <div className="w-full"> |
| 135 | + <h4 className="mb-2"> |
| 136 | + <FormattedMessage |
| 137 | + id="donate_widget.checkout.payment" |
| 138 | + defaultMessage="Payment" |
| 139 | + /> |
| 140 | + </h4> |
| 141 | + <PaymentElement className="" /> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + |
| 145 | + <div className="mt-4"> |
| 146 | + {errorMessage && ( |
| 147 | + <p className="text-error text-b3 mb-2">{errorMessage}</p> |
| 148 | + )} |
| 149 | + <Button |
| 150 | + disabled={isLoading} |
| 151 | + dark |
| 152 | + className={classNames( |
| 153 | + "flex gap-2 items-center justify-center", |
| 154 | + isLoading && "text-gray-2" |
| 155 | + )} |
| 156 | + fullWidth |
| 157 | + type="submit" |
| 158 | + > |
| 159 | + {isLoading ? ( |
| 160 | + <> |
| 161 | + <LoadingIcon className="motion-safe:animate-spin size-5" /> |
| 162 | + <FormattedMessage |
| 163 | + id="donate_widget.checkout.submitting" |
| 164 | + defaultMessage="Submitting…" |
| 165 | + /> |
| 166 | + </> |
| 167 | + ) : ( |
| 168 | + <FormattedMessage |
| 169 | + id="donate_widget.checkout.pay_button" |
| 170 | + defaultMessage="Pay {total} now" |
| 171 | + values={{ |
| 172 | + total: checkout.total.total.amount, |
| 173 | + }} |
| 174 | + /> |
| 175 | + )} |
| 176 | + </Button> |
| 177 | + </div> |
| 178 | + </form> |
| 179 | + ) |
| 180 | +} |
0 commit comments