VibecodingDeploy & go liveLevel 3: Workflows

How Do You Add Stripe Payments Without Getting Burned?

Create the checkout on your server using price IDs, never an amount from the browser. Grant access only from a webhook whose Stripe signature you've verified. Never rely on a success page. Test with Stripe's test cards, then repeat the whole flow in live mode, since keys, prices and webhook secrets all change.

Prompt to copy
Add Stripe Checkout to this app using these rules. Use test keys only for now.

1. Keys: STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET are server-only environment variables. Only pk_ may reach the browser.
2. Checkout: a server route creates the session. It takes a product name from the browser and maps it to a price ID on the server, and it never accepts an amount. Attach the signed-in user's ID from the verified token.
3. Webhook: use this shape for a Next.js route handler:

import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export async function POST(request: Request) {
  const body = await request.text(); // raw body, not request.json()
  const signature = request.headers.get("stripe-signature")!;
  let event;
  try {
    event = stripe.webhooks.constructEvent(body, signature, process.env.STRIPE_WEBHOOK_SECRET!);
  } catch {
    return new Response("Invalid signature", { status: 400 });
  }
  if (
    event.type === "checkout.session.completed" ||
    event.type === "checkout.session.async_payment_succeeded"
  ) {
    await fulfillCheckout(event.data.object.id);
  }
  return new Response(null, { status: 200 });
}

4. fulfillCheckout(sessionId) must: retrieve the session from Stripe with line_items expanded, check payment_status is not unpaid, skip if this session was already fulfilled (use a unique column on session ID), then grant access and record it.
5. Paid status lives in a table users can't write to. The browser never sets it.
6. The success page shows status only, or calls fulfillCheckout with the session ID from the URL. It never grants access from a query string alone.
7. Handle subscription cancellations and failed payments: [list what should happen].

Then tell me how to test each of these with the Stripe CLI, and what I must set up in the Stripe dashboard myself.
  1. 1Server creates CheckoutA price ID, never a browser amount
  2. 2Customer paysThey may never reach your success page
  3. 3Stripe sends a webhookIt can repeat or arrive out of order
  4. 4Verify the signatureRaw body, Stripe-Signature header, whsec_ secret
  5. 5Grant access onceA unique session ID blocks duplicates
Rule of thumb for the order, drawn from Stripe's docs. Access comes from the verified webhook, never from a success page.
  • Up to 10 secondswait for your webhook replyBefore redirecting from hosted Checkout
  • Up to 3 daysof retries in live modeSo the same event can arrive again
Why your webhook must reply fast and handle repeats. Source: Stripe docs, checked November 6, 2026.

Try it now

  1. 1In a Stripe sandbox, run stripe listen --forward-to localhost:4242/webhook with your own port and path, and use the whsec_ value it prints. Pay with 4242 4242 4242 4242, any future date and any CVC.
  2. 2Test failure and repeats. Card 4000000000000002 should decline. Then click Resend on the event in the Stripe Dashboard. Access shouldn't be granted twice.
  3. 3Try to fake it: curl -i -X POST https://<YOUR_DOMAIN>/api/webhook -H "Content-Type: application/json" -d '{"type":"checkout.session.completed"}'. It should return 400, and no access should change.
  4. 4You've got it when the paid feature opens only after a signed event, and visiting the success URL by hand changes nothing.

Use it when

Before you accept real money, and whenever you add a subscription, a new product or a new way of granting paid access.

Skip it when

If you only need to sell a few things, consider Stripe's Payment Links and fulfill orders by hand. Stripe says manual fulfillment works for low volume or experimental ventures. Also consider your builder's built-in integration before writing your own: Lovable can set this up, and a project can't use its own Stripe account and built-in payments at once.

Why it works

Each rule closes a specific gap. Stripe's docs say a customer may pay and lose their connection before your page loads, so you can't rely on the landing page alone. Webhooks are required for subscriptions and delayed payment methods. They also say that without verification, an attacker could send fake events to trigger fulfillment or grant access.

Verification needs the exact raw body, the Stripe-Signature header and your whsec_ secret. Any change to the body makes it fail, which is a classic framework bug. Delivery is messy too: events can arrive twice and out of order, and live mode retries for up to three days. So your fulfillment function must handle being called twice for the same session.

Prices must come from your server because anything in the browser can be edited. Stripe's Checkout examples pass a price ID such as line_items[0][price], created on your own server with your restricted or secret key.

Example

A course site grants access when the browser reaches /success?paid=true. Anyone can type that address. In a rebuild using the prompt above, the webhook marks access in a purchases table, with the Checkout Session ID as a unique value.

During testing, each event fails signature verification. The route had been using request.json(), so the body no longer matched what Stripe signed. Switching to request.text() fixes it. Going live, the owner registers a new live endpoint, copies its new whsec_ secret, swaps in live price IDs, and makes one real purchase on the published app, as Lovable's own go-live steps also suggest.

Going further

Stripe's go-live checklist has items an AI rarely mentions. Sandbox objects such as products and prices aren't usable in live mode. Register live webhook endpoints separately. Rotate your keys before launch in case they were saved somewhere during development. Stripe recommends restricted rk_ keys over sk_ for new work, and access policies on live keys so they only work from known servers.

Stripe waits up to 10 seconds for your webhook response before redirecting from a hosted Checkout, so reply quickly and defer slow work to a queue. If you use a framework with CSRF protection, the webhook route may need an exemption. Don't set the signature tolerance to 0, because that turns off the recency check.

Lovable has its own details. Webhooks are off by default, with payment status checked directly with Stripe, and the preview has no separate test environment, so a live key there takes real payments. Don't paste a key into its chat. Enter it only in the Connect Stripe form.

Common mistakes

  • Granting access from the success page or a URL parameter. It's reachable without paying.
  • Skipping signature verification because the webhook "works". It'll also work for anyone else who posts to it.
  • Using request.json() or a body parser before verifying, then disabling the check when it fails.
  • Going live with test price IDs or the CLI's whsec_ value. Each endpoint and mode has its own secret.

Sources

Checked 6 November 2026. Products change often, so confirm details with the vendor.

Related

Beyond the course