Spaces:
Sleeping
Sleeping
File size: 1,554 Bytes
fbf3514 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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].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 });
}
}
|