The Billing module takes payments through Stripe or Dodo Payments. Pick one with NEXT_PUBLIC_PAYMENT_GATEWAY:
| Value | Use when |
|---|---|
stripe | You handle tax and compliance yourself, or through Stripe Tax |
dodo | You want a Merchant of Record that handles global sales tax and VAT for you |
none | Billing UI without a gateway, for example while you decide |
A download keeps only the gateway you chose. Switching later is a free upgrade.
Stripe
- Get your secret key at dashboard.stripe.com/apikeys (
sk_test_…while testing). - Create a webhook endpoint at dashboard.stripe.com/webhooks:
- URL:
https://your-domain.com/api/payments/stripe/webhook - Event:
checkout.session.completed
- URL:
- Copy the endpoint's signing secret (
whsec_…).
NEXT_PUBLIC_PAYMENT_GATEWAY="stripe"
STRIPE_SECRET_KEY="sk_test_..."
STRIPE_WEBHOOK_SECRET="whsec_..."
Checkout uses Stripe-hosted pages with price_data, so you do not need to create products. pnpm doctor verifies the key and checks that an enabled webhook endpoint exists for your NEXT_PUBLIC_URL.
Testing locally
stripe login
stripe listen --forward-to localhost:3000/api/payments/stripe/webhook
stripe listen prints a whsec_… secret for local use; put it in STRIPE_WEBHOOK_SECRET. Pay with card 4242 4242 4242 4242, any future date and any CVC.
Dodo Payments
- In the Dodo dashboard, create a one-time product that represents 50 credits. Copy its product ID.
- Under Developer → API Keys, create a key. Keys work only in the mode they were created in (test or live).
- Under Developer → Webhooks, add
https://your-domain.com/api/payments/dodo/webhookand copy its secret.
NEXT_PUBLIC_PAYMENT_GATEWAY="dodo"
DODO_PAYMENTS_API_KEY="..."
DODO_PAYMENTS_WEBHOOK_KEY="whsec_..."
DODO_PAYMENTS_ENVIRONMENT="test_mode" # live_mode in production (the default)
DODO_CREDITS_PRODUCT_ID="pdt_..."
NEXT_PUBLIC_DODO_PAYMENTS_URL="https://test.dodopayments.com"
The checkout buys credits / 50 units of that product. NEXT_PUBLIC_DODO_PAYMENTS_URL is used to build the invoice link stored on each transaction.
Testing locally
Dodo needs a public URL. Expose your dev server with a tunnel and register the tunnel URL as the webhook endpoint:
cloudflared tunnel --url http://localhost:3000
# or
ngrok http 3000
How the webhooks work
Both handlers verify the signature first (Stripe with constructEvent, Dodo with @dodopayments/nextjs), then credit the user exactly once:
- Read the user ID and credit amount from the checkout metadata, and the unique checkout or payment ID.
- Return early if a
Transactionwith thatcheckoutSessionIdalready exists (gateways retry deliveries). - Inside
db.$transaction, check again, then create theTransactionand incrementcreditsTotal. - If two deliveries race past both checks, the unique constraint on
checkoutSessionIdmakes the second insert fail with PrismaP2002, which is caught and answered with success.
| Gateway | Event handled | Route |
|---|---|---|
| Stripe | checkout.session.completed | /api/payments/stripe/webhook |
| Dodo | payment.succeeded (onPaymentSucceeded) | /api/payments/dodo/webhook |
Webhook routes have no session; their route policy entries use auth: "webhook", a 1 MB body limit and provider-side validation. Follow the same four steps for any new webhook you add.
Going live
- Switch to live keys (
sk_live_…, or a live-mode Dodo key withDODO_PAYMENTS_ENVIRONMENT="live_mode"and the live product ID). - Create the live webhook endpoint and update the secret.
- Make one real purchase and refund it from the gateway dashboard.
- Check that the transaction and credits appear in Settings → Plans & Billing.
Troubleshooting
- Paid but no credits: check the gateway's webhook delivery log. A
400means a bad signature (wrong secret or a proxy changing the body); a timeout means the URL is not reachable. - Duplicate deliveries: expected. The handlers are idempotent and answer 200 with "already processed".
- Dodo key rejected: the key's mode does not match
DODO_PAYMENTS_ENVIRONMENT.
