Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
update stripe pricing
Marc Seitzde0e675cafc5
diff --git a/lib/stripe/utils.ts b/lib/stripe/utils.tsindex b20f0f0f..9d9860ed 100644--- a/lib/stripe/utils.ts+++ b/lib/stripe/utils.ts@@ -1,5 +1,3 @@-import type Stripe from "stripe";-export function getPlanFromPriceId(priceId: string) {const env =process.env.NEXT_PUBLIC_VERCEL_ENV === "production" ? "production" : "test";diff --git a/pages/api/stripe/webhook.ts b/pages/api/stripe/webhook.tsindex 64bc6a60..74f8bcd7 100644--- a/pages/api/stripe/webhook.ts+++ b/pages/api/stripe/webhook.ts@@ -1,6 +1,6 @@import { NextApiRequest, NextApiResponse } from "next";import { Readable } from "node:stream";-import Stripe from "stripe";+import type Stripe from "stripe";import prisma from "@/lib/prisma";import { stripe } from "@/lib/stripe";import { getPlanFromPriceId, isNewCustomer } from "@/lib/stripe/utils";@@ -158,37 +158,31 @@ export default async function webhookHandler(// If a project deletes their subscription, reset their usage limit in the database to 1000.// Also remove the root domain redirect for all their domains from Redis.- const user = await prisma.user.findUnique({+ const team = await prisma.team.update({where: {stripeId,},- select: {- id: true,- name: true,+ data: {+ plan: "free",+ subscriptionId: null,+ endsAt: null,+ startsAt: null,},+ select: { id: true },});- if (!user) {+ if (!team) {await log(- "User not found in Stripe webhook `customer.subscription.deleted` callback",+ "Team with stripeId: `" ++ stripeId ++ "`not found in Stripe webhook `customer.subscription.deleted` callback",);return;}- await Promise.all([- prisma.user.update({- where: {- stripeId,- },- data: {- plan: "free",- subscriptionId: null,- endsAt: null,- startsAt: null,- },- }),- log(":cry: User *`" + user.id + "`* deleted their subscription"),- ]);+ await log(+ ":cry: Team *`" + team.id + "`* deleted their subscription",+ );} else {throw new Error("Unhandled relevant event!");}diff --git a/pages/api/teams/[teamId]/billing/upgrade.ts b/pages/api/teams/[teamId]/billing/upgrade.tsindex a97c49a6..03613c49 100644--- a/pages/api/teams/[teamId]/billing/upgrade.ts+++ b/pages/api/teams/[teamId]/billing/upgrade.ts@@ -3,10 +3,11 @@ import { getServerSession } from "next-auth/next";import { authOptions } from "../../../auth/[...nextauth]";import { CustomUser } from "@/lib/types";import { stripe } from "@/lib/stripe";+import prisma from "@/lib/prisma";export default async function handle(req: NextApiRequest,- res: NextApiResponse+ res: NextApiResponse,) {if (req.method === "POST") {// POST /api/teams/:teamId/billing/upgrade@@ -21,24 +22,66 @@ export default async function handle(priceId: string;};- const userEmail = (session.user as CustomUser).email;+ const { id: userId, email: userEmail } = session.user as CustomUser;- const stripeSession = await stripe.checkout.sessions.create({- customer_email: userEmail ?? undefined,- billing_address_collection: "required",- success_url: `${process.env.NEXTAUTH_URL}/settings/billing?success=true`,- cancel_url: `${process.env.NEXTAUTH_URL}/settings/billing`,- line_items: [{ price: priceId, quantity: 1 }],- automatic_tax: {- enabled: true,+ const team = await prisma.team.findUnique({+ where: {+ id: teamId,+ users: {+ some: {+ userId,+ },+ },},- tax_id_collection: {- enabled: true,- },- mode: "subscription",- allow_promotion_codes: true,- client_reference_id: teamId,+ select: { stripeId: true },});++ if (!team) {+ res.status(404).end("Unauthorized");+ return;+ }++ let stripeSession;++ if (team.stripeId) {+ // if the team already has a stripeId (i.e. is a customer) let's use as a customer+ stripeSession = await stripe.checkout.sessions.create({+ customer: team.stripeId,+ customer_update: { name: "auto" },+ billing_address_collection: "required",+ success_url: `${process.env.NEXTAUTH_URL}/settings/billing?success=true`,+ cancel_url: `${process.env.NEXTAUTH_URL}/settings/billing`,+ line_items: [{ price: priceId, quantity: 1 }],+ automatic_tax: {+ enabled: true,+ },+ tax_id_collection: {+ enabled: true,+ },+ mode: "subscription",+ allow_promotion_codes: true,+ client_reference_id: teamId,+ });+ } else {+ // else initialize a new customer+ stripeSession = await stripe.checkout.sessions.create({+ customer_email: userEmail ?? undefined,+ billing_address_collection: "required",+ success_url: `${process.env.NEXTAUTH_URL}/settings/billing?success=true`,+ cancel_url: `${process.env.NEXTAUTH_URL}/settings/billing`,+ line_items: [{ price: priceId, quantity: 1 }],+ automatic_tax: {+ enabled: true,+ },+ tax_id_collection: {+ enabled: true,+ },+ mode: "subscription",+ allow_promotion_codes: true,+ client_reference_id: teamId,+ });+ }+return res.status(200).json(stripeSession);} else {// We only allow POST requestsdiff --git a/pages/settings/billing.tsx b/pages/settings/billing.tsxindex 60df7872..7edc5045 100644--- a/pages/settings/billing.tsx+++ b/pages/settings/billing.tsx@@ -233,7 +233,7 @@ export default function Billing() {))}{tier.id === 3 &&(plan ? (- tier.currentPlan ? (+ tier.hasPlan ? (<ButtononClick={() => {setClicked(true);@@ -254,7 +254,9 @@ export default function Billing() {}}loading={clicked}>- Manage Subscription+ {tier.currentPlan+ ? `Manage Subscription`+ : `Upgrade to Pro`}</Button>) : (<UpgradePlanModal clickedPlan={"Pro"}>