# 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.

## From the research

57 of 388 payment and webhook repair commits were labeled duplicate event or charge.

- [Duplicate event and charge count](https://github.com/ohad6k/viberaven-research/blob/2d45c586d8d73b8913651894f4451b8071086daf/data/fixcommits-subclasses.csv#L24)

## Apply the fix

### Authenticate the Stripe request in Deno

Set `verify_jwt = false` for the Stripe function only. Read the raw body with `await req.text()` and verify `Stripe-Signature` with `constructEventAsync` and `Stripe.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 and `ON CONFLICT DO NOTHING`. Only the winning insert applies fulfillment. Raise an error if fulfillment fails so the receipt also rolls back. Grant RPC execution to `service_role` and revoke it from `PUBLIC`, `anon` and `authenticated`. 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 `error` field. In this excerpt, `admin` is the server-only client, `accountId` comes from the order lookup, and `fulfill_checkout_once` returns true for fulfillment or false for a repeat. Both results can return 200.

```ts
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.

- [Fix commit study and counting rules](https://github.com/ohad6k/viberaven-research/blob/2d45c586d8d73b8913651894f4451b8071086daf/studies/fix-commits/README.md)
- [Total payment and webhook repair commits](https://github.com/ohad6k/viberaven-research/blob/2d45c586d8d73b8913651894f4451b8071086daf/data/fixcommits-summary.csv#L4)
- [Supabase example for signed Stripe webhooks in Deno](https://supabase.com/docs/guides/functions/examples/stripe-webhooks)
- [Supabase database functions and execute privileges](https://supabase.com/docs/guides/database/functions)
- [Stripe duplicate deliveries](https://docs.stripe.com/webhooks#handle-duplicate-events)

## Related guide

- [Use the transactional Postgres fulfillment example](https://viberaven.dev/stripe-webhook-duplicate-events)

Source: https://viberaven.dev/answers/supabase-edge-function-stripe-webhook-idempotency

Site index: https://viberaven.dev/llms.txt
