Packages paid through Stripe Checkout
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:
@@ -6,7 +6,16 @@ import type { NextRequest } from "next/server";
|
||||
import { BACKEND_URL } from "@/lib/server";
|
||||
import { SHARES_TAG } from "@/lib/share";
|
||||
|
||||
const REQUEST_HEADERS = ["accept", "accept-language", "authorization", "content-type", "cookie", "user-agent"];
|
||||
// stripe-signature: Stripe's webhook (the body is passed on byte for byte).
|
||||
const REQUEST_HEADERS = [
|
||||
"accept",
|
||||
"accept-language",
|
||||
"authorization",
|
||||
"content-type",
|
||||
"cookie",
|
||||
"stripe-signature",
|
||||
"user-agent",
|
||||
];
|
||||
// Hop-by-hop or re-computed by the runtime.
|
||||
const SKIP_RESPONSE_HEADERS = new Set(["connection", "content-encoding", "content-length", "set-cookie", "transfer-encoding"]);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { Metadata } from "next";
|
||||
import { AiNote } from "@/components/AiNote";
|
||||
import { PackageCards } from "@/components/PackageCards";
|
||||
import { RedeemCode } from "@/components/RedeemCode";
|
||||
import { payMethod } from "@/lib/payments";
|
||||
import { backendGet, requireMe } from "@/lib/server";
|
||||
import type { Package, PublicConfig } from "@/lib/types";
|
||||
|
||||
@@ -39,7 +40,7 @@ export default async function PackagesPage() {
|
||||
backendGet<PublicConfig>("/v1/config"),
|
||||
]);
|
||||
const packages = catalog?.packages ?? [];
|
||||
const canPay = config?.mock_payments ?? false;
|
||||
const canPay = payMethod(config, me);
|
||||
const left = me.analyses_left;
|
||||
const slots = me.persons;
|
||||
|
||||
|
||||
@@ -948,3 +948,12 @@ a.pair-row:hover strong { color: var(--gold); }
|
||||
/* Chart page: below the wheel and the summary, the facts and the next steps side by side */
|
||||
.chart-extras { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(300px, 100%), 1fr)); gap: 16px; align-items: stretch; }
|
||||
.chart-extras > .card { margin: 0; }
|
||||
|
||||
/* Back from Stripe: the payment's result at the bottom of the screen. */
|
||||
.pay-return { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 60; width: min(560px, calc(100% - 32px)); }
|
||||
.pay-return .msg {
|
||||
display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 0; padding: 14px 16px;
|
||||
background: var(--panel-2); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
|
||||
}
|
||||
.pay-return .msg-ok { border-color: color-mix(in srgb, var(--ok) 55%, var(--line)); }
|
||||
.pay-return .msg-error { border-color: color-mix(in srgb, var(--danger) 55%, var(--line)); }
|
||||
|
||||
@@ -6,6 +6,7 @@ import { ChartWheel } from "@/components/ChartWheel";
|
||||
import { DetailedAnalysis } from "@/components/DetailedAnalysis";
|
||||
import { ForecastKeys, ForecastTimeline, ForecastTransits, longDay } from "@/components/ForecastViews";
|
||||
import { Tabs } from "@/components/Tabs";
|
||||
import { payMethod } from "@/lib/payments";
|
||||
import { backendGet, requireMe } from "@/lib/server";
|
||||
import type { Analysis, Chart, ForecastDetail, ForecastList, PublicConfig } from "@/lib/types";
|
||||
|
||||
@@ -134,7 +135,7 @@ export default async function ForecastPage({
|
||||
initial={detailed}
|
||||
left={me.analyses_left.forecast}
|
||||
isAdmin={me.user.roles.includes("admin")}
|
||||
canPay={config?.mock_payments ?? false}
|
||||
canPay={payMethod(config, me)}
|
||||
questionsLeft={me.analyses_left.question ?? 0}
|
||||
/>
|
||||
),
|
||||
|
||||
@@ -10,6 +10,7 @@ import { ChartWheel, WheelLegend } from "@/components/ChartWheel";
|
||||
import { CompareButton } from "@/components/CompareButton";
|
||||
import { Tabs } from "@/components/Tabs";
|
||||
import { ASPECTS, ELEMENTS, MODALITIES, MOON_PHASES, countryName, formatBirthDate, pointName } from "@/lib/astro";
|
||||
import { payMethod } from "@/lib/payments";
|
||||
import { backendGet, requireMe } from "@/lib/server";
|
||||
import type { Analysis, Chart, ChartListItem, PublicConfig } from "@/lib/types";
|
||||
|
||||
@@ -181,7 +182,7 @@ export default async function ChartPage({ params }: { params: Promise<{ id: stri
|
||||
initial={detailed}
|
||||
left={me.analyses_left.natal}
|
||||
isAdmin={me.user.roles.includes("admin")}
|
||||
canPay={config?.mock_payments ?? false}
|
||||
canPay={payMethod(config, me)}
|
||||
questionsLeft={me.analyses_left.question ?? 0}
|
||||
/>
|
||||
),
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import type { Metadata } from "next";
|
||||
import { Cormorant_Garamond, Inter } from "next/font/google";
|
||||
import { Suspense } from "react";
|
||||
|
||||
import { PaymentReturn } from "@/components/PaymentReturn";
|
||||
import { SiteHeader } from "@/components/SiteHeader";
|
||||
import { getMe } from "@/lib/server";
|
||||
|
||||
@@ -25,6 +27,11 @@ export default async function RootLayout({ children }: { children: React.ReactNo
|
||||
<body>
|
||||
{me && <SiteHeader me={me} />}
|
||||
{children}
|
||||
{me && (
|
||||
<Suspense fallback={null}>
|
||||
<PaymentReturn />
|
||||
</Suspense>
|
||||
)}
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
@@ -8,6 +8,7 @@ import { CompositeTables, CrossGrid, HouseOverlays, InterAspectList, NatureBar }
|
||||
import { SynastryLegend, SynastryWheel } from "@/components/SynastryWheel";
|
||||
import { Tabs } from "@/components/Tabs";
|
||||
import { ASPECTS, formatBirthDate } from "@/lib/astro";
|
||||
import { payMethod } from "@/lib/payments";
|
||||
import { backendGet, requireMe } from "@/lib/server";
|
||||
import { sideName, sideTimeKnown } from "@/lib/synastry";
|
||||
import type { Analysis, ChartListItem, PublicConfig, Synastry, SynastrySide } from "@/lib/types";
|
||||
@@ -127,7 +128,7 @@ export default async function ComparisonPage({ params }: { params: Promise<{ id:
|
||||
initial={detailed}
|
||||
left={me.analyses_left.synastry}
|
||||
isAdmin={me.user.roles.includes("admin")}
|
||||
canPay={config?.mock_payments ?? false}
|
||||
canPay={payMethod(config, me)}
|
||||
note={`Az elemzésben az első fél: ${name1}, a második fél: ${name2}.`}
|
||||
questionsLeft={me.analyses_left.question ?? 0}
|
||||
/>
|
||||
|
||||
@@ -5,6 +5,7 @@ import { ChartCard } from "@/components/ChartCard";
|
||||
import { PersonActions } from "@/components/PersonActions";
|
||||
import { PendingInvites } from "@/components/PendingInvites";
|
||||
import { PersonPacks } from "@/components/PersonPacks";
|
||||
import { payMethod } from "@/lib/payments";
|
||||
import { backendGet, requireMe } from "@/lib/server";
|
||||
import type { ChartListItem, PublicConfig } from "@/lib/types";
|
||||
|
||||
@@ -102,7 +103,7 @@ export default async function PersonsPage() {
|
||||
? "Elfogytak a helyeid. Újabb személyhez válassz csomagot."
|
||||
: "A felvett személyek képletét és elemzéseit itt éred el. Egy törölt személy helye nem szabadul fel."}
|
||||
</p>
|
||||
<PersonPacks canPay={config?.mock_payments ?? false} />
|
||||
<PersonPacks canPay={payMethod(config, me)} />
|
||||
</section>
|
||||
)}
|
||||
</main>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { Metadata } from "next";
|
||||
|
||||
import { PersonPacks } from "@/components/PersonPacks";
|
||||
import { payMethod } from "@/lib/payments";
|
||||
import { backendGet, requireMe } from "@/lib/server";
|
||||
import type { PublicConfig } from "@/lib/types";
|
||||
|
||||
@@ -31,7 +32,7 @@ export default async function NewPersonPage() {
|
||||
full={
|
||||
<>
|
||||
<p className="muted">Elfogytak a helyeid. Újabb személyhez válassz csomagot, vagy hívd meg.</p>
|
||||
<PersonPacks canPay={config?.mock_payments ?? false} />
|
||||
<PersonPacks canPay={payMethod(config, me)} />
|
||||
</>
|
||||
}
|
||||
/>
|
||||
|
||||
@@ -4,7 +4,7 @@ import Link from "next/link";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import { api, ApiError, errorText } from "@/lib/api";
|
||||
import type { Question, UnitBalance } from "@/lib/types";
|
||||
import type { PayMethod, Question, UnitBalance } from "@/lib/types";
|
||||
|
||||
import { AnalysisLoading } from "./AnalysisLoading";
|
||||
import { PackageChooser } from "./PackageCards";
|
||||
@@ -49,7 +49,7 @@ export function AnalysisQuestions({
|
||||
analysisId: string;
|
||||
left: number;
|
||||
isAdmin: boolean;
|
||||
canPay: boolean;
|
||||
canPay: PayMethod | null;
|
||||
/** What the analysis is about: the heading and the example question follow it. */
|
||||
about: "natal" | "synastry" | "forecast";
|
||||
}) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import Link from "next/link";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import { api, errorText } from "@/lib/api";
|
||||
import type { Analysis } from "@/lib/types";
|
||||
import type { Analysis, PayMethod } from "@/lib/types";
|
||||
|
||||
import { AI_SHORT } from "./AiNote";
|
||||
import { AnalysisLoading } from "./AnalysisLoading";
|
||||
@@ -96,8 +96,8 @@ export function DetailedAnalysis({
|
||||
initial: Analysis | null;
|
||||
left: number;
|
||||
isAdmin: boolean;
|
||||
/** The stand-in payment is on (MOCK_PAYMENTS). */
|
||||
canPay: boolean;
|
||||
/** How the user can buy a package (null: purchases are off). */
|
||||
canPay: PayMethod | null;
|
||||
/** Shown above a finished analysis (e.g. who is the first and the second person). */
|
||||
note?: string;
|
||||
/** Questions left; given where questions can be asked. */
|
||||
@@ -245,7 +245,11 @@ export function DetailedAnalysis({
|
||||
step === "pay" ? (
|
||||
<div className="pay-box">
|
||||
<p className="pay-lead">
|
||||
Válassz csomagot. A vásárlás után az elemzés azonnal elindul, a maradék elemzések pedig nem járnak le.
|
||||
Válassz csomagot.{" "}
|
||||
{canPay === "mock"
|
||||
? "A vásárlás után az elemzés azonnal elindul"
|
||||
: "A fizetés után visszajössz ide, és rögtön elindíthatod az elemzést"}
|
||||
, a maradék elemzések pedig nem járnak le.
|
||||
</p>
|
||||
<PackageChooser ids={t.packages} canPay={canPay} onBought={start} />
|
||||
<div className="form-actions">
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
"use client";
|
||||
|
||||
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import { api, errorText } from "@/lib/api";
|
||||
|
||||
type Notice = { kind: "wait" | "ok" | "info" | "error"; text: string };
|
||||
|
||||
const POLLS = 20;
|
||||
const POLL_MS = 2000;
|
||||
|
||||
/**
|
||||
* Back from Stripe's payment page (`?fizetes=<session>` or
|
||||
* `?fizetes=megszakitva`): waits until the payment is confirmed, then shows
|
||||
* it and reloads the page data. Lives in the layout, so it works on every
|
||||
* page a purchase can start from.
|
||||
*/
|
||||
export function PaymentReturn() {
|
||||
const params = useSearchParams();
|
||||
const pathname = usePathname();
|
||||
const router = useRouter();
|
||||
const value = params.get("fizetes");
|
||||
const [notice, setNotice] = useState<Notice | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!value) return;
|
||||
// A reload should not show it again.
|
||||
const clean = () => {
|
||||
const rest = new URLSearchParams(window.location.search);
|
||||
rest.delete("fizetes");
|
||||
router.replace(rest.size ? `${pathname}?${rest}` : pathname, { scroll: false });
|
||||
};
|
||||
if (value === "megszakitva") {
|
||||
setNotice({ kind: "info", text: "A fizetés megszakadt, nem terheltünk semmit." });
|
||||
clean();
|
||||
return;
|
||||
}
|
||||
let stopped = false;
|
||||
setNotice({ kind: "wait", text: "A fizetés visszaigazolására várunk…" });
|
||||
(async () => {
|
||||
for (let i = 0; i < POLLS && !stopped; i++) {
|
||||
try {
|
||||
const r = await api<{ status: string; name: string | null }>(
|
||||
`/payments/checkout/${encodeURIComponent(value)}`,
|
||||
);
|
||||
if (stopped) return;
|
||||
if (r.status === "paid") {
|
||||
setNotice({ kind: "ok", text: `Köszönjük! Jóváírtuk: ${r.name ?? "a csomagod"}.` });
|
||||
clean();
|
||||
router.refresh();
|
||||
return;
|
||||
}
|
||||
if (r.status === "expired") {
|
||||
setNotice({ kind: "info", text: "A fizetési oldal lejárt, nem terheltünk semmit." });
|
||||
clean();
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
if (!stopped) setNotice({ kind: "error", text: errorText(e) });
|
||||
return;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, POLL_MS));
|
||||
}
|
||||
if (!stopped)
|
||||
setNotice({
|
||||
kind: "error",
|
||||
text: "A Stripe még nem igazolta vissza a fizetést. Pár perc múlva frissítsd az oldalt: ha a pénz levonódott, a csomag megérkezik.",
|
||||
});
|
||||
})();
|
||||
return () => {
|
||||
stopped = true;
|
||||
};
|
||||
}, [value, pathname, router]);
|
||||
|
||||
if (!notice) return null;
|
||||
const cls = notice.kind === "ok" ? "msg msg-ok" : notice.kind === "error" ? "msg msg-error" : "msg";
|
||||
return (
|
||||
<div className="pay-return" role={notice.kind === "error" ? "alert" : "status"}>
|
||||
<p className={cls}>
|
||||
<span>{notice.text}</span>
|
||||
{notice.kind !== "wait" && (
|
||||
<button type="button" className="btn btn-sm btn-ghost" onClick={() => setNotice(null)} aria-label="Bezárás">
|
||||
×
|
||||
</button>
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
"use client";
|
||||
|
||||
import type { PayMethod } from "@/lib/types";
|
||||
|
||||
import { PackageChooser } from "./PackageCards";
|
||||
|
||||
/** Packages that give more persons: a few more, or the family package. */
|
||||
export function PersonPacks({ canPay }: { canPay: boolean }) {
|
||||
export function PersonPacks({ canPay }: { canPay: PayMethod | null }) {
|
||||
return <PackageChooser ids={["addon_persons", "family"]} canPay={canPay} />;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import type { Me, PayMethod, PublicConfig } from "./types";
|
||||
|
||||
/** How this user can buy packages (with a Stripe test key: admins only). */
|
||||
export function payMethod(config: PublicConfig | null | undefined, me: Me): PayMethod | null {
|
||||
if (config?.payments === "mock") return "mock";
|
||||
if (config?.payments !== "stripe") return null;
|
||||
if (!config.payments_test) return "stripe";
|
||||
return me.user.roles.includes("admin") ? "stripe_test" : null;
|
||||
}
|
||||
@@ -92,6 +92,10 @@ export interface PublicConfig {
|
||||
password_login: boolean;
|
||||
password_min_length: number;
|
||||
mock_payments: boolean;
|
||||
/** How packages are paid: Stripe Checkout, the stand-in, or not at all. */
|
||||
payments: "stripe" | "mock" | null;
|
||||
/** Stripe has a test key: only admins can buy. */
|
||||
payments_test: boolean;
|
||||
/** "invite": new accounts need an invitation. */
|
||||
signup_mode: "invite" | "open";
|
||||
/** Users may invite others (off until the launch; admins always may). */
|
||||
@@ -573,3 +577,6 @@ export interface PersonInviteLanding {
|
||||
status: "pending" | "accepted" | "expired";
|
||||
expires_at: string;
|
||||
}
|
||||
|
||||
/** How this user can buy a package; `null`: purchases are off. */
|
||||
export type PayMethod = "stripe" | "stripe_test" | "mock";
|
||||
|
||||
Reference in New Issue
Block a user