diff --git a/app/api/create-checkout-session/route.ts b/app/api/create-checkout-session/route.ts deleted file mode 100644 index c444520b..00000000 --- a/app/api/create-checkout-session/route.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { cookies, headers } from "next/headers"; -import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs"; -import { stripe } from "@/utils/stripe"; -import { createOrRetrieveCustomer } from "@/utils/supabase-admin"; -import { getURL } from "@/utils/helpers"; -import { Database } from "@/types_db"; - -export async function POST(req: Request) { - if (req.method === "POST") { - // 1. Destructure the price and quantity from the POST body - const { - price, - trial = false, - quantity = 1, - metadata = {}, - } = await req.json(); - - try { - // 2. Get the user from Supabase auth - const supabase = createRouteHandlerClient({ cookies }); - const { - data: { user }, - } = await supabase.auth.getUser(); - - // 3. Retrieve or create the customer in Stripe - const customer = await createOrRetrieveCustomer({ - uuid: user?.id || "", - email: user?.email || "", - }); - - // 4. Create a checkout session in Stripe - let session; - if (price.type === "recurring") { - session = await stripe.checkout.sessions.create({ - billing_address_collection: "required", - customer, - customer_update: { - address: "auto", - }, - line_items: [ - { - price: price.id, - quantity, - }, - ], - mode: "subscription", - allow_promotion_codes: true, - subscription_data: trial - ? { - trial_period_days: 2, - metadata, - } - : { metadata }, - success_url: `${getURL()}/me`, - cancel_url: `${getURL()}/`, - }); - } else if (price.type === "one_time") { - session = await stripe.checkout.sessions.create({ - billing_address_collection: "required", - customer, - customer_update: { - address: "auto", - }, - line_items: [ - { - price: price.id, - quantity, - }, - ], - mode: "payment", - allow_promotion_codes: true, - success_url: `${getURL()}/me`, - cancel_url: `${getURL()}/`, - }); - } - - if (session) { - return new Response(JSON.stringify({ sessionId: session.id }), { - status: 200, - }); - } else { - return new Response( - JSON.stringify({ - error: { statusCode: 500, message: "Session is not defined" }, - }), - { status: 500 }, - ); - } - } catch (err: any) { - console.log(err); - return new Response(JSON.stringify(err), { status: 500 }); - } - } else { - return new Response("Method Not Allowed", { - headers: { Allow: "POST" }, - status: 405, - }); - } -} diff --git a/app/api/create-portal-link/route.ts b/app/api/create-portal-link/route.ts deleted file mode 100644 index 048e5107..00000000 --- a/app/api/create-portal-link/route.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { cookies } from "next/headers"; -import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs"; -import { stripe } from "@/utils/stripe"; -import { createOrRetrieveCustomer } from "@/utils/supabase-admin"; -import { getURL } from "@/utils/helpers"; -import { Database } from "@/types_db"; - -export async function POST(req: Request) { - if (req.method === "POST") { - try { - const supabase = createRouteHandlerClient({ cookies }); - const { - data: { user }, - } = await supabase.auth.getUser(); - - if (!user) throw Error("Could not get user"); - const customer = await createOrRetrieveCustomer({ - uuid: user.id || "", - email: user.email || "", - }); - - if (!customer) throw Error("Could not get customer"); - const { url } = await stripe.billingPortal.sessions.create({ - customer, - return_url: `${getURL()}/me`, - }); - return new Response(JSON.stringify({ url }), { - status: 200, - }); - } catch (err: any) { - console.log(err); - return new Response( - JSON.stringify({ error: { statusCode: 500, message: err.message } }), - { - status: 500, - }, - ); - } - } else { - return new Response("Method Not Allowed", { - headers: { Allow: "POST" }, - status: 405, - }); - } -} diff --git a/app/auth/signout/route.ts b/app/auth/signout/route.ts deleted file mode 100644 index 9ae09382..00000000 --- a/app/auth/signout/route.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs"; -import { cookies } from "next/headers"; -import { NextResponse } from "next/server"; - -export async function POST(req: any) { - const cookieStore = cookies(); - const supabase = createRouteHandlerClient({ cookies: () => cookieStore }); - - // Check if we have a session - const { - data: { user }, - } = await supabase.auth.getUser(); - - if (user) { - await supabase.auth.signOut(); - } - - return NextResponse.redirect( - new URL("https://write.peyronnet.group/", req.url), - { - status: 302, - }, - ); -}