From b6a20cc4d738221fbbf88d4f02d9239d5ec562d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20L=C3=B3pez?= Date: Fri, 4 Mar 2022 09:38:42 -0700 Subject: [PATCH] Fixes upgrade for users without customer id (#2059) --- apps/web/ee/lib/stripe/customer.ts | 3 ++- apps/web/pages/api/upgrade.ts | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/web/ee/lib/stripe/customer.ts b/apps/web/ee/lib/stripe/customer.ts index 496a4f1c07..88bd7cdec8 100644 --- a/apps/web/ee/lib/stripe/customer.ts +++ b/apps/web/ee/lib/stripe/customer.ts @@ -34,7 +34,8 @@ const userType = Prisma.validator()({ }); type UserType = Prisma.UserGetPayload; -export async function getStripeCustomerId(user: UserType): Promise { +/** This will retrieve the customer ID from Stripe or create it if it doesn't exists yet. */ +export async function getStripeCustomerId(user: UserType): Promise { let customerId: string | null = null; if (user?.metadata && typeof user.metadata === "object" && "stripeCustomerId" in user.metadata) { diff --git a/apps/web/pages/api/upgrade.ts b/apps/web/pages/api/upgrade.ts index bcfe02a3c5..ddee63c267 100644 --- a/apps/web/pages/api/upgrade.ts +++ b/apps/web/pages/api/upgrade.ts @@ -1,6 +1,8 @@ import { Prisma } from "@prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; +import { getStripeCustomerId } from "@ee/lib/stripe/customer"; + import { getSession } from "@lib/auth"; import { WEBSITE_URL } from "@lib/config/constants"; import { HttpError as HttpCode } from "@lib/core/http/error"; @@ -27,6 +29,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) }, }); + const stripeCustomerId = await getStripeCustomerId(user); + try { const response = await fetch(`${WEBSITE_URL}/api/upgrade`, { method: "POST", @@ -35,7 +39,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) "Content-Type": "application/json", }, body: JSON.stringify({ - stripeCustomerId: (user.metadata as Prisma.JsonObject)?.stripeCustomerId, + stripeCustomerId, email: user.email, fromApp: true, }),