From cb0987e41c628aba074af9641b6291c89ea499d8 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 27 Jun 2024 12:09:54 +0200 Subject: [PATCH] =?UTF-8?q?:wrench:=20(billing)=20New=20checkout=20session?= =?UTF-8?q?=20should=20work=20with=20existi=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../billing/api/createCheckoutSession.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ee/packages/billing/api/createCheckoutSession.ts b/ee/packages/billing/api/createCheckoutSession.ts index 8c3f570a8..ff2aee8f8 100644 --- a/ee/packages/billing/api/createCheckoutSession.ts +++ b/ee/packages/billing/api/createCheckoutSession.ts @@ -74,14 +74,28 @@ export const createCheckoutSession = async ({ }, }) - const customer = await stripe.customers.create({ + const existingCustomer = await stripe.customers.list({ + email, + }) + + if (existingCustomer && email !== existingCustomer.data[0].email) + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Make sure to log in with the same email as the one provided', + }) + + const customerData = { email, name: company, metadata: { workspaceId }, tax_id_data: vat ? [vat as Stripe.CustomerCreateParams.TaxIdDatum] : undefined, - }) + } + const customer = + existingCustomer.data.length > 0 + ? await stripe.customers.update(existingCustomer.data[0].id, customerData) + : await stripe.customers.create(customerData) const checkoutUrl = await createCheckoutSessionUrl(stripe)({ customerId: customer.id,