Files
CatoandClaude 9e63512686 feat(plans): PlansScreen, own-key sheet, test-only Stripe guard (0.5.0) (#20)
* feat(plans): PlansScreen, three-screen checkout, own-key sheet, Stripe test-only guard (0.5.0)

Co-Authored-By: Claude <noreply@anthropic.com>

* feat(plans): one 'Pay X to Y' checkout screen with a method list and a Bitcoin scan view

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-12-03 01:56:38 +01:00

228 lines
19 KiB
JavaScript

"use client";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from "react";
import { formatChf, yearlySaving } from "./config.js";
export const DEFAULT_PLANS_LABELS = {
monthly: "Monthly",
yearly: "Yearly",
save: (p, chf) => `Save ${p}% (${chf} a year)`,
perMonth: "/ month",
perMonthBilledYearly: (y) => `/ month, billed ${y} a year`,
free: "Free",
custom: "Custom",
models: "Models",
startFree: "Start free",
getPlan: (n) => `Get ${n}`,
comingSoon: "Coming soon, get notified",
contactUs: "Contact us",
notifyPlaceholder: "you@example.com",
notifySubmit: "Notify me",
notifyThanks: "Thanks. We will write to you when it is on sale.",
notifyInvalid: "That does not look like an email address.",
recommended: "Recommended",
currentPlan: "Your plan",
ownKeyLink: "Use your own API key",
trustPrices: "Prices in CHF.",
trustHosting: "Hosted in the EU (Germany, Hetzner).",
trustNoRenew: "Bitcoin passes do not renew automatically.",
};
const CSS = `
.pk-plans{display:grid;gap:16px;grid-template-columns:minmax(0,1fr);margin:0;padding:0;list-style:none}
.pk-plans .pk-paid{order:-1}
@media (min-width:768px){.pk-plans{grid-template-columns:repeat(3,minmax(0,1fr))}.pk-plans .pk-paid{order:0}}
.pk-card{display:flex;flex-direction:column;gap:12px;padding:20px;border:1px solid var(--pk-border,#d0d5dd);border-radius:12px;background:var(--pk-card,#fff);color:var(--pk-text,#101828)}
.pk-card.pk-paid{border-color:var(--pk-accent,#101828);border-width:2px}
.pk-cta{display:block;box-sizing:border-box;width:100%;min-height:44px;padding:10px 16px;border-radius:8px;border:1px solid var(--pk-accent,#101828);background:var(--pk-accent,#101828);color:var(--pk-on-accent,#fff);font:inherit;font-weight:600;text-align:center;text-decoration:none;cursor:pointer}
.pk-card:not(.pk-paid) .pk-cta{background:transparent;color:var(--pk-accent,#101828)}
.pk-feats{margin:0;padding-left:18px;display:grid;gap:4px;font-size:14px}
.pk-models{margin:0;font-size:14px;display:flex;gap:6px}
.pk-models dt{font-weight:600}.pk-models dd{margin:0}
.pk-toggle{display:inline-flex;gap:4px;padding:4px;border:1px solid var(--pk-border,#d0d5dd);border-radius:999px}
.pk-toggle button{min-height:36px;padding:4px 14px;border:0;border-radius:999px;background:transparent;color:inherit;font:inherit;cursor:pointer}
.pk-toggle button[aria-pressed=true]{background:var(--pk-accent,#101828);color:var(--pk-on-accent,#fff)}
.pk-trust{margin:16px 0 0;padding:0;list-style:none;display:flex;flex-wrap:wrap;gap:4px 16px;font-size:13px;color:var(--pk-muted,#475467)}
.pk-own{display:inline-block;margin-top:12px;padding:0;border:0;background:none;color:var(--pk-muted,#475467);font:inherit;font-size:13px;text-decoration:underline;cursor:pointer}
.pk-sheet{display:grid;gap:14px;box-sizing:border-box;max-width:420px;width:100%;margin:0 auto;padding:20px;color:var(--pk-text,#101828)}
.pk-methods{display:grid;gap:8px;margin:0;padding:0;border:0;min-width:0}
.pk-opt{position:relative;display:flex;align-items:center;gap:12px;min-height:56px;padding:10px 14px;border:1px solid var(--pk-border,#d0d5dd);border-radius:10px;background:var(--pk-card,#fff);cursor:pointer}
.pk-opt input{position:absolute;opacity:0;inset:0;width:100%;height:100%;margin:0;cursor:pointer}
.pk-opt::before{content:"";flex:none;width:18px;height:18px;border:2px solid var(--pk-muted,#475467);border-radius:50%;box-sizing:border-box}
.pk-opt:has(input:checked){border-color:var(--pk-accent,#101828);box-shadow:0 0 0 1px var(--pk-accent,#101828)}
.pk-opt:has(input:checked)::before{border-color:var(--pk-accent,#101828);background:radial-gradient(var(--pk-accent,#101828) 45%,transparent 50%)}
.pk-opt:has(input:focus-visible){outline:3px solid var(--pk-focus,#2563eb);outline-offset:2px}
.pk-opt-body{display:grid;gap:2px}.pk-opt-body small{color:var(--pk-muted,#475467);font-size:13px}
.pk-sats,.pk-hint{margin:0;font-size:14px;color:var(--pk-muted,#475467)}
.pk-qr{display:flex;justify-content:center;padding:12px;border:1px solid var(--pk-border,#d0d5dd);border-radius:12px;background:#fff}
.pk-qr svg,.pk-qr img,.pk-qr canvas{max-width:100%;height:auto}
.pk-code{display:block;max-height:4.5em;overflow:auto;overflow-wrap:anywhere;padding:8px 10px;border-radius:8px;background:var(--pk-code-bg,#f2f4f7);font-size:12px}
.pk-link{min-height:44px;padding:0 8px;border:0;background:none;color:var(--pk-text,#101828);font:inherit;font-size:14px;text-decoration:underline;cursor:pointer}
.pk-cta:disabled{opacity:.6;cursor:default}
.pk-cta:focus-visible,.pk-link:focus-visible,.pk-own:focus-visible,.pk-toggle button:focus-visible{outline:3px solid var(--pk-focus,#2563eb);outline-offset:2px}
.pk-sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
`;
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
export function PlansScreen(props) {
const { config, onChoose, onNotify, onOpenOwnKey, currentPlan, locale = "en", } = props;
const L = { ...DEFAULT_PLANS_LABELS, ...props.labels };
const [period, setPeriod] = useState("year");
const [notifying, setNotifying] = useState(false);
const [email, setEmail] = useState("");
const [notice, setNotice] = useState(null);
const [done, setDone] = useState(false);
const { paid } = config;
const saving = yearlySaving(paid.monthlyChf, paid.yearlyChf);
const yearly = period === "year";
const price = yearly ? paid.yearlyChf / 12 : paid.monthlyChf;
const priceNote = yearly
? L.perMonthBilledYearly(formatChf(paid.yearlyChf, locale))
: L.perMonth;
async function submitNotify(e) {
e.preventDefault();
if (!EMAIL.test(email.trim())) {
setNotice(L.notifyInvalid);
return;
}
try {
await onNotify?.(email.trim());
setDone(true);
setNotice(null);
}
catch {
setNotice(L.notifyInvalid);
}
}
const showOwnKey = Boolean(onOpenOwnKey) && currentPlan !== "free";
return (_jsxs("section", { "aria-label": "Plans", "data-plans-screen": true, children: [_jsx("style", { children: CSS }), _jsx("div", { style: {
display: "flex",
justifyContent: "center",
margin: "0 0 16px",
}, children: _jsxs("div", { className: "pk-toggle", role: "group", "aria-label": "Billing period", children: [_jsx("button", { type: "button", "aria-pressed": !yearly, onClick: () => setPeriod("month"), children: L.monthly }), _jsxs("button", { type: "button", "aria-pressed": yearly, onClick: () => setPeriod("year"), children: [L.yearly, saving
? ` · ${L.save(saving.percent, formatChf(saving.chf, locale))}`
: ""] })] }) }), _jsxs("ul", { className: "pk-plans", children: [_jsxs("li", { className: "pk-card pk-paid", "data-plan": "paid", children: [_jsx("h3", { style: { margin: 0 }, children: paid.name }), paid.tagline ? _jsx("p", { style: { margin: 0 }, children: paid.tagline }) : null, _jsxs("p", { style: { margin: 0 }, children: [_jsx("strong", { style: { fontSize: 28 }, children: formatChf(price, locale) }), " ", _jsx("span", { children: priceNote })] }), paid.allowance ? (_jsx("p", { style: { margin: 0 }, children: paid.allowance })) : null, _jsx(Features, { features: paid.features, models: paid.models, label: L.models }), !config.salesEnabled && notifying ? (done ? (_jsx("p", { role: "status", style: { margin: 0 }, children: L.notifyThanks })) : (_jsxs("form", { onSubmit: submitNotify, style: { display: "grid", gap: 8 }, children: [_jsx("input", { type: "email", required: true, autoComplete: "email", "aria-label": "Email", placeholder: L.notifyPlaceholder, value: email, onChange: (e) => setEmail(e.target.value), style: { minHeight: 44, padding: "0 12px", font: "inherit" } }), notice ? (_jsx("span", { role: "alert", style: { fontSize: 13 }, children: notice })) : null, _jsx("button", { type: "submit", className: "pk-cta", children: L.notifySubmit })] }))) : config.salesEnabled ? (_jsx("button", { type: "button", className: "pk-cta", onClick: () => onChoose({ plan: "paid", period }), children: L.getPlan(paid.name) })) : (_jsx("button", { type: "button", className: "pk-cta", onClick: () => setNotifying(true), children: L.comingSoon }))] }), _jsxs("li", { className: "pk-card pk-free", "data-plan": "free", children: [_jsx("h3", { style: { margin: 0 }, children: config.free.name }), config.free.tagline ? (_jsx("p", { style: { margin: 0 }, children: config.free.tagline })) : null, _jsx("p", { style: { margin: 0 }, children: _jsx("strong", { style: { fontSize: 28 }, children: formatChf(0, locale) }) }), config.free.allowance ? (_jsx("p", { style: { margin: 0 }, children: config.free.allowance })) : null, _jsx(Features, { features: config.free.features, models: config.free.models, label: L.models }), _jsx("button", { type: "button", className: "pk-cta", onClick: () => onChoose({ plan: "free", period }), children: L.startFree })] }), _jsxs("li", { className: "pk-card pk-org", "data-plan": "org", children: [_jsx("h3", { style: { margin: 0 }, children: config.org.name }), config.org.tagline ? (_jsx("p", { style: { margin: 0 }, children: config.org.tagline })) : null, _jsx("p", { style: { margin: 0 }, children: _jsx("strong", { style: { fontSize: 28 }, children: L.custom }) }), config.org.allowance ? (_jsx("p", { style: { margin: 0 }, children: config.org.allowance })) : null, _jsx(Features, { features: config.org.features, models: config.org.models, label: L.models }), _jsx("a", { className: "pk-cta", href: config.org.contactHref, children: L.contactUs })] })] }), showOwnKey ? (_jsx("p", { style: { margin: 0, textAlign: "center" }, children: _jsx("button", { type: "button", className: "pk-own", onClick: onOpenOwnKey, children: L.ownKeyLink }) })) : null, _jsxs("ul", { className: "pk-trust", "data-trust": true, children: [_jsx("li", { children: L.trustPrices }), _jsx("li", { children: L.trustHosting }), config.bitcoin === false ? null : _jsx("li", { children: L.trustNoRenew })] })] }));
}
function Features(props) {
return (_jsxs(_Fragment, { children: [_jsx("ul", { className: "pk-feats", children: props.features.map((f) => (_jsx("li", { children: f }, f))) }), _jsxs("dl", { className: "pk-models", children: [_jsx("dt", { children: props.label }), _jsx("dd", { children: props.models })] })] }));
}
export const DEFAULT_CHECKOUT_LABELS = {
title: (amount, recipient) => `Pay ${amount} to ${recipient}`,
chooseMethod: "Pay with",
bitcoin: "Bitcoin via OrangeCat",
card: "Card",
twint: "TWINT",
bitcoinNote: "One payment. It does not renew by itself.",
cardNote: "Visa, Mastercard and wallets",
twintNote: "Pay with the TWINT app",
pay: (amount) => `Pay ${amount}`,
sats: (n) => `About ${n} sats`,
scanQr: "Scan QR code",
scanTitle: "Scan with your Bitcoin wallet",
scanHint: "Open your wallet, scan the code, confirm. This page updates when the payment arrives.",
copy: "Copy payment code",
copied: "Copied",
back: "Back",
testMode: "Test mode: no real payment is taken.",
working: "One moment…",
failed: "That did not work. Please try again.",
};
export function CheckoutSheet(props) {
const L = { ...DEFAULT_CHECKOUT_LABELS, ...props.labels };
const locale = props.locale ?? "en";
const options = [
...props.methods.map((m) => ({
id: m,
label: L[m],
note: m === "bitcoin" ? L.bitcoinNote : m === "card" ? L.cardNote : L.twintNote,
})),
...(props.extraMethods ?? []),
];
const [picked, setPicked] = useState(options[0]?.id ?? "");
const [busy, setBusy] = useState(false);
const [failed, setFailed] = useState(false);
const [code, setCode] = useState(null);
const [copied, setCopied] = useState(false);
const go = props.navigate ?? ((url) => window.location.assign(url));
const amount = formatChf(props.amountChf, locale);
const title = L.title(amount, props.recipient);
const bitcoinPicked = picked === "bitcoin";
const showSats = bitcoinPicked && props.sats !== undefined && props.sats > 0;
async function pay() {
setBusy(true);
setFailed(false);
try {
go(await props.onPay(picked));
}
catch {
setFailed(true);
setBusy(false);
}
}
async function scan() {
if (!props.onShowQr)
return;
setBusy(true);
setFailed(false);
try {
setCode(await props.onShowQr());
}
catch {
setFailed(true);
}
setBusy(false);
}
async function copy() {
if (code === null)
return;
try {
await navigator.clipboard.writeText(code);
setCopied(true);
}
catch {
setCopied(false);
}
}
const head = (_jsxs("header", { style: { display: "grid", gap: 4 }, children: [_jsx("h3", { id: "pk-co-title", style: { margin: 0, fontSize: 20, lineHeight: 1.3 }, children: title }), props.summary ? (_jsx("p", { style: { margin: 0, color: "var(--pk-muted,#475467)", fontSize: 14 }, children: props.summary })) : null] }));
if (code !== null) {
return (_jsxs("div", { role: "dialog", "aria-modal": "true", "aria-labelledby": "pk-co-title", "data-checkout-sheet": true, "data-screen": "scan", className: "pk-sheet", children: [_jsx("style", { children: CSS }), head, _jsx("p", { style: { margin: 0, fontWeight: 600 }, children: L.scanTitle }), props.renderQr ? (_jsx("div", { className: "pk-qr", "data-qr": true, children: props.renderQr(code) })) : null, showSats ? (_jsx("p", { className: "pk-sats", children: L.sats(props.sats.toLocaleString(locale)) })) : null, _jsx("p", { className: "pk-hint", children: L.scanHint }), _jsx("code", { className: "pk-code", "data-code": true, children: code }), _jsx("button", { type: "button", className: "pk-cta", onClick: copy, children: copied ? L.copied : L.copy }), _jsx("span", { role: "status", className: "pk-sr", children: copied ? L.copied : "" }), _jsx("button", { type: "button", className: "pk-link", onClick: () => {
setCode(null);
setCopied(false);
}, children: L.back })] }));
}
return (_jsxs("div", { role: "dialog", "aria-modal": "true", "aria-labelledby": "pk-co-title", "data-checkout-sheet": true, "data-screen": "pay", className: "pk-sheet", children: [_jsx("style", { children: CSS }), head, _jsxs("fieldset", { className: "pk-methods", disabled: busy, children: [_jsx("legend", { className: "pk-sr", children: L.chooseMethod }), options.map((o) => (_jsxs("label", { className: "pk-opt", "data-method": o.id, children: [_jsx("input", { type: "radio", name: "pk-method", value: o.id, checked: picked === o.id, onChange: () => setPicked(o.id) }), _jsxs("span", { className: "pk-opt-body", children: [_jsx("strong", { children: o.label }), o.note ? _jsx("small", { children: o.note }) : null] })] }, o.id)))] }), showSats ? (_jsx("p", { className: "pk-sats", "data-sats": true, children: L.sats(props.sats.toLocaleString(locale)) })) : null, props.testMode && picked !== "bitcoin" ? (_jsx("small", { className: "pk-hint", children: L.testMode })) : null, failed ? _jsx("span", { role: "alert", children: L.failed }) : null, _jsx("button", { type: "button", className: "pk-cta", disabled: busy || !picked, onClick: pay, children: busy ? L.working : L.pay(amount) }), bitcoinPicked && props.onShowQr ? (_jsx("button", { type: "button", className: "pk-link", disabled: busy, onClick: scan, children: L.scanQr })) : null, props.onClose ? (_jsx("button", { type: "button", className: "pk-link", onClick: props.onClose, children: L.back })) : null] }));
}
export const DEFAULT_OWNKEY_LABELS = {
title: "Use your own API key",
provider: "Provider",
keyField: "API key",
save: "Save key",
remove: "Remove key",
close: "Close",
saved: (l) => `Key saved, ending in ${l}.`,
privacy: "Stored encrypted on our server. It is never shown again.",
skipsAllowance: "Answers on your own key are billed by your provider and do not use your monthly allowance.",
failed: "That key was not accepted.",
};
export function OwnKeySheet(props) {
const L = { ...DEFAULT_OWNKEY_LABELS, ...props.labels };
const [vendor, setVendor] = useState(props.vendors[0]?.id ?? "");
const [key, setKey] = useState("");
const [last4, setLast4] = useState(props.saved?.last4 ?? null);
const [failed, setFailed] = useState(false);
const link = props.vendors.find((v) => v.id === vendor)?.keyUrl;
async function save(e) {
e.preventDefault();
setFailed(false);
try {
const out = await props.onSave({ vendor, key });
setLast4(out.last4);
setKey("");
}
catch {
setFailed(true);
}
}
return (_jsxs("div", { role: "dialog", "aria-modal": "true", "aria-label": L.title, "data-ownkey-sheet": true, style: { display: "grid", gap: 12 }, children: [_jsx("style", { children: CSS }), _jsx("h3", { style: { margin: 0 }, children: L.title }), _jsxs("form", { onSubmit: save, style: { display: "grid", gap: 8 }, children: [_jsxs("label", { style: { display: "grid", gap: 4 }, children: [L.provider, _jsx("select", { value: vendor, onChange: (e) => setVendor(e.target.value), style: { minHeight: 44, font: "inherit" }, children: props.vendors.map((v) => (_jsx("option", { value: v.id, children: v.label }, v.id))) })] }), _jsxs("label", { style: { display: "grid", gap: 4 }, children: [L.keyField, _jsx("input", { type: "password", autoComplete: "off", spellCheck: false, required: true, value: key, onChange: (e) => setKey(e.target.value), style: { minHeight: 44, padding: "0 12px", font: "inherit" } })] }), link ? (_jsx("a", { href: link, target: "_blank", rel: "noreferrer noopener", style: { fontSize: 13 }, children: props.vendors.find((v) => v.id === vendor)?.label })) : null, _jsx("button", { type: "submit", className: "pk-cta", children: L.save })] }), last4 ? (_jsx("p", { role: "status", style: { margin: 0 }, children: L.saved(last4) })) : null, failed ? (_jsx("p", { role: "alert", style: { margin: 0 }, children: L.failed })) : null, _jsx("small", { children: L.privacy }), _jsx("small", { children: L.skipsAllowance }), _jsxs("div", { style: { display: "flex", gap: 12 }, children: [last4 && props.onRemove ? (_jsx("button", { type: "button", className: "pk-own", onClick: async () => {
await props.onRemove?.();
setLast4(null);
}, children: L.remove })) : null, _jsx("button", { type: "button", className: "pk-own", onClick: props.onClose, children: L.close })] })] }));
}