Take payments in Next.js with Stripe: Checkout, webhooks and orders that can’t be faked
Sell a product from a Next.js 16 app with Stripe Checkout, record every order exactly once from a verified webhook, and only hand over what was bought when Stripe says it’s paid. Every step was run on a fresh project, with a real test payment, before this was published.
Tested step by step on with Next.js 16.4.6, Stripe 23.2.0 and Prisma 7.10.1
By the end of this guide your app sells something: a Buy button that opens Stripe’s payment page, a success page, and a download that only works for someone who really paid. More importantly, you’ll build the three things most payment tutorials skip: checking that a webhook really came from Stripe, recording each order once even when Stripe sends the same event twice, and never trusting the browser’s word that a payment happened.
Every file is shown in full. The guide was run from start to finish on a fresh create-next-app project and a fresh shadcn init project, with Next.js 16.4, the stripe package 23, Prisma 7.10 and Stripe’s test mode, including a real test payment with Stripe’s test card.
Before you start
- A Next.js 16 app with Prisma and Postgres. Do steps 1 to 6 of Next.js 16 + Prisma 7 + Postgres on a fresh project first. This guide adds to exactly that setup.
- A free Stripe account. You’ll stay in test mode (Stripe calls a test account a sandbox): no real money moves, ever.
- The Stripe CLI. On a Mac,
brew install stripe/stripe-cli/stripe; for Windows and Linux, follow Stripe’s install page. Check withstripe --version.
1. Install Stripe’s package and sign the CLI in
stripe login opens your browser to connect the CLI to your account. Choose your sandbox (test mode) and allow access. The CLI can then create products and forward webhooks to your computer.
2. Create the product and its price
This creates a product and its $29.00 price in one go (unit-amount is in cents). The command prints the new price as JSON; the first line that matters is "id": "price_…". Copy that id. (You can also do this in the Dashboard under Product catalog → Add product: open the product and copy the price’s id.)
3. Add your Stripe settings
Get the webhook signing secret your CLI will use (you’ll start it in step 11):
It prints one line starting with whsec_. Now add four lines to the end of .env, replacing the three values in capitals:
.env (already ignored by Git) and never in a file a browser loads: no NEXT_PUBLIC_ prefix, ever.4. Add an orders table
Replace prisma/schema.prisma with this. It keeps your Task model and adds an Order model. stripeSessionId is @unique: that one word is what will stop a payment being recorded twice.
As in the Prisma guide: the first ends with “Your database is now in sync with your schema.”, the second with “Generated Prisma Client”.
5. Create the Stripe client
Only server code imports this file: server actions, route handlers and server components. That keeps the secret key on the server.
6. Record an order — once
Two different places will record the same payment: the webhook (step 8) and the success page (step 9). Stripe may also send the same webhook more than once. So the code that records an order has to be safe to run any number of times:
payment_status !== "paid": a session can complete without being paid yet (bank payments take days). Those wait for theasync_payment_succeededevent.upsertwith an emptyupdate: the first call creates the row; every later call finds it and changes nothing. BecausestripeSessionIdis unique, Postgres itself refuses a second row even if two calls arrive at the same moment.
7. Start the checkout
A server action creates a Checkout session and sends the browser to Stripe’s payment page. Create the folder app/buy and these two files in it:
- The price comes from your Stripe price id on the server, never from the page: nobody can change what they pay by editing the HTML.
{CHECKOUT_SESSION_ID}is written exactly like that. Stripe swaps in the real session id when it sends the buyer back.redirect()works from a server action, even to another site likecheckout.stripe.com.
8. Receive Stripe’s webhook
When a payment succeeds, Stripe sends a request to your server: a webhook. It’s the one message you can trust, because it carries a signature only Stripe can make. Create the folders app/api/webhooks/stripe and this file:
constructEventchecks the signature and the timestamp. A request that didn’t come from Stripe, or came long ago, never gets past it — and nothing is recorded.- If your route answers with anything other than a 2xx status, Stripe tries again later (for up to three days in live mode). Answer
200once you’ve handled it, and keep the work quick.
9. Show the receipt — after asking Stripe
The buyer comes back to /success?session_id=cs_test_…. Anyone can type that address, so the page asks Stripe whether that session is really paid before it says “thank you”:
It calls recordOrder too, so the order exists even if the webhook is late or missed — and thanks to step 6 that never makes a second one. <Suspense> and connection() are the same pattern as the Prisma guide’s task list: this part needs a real request.
10. Hand over what was bought
The download only answers for an order that’s in your database — which only a verified payment can put there. Create app/download/route.ts:
In a real shop this would stream a file from storage or open a course; the check in front of it is the part that matters.
11. Make a test purchase
You need two terminals. In the first, start your app:
In the second, forward Stripe’s webhooks to it:
It prints “Ready!” and the same whsec_ secret as in step 3. Leave it running. Then open http://localhost:3000/buy, click Buy for $29, and pay with Stripe’s test card:
- Email: any address. Card number:
4242 4242 4242 4242. - Expiry: any future date, like
12 / 34. Security code: any three digits. Name: anything. - If Stripe offers your local currency next to $29.00, either is fine: it’s Stripe’s Adaptive Pricing, and the order records whichever you pay in.
You land on “Thank you!” with a Download the guide button. In the second terminal you’ll see checkout.session.completed followed by [200] POST http://localhost:3000/api/webhooks/stripe. Run npx prisma studio and open Order: there’s one row, with your email and the amount in cents (2900 if you paid in dollars).
12. Prove it can’t be faked
Keep both terminals running and try to break it from a third:
A forged webhook: the answer is HTTP/1.1 400 Bad Request and “Invalid signature”, and no order appears. Now replay a real event. In the stripe listen terminal, copy the id after checkout.session.completed (it starts evt_) and send it again:
Stripe delivers it a second time (the listen terminal shows another [200]), and Order still has one row. Finally, open http://localhost:3000/success?session_id=cs_test_madeup: “We couldn’t find that order.” And if you start a checkout and close Stripe’s page without paying, that session’s /download link answers “No paid order for that link.”
13. Go live
- Deploy as in the Prisma guide (step 13), and add
STRIPE_SECRET_KEY,STRIPE_PRICE_IDandAPP_URL(your real address, likehttps://your-app.vercel.app) to the Vercel project’s environment variables. stripe listenonly forwards to your computer. For the deployed app, open Stripe’s Dashboard → Developers → Webhooks → Add destination. Choose the eventscheckout.session.completedandcheckout.session.async_payment_succeeded, and set the URL tohttps://your-app.vercel.app/api/webhooks/stripe.- That destination has its own signing secret (also
whsec_…). Put it in Vercel asSTRIPE_WEBHOOK_SECRETand redeploy. - To take real money: activate your Stripe account, switch the Dashboard out of test mode, create the price again there, and use the live secret key, price id and webhook secret. Nothing in the code changes.
If something goes wrong
- “Neither apiKey nor config.authenticator provided”.
STRIPE_SECRET_KEYisn’t set. Check.env, then stop and restartnpm run dev(it reads.envwhen it starts). - “No such price: 'price_…'”. The price id is mistyped, or it belongs to another account or to live mode. Copy it again from step 2’s output.
stripe listenshows[400]for every event. The signature check is failing:STRIPE_WEBHOOK_SECRETdoesn’t match whatstripe listen --print-secretprints (restartnpm run devafter changing it). In your own code, the classic cause is parsing the body before checking it: Stripe’s message is “No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?”- You paid but no
Orderrow appeared, andstripe listenwasn’t running. Open the success page’s address again: it records the order itself. In production, check the destination’s deliveries in Stripe’s Dashboard (Developers → Webhooks). - The success page says the order hasn’t been paid. The checkout was closed before paying, or the payment method takes time (bank debits). The
async_payment_succeededwebhook records it when it clears.
Questions people ask
Do I still need a webhook if I have a success page?
Yes. Some payments only finish later (bank payments can take days), and a buyer can close the tab before the success page ever loads. The webhook is Stripe telling your server directly, signed so nobody else can fake it. Here both record the order, and the unique session id makes sure it’s only recorded once.
How do I stop one payment being recorded twice?
Make the Checkout session id @unique on your orders table, and record orders with an upsert whose update is empty. Stripe can send the same event more than once, and the success page records the order too. The first call creates the row; every call after that finds it and changes nothing.
Can someone change the price in their browser?
Not with this setup. The price comes from your Stripe price id, which is read on the server when the Checkout session is created. The page only says “I want to buy this”, so editing the HTML or the request does nothing to what Stripe charges.
Which card do I use to test Stripe Checkout?
4242 4242 4242 4242, with any future expiry date, any three-digit security code and any name. It only works in test mode (a sandbox, in Stripe’s words), where no real money ever moves.
Where to go next
- Tie purchases to accounts: with Better Auth, pass the signed-in user’s id as
client_reference_idwhen creating the session, save it on the order, and show “Your purchases” from the database. - Handle refunds: listen for
charge.refundedthe same verified way and mark the order refunded, so the download stops answering. - Read Stripe’s Checkout fulfilment guide for subscriptions and more payment methods.