Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix(stripe): acknowledge webhook error
Marc Seitzca40ec616e0d
diff --git a/pages/api/stripe/webhook.ts b/pages/api/stripe/webhook.tsindex bb0069ee..766c0d63 100644--- a/pages/api/stripe/webhook.ts+++ b/pages/api/stripe/webhook.ts@@ -1,5 +1,11 @@import { NextApiRequest, NextApiResponse } from "next";+import {+ BUSINESS_PLAN_LIMITS,+ DATAROOMS_PLAN_LIMITS,+ FREE_PLAN_LIMITS,+ PRO_PLAN_LIMITS,+} from "@/ee/limits/constants";import { Readable } from "node:stream";import type Stripe from "stripe";@@ -12,12 +18,6 @@ import {isUpgradedCustomer,} from "@/lib/stripe/utils";import { log } from "@/lib/utils";-import {- BUSINESS_PLAN_LIMITS,- DATAROOMS_PLAN_LIMITS,- FREE_PLAN_LIMITS,- PRO_PLAN_LIMITS,-} from "@/ee/limits/constants";// Stripe requires the raw body to construct the event.export const config = {@@ -173,12 +173,14 @@ export default async function webhookHandler(});let planLimits = structuredClone(PRO_PLAN_LIMITS);- if (plan.slug === "pro") {- planLimits = structuredClone(PRO_PLAN_LIMITS);- } else if (plan.slug === "business") {- planLimits = structuredClone(BUSINESS_PLAN_LIMITS);- } else if (plan.slug === "datarooms") {- planLimits = structuredClone(DATAROOMS_PLAN_LIMITS);+ if (plan) {+ if (plan.slug === "pro") {+ planLimits = structuredClone(PRO_PLAN_LIMITS);+ } else if (plan.slug === "business") {+ planLimits = structuredClone(BUSINESS_PLAN_LIMITS);+ } else if (plan.slug === "datarooms") {+ planLimits = structuredClone(DATAROOMS_PLAN_LIMITS);+ }}// if user upgrades from Pro to Business, checkout event won't be fired@@ -213,39 +215,49 @@ export default async function webhookHandler(// reset the plan limits to freeconst planlimits = structuredClone(FREE_PLAN_LIMITS);- const team = await prisma.team.update({- where: {- stripeId,- subscriptionId,- },- data: {- plan: "free",- subscriptionId: null,- endsAt: null,- startsAt: null,- limits: planlimits,- },- select: { id: true },- });+ try {+ const team = await prisma.team.update({+ where: {+ stripeId,+ subscriptionId,+ },+ data: {+ plan: "free",+ subscriptionId: null,+ endsAt: null,+ startsAt: null,+ limits: planlimits,+ },+ select: { id: true },+ });++ if (!team) {+ throw new Error("Team not found in database");+ }- if (!team) {+ await log({+ message:+ ":cry: Team *`" + team.id + "`* deleted their subscription",+ type: "info",+ });+ } catch (error) {await log({message:"Team with stripeId: `" +stripeId +"` and subscriptionId `" +subscriptionId +- "` not found in Stripe webhook `customer.subscription.deleted` callback",+ "` not found in Stripe webhook `customer.subscription.deleted` callback" ++ `\n\n Error: ${(error as Error).message}` ++ `\n\n Event: https://dashboard.stripe.com/events/${event.id}`,type: "error",});- return;+ return res+ .status(200)+ .send(+ `Acknowledged error in webhook: ${(error as Error).message}`,+ );}-- await log({- message:- ":cry: Team *`" + team.id + "`* deleted their subscription",- type: "info",- });} else {throw new Error("Unhandled relevant event!");}