How to accept payments in your SaaS (Stripe or Dodo)
Checkout, webhooks, a payment ledger and an access rule for your SaaS: every manual step, and what Launchfast ships for Dodo or Stripe.
Short answer: send the buyer to a hosted checkout from a server action, receive the provider's webhooks on a verified route, save each payment and subscription in your own Postgres tables, and decide access by reading those tables. Never call the payment provider while rendering a page. Launchfast already has all four pieces for Dodo Payments and Stripe, and npm run setup creates the products and the webhook.
What does taking payments involve?
A payment button is the small part. The rest is making your database agree with the provider, because the browser redirect after checkout proves nothing. The buyer can close the tab, the redirect can fail, and a renewal happens weeks later with nobody on your site.
The symptoms people search for:
- The customer paid but the app still shows the free plan.
No signatures found matching the expected signature for payloadfrom Stripe, usually because the body was parsed before it was verified.- The same payment appears twice after the provider retried a webhook.
- A cancelled subscription still has access, because an old event overwrote a newer one.
How do you do it by hand?
- Pick a provider and create the product. Stripe needs a product with a price. Dodo Payments needs a product. Create them in test mode first. (Stripe does not act as merchant of record, so sales tax is yours to handle. Check each provider's docs for what they cover. This is not legal or tax advice.)
- Get API keys and keep them server only. Put them in
.env.local, and import the SDK only in server code. - Decide who the customer is. Create a customer at the provider the first time a signed-in user checks out, and store the link between your user ID and the provider's customer ID. Don't trust a user ID sent from the browser.
- Write a checkout server action. It reads the session, finds the plan, creates a hosted checkout session for that customer and redirects to its URL. Signed-out visitors need to sign up first.
- Create the tables. You need a customer link, a subscription table, and a ledger of every successful charge. Store money as integers in the smallest currency unit, with the currency next to it.
- Add a webhook route. Read the raw body with
request.text(). Verify the signature before you parse anything, and return401when it fails. For Dodo, pass the request headers to the SDK, because it silently skips verification when headers are missing. - Handle each event type. One-time payments, subscription created, updated and cancelled, renewals, and refunds. Look the user up through the customer link.
- Make handlers idempotent. Providers retry, so insert payments with a unique key and
on conflict do nothing. - Survive out-of-order events. Store the time of the last applied event on each subscription and ignore anything older. Stripe says delivery order isn't guaranteed, so some handlers read the subscription from the API instead of trusting the event.
- Return
500on real errors, so the provider retries later. - Write the access rule. For example: a paid one-time payment grants access, and so does a subscription with status
active. Put it in one function. - Add a billing portal link so customers can update a card or cancel, and register the webhook URL in the provider's dashboard for production.
- Test the whole loop with test cards and a tunnel (Stripe CLI for Stripe), then repeat with live keys on production.
The traps: parsing JSON before verifying, forgetting that test and live mode have separate customers, partial refunds being treated as full refunds, and a $0 trial invoice counted as revenue.
What does Launchfast already do?
Launchfast supports Dodo Payments and Stripe, one at a time, chosen by BILLING_PROVIDER (Dodo by default). All Dodo code is in src/lib/billing/dodo.ts and all Stripe code in src/lib/billing/stripe.ts. Both write the same tables: billing_customer, subscription and payment.
Plans live in one file. plans in src/config.ts lists what you sell, with the amount in the smallest unit, an interval ("month", "year" or null for one-time), and the env vars that hold the Dodo product ID and the Stripe price ID. The kit ships with one plan, priced at $49 one time.
Checkout is a server action. src/lib/billing/actions.ts finds the plan, sends signed-out visitors to /sign-up, and creates the checkout on the server:
const session = await getSession();
if (!session) redirect(`/sign-up?plan=${encodeURIComponent(plan.slug)}`);
const url = await createCheckoutUrl(session.user, plan, `${siteConfig.url}/?checkout=success`, ...);
redirect(url);
The customer link is created server side. getOrCreateCustomerId() in each provider module creates the customer on first checkout and saves it in billing_customer. If the saved customer is missing (for example after moving from test to live keys) or belongs to the other provider, it is replaced.
Webhooks are verified. src/app/api/webhooks/dodo/route.ts and .../stripe/route.ts read the raw body, call verifyWebhook(), return 401 for a bad signature, and let other errors return 500 so the provider retries:
const body = await request.text();
const event = verifyWebhook(body, request.headers);
const { userId } = await handleWebhookEvent(event);
Payments are a ledger, and replays are safe. handleWebhookEvent() inserts each charge into payment with onConflictDoNothing(). Subscriptions use an upsert that only applies when the event is newer than the stored providerUpdatedAt. Stripe's handler also reads the subscription from the API instead of trusting the event snapshot. A $0 Stripe invoice is not recorded as revenue, and only a full refund marks a payment as refunded. The details are in How to handle Dodo Payments webhooks safely and What happens when Dodo Payments sends the same webhook twice.
Access is one function. getBillingStatus(userId) in src/lib/billing/index.ts reads only your own tables. A paid one_time payment wins, otherwise a subscription with status active grants the plan:
const { plan, source } = await getBillingStatus(user.id);
if (plan?.slug === "launchfast") {
// show the paid feature
}
Setup does the dashboard work. npm run setup asks which provider you want, takes the API key, detects test or live mode from it, creates the products (and prices for Stripe), and registers the webhook when you deploy. For Stripe, the Stripe events it enables are checkout.session.completed, checkout.session.async_payment_succeeded, the three customer.subscription.* events, invoice.paid and charge.refunded. For local Stripe testing, setup tells you to run stripe listen --forward-to localhost:3000/api/webhooks/stripe. A billing portal link comes from createPortalUrl(). With Stripe, turn the Customer portal on once in the dashboard.
What should you check before going live?
Tax and legal setup is still yours. Dodo Payments is a merchant of record, and Stripe isn't, so pick with that in mind and ask an accountant what applies where you sell. Also check that your live webhook secret is set on your host, and that setup's warning about live keys in .env.local doesn't apply to your machine.
How much time does this save?
You skip the checkout action, the customer mapping, two webhook handlers with replay protection, the payment tables and the access rule. You choose a provider and paste a key.
Start from the Launchfast home page. If you are planning the rest of your launch, read the checklist I run before launching a SaaS, How to set up a Postgres database for your web app and Launchfast vs ShipFast, Makerkit and Supastarter.