🔧 (billing) New checkout session should work with existi…
This commit is contained in:
@ -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,
|
email,
|
||||||
name: company,
|
name: company,
|
||||||
metadata: { workspaceId },
|
metadata: { workspaceId },
|
||||||
tax_id_data: vat
|
tax_id_data: vat
|
||||||
? [vat as Stripe.CustomerCreateParams.TaxIdDatum]
|
? [vat as Stripe.CustomerCreateParams.TaxIdDatum]
|
||||||
: undefined,
|
: 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)({
|
const checkoutUrl = await createCheckoutSessionUrl(stripe)({
|
||||||
customerId: customer.id,
|
customerId: customer.id,
|
||||||
|
Reference in New Issue
Block a user