Skip to content

Commit

Permalink
feat: Implementing idempotency key verification on the webhook to pre…
Browse files Browse the repository at this point in the history
…vent triggering the webhook twice if payment service encounters an error.
  • Loading branch information
SwiichyCode committed Jan 31, 2024
1 parent 40ecb92 commit 8f0aa7e
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/app/api/webhooks/checkout/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ export async function POST(req: Request) {
throw new Error("Payment intent is not defined");
}

// if (event.pending_webhooks > 1) {
// throw new Error("Webhook is already processing");
// }

const idempotency_key = await CheckoutService.getIdempotencyKey({
sessionId: session.id,
});

if (!idempotency_key?.idempotencyKey) {
if (!idempotency_key?.idempotencyKey || event.pending_webhooks > 1) {
// Fix case if not a physical product
if (customerDetails?.name && customerDetails?.address) {
await CheckoutService.processCheckoutSession({
Expand All @@ -49,7 +45,7 @@ export async function POST(req: Request) {
});
}
} else {
throw new Error("Idempotency key is already defined");
throw new Error("Webhook is already processing");
}

const customerEmail = event.data.object.customer_details?.email;
Expand Down

0 comments on commit 8f0aa7e

Please sign in to comment.