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.

Payment and webhook repair commits
Duplicate event or charge
57 of 388
Duplicate event and charge count

Apply the fix

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

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

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

    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.