GuidesE-commerce
Tutorial: adding Stripe Checkout payments to a web app
1. Create the session (server)
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST() {
const session = await stripe.checkout.sessions.create({
mode: 'payment',
line_items: [{ price: 'price_123', quantity: 1 }],
success_url: 'https://example.com/thanks?session_id={CHECKOUT_SESSION_ID}',
cancel_url: 'https://example.com/cart',
});
return Response.redirect(session.url!, 303);
}
The price_123 price is created in the Stripe dashboard or through the API: the amount never comes from the browser.
2. The flow
- 1Click on PayThe browser calls your server.
- 2Session createdYour server redirects to the Stripe page.
- 3PaymentOn Stripe: card, Apple Pay, Google Pay, 3-D Secure.
- 4Webhook
checkout.session.completedconfirms the order.
3. The return page
It says thank you and shows the summary by reading the session back (session_id). It creates nothing: that is the webhook’s job.
4. Subscriptions
Same code with mode: 'subscription' and a recurring price. Stripe’s customer portal then handles cards, invoices and cancellation.
The essential next step: reliable Stripe webhooks. The basics: what a webhook is.
Frequently asked questions
- Checkout or an embedded form?
- Checkout takes about a day to set up and handles cards, wallets and 3-D Secure. An embedded form (Payment Element) gives more design freedom, for more work.
- Why not confirm the order on the return page?
- The customer may close the tab before reaching it. The webhook always arrives, and Stripe resends it if it fails.
- How do I test without paying?
- In test mode, with the test keys and the test card numbers Stripe provides.