import { env } from "cloudflare:workers"; import type Stripe from "stripe"; import { first, run } from "../db"; import { applyCreditMovement, isCreditMeter, purchasesForPayment } from "../metering/credits"; import { setPlan } from "../metering/quota"; import { stripe, stripeEnabled, webCrypto } from "./config"; import { ACTIVE_STATUSES, accountForCustomer, customerFor, linkCustomer } from "./checkout"; export const STRIPE_WEBHOOK_PATH = "/stripe/webhook"; const MAX_BODY_BYTES = 512 * 1024; function idOf(value: string | { id: string } | null | undefined): string | null { if (!value) return null; return typeof value === "string" ? value : value.id; } /** The account a Stripe object belongs to: the mapped customer first, else our metadata (then it is linked). */ async function accountFor( customer: string | null, metadata: Stripe.Metadata | null | undefined, ): Promise { if (customer) { const mapped = await accountForCustomer(customer); if (mapped) return mapped; } const claimed = metadata?.account_id; if (!claimed || !(await first("SELECT 1 AS one FROM accounts WHERE id = ?", claimed))) return null; if (customer) await linkCustomer(claimed, customer); // A customer already linked to another account never moves: trust the mapping, not the metadata. if (customer && (await accountForCustomer(customer)) !== claimed) return accountForCustomer(customer); return claimed; } async function saveSubscription( accountId: string, fields: { id: string; status: string; priceId: string | null; periodEnd: number | null; cancelAtPeriodEnd: boolean }, eventAt: number, ): Promise { const result = await run( `UPDATE billing_customers SET subscription_id = ?, subscription_status = ?, price_id = ?, current_period_end = ?, cancel_at_period_end = ?, subscription_event_at = ?, updated_at = ? WHERE account_id = ? AND subscription_event_at <= ?`, fields.id, fields.status, fields.priceId, fields.periodEnd, fields.cancelAtPeriodEnd ? 1 : 0, eventAt, Date.now(), accountId, eventAt, ); return result.meta.changes > 0; } function isProPrice(priceId: string | null): boolean { return Boolean(priceId && env.STRIPE_PRICE_PRO && priceId === env.STRIPE_PRICE_PRO); } async function onSubscription(subscription: Stripe.Subscription, eventAt: number): Promise { const customer = idOf(subscription.customer); const accountId = await accountFor(customer, subscription.metadata); if (!accountId) return; const item = subscription.items.data.find((entry) => isProPrice(entry.price.id)) ?? subscription.items.data[0]; const priceId = item?.price.id ?? null; const current = await customerFor(accountId); // Only the account's current subscription (or a new one replacing an ended one) drives the plan. if (current?.subscription_id && current.subscription_id !== subscription.id) { const replacing = !current.subscription_status || !ACTIVE_STATUSES.has(current.subscription_status); if (!replacing || !ACTIVE_STATUSES.has(subscription.status)) return; } const applied = await saveSubscription( accountId, { id: subscription.id, status: subscription.status, priceId, periodEnd: item?.current_period_end ? item.current_period_end * 1000 : null, cancelAtPeriodEnd: subscription.cancel_at_period_end, }, eventAt, ); if (!applied) return; await setPlan(accountId, ACTIVE_STATUSES.has(subscription.status) && isProPrice(priceId) ? "pro" : "free"); } async function onCheckoutPaid(session: Stripe.Checkout.Session, eventAt: number): Promise { const accountId = await accountFor(idOf(session.customer), session.metadata); if (!accountId) return; if (session.mode === "subscription") { const subscriptionId = idOf(session.subscription); if (!subscriptionId || session.status !== "complete") return; const current = await customerFor(accountId); if (current && current.subscription_event_at > eventAt) return; // The subscription events carry the authoritative status; this only makes the upgrade visible immediately. await run( `UPDATE billing_customers SET subscription_id = ?, subscription_status = COALESCE(subscription_status, 'active'), price_id = COALESCE(price_id, ?), updated_at = ? WHERE account_id = ? AND (subscription_id IS NULL OR subscription_id = ?)`, subscriptionId, env.STRIPE_PRICE_PRO ?? null, Date.now(), accountId, subscriptionId, ); const row = await customerFor(accountId); if ( row?.subscription_id === subscriptionId && row.subscription_status && ACTIVE_STATUSES.has(row.subscription_status) ) { await setPlan(accountId, "pro"); } return; } if (session.mode !== "payment" || session.payment_status !== "paid") return; const meter = session.metadata?.meter ?? ""; const quantity = Number(session.metadata?.quantity); if (session.metadata?.kind !== "topup" || !isCreditMeter(meter) || !Number.isFinite(quantity) || quantity <= 0) return; await applyCreditMovement({ ref: `purchase:${session.id}`, accountId, meter, kind: "purchase", quantity, pack: session.metadata?.pack ?? null, paymentIntent: idOf(session.payment_intent), amountCents: session.amount_total, }); } /** A failed renewal: record past_due now; drop to free once Stripe has no retry left. */ async function onPaymentFailed(invoice: Stripe.Invoice, eventAt: number): Promise { const customer = idOf(invoice.customer); const accountId = customer ? await accountForCustomer(customer) : null; if (!accountId) return; const current = await customerFor(accountId); if (!current?.subscription_id || current.subscription_event_at > eventAt) return; const final = invoice.next_payment_attempt === null; await run( "UPDATE billing_customers SET subscription_status = ?, updated_at = ? WHERE account_id = ?", final ? "unpaid" : "past_due", Date.now(), accountId, ); if (final) await setPlan(accountId, "free"); } /** Refunds reverse the refunded share of each pack bought with this payment, never more than is unspent. */ async function onRefund(charge: Stripe.Charge): Promise { const paymentIntent = idOf(charge.payment_intent); if (!paymentIntent || charge.amount <= 0) return; const fraction = Math.min(1, charge.amount_refunded / charge.amount); for (const purchase of await purchasesForPayment(paymentIntent)) { const target = purchase.quantity * fraction - purchase.reversed; if (target <= 0) continue; await applyCreditMovement({ ref: `refund:${charge.id}:${charge.amount_refunded}:${purchase.ref}`, accountId: purchase.accountId, meter: purchase.meter, kind: "refund", quantity: target, paymentIntent, }); } } async function onDispute(dispute: Stripe.Dispute): Promise { const paymentIntent = idOf(dispute.payment_intent); if (!paymentIntent) return; for (const purchase of await purchasesForPayment(paymentIntent)) { const target = purchase.quantity - purchase.reversed; if (target <= 0) continue; await applyCreditMovement({ ref: `dispute:${dispute.id}:${purchase.ref}`, accountId: purchase.accountId, meter: purchase.meter, kind: "dispute", quantity: target, paymentIntent, }); } } export const HANDLED_EVENTS = [ "checkout.session.completed", "checkout.session.async_payment_succeeded", "customer.subscription.created", "customer.subscription.updated", "customer.subscription.deleted", "invoice.payment_failed", "charge.refunded", "charge.dispute.created", ] as const; /** * Applies one verified event at most once. The event id is recorded only after the handler succeeds, so a * failure makes Stripe retry; every handler is itself idempotent, so a concurrent duplicate is harmless. */ export async function processStripeEvent(event: Stripe.Event): Promise<"processed" | "duplicate" | "ignored"> { if (await first("SELECT 1 AS one FROM stripe_events WHERE id = ?", event.id)) return "duplicate"; const eventAt = event.created * 1000; let handled = true; switch (event.type) { case "checkout.session.completed": case "checkout.session.async_payment_succeeded": await onCheckoutPaid(event.data.object, eventAt); break; case "customer.subscription.created": case "customer.subscription.updated": case "customer.subscription.deleted": await onSubscription(event.data.object, eventAt); break; case "invoice.payment_failed": await onPaymentFailed(event.data.object, eventAt); break; case "charge.refunded": await onRefund(event.data.object); break; case "charge.dispute.created": await onDispute(event.data.object); break; default: handled = false; } await run( "INSERT OR IGNORE INTO stripe_events (id, type, processed_at) VALUES (?, ?, ?)", event.id, event.type, Date.now(), ); return handled ? "processed" : "ignored"; } /** Verifies the Stripe-Signature header against the raw body; returns null when it is missing or wrong. */ export async function verifyStripeEvent(body: string, signature: string | null): Promise { const secret = env.STRIPE_WEBHOOK_SECRET; if (!secret || !signature) return null; try { return await stripe().webhooks.constructEventAsync(body, signature, secret, undefined, webCrypto); } catch { return null; } } export async function handleStripeWebhook(request: Request): Promise { if (!stripeEnabled() || !env.STRIPE_WEBHOOK_SECRET) return new Response("Not found\n", { status: 404 }); if (request.method !== "POST") return new Response("Method not allowed\n", { status: 405 }); const length = Number(request.headers.get("content-length") ?? 0); if (length > MAX_BODY_BYTES) return new Response("Payload too large\n", { status: 413 }); const body = await request.text(); if (body.length > MAX_BODY_BYTES) return new Response("Payload too large\n", { status: 413 }); const event = await verifyStripeEvent(body, request.headers.get("stripe-signature")); if (!event) return Response.json({ error: "Invalid signature" }, { status: 400 }); const outcome = await processStripeEvent(event); return Response.json({ received: true, outcome }); }