Gift packages: buy a main package for someone, with a printable gift card

- /ajandek: the package, whom it is for and from whom, the recipient's
  address typed twice (or "I hand it over"), a message; paid on Stripe
  like any package, the invoice is the buyer's
- On payment the gift gets its own code (not a promo code: it cannot be
  withdrawn, and it adds the whole package); redeemed on /bevaltas
- The recipient's e-mail carries the message and the code, with the gift
  card attached as a PDF; the buyer's confirmation says it was a gift
- /ajandekaim: codes, state, the card to download, sending it by e-mail
  (again, or to another address) up to three times
- crates/gift-card draws the A4 card (example chart, questions avatar,
  QR code) as SVG and makes a PDF with krilla, fonts compiled in; the
  backend draws downloads on a blocking thread, the worker the e-mails
- Telegram notices say "(ajándék)"; unpaid gifts go after a week

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-23 20:28:11 +00:00
co-authored by Claude Opus 5.5
parent 699ce2abbd
commit c90232f40c
46 changed files with 3647 additions and 88 deletions
@@ -0,0 +1,251 @@
"use client";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useId, useState } from "react";
import { huf } from "@/components/PackageCards";
import { api, errorText } from "@/lib/api";
import { contents } from "@/lib/packageContents";
import type { Package, PayMethod } from "@/lib/types";
const MAX_MESSAGE = 300;
const MAX_NAME = 60;
const looksLikeEmail = (s: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s.trim());
const same = (a: string, b: string) => a.trim().toLowerCase() === b.trim().toLowerCase();
/**
* The gift order: which package, whom it is for and from whom, how it gets
* to the recipient (we e-mail it, the address typed twice; or the buyer
* hands it over), and a message. Then Stripe's payment page, back to "My
* gifts".
*/
export function GiftForm({
packages,
initial,
defaultFrom,
canPay,
termsConsent,
}: {
packages: Package[];
initial?: string;
defaultFrom: string;
canPay: PayMethod | null;
termsConsent: boolean;
}) {
const router = useRouter();
const id = useId();
const start = packages.find((p) => p.id === initial) ?? packages.find((p) => p.id === "couple") ?? packages[0];
const [pkg, setPkg] = useState(start?.id ?? "");
const [name, setName] = useState("");
const [byEmail, setByEmail] = useState(true);
const [email, setEmail] = useState("");
const [confirm, setConfirm] = useState("");
const [from, setFrom] = useState(defaultFrom.slice(0, MAX_NAME));
const [message, setMessage] = useState("");
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
// Back from Stripe's page (the browser may restore this page as it was).
useEffect(() => {
const reset = (e: PageTransitionEvent) => {
if (e.persisted) setBusy(false);
};
window.addEventListener("pageshow", reset);
return () => window.removeEventListener("pageshow", reset);
}, []);
const chosen = packages.find((p) => p.id === pkg);
const mismatch = byEmail && confirm.trim() !== "" && !same(email, confirm);
const emailOk = !byEmail || (looksLikeEmail(email) && same(email, confirm));
const stripe = canPay === "stripe" || canPay === "stripe_test";
async function submit(e: React.FormEvent) {
e.preventDefault();
if (!chosen || !emailOk || !name.trim()) return;
setBusy(true);
setError(null);
try {
const r = await api<{ id: string; url: string | null }>("/gifts/checkout", {
body: {
package: chosen.id,
recipient_name: name.trim(),
recipient_email: byEmail ? email.trim() : null,
recipient_email_confirm: byEmail ? confirm.trim() : null,
from_name: from.trim() || null,
message: message.trim() || null,
},
});
if (r.url) {
window.location.assign(r.url);
return;
}
router.push("/ajandekaim");
} catch (err) {
setError(errorText(err));
setBusy(false);
}
}
return (
<form className="card gift-form" onSubmit={submit}>
<fieldset className="gift-set">
<legend className="label">Melyik csomagot adod?</legend>
<div className="gift-packages">
{packages.map((p) => (
<label key={p.id} className={p.id === pkg ? "gift-option is-chosen" : "gift-option"}>
<input type="radio" name={`${id}-pkg`} value={p.id} checked={p.id === pkg} onChange={() => setPkg(p.id)} />
<span className="gift-option-head">
<strong>{p.name}</strong>
<span className="num">{huf(p.price_huf)}</span>
</span>
<span className="hint">{contents(p).join(" · ")}</span>
</label>
))}
</div>
</fieldset>
<div className="grid-2">
<div className="field">
<label htmlFor={`${id}-name`}>Kinek szól?</label>
<input
id={`${id}-name`}
className="inp"
value={name}
maxLength={MAX_NAME}
onChange={(e) => setName(e.target.value)}
placeholder="Például: Bea"
aria-describedby={`${id}-name-hint`}
required
/>
<span id={`${id}-name-hint`} className="hint">
Az utalványon: „{name.trim() || "Bea"} részére”
{byEmail ? `, a levélben: „Szia ${name.trim() || "Bea"}!”` : ""}.
</span>
</div>
<div className="field">
<label htmlFor={`${id}-from`}>Kitől? (a te neved)</label>
<input
id={`${id}-from`}
className="inp"
value={from}
maxLength={MAX_NAME}
onChange={(e) => setFrom(e.target.value)}
placeholder="Például: Anna"
/>
</div>
</div>
<fieldset className="gift-set">
<legend className="label">Hogyan kapja meg?</legend>
<div className="gift-ways">
<label className={byEmail ? "gift-option is-chosen" : "gift-option"}>
<input type="radio" name={`${id}-way`} checked={byEmail} onChange={() => setByEmail(true)} />
<strong>E-mailben küldjük el neki</strong>
<span className="hint">Fizetés után azonnal megkapja a kódot, mellé az utalványt PDF-ben.</span>
</label>
<label className={!byEmail ? "gift-option is-chosen" : "gift-option"}>
<input type="radio" name={`${id}-way`} checked={!byEmail} onChange={() => setByEmail(false)} />
<strong>Én adom át</strong>
<span className="hint">
Letöltöd az utalványt PDF-ben: kinyomtathatod, vagy elküldheted te magad. Később innen is kérheted, hogy
e-mailben küldjük el.
</span>
</label>
</div>
{byEmail && (
<div className="grid-2">
<div className="field">
<label htmlFor={`${id}-email`}>A címzett e-mail-címe</label>
<input
id={`${id}-email`}
className="inp"
type="email"
autoComplete="off"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
</div>
<div className="field">
<label htmlFor={`${id}-confirm`}>Még egyszer, ellenőrzésképp</label>
<input
id={`${id}-confirm`}
className="inp"
type="email"
autoComplete="off"
value={confirm}
onChange={(e) => setConfirm(e.target.value)}
aria-invalid={mismatch || undefined}
aria-describedby={`${id}-confirm-msg`}
required
/>
<span id={`${id}-confirm-msg`} className={mismatch ? "msg msg-error" : "hint"} aria-live="polite">
{mismatch ? "A két cím nem egyezik." : "Hogy biztosan jó címre menjen."}
</span>
</div>
</div>
)}
</fieldset>
<div className="field">
<label htmlFor={`${id}-message`}>Személyes üzenet (nem kötelező)</label>
<textarea
id={`${id}-message`}
className="inp"
value={message}
maxLength={MAX_MESSAGE}
onChange={(e) => setMessage(e.target.value)}
placeholder="Boldog születésnapot! Nézd meg, mit mesélnek rólad a csillagok."
aria-describedby={`${id}-message-hint`}
/>
<span id={`${id}-message-hint`} className="hint">
Rajta lesz az utalványon és a levélben. Link nem lehet benne. {message.length}/{MAX_MESSAGE}
</span>
</div>
{error && (
<p className="msg msg-error" role="alert">
{error}
</p>
)}
<div className="gift-submit">
{chosen && (
<p className="gift-total">
<span className="muted">
„{chosen.name}” csomag ajándékba{name.trim() ? `, ${name.trim()} részére` : ""}
</span>
<strong className="num">{huf(chosen.price_huf)}</strong>
</p>
)}
{canPay ? (
<button type="submit" className="btn btn-primary" disabled={busy || !chosen || !emailOk || !name.trim()}>
{busy ? (stripe ? "Átirányítás…" : "Fizetés…") : stripe ? "Tovább a fizetéshez" : "Megveszem ajándékba"}
</button>
) : (
<button type="button" className="btn" disabled>
Hamarosan
</button>
)}
</div>
{canPay === "mock" && <p className="hint">Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.</p>}
{stripe && (
<p className="hint">
<span aria-hidden="true">🔒</span> A fizetés a Stripe biztonságos oldalán történik. A számlát te kapod.
{termsConsent && (
<>
{" "}
Az ajándékkód a fizetés után azonnal elkészül, ezért a fizetésnél kéred a teljesítés megkezdését, és ezzel
lemondasz a 14 napos elállási jogról. <Link href="/aszf">ÁSZF</Link>
</>
)}
</p>
)}
{canPay === "stripe_test" && (
<p className="hint">
Stripe tesztmód (csak adminoknak látszik): kártyaszám 4242 4242 4242 4242, bármilyen jövőbeli lejárat és CVC.
</p>
)}
</form>
);
}