Prisma and Stripe

Use a unique Stripe event claim to prevent duplicate processing in Prisma

Use a unique Stripe event key in Prisma and claim it inside the same transaction that fulfills the order. On PostgreSQL, Prisma Client can use createMany with skipDuplicates. Only a claim that inserts a row can fulfill the order.

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

Apply the fix

  1. Create a unique receipt key in the database

    Add and apply a migration. The compound key below scopes a Stripe event ID to its account. Use the owning Stripe account ID consistently for both platform and connected-account events. Use separate test and live databases or include the mode in the key.

    model StripeEvent {
      accountId String
      eventId   String
    
      @@id([accountId, eventId])
    }
  2. Branch on the insert count inside the transaction

    After signature verification and payment validation, use this Prisma Client 6 or 7 example for PostgreSQL. fulfillOrder represents your database writes and must use tx throughout. Let any fulfillment error escape so Prisma rolls back both writes. Return success only after the transaction resolves.

    await prisma.$transaction(async (tx) => {
      const claim = await tx.stripeEvent.createMany({
        data: [{ accountId, eventId: event.id }],
        skipDuplicates: true,
      });
      if (claim.count === 0) return;
      await fulfillOrder(tx, orderId);
    });
  3. Give fulfillment its own key

    If several event types or a success page can fulfill the same purchase, enforce one fulfillment per Checkout Session or order ID. The event receipt handles repeat deliveries of one Stripe event. For email or another external call, save a durable job in the transaction and give that job a stable idempotency key.

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 database, send a verified event twice concurrently and check for one receipt and one fulfillment. Force the first fulfillment to throw and confirm a later delivery can claim the event again.

Research and sources

We labeled 57 of 388 payment and webhook repair commits as duplicate event or charge. The denominator is repair commits across payment providers and database clients. Some address duplicate charges, and others address repeated event handling.