36 lines
914 B
TypeScript

import Stripe from "stripe";
import { usePrisma } from "../../utils/prisma";
/**
* POST /api/stripe/portal
* Erstellt eine Stripe Billing Portal Session (Abo verwalten/kündigen).
*/
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig();
const stripe = new Stripe(config.stripeSecretKey);
const user = await requireUser(event);
const db = usePrisma();
const profile = await db.profile.findUnique({
where: { id: user.id },
select: { stripeCustomerId: true },
});
if (!profile?.stripeCustomerId) {
throw createError({
statusCode: 400,
message: "Kein aktives Abo gefunden",
});
}
const appUrl = config.public.appUrl || "https://rebreak.app";
const session = await stripe.billingPortal.sessions.create({
customer: profile.stripeCustomerId,
return_url: `${appUrl}/app/settings`,
});
return { url: session.url };
});