Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

team scoped /api/billings

Aashish-Upadhyay-10136f3c6981a2a
diff --git a/pages/api/billing/index.ts b/pages/api/billing/index.ts
deleted file mode 100644
index cec31838..00000000
--- a/pages/api/billing/index.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-import { NextApiRequest, NextApiResponse } from "next";
-import { getServerSession } from "next-auth/next";
-import { authOptions } from "../auth/[...nextauth]";
-import { CustomUser } from "@/lib/types";
-import prisma from "@/lib/prisma";
-
-export default async function handle(
- req: NextApiRequest,
- res: NextApiResponse
-) {
- if (req.method === "GET") {
- // POST /api/billing/manage – manage a user's subscription
- const session = await getServerSession(req, res, authOptions);
- if (!session) {
- res.status(401).end("Unauthorized");
- return;
- }
-
- const user = await prisma.user.findUnique({
- where: { id: (session.user as CustomUser).id },
- select: {
- id: true,
- subscriptionId: true,
- startsAt: true,
- endsAt: true,
- plan: true,
- _count: {
- select: { documents: true },
- },
- },
- });
-
- if (!user) {
- return res.status(400).json({ error: "User does not exists" });
- }
-
- // console.log("user", user);
-
- return res.status(200).json(user);
- } else {
- res.setHeader("Allow", ["GET"]);
- return res.status(405).end(`Method ${req.method} Not Allowed`);
- }
-}
diff --git a/pages/api/billing/manage.ts b/pages/api/billing/manage.ts
deleted file mode 100644
index 5d41c083..00000000
--- a/pages/api/billing/manage.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import { NextApiRequest, NextApiResponse } from "next";
-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
-) {
- if (req.method === "POST") {
- // POST /api/billing/manage – manage a user's subscription
- const session = await getServerSession(req, res, authOptions);
- if (!session) {
- res.status(401).end("Unauthorized");
- return;
- }
-
- const user = await prisma.user.findUnique({
- where: { id: (session.user as CustomUser).id },
- select: { stripeId: true },
- });
-
- if (!user) {
- return res.status(400).json({ error: "User does not exists" });
- }
- if (!user.stripeId) {
- return res.status(400).json({ error: "No Stripe customer ID" });
- }
-
- const { url } = await stripe.billingPortal.sessions.create({
- customer: user.stripeId,
- return_url: `${process.env.NEXTAUTH_URL}/settings/billing`,
- });
- return res.status(200).json(url);
- } else {
- res.setHeader("Allow", ["POST"]);
- return res.status(405).end(`Method ${req.method} Not Allowed`);
- }
-}
diff --git a/pages/api/teams/[teamId]/billing/index.ts b/pages/api/teams/[teamId]/billing/index.ts
new file mode 100644
index 00000000..988d6475
--- /dev/null
+++ b/pages/api/teams/[teamId]/billing/index.ts
@@ -0,0 +1,53 @@
+import { NextApiRequest, NextApiResponse } from "next";
+import { getServerSession } from "next-auth/next";
+import { authOptions } from "../../../auth/[...nextauth]";
+import { CustomUser } from "@/lib/types";
+import prisma from "@/lib/prisma";
+import { errorHanlder } from "@/lib/errorHandler";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (req.method === "GET") {
+ // POST /api/teams/:teamId/billing/manage – manage a user's subscription
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ res.status(401).end("Unauthorized");
+ return;
+ }
+
+ const { teamId } = req.query as { teamId: string };
+
+ try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ select: {
+ id: true,
+ subscriptionId: true,
+ startsAt: true,
+ endsAt: true,
+ plan: true,
+ _count: {
+ select: {
+ documents: true,
+ },
+ },
+ },
+ });
+ if (!team) {
+ return res.status(400).json({ error: "Team does not exists" });
+ }
+ return res.status(200).json(team);
+ } catch (error) {
+ errorHanlder(error, res);
+ }
+
+ // console.log("user", user);
+ } else {
+ res.setHeader("Allow", ["GET"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+}
diff --git a/pages/api/teams/[teamId]/billing/manage.ts b/pages/api/teams/[teamId]/billing/manage.ts
new file mode 100644
index 00000000..ebcec010
--- /dev/null
+++ b/pages/api/teams/[teamId]/billing/manage.ts
@@ -0,0 +1,51 @@
+import { NextApiRequest, NextApiResponse } from "next";
+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";
+import { errorHanlder } from "@/lib/errorHandler";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (req.method === "POST") {
+ // POST /api/billing/manage – manage a user's subscription
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ res.status(401).end("Unauthorized");
+ return;
+ }
+
+ const { teamId } = req.query as { teamId: string };
+ try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ },
+ select: {
+ stripeId: true,
+ },
+ });
+
+ if (!team) {
+ return res.status(400).json({ error: "Team does not exists" });
+ }
+ if (!team.stripeId) {
+ return res.status(400).json({ error: "No Stripe customer ID" });
+ }
+
+ const { url } = await stripe.billingPortal.sessions.create({
+ customer: team.stripeId,
+ return_url: `${process.env.NEXTAUTH_URL}/settings/billing`,
+ });
+ return res.status(200).json(url);
+ } catch (error) {
+ errorHanlder(error, res);
+ }
+ } else {
+ res.setHeader("Allow", ["POST"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+}
diff --git a/pages/api/billing/upgrade.ts b/pages/api/teams/[teamId]/billing/upgrade.ts
similarity index 77%
rename from pages/api/billing/upgrade.ts
rename to pages/api/teams/[teamId]/billing/upgrade.ts
index e3be4621..a97c49a6 100644
--- a/pages/api/billing/upgrade.ts
+++ b/pages/api/teams/[teamId]/billing/upgrade.ts
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
-import { authOptions } from "../auth/[...nextauth]";
+import { authOptions } from "../../../auth/[...nextauth]";
import { CustomUser } from "@/lib/types";
import { stripe } from "@/lib/stripe";
@@ -9,17 +9,22 @@ export default async function handle(
res: NextApiResponse
) {
if (req.method === "POST") {
- // POST /api/billing/upgrade
+ // POST /api/teams/:teamId/billing/upgrade
const session = await getServerSession(req, res, authOptions);
if (!session) {
res.status(401).end("Unauthorized");
return;
}
- const { priceId } = req.query as { priceId: string };
+ const { teamId, priceId } = req.query as {
+ teamId: string;
+ priceId: string;
+ };
+
+ const userEmail = (session.user as CustomUser).email;
const stripeSession = await stripe.checkout.sessions.create({
- customer_email: (session.user as CustomUser).email ?? undefined,
+ 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`,
@@ -32,7 +37,7 @@ export default async function handle(
},
mode: "subscription",
allow_promotion_codes: true,
- client_reference_id: (session.user as CustomUser).id,
+ client_reference_id: teamId,
});
return res.status(200).json(stripeSession);
} else {