cal.pub0.org/packages/app-store/stripepayment/api/portal.ts

24 lines
748 B
TypeScript

import type { NextApiRequest, NextApiResponse } from "next";
import { getStripeCustomerIdFromUserId } from "../lib/customer";
import stripe from "../lib/server";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST" || req.method === "GET") {
const customerId = await getStripeCustomerIdFromUserId(req.session!.user.id);
if (!customerId) {
res.status(500).json({ message: "Missing customer id" });
return;
}
const return_url = `${process.env.NEXT_PUBLIC_WEBAPP_URL}/settings/billing`;
const stripeSession = await stripe.billingPortal.sessions.create({
customer: customerId,
return_url,
});
res.redirect(302, stripeSession.url);
}
}