Supabase Edge Functions and Stripe
Process Stripe events once in a Supabase Edge Function
Verify the Stripe signature in Deno, then call one Postgres RPC that claims event.id and applies the change in the same transaction. Check the RPC response before sending Stripe a success status. A second Edge Function invocation can then safely acknowledge the same event.
- Duplicate event or charge
- 57 of 388
Apply the fix
Authenticate the Stripe request in Deno
Set
verify_jwt = falsefor the Stripe function only. Read the raw body withawait req.text()and verifyStripe-SignaturewithconstructEventAsyncandStripe.createSubtleCryptoProvider(). Reject verification failures with 400 before calling the database.Make one server-only RPC call
For an eligible payment, resolve its account from the order records and invoke the RPC with the verified
event.id. Inside Postgres, insert the receipt using a unique key andON CONFLICT DO NOTHING. Only the winning insert applies fulfillment. Raise an error if fulfillment fails so the receipt also rolls back. Grant RPC execution toservice_roleand revoke it fromPUBLIC,anonandauthenticated. Keep the elevated client in the Edge Function.Check the RPC result before acknowledging
Separate Data API calls to insert a receipt and apply fulfillment can commit independently. Await a single RPC and check its
errorfield. In this excerpt,adminis the server-only client,accountIdcomes from the order lookup, andfulfill_checkout_oncereturns true for fulfillment or false for a repeat. Both results can return 200.const { error } = await admin.rpc('fulfill_checkout_once', { p_event_id: event.id, p_account_id: accountId, }); if (error) return new Response('Fulfillment failed', { status: 500 }); return new Response('OK', { status: 200 });
Verify the change
Run npx viberaven check from your repo root. The webhook check starts in VibeRaven 1.15. To run the current release explicitly, run npx viberaven@1.20.1 check. Review payment_webhook_duplicate warnings in the handler. In a test project, send the same valid event twice concurrently and expect one committed fulfillment. Force the RPC to fail, confirm a non-2xx response, then retry and confirm Stripe can still trigger fulfillment.
Research and sources
We labeled 57 of 388 payment and webhook repair commits as duplicate event or charge. The category combines duplicate event and charge repairs across providers and stacks. The denominator is repair commits across those systems.