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

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

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

```prisma
model StripeEvent {
  accountId String
  eventId   String

  @@id([accountId, eventId])
}
```

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

```ts
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);
});
```

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

- [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)
- [Prisma Client 7 interactive transactions](https://www.prisma.io/docs/orm/v7/prisma-client/queries/transactions)
- [Prisma Client 7 createMany and skipDuplicates](https://www.prisma.io/docs/orm/v7/reference/prisma-client-reference#createmany)
- [Stripe Checkout fulfillment and repeat calls](https://docs.stripe.com/checkout/fulfillment)

## Related guide

- [How Stripe retries and duplicate events work](https://viberaven.dev/stripe-webhook-duplicate-events)

Source: https://viberaven.dev/answers/prisma-stripe-webhook-duplicate-event

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