From 81d09dd07cb8023170882181efc1202ff24ffd09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Jedlicska?= Date: Fri, 18 Oct 2024 12:39:51 +0200 Subject: [PATCH] feat(gatekeeper): make completeCheckout callback idempotent properly --- .../server/modules/gatekeeper/rest/billing.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/server/modules/gatekeeper/rest/billing.ts b/packages/server/modules/gatekeeper/rest/billing.ts index f33a04753..b4752f0e5 100644 --- a/packages/server/modules/gatekeeper/rest/billing.ts +++ b/packages/server/modules/gatekeeper/rest/billing.ts @@ -40,6 +40,7 @@ import { upsertPaidWorkspacePlanFactory } from '@/modules/gatekeeper/repositories/billing' import { GetWorkspacePlanPrice } from '@/modules/gatekeeper/domain/billing' +import { WorkspaceAlreadyPaidError } from '@/modules/gatekeeper/errors/billing' const router = Router() @@ -178,10 +179,18 @@ router.post('/api/v1/billing/webhooks', async (req, res) => { }) }) - await completeCheckout({ - sessionId: session.id, - subscriptionId - }) + try { + await completeCheckout({ + sessionId: session.id, + subscriptionId + }) + } catch (err) { + if (err instanceof WorkspaceAlreadyPaidError) { + // ignore the request, this is prob a replay from stripe + } else { + throw err + } + } } break