From 263c0ac7ef8663c72a85a4187031c38b9a328023 Mon Sep 17 00:00:00 2001 From: Martin Sione Date: Mon, 8 Jul 2024 15:00:51 -0300 Subject: [PATCH] fix: Update stripe customer email when a user updates cal.com email (#15299) * fix: Update stripe customer email when a user updates cal.com email * Only update email on stripe if stripe is enabled and if there is a stripeAccountId already associated with the user --------- Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> --- apps/web/pages/api/auth/verify-email.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/web/pages/api/auth/verify-email.ts b/apps/web/pages/api/auth/verify-email.ts index 1509f071f3..21797cf9f4 100644 --- a/apps/web/pages/api/auth/verify-email.ts +++ b/apps/web/pages/api/auth/verify-email.ts @@ -1,8 +1,10 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { z } from "zod"; +import stripe from "@calcom/app-store/stripepayment/lib/server"; import dayjs from "@calcom/dayjs"; import { WEBAPP_URL } from "@calcom/lib/constants"; +import { IS_STRIPE_ENABLED } from "@calcom/lib/constants"; import { prisma } from "@calcom/prisma"; import { userMetadata } from "@calcom/prisma/zod-utils"; @@ -66,7 +68,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) id: true, }, }); - if (existingUser) { return res.status(401).json({ message: USER_ALREADY_EXISTING_MESSAGE }); } @@ -101,6 +102,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) }, }); + if (IS_STRIPE_ENABLED && userMetadataParsed.stripeCustomerId) { + await stripe.customers.update(userMetadataParsed.stripeCustomerId, { + email: updatedEmail, + }); + } + // The user is trying to update the email to an already existing unverified secondary email of his // so we swap the emails and its verified status if (existingSecondaryUser?.userId === user.id) {