Skip to content

Scheduling infrastructure for absolutely everyone.

Fix billing portal for users without stripeCustomerId (#1936)

alannncf8aa274b0703
diff --git a/apps/web/ee/lib/stripe/customer.ts b/apps/web/ee/lib/stripe/customer.ts
index 9f60e1da875..ee0097b0de5 100644
--- a/apps/web/ee/lib/stripe/customer.ts
+++ b/apps/web/ee/lib/stripe/customer.ts
@@ -46,7 +46,23 @@ export async function getStripeCustomerId(user: UserType): Promise<string | null
});
if (customersResponse.data[0]?.id) {
customerId = customersResponse.data[0].id;
+ } else {
+ /* Creating customer on Stripe and saving it on prisma */
+ const customer = await stripe.customers.create({ email: user.email });
+ customerId = customer.id;
}
+
+ await prisma.user.update({
+ where: {
+ email: user.email,
+ },
+ data: {
+ metadata: {
+ ...(user.metadata as Prisma.JsonObject),
+ stripeCustomerId: customerId,
+ },
+ },
+ });
}
return customerId;