2026-06-23 20:36:07 -04:00
|
|
|
import { NextResponse } from "next/server"
|
|
|
|
|
import { eq } from "drizzle-orm"
|
|
|
|
|
import { db } from "@/lib/db"
|
|
|
|
|
import { profiles } from "@/lib/db/schema"
|
|
|
|
|
import { getSessionUser } from "@/lib/session"
|
|
|
|
|
import { stripe } from "@/lib/stripe/client"
|
|
|
|
|
import { PLAN_PRICES } from "@/lib/stripe/plans"
|
2026-07-02 13:42:34 -04:00
|
|
|
import { resolvePriceId } from "@/lib/stripe/prices"
|
2026-06-23 20:36:07 -04:00
|
|
|
|
|
|
|
|
export async function POST(request: Request) {
|
|
|
|
|
const user = await getSessionUser()
|
|
|
|
|
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
|
|
|
|
|
2026-07-02 13:42:34 -04:00
|
|
|
const { plan, interval } = await request.json() as {
|
|
|
|
|
plan: string
|
|
|
|
|
interval?: "month" | "year"
|
|
|
|
|
}
|
2026-06-23 20:36:07 -04:00
|
|
|
|
|
|
|
|
const planConfig = PLAN_PRICES[plan]
|
|
|
|
|
if (!planConfig) return NextResponse.json({ error: "Invalid plan" }, { status: 400 })
|
|
|
|
|
|
2026-07-02 13:42:34 -04:00
|
|
|
const billingInterval = interval === "year" ? "year" : "month"
|
|
|
|
|
const priceId = await resolvePriceId(planConfig.plan, billingInterval)
|
|
|
|
|
if (!priceId) {
|
|
|
|
|
return NextResponse.json({ error: "Could not resolve the plan price. Check STRIPE_SECRET_KEY." }, { status: 400 })
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-23 20:36:07 -04:00
|
|
|
const profile = await db.query.profiles.findFirst({
|
|
|
|
|
where: eq(profiles.id, user.id),
|
|
|
|
|
columns: { stripe_customer_id: true, email: true, full_name: true },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Get or create Stripe customer
|
|
|
|
|
let customerId = profile?.stripe_customer_id
|
|
|
|
|
|
|
|
|
|
if (!customerId) {
|
|
|
|
|
const customer = await stripe.customers.create({
|
|
|
|
|
email: profile?.email ?? user.email,
|
|
|
|
|
name: profile?.full_name ?? undefined,
|
2026-07-02 13:42:34 -04:00
|
|
|
metadata: { user_id: user.id },
|
2026-06-23 20:36:07 -04:00
|
|
|
})
|
|
|
|
|
customerId = customer.id
|
|
|
|
|
|
|
|
|
|
await db
|
|
|
|
|
.update(profiles)
|
|
|
|
|
.set({ stripe_customer_id: customerId })
|
|
|
|
|
.where(eq(profiles.id, user.id))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isLifetime = planConfig.interval === "one_time"
|
|
|
|
|
const appUrl = process.env.NEXT_PUBLIC_APP_URL!
|
|
|
|
|
|
|
|
|
|
const session = await stripe.checkout.sessions.create({
|
|
|
|
|
customer: customerId,
|
|
|
|
|
mode: isLifetime ? "payment" : "subscription",
|
2026-07-02 13:42:34 -04:00
|
|
|
line_items: [{ price: priceId, quantity: 1 }],
|
2026-06-23 20:36:07 -04:00
|
|
|
success_url: `${appUrl}/settings/billing?success=true`,
|
|
|
|
|
cancel_url: `${appUrl}/settings/billing?canceled=true`,
|
|
|
|
|
metadata: {
|
2026-07-02 13:42:34 -04:00
|
|
|
user_id: user.id,
|
2026-06-23 20:36:07 -04:00
|
|
|
plan: planConfig.plan,
|
|
|
|
|
},
|
|
|
|
|
...(isLifetime ? {} : {
|
|
|
|
|
subscription_data: {
|
2026-07-02 13:42:34 -04:00
|
|
|
metadata: { user_id: user.id, plan: planConfig.plan },
|
2026-06-23 20:36:07 -04:00
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({ url: session.url })
|
|
|
|
|
}
|