- The buyer may withdraw from a gift purchase within 14 days while the
code is not redeemed (ÁSZF 3., 5., 6.; terms version 2026-11-23); the
consent on Stripe's page and the confirmation e-mail say so
- Admin → Ajándékok: the gifts with payment, invoice and storno; the
refund button locks the gift and refuses a redeemed one, withdraws the
code, refunds the PaymentIntent on Stripe (one refund per gift via the
Idempotency-Key, retry when Stripe refuses) and queues the storno
- Worker: cancels the issued invoice in Billingo (POST /documents/{id}/
cancel, e-mailed to the buyer), keeps the storno invoice's id and
number; a lost answer is not asked again but left for an admin
- A withdrawn code cannot be redeemed, sent or downloaded; "My gifts"
shows it as refunded
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
183 lines
6.6 KiB
TypeScript
183 lines
6.6 KiB
TypeScript
"use client";
|
||
|
||
import { useId, useState } from "react";
|
||
|
||
import { api, errorText } from "@/lib/api";
|
||
import type { Gift } from "@/lib/types";
|
||
|
||
const day = (iso: string) =>
|
||
new Date(iso).toLocaleDateString("hu-HU", { timeZone: "Europe/Budapest", year: "numeric", month: "long", day: "numeric" });
|
||
const time = (iso: string) => new Date(iso).toLocaleTimeString("hu-HU", { timeZone: "Europe/Budapest", hour: "2-digit", minute: "2-digit" });
|
||
|
||
/** The user's gifts: code, state, the card to download, and sending it by e-mail. */
|
||
export function GiftList({ gifts: initial }: { gifts: Gift[] }) {
|
||
const [gifts, setGifts] = useState(initial);
|
||
return (
|
||
<ul className="gift-list">
|
||
{gifts.map((g) => (
|
||
<GiftItem key={g.id} gift={g} onChange={(next) => setGifts((all) => all.map((x) => (x.id === next.id ? next : x)))} />
|
||
))}
|
||
</ul>
|
||
);
|
||
}
|
||
|
||
function GiftItem({ gift, onChange }: { gift: Gift; onChange: (g: Gift) => void }) {
|
||
const id = useId();
|
||
const [sending, setSending] = useState(false);
|
||
const [email, setEmail] = useState("");
|
||
const [confirm, setConfirm] = useState("");
|
||
const [busy, setBusy] = useState(false);
|
||
const [error, setError] = useState<string | null>(null);
|
||
const [sent, setSent] = useState<string | null>(null);
|
||
const [copied, setCopied] = useState(false);
|
||
const open = gift.status === "open";
|
||
const mismatch = confirm.trim() !== "" && email.trim().toLowerCase() !== confirm.trim().toLowerCase();
|
||
|
||
async function send(e: React.FormEvent) {
|
||
e.preventDefault();
|
||
setBusy(true);
|
||
setError(null);
|
||
try {
|
||
const next = await api<Gift>(`/gifts/${gift.id}/send`, {
|
||
body: { recipient_email: email.trim(), recipient_email_confirm: confirm.trim() },
|
||
});
|
||
onChange(next);
|
||
setSent(next.recipient_email);
|
||
setSending(false);
|
||
setEmail("");
|
||
setConfirm("");
|
||
} catch (err) {
|
||
setError(errorText(err));
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
async function copy() {
|
||
try {
|
||
await navigator.clipboard.writeText(gift.code);
|
||
setCopied(true);
|
||
setTimeout(() => setCopied(false), 2000);
|
||
} catch {
|
||
// The code stays visible and selectable.
|
||
}
|
||
}
|
||
|
||
return (
|
||
<li className="card gift-item">
|
||
<div className="gift-item-head">
|
||
<div>
|
||
<h2 className="gift-item-title">
|
||
„{gift.package.name ?? gift.package.id}” csomag
|
||
{gift.recipient_name && <span className="gift-item-for"> {gift.recipient_name} részére</span>}
|
||
</h2>
|
||
<span className="hint">Vásárolva: {day(gift.paid_at)}</span>
|
||
</div>
|
||
{gift.status === "redeemed" ? (
|
||
<span className="badge">Beváltva{gift.redeemed_at ? `: ${day(gift.redeemed_at)}` : ""}</span>
|
||
) : gift.status === "refunded" ? (
|
||
<span className="badge">Visszatérítve{gift.refunded_at ? `: ${day(gift.refunded_at)}` : ""}</span>
|
||
) : (
|
||
<span className="badge badge-gold">Még nincs beváltva</span>
|
||
)}
|
||
</div>
|
||
|
||
<div className="gift-code-row">
|
||
<span className="eyebrow">Ajándékkód</span>
|
||
<code className="gift-code">{gift.code}</code>
|
||
<button type="button" className="btn btn-sm btn-ghost" onClick={copy}>
|
||
{copied ? "Másolva" : "Másolás"}
|
||
</button>
|
||
</div>
|
||
|
||
<p className="gift-to">
|
||
{gift.recipient_email ? (
|
||
<>
|
||
E-mailben elküldve ide: <strong>{gift.recipient_email}</strong>
|
||
{gift.sendings > 1 ? ` (${gift.sendings}×)` : ""}
|
||
</>
|
||
) : open ? (
|
||
"Te adod át: töltsd le az utalványt."
|
||
) : (
|
||
"Nem e-mailben ment."
|
||
)}
|
||
{gift.from_name && <span className="hint"> · Kitől: {gift.from_name}</span>}
|
||
</p>
|
||
{gift.message && <blockquote className="gift-message">{gift.message}</blockquote>}
|
||
|
||
{gift.status === "refunded" ? (
|
||
<p className="hint">Elálltál a vásárlástól: a kód már nem érvényes, az árát visszatérítettük.</p>
|
||
) : (
|
||
<div className="form-actions">
|
||
<a className="btn btn-primary btn-sm" href={`/api/v1/gifts/${gift.id}/card.pdf`} download>
|
||
Utalvány letöltése (PDF)
|
||
</a>
|
||
{open && gift.can_send && !sending && (
|
||
<button type="button" className="btn btn-sm" onClick={() => setSending(true)}>
|
||
{gift.recipient_email ? "Újraküldés e-mailben" : "Küldés e-mailben"}
|
||
</button>
|
||
)}
|
||
</div>
|
||
)}
|
||
{open && !gift.can_send && gift.sendings > 0 && (
|
||
<p className="hint">E-mailben már többször elküldtük. Ha nem érkezett meg, töltsd le az utalványt, és küldd el te.</p>
|
||
)}
|
||
{sent && (
|
||
<p className="msg msg-ok" role="status">
|
||
Elküldtük ide: {sent}.
|
||
</p>
|
||
)}
|
||
|
||
{sending && (
|
||
<form className="gift-send" onSubmit={send}>
|
||
<div className="grid-2">
|
||
<div className="field">
|
||
<label htmlFor={`${id}-email`}>Kinek küldjük?</label>
|
||
<input
|
||
id={`${id}-email`}
|
||
className="inp"
|
||
type="email"
|
||
autoComplete="off"
|
||
value={email}
|
||
onChange={(e) => setEmail(e.target.value)}
|
||
placeholder={gift.recipient_email ?? ""}
|
||
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}
|
||
required
|
||
/>
|
||
{mismatch && <span className="msg msg-error">A két cím nem egyezik.</span>}
|
||
</div>
|
||
</div>
|
||
{gift.send_again_at && (
|
||
<p className="hint">Most küldtük el: {time(gift.send_again_at)} után küldheted újra.</p>
|
||
)}
|
||
{error && (
|
||
<p className="msg msg-error" role="alert">
|
||
{error}
|
||
</p>
|
||
)}
|
||
<div className="form-actions">
|
||
<button type="submit" className="btn btn-primary btn-sm" disabled={busy || mismatch || !email.trim()}>
|
||
{busy ? "Küldés…" : "Elküldöm"}
|
||
</button>
|
||
<button type="button" className="btn btn-sm btn-ghost" onClick={() => setSending(false)}>
|
||
Mégse
|
||
</button>
|
||
</div>
|
||
</form>
|
||
)}
|
||
</li>
|
||
);
|
||
}
|