remove supabase

This commit is contained in:
Reihan 2025-04-08 14:59:55 +07:00
parent ca5cd7dbd6
commit 7235a16f65

View File

@ -1,81 +1,82 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import Stripe from "stripe"; import Stripe from "stripe";
import { createClient } from "@supabase/supabase-js"; // import { createClient } from "@supabase/supabase-js";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY); const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
const supabase = createClient(process.env.SUPABASE_SERVICE_ROLE_KEY); // const supabase = createClient(process.env.SUPABASE_SERVICE_ROLE_KEY);
export async function POST(request) { export async function POST(request) {
try { try {
const { clientId, customerId } = await request.json(); // const { clientId, customerId } = await request.json();
const { data: userData, error: userError } = await supabase // const { data: userData, error: userError } = await supabase
.from("subscription_data") // .from("subscription_data")
.select("*") // .select("*")
.eq("customer_id", customerId) // .eq("customer_id", customerId)
.maybeSingle(); // .maybeSingle();
if (userError || !userData) { // if (userError || !userData) {
return NextResponse.json( // return NextResponse.json(
{ error: "subscription_data not found" }, // { error: "subscription_data not found" },
{ status: 404 } // { status: 404 }
); // );
} // }
const { subscription_cost } = userData; // const { subscription_cost } = userData;
// Create a new product and price // // Create a new product and price
const product = await stripe.products.create({ // const product = await stripe.products.create({
name: "RankRunners Client Subscription", // name: "RankRunners Client Subscription",
}); // });
const price = await stripe.prices.create({ // const price = await stripe.prices.create({
product: product.id, // product: product.id,
unit_amount: subscription_cost * 100, // unit_amount: subscription_cost * 100,
currency: "usd", // currency: "usd",
recurring: { interval: "month" }, // recurring: { interval: "month" },
}); // });
// Create a Checkout session // // Create a Checkout session
const session = await stripe.checkout.sessions.create({ // const session = await stripe.checkout.sessions.create({
customer: customerId, // customer: customerId,
payment_method_types: ["card"], // payment_method_types: ["card"],
line_items: [ // line_items: [
{ // {
price: price.id, // price: price.id,
quantity: 1, // quantity: 1,
}, // },
], // ],
mode: "subscription", // mode: "subscription",
success_url: `${process.env.NEXT_PUBLIC_URL}/client-area?session_id={CHECKOUT_SESSION_ID}`, // success_url: `${process.env.NEXT_PUBLIC_URL}/client-area?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.NEXT_PUBLIC_URL}/client-area`, // cancel_url: `${process.env.NEXT_PUBLIC_URL}/client-area`,
}); // });
if (!session) { // if (!session) {
return NextResponse.json( // return NextResponse.json(
{ error: "Failed to create checkout session" }, // { error: "Failed to create checkout session" },
{ status: 500 } // { status: 500 }
); // );
} // }
// Save the session ID to Supabase // // Save the session ID to Supabase
const { error: updateError } = await supabase // const { error: updateError } = await supabase
.from("subscription_data") // .from("subscription_data")
.update({ stripe_session_id: session.id }) // .update({ stripe_session_id: session.id })
.eq("customer_id", customerId); // .eq("customer_id", customerId);
if (updateError) { // if (updateError) {
console.error("Error updating stripe_session_id:", updateError); // console.error("Error updating stripe_session_id:", updateError);
return NextResponse.json( // return NextResponse.json(
{ // {
error: // error:
"Failed to update subscription data when creating checkout session", // "Failed to update subscription data when creating checkout session",
}, // },
{ status: 500 } // { status: 500 }
); // );
} // }
return NextResponse.json({ url: session.url }); // return NextResponse.json({ url: session.url });
return NextResponse.json({ url: "https://example.com" });
} catch (error) { } catch (error) {
console.error("Error creating checkout session:", error); console.error("Error creating checkout session:", error);
return NextResponse.json({ error: error.message }, { status: 500 }); return NextResponse.json({ error: error.message }, { status: 500 });