Spaces:
Sleeping
Sleeping
| import { NextRequest, NextResponse } from "next/server"; | |
| import { getRazorpay, PLANS } from "@/lib/razorpay"; | |
| import { createClient } from "@/lib/supabase/server"; | |
| export async function POST(req: NextRequest) { | |
| try { | |
| const supabase = await createClient(); | |
| const { data: { user } } = await supabase.auth.getUser(); | |
| if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 }); | |
| const { plan } = await req.json(); | |
| if (plan !== "pro" && plan !== "team") { | |
| return NextResponse.json({ error: "Invalid plan" }, { status: 400 }); | |
| } | |
| const planId = PLANS[plan as keyof typeof PLANS].razorpay_plan_id; | |
| if (!planId) return NextResponse.json({ error: "Plan not configured" }, { status: 500 }); | |
| const razorpay = getRazorpay(); | |
| const subscription = await razorpay.subscriptions.create({ | |
| plan_id: planId, | |
| total_count: 120, | |
| quantity: 1, | |
| customer_notify: 1, | |
| notes: { | |
| user_id: user.id, | |
| email: user.email || "", | |
| plan: plan, | |
| }, | |
| }); | |
| // Save subscription ID to profile | |
| await supabase | |
| .from("profiles") | |
| .update({ | |
| razorpay_subscription_id: subscription.id, | |
| updated_at: new Date().toISOString(), | |
| }) | |
| .eq("id", user.id); | |
| return NextResponse.json({ | |
| subscription_id: subscription.id, | |
| short_url: subscription.short_url, | |
| }); | |
| } catch (error: any) { | |
| console.error("Subscription create error:", error); | |
| return NextResponse.json({ error: error.message || "Failed" }, { status: 500 }); | |
| } | |
| } | |