---
name: stripe-billing-setup
description: Set up Stripe billing for a SaaS — products and prices, Checkout sessions, customer portal, webhook handling with signature verification and idempotency, subscription status sync to the database, and a test-mode checklist. Use for subscriptions, payments integration, or 'add Stripe to my app'.
license: Proprietary — Lendskill original
---

# Stripe Billing Setup

Work in **test mode** with test keys until the user explicitly switches to live. Never put secret keys in client code or the repo.

## 1. Catalog
Products and recurring prices matching the pricing plan (monthly + annual), lookup keys for each price so code doesn't hard-code IDs.

## 2. Checkout
Server endpoint creates a Checkout Session (`mode: 'subscription'`, `customer` or `customer_email`, `client_reference_id` = your user id, success/cancel URLs). Customer portal for upgrades, downgrades, cancellation and invoices.

## 3. Webhooks (the source of truth)
Verify the signature with the endpoint secret on the raw body. Handle at least: `checkout.session.completed`, `customer.subscription.created/updated/deleted`, `invoice.paid`, `invoice.payment_failed`. Make handlers idempotent (store processed event IDs). Write plan, status, current period end and customer id to your `subscriptions` table; gate features on that table, not on redirect pages.

## 4. Test
Stripe CLI (`stripe listen --forward-to localhost:PORT/api/stripe/webhook`, `stripe trigger …`), test cards for success, decline and 3-D Secure, cancellation and failed renewal. Checklist of what to change for live (keys, webhook endpoint, tax settings) — the user does the switch.

## Output format
- Products and prices to create (test mode first)
- Checkout, webhook and customer-portal code with the events handled
- A test plan using Stripe test cards and the CLI
- A go-live checklist
