Fix billing portal for users without stripeCustomerId (#1936)

pull/1958/head^2
alannnc 2022-02-23 15:51:10 -07:00 committed by GitHub
parent 3d2fd28214
commit f8aa274b07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 0 deletions

View File

@ -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;