Files
sorsfonalak/services/frontend/src/components/PackageCards.tsx
T
fecoandClaude Opus 5.5 e530e78c54
CI / test (push) Successful in 1m25s
CI / build (push) Successful in 5m20s
CI / deploy (push) Skipped
Stripe: company purchases, payment details kept for the invoice
The checkout offers buying as a company (name, tax number). A confirmed
payment keeps Stripe's payment id, the amount charged and the buyer's
billing details, and queues the invoice for a background job (Billingo,
once the company exists). Payments outlive a deleted account for the
accounting law's 8 years. The package cards, the FAQ and the privacy
policy say that card details go to Stripe only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-20 07:26:03 +00:00

200 lines
7.1 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { api, errorText } from "@/lib/api";
import type { Package, PayMethod } from "@/lib/types";
export const huf = (n: number) => `${n.toLocaleString("hu-HU")} Ft`;
/** What a package contains, as short lines. */
export function contents(p: Package): string[] {
const lines: string[] = [];
if (p.natal) lines.push(`${p.natal} részletes képletelemzés`);
if (p.synastry) lines.push(`${p.synastry} kapcsolati elemzés`);
if (p.forecast) lines.push(`${p.forecast} éves előrejelzés`);
if (p.persons) lines.push(`${p.persons} személy felvétele`);
if (p.questions) lines.push(`${p.questions} kérdés az elemzéseidről`);
return lines;
}
/**
* Package cards with a buy button. Bigger packages show what they save and
* 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,
ids,
canPay,
compact = false,
onBought,
}: {
packages: Package[];
/** Which packages, in this order (default: the main ladder). */
ids?: string[];
/** How the user can pay (null: purchases are off, the buttons say "soon"). */
canPay: PayMethod | null;
compact?: boolean;
/** 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");
async function buy(p: Package) {
setBusy(p.id);
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));
setBusy(null);
}
}
return (
<div className="field" style={{ gap: 12 }}>
<div className={compact ? "pkg-grid pkg-grid-compact" : "pkg-grid"}>
{shown.map((p, i) => {
const prev = i > 0 ? shown[i - 1] : null;
const step = prev && p.tier === "main" && prev.tier === "main" ? p.price_huf - prev.price_huf : null;
return (
<article key={p.id} className={p.badge ? "pkg pkg-featured" : "pkg"} aria-labelledby={`pkg-${p.id}`}>
<div className="pkg-tags">
{p.badge && <span className="pkg-badge">{p.badge}</span>}
{p.savings_percent ? <span className="pkg-save">−{p.savings_percent}%</span> : null}
</div>
<h3 id={`pkg-${p.id}`} className="pkg-name">
{p.name}
</h3>
{!compact && <p className="muted pkg-tagline">{p.tagline}</p>}
<div className="pkg-price">
<strong>{huf(p.price_huf)}</strong>
{p.list_price_huf > p.price_huf && (
<s className="hint" aria-label={`Külön megvéve ${huf(p.list_price_huf)}`}>
{huf(p.list_price_huf)}
</s>
)}
</div>
<ul className="pkg-list">
{contents(p).map((line) => (
<li key={line}>{line}</li>
))}
</ul>
<p className="hint pkg-per">
{p.per_analysis_huf ? `Elemzésenként ${huf(p.per_analysis_huf)}` : " "}
</p>
{step !== null && step > 0 && prev && (
<p className="pkg-step">
Csak +{huf(step)} a(z) {prev.name} csomaghoz képest
</p>
)}
{canPay ? (
<button
type="button"
className={p.badge ? "btn btn-primary" : "btn"}
onClick={() => buy(p)}
disabled={busy !== null}
>
{busy === p.id ? (canPay === "mock" ? "Fizetés…" : "Átirányítás…") : "Megveszem"}
</button>
) : (
<button type="button" className="btn" disabled>
Hamarosan
</button>
)}
</article>
);
})}
</div>
{error && (
<p className="msg msg-error" role="alert">
{error}
</p>
)}
{done && (
<p className="msg msg-ok" role="status">
A(z) {done} csomag jóváírva.
</p>
)}
{canPay === "mock" && <p className="hint">Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.</p>}
{(canPay === "stripe" || canPay === "stripe_test") && (
<p className="hint">
<span aria-hidden="true">🔒</span> A fizetés a Stripe biztonságos oldalán történik (bankkártya, Apple Pay,
Google Pay). A kártyaadataidat közvetlenül a Stripe-nak adod meg: mi nem látjuk és nem tároljuk őket.
</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>
);
}
/** Loads the catalog in the browser (for choosers inside other components). */
export function usePackages(): Package[] | null {
const [packages, setPackages] = useState<Package[] | null>(null);
useEffect(() => {
api<{ packages: Package[] }>("/packages")
.then((d) => setPackages(d.packages))
.catch(() => setPackages([]));
}, []);
return packages;
}
/** A few packages to choose from, loaded on demand, with a link to all of them. */
export function PackageChooser({
ids,
canPay,
onBought,
}: {
ids: string[];
canPay: PayMethod | null;
onBought?: (p: Package) => Promise<void> | void;
}) {
const packages = usePackages();
if (packages === null) return <p className="muted">Csomagok betöltése…</p>;
return (
<div className="field" style={{ gap: 8 }}>
<PackageCards packages={packages} ids={ids} canPay={canPay} compact onBought={onBought} />
<Link href="/csomagok" className="hint">
Összes csomag és a részletek →
</Link>
</div>
);
}