Wave E verifier (Y-2/Y-3): - A card check started a fresh 14-day trial every time: cancel or refund, pay $1 again (credited back) and get another trial, for $0 net. The trial now starts only when trial_ends_at is unset; refunds keep it set and a paid plan marks it used. access() sends a card-on-file org that used its trial to the plan, and the Billing screen says so. - startCheckout opened a second plan checkout for an already subscribed (or cancelling) org, which bills twice; and a second card check for an org with a card on file. Both are refused (BillingError, ?error=already). - A refund of an older card-check payment kept its dollar credit, and a second refund event for the same payment could take it back twice. The debit now always applies once per payment, tracked in billing_events. - subscription.* events of a subscription that is not the org's current one (an old one expiring) no longer end or pause the current plan. - Deploy records normalise the app name the CLI sends (folder 'My Shop' -> my-shop) so the second deploy is not a 409; rollback_of must be a uuid (was a database error). Tests: 7 new (fail before, pass after); console 37/37, tsc clean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
3.4 KiB
TypeScript
73 lines
3.4 KiB
TypeScript
// Billing actions behind the Billing screen: the $1 card check, the plan
|
|
// checkout (monthly or yearly), the paused switch and one-click cancel.
|
|
|
|
import type { Config } from "./config.ts";
|
|
import { Dodo, signMetadata } from "./dodo.ts";
|
|
import { access, endTrial, localPause } from "./ladder.ts";
|
|
import { randomToken } from "./crypto.ts";
|
|
import type { Repo } from "./repo.ts";
|
|
import { emptyEntitlement, type Entitlement } from "./types.ts";
|
|
import type { Interval } from "./plans.ts";
|
|
|
|
export type CheckoutKind = "card_check" | "plan";
|
|
|
|
export class BillingError extends Error {
|
|
code: "already_subscribed" | "card_on_file";
|
|
constructor(code: "already_subscribed" | "card_on_file", message: string) {
|
|
super(message);
|
|
this.code = code;
|
|
}
|
|
}
|
|
|
|
export async function startCheckout(
|
|
deps: { repo: Repo; dodo: Pick<Dodo, "createCheckout">; cfg: Pick<Config, "consoleUrl" | "sessionSecret" | "products"> },
|
|
who: { orgId: string; email: string },
|
|
kind: CheckoutKind,
|
|
interval: Interval = "month",
|
|
): Promise<string> {
|
|
// Never open a second charge for something the org already has: a second
|
|
// subscription would bill twice, a second card check could restart a trial.
|
|
const ent = await deps.repo.getEntitlement(who.orgId);
|
|
if (kind === "plan" && ent?.plan === "railyard" && ent.subscription_id) throw new BillingError("already_subscribed", "This org already has the Railyard plan.");
|
|
if (kind === "card_check" && ent?.card_verified_at) throw new BillingError("card_on_file", "A card is already on file.");
|
|
const requestId = randomToken(12);
|
|
const productId = kind === "card_check" ? deps.cfg.products.cardCheck : interval === "year" ? deps.cfg.products.year : deps.cfg.products.month;
|
|
const metadata: Record<string, string> = {
|
|
service: "railyard",
|
|
org_id: who.orgId,
|
|
kind,
|
|
request_id: requestId,
|
|
sig: await signMetadata(deps.cfg.sessionSecret, who.orgId, kind, requestId),
|
|
};
|
|
if (kind === "plan") metadata.interval = interval;
|
|
const { checkoutUrl } = await deps.dodo.createCheckout({
|
|
productId,
|
|
email: who.email,
|
|
metadata,
|
|
returnUrl: `${deps.cfg.consoleUrl}/billing?checkout=${kind}`,
|
|
});
|
|
return checkoutUrl;
|
|
}
|
|
|
|
async function current(repo: Repo, orgId: string, now: Date): Promise<Entitlement> {
|
|
return (await repo.getEntitlement(orgId)) ?? emptyEntitlement(orgId, now);
|
|
}
|
|
|
|
/** The paused switch. A subscription is paused at the provider; a trial is paused here. */
|
|
export async function setPaused(deps: { repo: Repo; dodo: Pick<Dodo, "setPaused"> }, orgId: string, paused: boolean, now: Date): Promise<Entitlement> {
|
|
const ent = await current(deps.repo, orgId, now);
|
|
if (ent.subscription_id) await deps.dodo.setPaused(ent.subscription_id, paused);
|
|
return deps.repo.saveEntitlement(localPause(ent, paused, now));
|
|
}
|
|
|
|
/** One click, no survey. A subscription ends at its next billing date; a trial ends now. */
|
|
export async function cancel(deps: { repo: Repo; dodo: Pick<Dodo, "cancelAtPeriodEnd"> }, orgId: string, now: Date): Promise<Entitlement> {
|
|
const ent = await current(deps.repo, orgId, now);
|
|
if (ent.subscription_id && ent.plan === "railyard") {
|
|
await deps.dodo.cancelAtPeriodEnd(ent.subscription_id);
|
|
return deps.repo.saveEntitlement({ ...ent, cancel_at_period_end: true, updated_at: now.toISOString() });
|
|
}
|
|
if (access(ent, now).state === "trial" || ent.plan === "trial") return deps.repo.saveEntitlement(endTrial(ent, now));
|
|
return ent;
|
|
}
|