Mendlark / Guides
Stripe success page unlocks paid access for free: how to fix it
A lot of AI-built apps handle payments like this: the customer pays on Stripe Checkout, Stripe sends them to /success, and the success page marks them as paid. It works in testing. It also lets anyone get your product for free, and it can leave real customers locked out. Here's why, and how to unlock access properly with a verified webhook.
Why the success page can't be trusted
The success page is just a URL. Anything that runs when someone visits it can be triggered without paying:
- Anyone can visit it. If
/successruns code likeupdate profiles set is_pro = true, typing the address into the browser unlocks the app. Generated apps often put the URL in the code, so it's easy to find. - Browser code can be edited. If the unlock happens in front-end JavaScript using the Supabase anon key, a user can run the same update from the browser console, with no visit to Stripe at all, unless your database rules stop them.
- Paying customers can miss it. If someone pays and then closes the tab, loses signal, or their bank's 3D Secure check redirects strangely, they never reach
/success. They've paid and have no access, which is how you get angry emails and chargebacks. - It doesn't handle what happens later. Renewals, failed payments, cancellations and refunds never visit your success page, so subscribers who stop paying keep access forever.
The fix is to let Stripe tell your server directly, in a message you can verify, and to make the success page display-only.
1. Make the success page display-only
The success page should say "Thanks, we're activating your account" and then read the user's status from your database. It must never write the paid status. If you want to show order details, pass ?session_id={CHECKOUT_SESSION_ID} in your success_url (Stripe fills it in) and look up the session on the server with your secret key, but treat that as a display aid, not the source of truth.
2. Link the payment to the user
When you create the Checkout Session on your server, attach the signed-in user's id so the webhook knows who paid. Create the session server-side, never from the browser:
// app/api/checkout/route.ts (Next.js, App Router)
import Stripe from 'stripe'
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!)
export async function POST(req: Request) {
const user = await getSignedInUser(req) // your auth check
if (!user) return new Response('Unauthorized', { status: 401 })
const session = await stripe.checkout.sessions.create({
mode: 'subscription', // or 'payment'
line_items: [{ price: process.env.STRIPE_PRICE_ID!, quantity: 1 }],
client_reference_id: user.id,
metadata: { user_id: user.id },
success_url: `${process.env.SITE_URL}/success?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.SITE_URL}/pricing`,
})
return Response.json({ url: session.url })
}
If you use Stripe Payment Links instead, you can add ?client_reference_id=USER_ID to the link URL and it appears on the session in the same way.
3. Unlock access in a verified webhook
A webhook is a server endpoint Stripe calls when something happens. Stripe signs each request, and you verify that signature with your webhook signing secret (whsec_…). That proves the message came from Stripe and wasn't faked. Verification needs the raw request body, so read it as text, not parsed JSON.
// app/api/stripe-webhook/route.ts (Next.js, App Router)
import Stripe from 'stripe'
import { createClient } from '@supabase/supabase-js'
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!)
const supabase = createClient(
process.env.SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY! // server only, never in the browser
)
export async function POST(req: Request) {
const body = await req.text()
const sig = req.headers.get('stripe-signature')!
let event: Stripe.Event
try {
event = stripe.webhooks.constructEvent(body, sig, process.env.STRIPE_WEBHOOK_SECRET!)
} catch {
return new Response('Bad signature', { status: 400 })
}
if (event.type === 'checkout.session.completed') {
const s = event.data.object as Stripe.Checkout.Session
if (s.payment_status === 'paid') {
await supabase.from('profiles').update({
is_pro: true,
stripe_customer_id: s.customer as string,
}).eq('id', s.client_reference_id)
}
}
if (event.type === 'customer.subscription.deleted') {
const sub = event.data.object as Stripe.Subscription
await supabase.from('profiles').update({ is_pro: false })
.eq('stripe_customer_id', sub.customer as string)
}
return new Response('ok')
}
Notes on this example:
- It checks
payment_status === 'paid'. Some payment methods (like bank debits) complete the session before the money arrives; for those, also handlecheckout.session.async_payment_succeeded. - For subscriptions, also listen to
customer.subscription.updatedandinvoice.payment_failedto handle renewals and lapsed cards. - Stripe can send the same event more than once. Updates like "set
is_proto true" are safe to repeat; if you do something that shouldn't happen twice (like adding credits or sending an email), store theevent.idand skip ones you've seen. - If you use the older Pages Router, turn off Next.js body parsing for this route (
export const config = { api: { bodyParser: false } }) and read the raw body yourself, otherwise the signature check always fails.
Using Supabase without Next.js? The same logic works in a Supabase Edge Function. There, use await stripe.webhooks.constructEventAsync(body, sig, secret), because Deno's crypto is asynchronous, and deploy the function with JWT verification turned off (--no-verify-jwt) since Stripe doesn't send a Supabase token. The Stripe signature check is what protects it.
4. Register the webhook in Stripe
- In the Stripe Dashboard, open Developers → Webhooks and add an endpoint:
https://yourapp.com/api/stripe-webhook. - Select the events you handle:
checkout.session.completed, plus the subscription events above if you sell subscriptions. - Copy the signing secret (
whsec_…) intoSTRIPE_WEBHOOK_SECRETon your host, along withSTRIPE_SECRET_KEYandSUPABASE_SERVICE_ROLE_KEY. None of these should have aNEXT_PUBLIC_orVITE_prefix. Redeploy. - Test and live mode have separate endpoints and secrets. Set up both, and test locally with the Stripe CLI:
stripe listen --forward-to localhost:3000/api/stripe-webhook.
5. Lock the paid column in the database
Moving the unlock to a webhook doesn't help if users can still set is_pro themselves. In Supabase, Row Level Security policies that let users update their own profile row allow them to update every column in it, including is_pro. Either move paid status into a separate table (for example subscriptions) that users can read but never write, or revoke update rights on that column:
revoke update (is_pro, stripe_customer_id) on public.profiles from authenticated, anon;
The webhook uses the service role key, which bypasses these rules, so it can still write. Then check every paid feature on the server, or in RLS policies, rather than only hiding buttons in the front end.
Quick checklist
- Success page only reads status; it never writes it
- Checkout Session created on the server with
client_reference_idset to the user id - Webhook verifies the Stripe signature using the raw body
- Access granted on
checkout.session.completedwithpayment_statuspaid, removed on cancellation - Secrets set on the host without a public prefix; test and live endpoints both registered
- Users can't update the paid column themselves
Still stuck?
Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Start with a free scan, or read our guide to the Supabase auth redirect loop on Vercel.
Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.