ADR 0007 — The Billing Provider Is a Port
Status: Accepted ★
Context
Section titled “Context”PayKit put Stripe in its schema (0001_stripe_only_schema.sql) and had to drop its Polar/Razorpay
work. The research verdict: keep plan state provider-agnostic and sync from any provider.
Decision
Section titled “Decision”Subscriptions in state are provider-agnostic (plan, version, status, periodStart/End,
trialEndsAt, quantity, provider, providerRef). Providers implement:
interface BillingProvider { readonly id: string; // "stripe" | "polar" | … parseWebhook(request: Request): Promise<BillingWebhook>; // verify signature, normalize fetchSubscriptions?(providerCustomerId: string): Promise<ProviderSubscription[]>; // reconcile createCheckout?(input: CheckoutInput): Promise<{ url: string }>; // link-out createPortalSession?(input: PortalInput): Promise<{ url: string }>; // link-out reportUsage?(records: UsageRecord[]): Promise<void>; // optional usage export}- Mapping to plans comes from the catalog:
plan({ providers: { stripe: ["price_…"], polar: ["prod_…"] } })(provider item ids) (Polar products, Paddle prices, Lemon Squeezy variants). Unmapped items are ignored with a warning. - Customer resolution: our customer id from provider metadata (
metadataKey, default"customerId"; Stripe alsoclient_reference_id), else the link tablelinks/{provider}/{providerCustomerId}maintained byent.billing.link()and checkout. - Intake (
POST {basePath}/webhooks/{provider}): verify,claimthe event id (idempotent), drop events older than the subscription’sproviderUpdatedAt(out-of-order safety), apply, update cache, firesubscription.changedhooks. Unknown event types →200 ignored. - Status mapping is per provider into
active | trialing | past_due | paused | canceled | incomplete;entitledStatuses(defaultactive,trialing,past_due) decides access. - Verification is in-house and zero-dependency (Web Crypto HMAC, constant-time compare,
timestamp tolerance): Stripe
t=…,v1=…, Paddlets=…;h1=…, Lemon SqueezyX-Signature, Polar Standard Webhooks (byte-compatible with@xtandard/webhooks/receiver, tested against it). No provider SDKs: REST calls usefetch(injectable for tests). - better-auth Stripe has no webhook of ours:
betterAuthStripe(ent)returns the plugin’ssubscriptioncallbacks plusplansgenerated from the catalog. - manual: the admin UI /
ent.subscribe()set plans directly (comps, invoiced enterprise deals). - A transition matrix (trial → active → past_due → canceled, upgrade/downgrade, quantity change, cancel at period end, resubscribe, out-of-order delivery, duplicate delivery) is tested per provider with recorded fixtures.
Consequences
Section titled “Consequences”- Switching providers is a catalog-mapping change, not a migration.
- No SDK version drift; we own a small amount of signature code, covered by fixtures from each provider’s docs.