Loading...
Loading...
Add subscription management and payments to billing templates.
Fabrk uses Polar.sh for subscription billing. Configure it to enable the Billing Dashboard and Pricing Page templates.
[WHAT YOU'LL INTEGRATE]:
1$# .env.local2$POLAR_ACCESS_TOKEN="your-polar-access-token"3$POLAR_WEBHOOK_SECRET="your-webhook-secret"4$NEXT_PUBLIC_POLAR_ORGANIZATION_ID="your-org-id"
1// app/api/polar/checkout/route.ts2import { polar } from "@/lib/polar";3import { auth } from "@/lib/auth";45export async function POST(req: Request) {6 const session = await auth();7 if (!session?.user) {8 return Response.json({ error: "Unauthorized" }, { status: 401 });9 }1011 const { priceId } = await req.json();1213 const checkout = await polar.checkouts.create({14 priceId,15 customerEmail: session.user.email,16 successUrl: `${process.env.NEXT_PUBLIC_URL}/billing?success=true`,17 });1819 return Response.json({ url: checkout.url });20}
Handle subscription events in app/api/webhooks/polar/route.ts
1import { prisma } from "@/lib/db";23export async function POST(req: Request) {4 const event = await req.json();56 if (event.type === "checkout.completed") {7 await prisma.payment.create({8 data: {9 userId: event.data.customerId,10 amount: event.data.amount,11 status: "completed",12 },13 });14 }1516 return Response.json({ received: true });17}