Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions scripts/stripe-setup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
// the live secret. An existing endpoint keeps its secret, since Stripe shows a secret only once (--new-webhook
// deletes GenClass's endpoint, the one with that exact URL and description, and makes a new one);
// 4. its own Billing Portal configuration (metadata genclass=1, a headline naming GenClass): cancel at the end of the
// period, update the card, invoices, and change the email or tax id on the GenClass customer (no address: GenClass
// has no use for one). It offers no plan switch: a subscription sold through Managed Payments cannot take another
// period (the worker's webhook ends a trial cancelled there at once: telemetry-worker/src/stripe.ts
// trialCancelled), update the card, invoices, and change the email or tax id on the GenClass customer (no address:
// GenClass has no use for one). It offers no plan switch: a subscription sold through Managed Payments cannot take another
// plan's price, so the billing page switches plans with a new Checkout (telemetry-worker/src/billing.ts). To find
// it the script lists the non-default configurations (is_default=false), reading their settings and changing none
// but its own: the account's default configuration is never read or changed, and GenClass's is never made the
Expand Down Expand Up @@ -336,6 +337,8 @@ export async function main({ argv = process.argv.slice(2), env = process.env, fe
customer_update: { enabled: true, allowed_updates: ["email", "tax_id"] },
invoice_history: { enabled: true },
payment_method_update: { enabled: true },
// one mode for every subscription: "immediately" would end paid plans at once too, so a paid plan runs to the end
// of its paid month, and the worker's webhook ends a trial cancelled here at once (src/stripe.ts trialCancelled)
subscription_cancel: {
enabled: true,
mode: "at_period_end",
Expand Down
7 changes: 6 additions & 1 deletion telemetry-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ subscription bought per project under version 4 still covers its own app (never
Pro. Free covers one app (`free_projects` past it). Every new app of an account opens its plan page once, a paying
account's too: "Continue with Pro" (`POST /api/billing/projects/<id>/continue` writes `plan_chosen`). Pro's
3-day trial (card up front, once per account) starts on the account's first Checkout (`src/billing.ts` trialFor).
A cancel during the trial ends the trial and Pro at once (the owner, 2026-11-15): the portal schedules it for the
trial's end, and the webhook then cancels the subscription in Stripe and writes Free (`src/stripe.ts` trialCancelled).
A paid plan cancelled in the portal still runs to the end of its paid month.
**The genclass command is Pro's** (the owner's "let's make the entire CLI paid"): a CLI session of an account with
no plan (paid, trialing, a grant) and no active place in a paying team gets 402 `needs_pro` from every `/api/cli`
route but the overview and the account, which carry `cli` (`src/cli.ts` cliPaid). Nothing is locked for a browser
Expand Down Expand Up @@ -814,7 +817,9 @@ does about it.
payment methods, billing address); the Customer Portal can be offered too; `subscription.default_payment_method`
cannot be changed through the API for these subscriptions, only by the buyer in the portal
([how it works](https://docs.stripe.com/payments/managed-payments/how-it-works)). GenClass's portal keeps card,
invoices, email, tax id and cancel at period end. Cancelling with `prorate` and `invoice_now` would put the credit
invoices, email, tax id and cancel at period end. The portal's cancel mode is one setting for every
subscription, so it stays at period end for paid plans; a trial cancelled there is ended at once by the webhook
(`src/stripe.ts` trialCancelled). Cancelling with `prorate` and `invoice_now` would put the credit
for unused time on the customer's balance for future invoices; both default to false
([cancel](https://docs.stripe.com/billing/subscriptions/cancel)), and GenClass sends neither: it refunds the unused
time to the card instead (a refund can be partial and goes to the payment method charged:
Expand Down
2 changes: 1 addition & 1 deletion telemetry-worker/src/billing-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function statusLine(v: BillingView, plan: Plan): string {
return `You are on the ${name} plan until ${day(b.current_period_end)}. It is cancelled and will not renew; you can resume it in Manage billing before then.`;
}
if (b.subscription_status === "trialing") {
return `You are on the ${name} plan, in a trial until ${day(b.current_period_end)}. When it ends your card is charged for the first month (${esc(priceText(plan))}), unless you cancel before in Manage billing.`;
return `You are on the ${name} plan, in a trial until ${day(b.current_period_end)}. When it ends your card is charged for the first month (${esc(priceText(plan))}). Cancel in Manage billing before then and nothing is charged; ${esc(plan.name)} then ends at once.`;
}
if (plan.per_seat === true && v.access.seats !== null) {
const n = v.access.seats;
Expand Down
26 changes: 25 additions & 1 deletion telemetry-worker/src/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
// has never had a subscription (billing.ts checkoutProject), never on a switch or a later one. Every Checkout still
// collects a payment method up front (payment_method_collection "always"), and a subscription in Stripe's trialing
// status that the account has not been granted yet counts only with a saved payment method (cardOnFile); Stripe
// charges the first month when the trial ends unless it is cancelled before.
// charges the first month when the trial ends unless it is cancelled before. A cancel during the trial ends it at
// once: Stripe's portal schedules it for the trial's end, and the webhook then cancels the subscription now
// (trialCancelled). A paid subscription cancelled in the portal still runs to the end of its paid month.
// - One subscription per account. Checkout asks Stripe (not the database, which a late webhook leaves behind) whether
// the customer already has one: the same plan is refused, the other plan is a switch (a Checkout whose subscription
// carries metadata genclass_switch_from naming the one it replaces); an account's older unpaid Checkout pages are
Expand Down Expand Up @@ -800,6 +802,20 @@ export function cardOnFile(sub: Obj): boolean {
return !!(idOf(sub?.default_payment_method) ?? idOf(sub?.default_source) ?? idOf(c?.invoice_settings?.default_payment_method) ?? idOf(c?.default_source));
}

/**
* A subscription in its free trial that is set to cancel by the end of that trial: Stripe's portal (flexible billing
* mode) sets cancel_at to the period end, which in a trial is the trial's end, and an older integration sets
* cancel_at_period_end. GenClass ends such a trial at once instead of letting it run out (the owner, 2026-11-15). A
* cancel set for a later date (after a paid month, by hand in the Stripe dashboard) is left to Stripe, and so is any
* subscription past its trial: a paid plan cancelled in the portal still runs to the end of its paid month. Exported
* for tests.
*/
export function trialCancelled(sub: Obj): boolean {
if (sub?.status !== "trialing") return false;
if (sub?.cancel_at_period_end === true) return true;
return typeof sub?.cancel_at === "number" && typeof sub?.trial_end === "number" && sub.cancel_at <= sub.trial_end;
}

/** The project a per-project subscription is for (its metadata genclass_project, billing/plans.json version 4), or null. */
export const projectOf = (s: Obj): string | null => {
const v = s?.metadata?.genclass_project;
Expand Down Expand Up @@ -856,6 +872,14 @@ export async function handleEvent(x: EventContext, event: Obj): Promise<EventRes
if (!accountsSubscription(account, sub)) return ignored("the subscription is not the account's", event, true);
// a trial from before GenClass dropped trials whose card failed the $1 check stays out of the account for good
if (sub?.metadata?.genclass_card_check === "failed") return ignored("its card failed the $1 check", event, true);
// cancelled in its free trial: it ends now, not when the trial would have (trialCancelled). Nothing was paid, so
// nothing is refunded; the subscription as Stripe has it after the cancel (canceled) is what is written below, and
// the customer.subscription.deleted that follows writes the same. A failure answers 500 and Stripe's retry finishes it
if (trialCancelled(sub) && isGenClassSubscription(sub)) {
await cancelNow(x.env, x.fetch, subId);
console.log(`stripe webhook: account ${account.id}: subscription ${subId} was cancelled in its trial; ended it now`);
sub = await fetchSubscription(x, subId);
}
// a plan per project (billing/plans.json version 4): written to the project its metadata names, never to the account
if (projectOf(sub)) return applyProjectSubscription(x, account, sub, event);
// the subscription already recorded for the account stays readable whatever its price now says (it then counts as
Expand Down
Loading
Loading