mirror of
https://github.com/bitbaum/kits.git
synced 2026-12-04 12:19:15 +01:00
Pay as you go beside the subscriptions, the way OpenRouter charges: - ./credits: append-only CreditLedger with idempotency keys and an in-memory ledger; balance, FIFO lots and debt; charge refused with 402 below the app's floor or over a spending key's daily/weekly/monthly limit; hold then settle or release; top-up quotes with a fee per rail in integer arithmetic; refund eligibility (window, fee kept, rails that never refund); expiry of old lots; auto top-up decision with backoff and a failure cap; credit events applied idempotently; top-ups through any PaymentProvider (OrangeCat: sats, never refundable). - ./stripe: stripeCredits — top-up Checkout Session (saves the method off-session when auto top-up is on), off-session PaymentIntent with an Idempotency-Key, refunds, and webhook mapping for checkout.session.completed, payment_intent.succeeded/payment_failed and charge.refunded. The pass provider ignores top-up sessions. - ./react: CreditBalance, TopUp, AutoTopUp, CreditActivity; PickerTier gains optional rates for a pay-as-you-go tier. No price, fee or threshold is built in; all are the app's config. Claude-Session: https://claude.ai/code/session_019MDtmtfseicWGK2CMC2U5r Co-authored-by: Claude <noreply@anthropic.com>
84 lines
7.9 KiB
JavaScript
84 lines
7.9 KiB
JavaScript
"use client";
|
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
/**
|
|
* Choosing a plan: one switch for the tier, what that tier gives, how often
|
|
* to pay, one button, and the fine print under it.
|
|
*
|
|
* WHY IT IS HERE. Every product that sells a subscription needs this screen,
|
|
* and the shape people already know (a segmented switch, two period cards
|
|
* side by side with the yearly saving on one, a single primary button and the
|
|
* renewal terms right under it) is the same for all of them. The app passes
|
|
* words, prices, benefits and where each button goes; the layout, the focus
|
|
* order, the selection and the 320 px behaviour are here, once.
|
|
*
|
|
* WHAT IS COMPUTED, NOT TYPED. A period card prints what it is given; the
|
|
* numbers on it (the year per month, the saving in percent) come from
|
|
* `subscriptionPricing` in `@bitbaum/paykit/plans`, so a page cannot promise
|
|
* a saving the prices do not give.
|
|
*
|
|
* TIERS OF EVERY KIND have the same parts in the same place (name, one line,
|
|
* price, what is in it, one action): a subscription with periods, a free
|
|
* tier, "your own key" (the product's AI is paid by the person's own key,
|
|
* at no charge from the product), "pay as you go" (prepaid credit, with
|
|
* `rates`: what each use costs), or "contact us". Only a subscription has
|
|
* period cards.
|
|
*
|
|
* STYLE comes from `@bitbaum/paykit/styles.css` (`pk-*` classes) and the
|
|
* `--pk-*` variables, which fall back to `--sk-*` and then to the system's
|
|
* own light or dark colours. An app maps them to its tokens once.
|
|
*/
|
|
import { useId, useState } from "react";
|
|
const CHECK = (_jsx("svg", { className: "pk-check", viewBox: "0 0 16 16", "aria-hidden": "true", focusable: "false", children: _jsx("path", { d: "M3.5 8.5l3 3 6-7", fill: "none", stroke: "currentColor", strokeWidth: "1.75", strokeLinecap: "round", strokeLinejoin: "round" }) }));
|
|
function Action({ action, period }) {
|
|
const [busy, setBusy] = useState(false);
|
|
switch (action.kind) {
|
|
case "link":
|
|
return (_jsx("a", { className: "pk-button", href: action.href, "data-pk": "action", children: action.label }));
|
|
case "custom":
|
|
return _jsx("div", { className: "pk-custom", children: action.node });
|
|
case "current":
|
|
return (_jsx("p", { className: "pk-current", role: "status", "data-pk": "current", children: action.label }));
|
|
case "button":
|
|
return (_jsx("button", { type: "button", className: "pk-button", "data-pk": "action", disabled: busy, "aria-busy": busy || undefined, onClick: async () => {
|
|
if (busy)
|
|
return;
|
|
setBusy(true);
|
|
try {
|
|
await action.onSelect(period);
|
|
}
|
|
finally {
|
|
setBusy(false);
|
|
}
|
|
}, children: busy && action.busyLabel ? action.busyLabel : action.label }));
|
|
}
|
|
}
|
|
export function PlanPicker({ tiers, defaultTier, label, className, }) {
|
|
const id = useId();
|
|
const [tierId, setTierId] = useState(() => tiers.find((t) => t.id === defaultTier)?.id ?? tiers[0]?.id ?? "");
|
|
const tier = tiers.find((t) => t.id === tierId) ?? tiers[0];
|
|
const [periods, setPeriods] = useState(() => Object.fromEntries(tiers.filter((t) => t.periods?.length).map((t) => [t.id, t.periods?.find((p) => p.id === t.defaultPeriod)?.id ?? t.periods?.[0]?.id ?? ""])));
|
|
if (!tier)
|
|
return null;
|
|
const period = tier.periods?.length ? (periods[tier.id] ?? tier.periods[0]?.id ?? null) : null;
|
|
function move(delta) {
|
|
const at = tiers.findIndex((t) => t.id === tier?.id);
|
|
const next = tiers[(at + delta + tiers.length) % tiers.length];
|
|
if (!next)
|
|
return;
|
|
setTierId(next.id);
|
|
document.getElementById(`${id}-tab-${next.id}`)?.focus();
|
|
}
|
|
return (_jsxs("div", { className: ["pk-picker", className].filter(Boolean).join(" "), "data-pk": "picker", children: [tiers.length > 1 && (_jsx("div", { className: "pk-switch", role: "tablist", "aria-label": label, "data-count": tiers.length, onKeyDown: (event) => {
|
|
if (event.key === "ArrowRight")
|
|
move(1);
|
|
else if (event.key === "ArrowLeft")
|
|
move(-1);
|
|
else
|
|
return;
|
|
event.preventDefault();
|
|
}, children: tiers.map((t) => (_jsxs("button", { id: `${id}-tab-${t.id}`, type: "button", role: "tab", className: "pk-switch-tab", "aria-selected": t.id === tier.id, "aria-controls": `${id}-panel`, tabIndex: t.id === tier.id ? 0 : -1, "data-tier": t.id, onClick: () => setTierId(t.id), children: [_jsx("span", { className: "pk-switch-name", children: t.name }), t.badge && _jsx("span", { className: "pk-switch-dot", "aria-hidden": "true" })] }, t.id))) })), _jsxs("section", { id: `${id}-panel`, className: "pk-panel", role: tiers.length > 1 ? "tabpanel" : undefined, "aria-labelledby": tiers.length > 1 ? `${id}-tab-${tier.id}` : undefined, "data-tier": tier.id, children: [_jsxs("header", { className: "pk-head", children: [tier.badge && (_jsx("p", { className: "pk-badge", "data-pk": "badge", children: tier.badge })), _jsx("h2", { className: "pk-name", children: tier.name }), tier.tagline && _jsx("p", { className: "pk-tagline", children: tier.tagline })] }), tier.periods && tier.periods.length > 0 ? (_jsxs("fieldset", { className: "pk-periods", "data-count": tier.periods.length, children: [tier.periodsLabel && _jsx("legend", { className: "pk-visually-hidden", children: tier.periodsLabel }), tier.periods.map((p) => {
|
|
const checked = p.id === period;
|
|
return (_jsxs("label", { className: "pk-period", "data-period": p.id, "data-checked": checked || undefined, children: [_jsx("input", { className: "pk-visually-hidden", type: "radio", name: `${id}-${tier.id}-period`, value: p.id, checked: checked, onChange: () => setPeriods((all) => ({ ...all, [tier.id]: p.id })) }), _jsxs("span", { className: "pk-period-top", children: [_jsx("span", { className: "pk-period-label", children: p.label }), p.badge && (_jsx("span", { className: "pk-period-badge", "data-pk": "save", children: p.badge }))] }), _jsxs("span", { className: "pk-period-price", children: [_jsx("span", { className: "pk-amount", children: p.price }), " ", _jsx("span", { className: "pk-per", children: p.per })] }), p.highlight && (_jsx("span", { className: "pk-period-highlight", "data-pk": "intro", children: p.highlight })), p.detail && _jsx("span", { className: "pk-period-detail", children: p.detail })] }, p.id));
|
|
})] })) : (tier.price && (_jsxs("p", { className: "pk-price", children: [_jsx("span", { className: "pk-amount", children: tier.price }), tier.per && _jsx("span", { className: "pk-price-per", children: tier.per })] }))), tier.rates && tier.rates.length > 0 && (_jsxs("div", { className: "pk-rates-wrap", "data-pk": "rates", children: [tier.ratesLabel && _jsx("h3", { className: "pk-benefits-label", children: tier.ratesLabel }), _jsx("dl", { className: "pk-rates", children: tier.rates.map((rate) => (_jsxs("div", { className: "pk-rate", children: [_jsx("dt", { children: rate.label }), _jsxs("dd", { children: [_jsx("span", { className: "pk-rate-price", children: rate.price }), rate.per && _jsxs("span", { className: "pk-per", children: [" ", rate.per] })] })] }, rate.label))) })] })), tier.benefits.length > 0 && (_jsxs("div", { className: "pk-benefits-wrap", children: [tier.benefitsLabel && _jsx("h3", { className: "pk-benefits-label", children: tier.benefitsLabel }), _jsx("ul", { className: "pk-benefits", children: tier.benefits.map((benefit) => (_jsxs("li", { children: [CHECK, _jsx("span", { children: benefit })] }, benefit))) })] })), _jsx("div", { className: "pk-action", children: _jsx(Action, { action: tier.action, period: period }, `${tier.id}:${period ?? ""}`) }), tier.fine && _jsx("div", { className: "pk-fine", children: tier.fine })] })] }));
|
|
}
|