Reference implementation from raztrix's production code (Next.js + Drizzle + Postgres). Anonymized: no secrets, no tenant data. Logger calls left in for shape — replace with your logger.
lib-stripe.ts— lazy SDK init. Use the Proxy pattern. Module-levelnew Stripe()breaks Next.js standalone builds and crash-loops on missing env vars at boot.webhook-route.ts— Stripe event handler. Signature verification, switch on event type, 5 handlers (checkout.session.completed,customer.subscription.{created,updated,deleted},invoice.payment_{succeeded,failed}).create-checkout-session-route.ts— POST endpoint. Creates Stripe customer if absent, opens subscription Checkout with 14-day trial, promo codes enabled, success/cancel URLs.