Skip to content

Commit

Permalink
🔧 (billing) New checkout session should work with existi…
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jun 27, 2024
1 parent 99eae33 commit cb0987e
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ee/packages/billing/api/createCheckoutSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit cb0987e

Please sign in to comment.