Packages paid through Stripe Checkout
CI / test (push) Successful in 1m25s
CI / build (push) Successful in 5m24s
CI / deploy (push) Skipped

The catalog stays in packages.rs: every checkout session carries the
package's name and gross price, nothing has to be kept in Stripe. The
package is granted once, when Stripe confirms the payment: by the signed
webhook, or by the return page asking Stripe when the webhook is late.
With a test key only admins can buy, so Stripe's test cards give nobody
free packages. Without the keys nothing changes.

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-20 06:41:21 +00:00
co-authored by Claude Opus 5.5
parent 55101f8637
commit 42a982010d
31 changed files with 842 additions and 37 deletions
@@ -5,7 +5,7 @@ import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { api, errorText } from "@/lib/api";
import type { Package } from "@/lib/types";
import type { Package, PayMethod } from "@/lib/types";
export const huf = (n: number) => `${n.toLocaleString("hu-HU")} Ft`;
@@ -22,8 +22,9 @@ export function contents(p: Package): string[] {
/**
* Package cards with a buy button. Bigger packages show what they save and
* how little more they cost than the previous one. Until Stripe is connected
* the payment is a stand-in (MOCK_PAYMENTS) that grants the package at once.
* how little more they cost than the previous one. With Stripe the button
* goes to Stripe's payment page and comes back to this page (PaymentReturn
* shows the result); the stand-in payment (MOCK_PAYMENTS) grants at once.
*/
export function PackageCards({
packages,
@@ -35,15 +36,24 @@ export function PackageCards({
packages: Package[];
/** Which packages, in this order (default: the main ladder). */
ids?: string[];
canPay: boolean;
/** How the user can pay (null: purchases are off, the buttons say "soon"). */
canPay: PayMethod | null;
compact?: boolean;
/** After a purchase (default: reload the page data). */
/** After a stand-in purchase (default: reload the page data). */
onBought?: (p: Package) => Promise<void> | void;
}) {
const router = useRouter();
const [busy, setBusy] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [done, setDone] = 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(null);
};
window.addEventListener("pageshow", reset);
return () => window.removeEventListener("pageshow", reset);
}, []);
const shown = ids
? ids.map((id) => packages.find((p) => p.id === id)).filter((p): p is Package => p !== undefined)
: packages.filter((p) => p.tier === "main");
@@ -53,15 +63,22 @@ export function PackageCards({
setError(null);
setDone(null);
try {
if (canPay === "stripe" || canPay === "stripe_test") {
const { url } = await api<{ url: string }>("/payments/checkout", {
body: { package: p.id, return_path: window.location.pathname },
});
window.location.assign(url);
return;
}
await api("/payments/mock", { body: { package: p.id } });
if (onBought) await onBought(p);
else {
setDone(p.name);
router.refresh();
}
setBusy(null);
} catch (e) {
setError(errorText(e));
} finally {
setBusy(null);
}
}
@@ -110,7 +127,7 @@ export function PackageCards({
onClick={() => buy(p)}
disabled={busy !== null}
>
{busy === p.id ? "Fizetés…" : "Megveszem"}
{busy === p.id ? (canPay === "mock" ? "Fizetés…" : "Átirányítás…") : "Megveszem"}
</button>
) : (
<button type="button" className="btn" disabled>
@@ -131,7 +148,19 @@ export function PackageCards({
A(z) {done} csomag jóváírva.
</p>
)}
{canPay && <p className="hint">Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.</p>}
{canPay === "mock" && <p className="hint">Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.</p>}
{canPay === "stripe" && !compact && (
<p className="hint">
A fizetés a Stripe biztonságos oldalán történik (bankkártya, Apple Pay, Google Pay); a kártyaadataidat mi nem
látjuk.
</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.
Valódi pénz nem mozdul.
</p>
)}
</div>
);
}
@@ -154,7 +183,7 @@ export function PackageChooser({
onBought,
}: {
ids: string[];
canPay: boolean;
canPay: PayMethod | null;
onBought?: (p: Package) => Promise<void> | void;
}) {
const packages = usePackages();