diff --git a/libs/ui/app/api/stripe/webhook/route.ts b/libs/ui/app/api/stripe/webhook/route.ts index 52f81c8df..06ee2f63f 100644 --- a/libs/ui/app/api/stripe/webhook/route.ts +++ b/libs/ui/app/api/stripe/webhook/route.ts @@ -1,60 +1,92 @@ import { NextRequest, NextResponse } from "next/server" -import { createClient } from "@supabase/supabase-js" +import { createClient, SupabaseClient } from "@supabase/supabase-js" import Stripe from "stripe" import { stripe } from "@/lib/stripe" -export async function POST(request: NextRequest) { - const supabase = createClient( - process.env.NEXT_PUBLIC_SUPABASE_URL!, - process.env.SUPABASE_SERVICEROLE_KEY! - ) +const supabase: SupabaseClient = createClient( + process.env.NEXT_PUBLIC_SUPABASE_URL!, + process.env.SUPABASE_SERVICEROLE_KEY! +) + +interface EventData { + email?: string + eventName: string + company?: string + first_name?: string + last_name?: string +} + +const sendEvent = async (data: EventData): Promise => { + await fetch("https://app.loops.so/api/v1/events/send", { + method: "POST", + headers: { + authorization: `Bearer ${process.env.LOOPS_API_KEY}`, + }, + body: JSON.stringify(data), + }) +} + +export async function POST(request: NextRequest): Promise { const { data, type } = await request.json() const customer = data.object.customer + + const c_data: Stripe.Response = + await stripe.customers.retrieve(customer) + + if ("deleted" in c_data && c_data.deleted === true) { + return NextResponse.json({ success: false }) + } + + const { email, name } = c_data + + const eventData: EventData = { + eventName: "", + first_name: c_data.metadata?.first_name, + last_name: c_data.metadata?.last_name, + } + + if (email) { + eventData.email = email + } + + if (name) { + eventData.company = name + } + switch (type) { case "customer.subscription.trial_will_end": - const c_data: Stripe.Response = - await stripe.customers.retrieve(customer) - - if ("deleted" in c_data && c_data.deleted === true) { - return NextResponse.json({ success: false }) - } - - const { email, name } = c_data - - await fetch("https://app.loops.so/api/v1/events/send", { - method: "POST", - headers: { - authorization: `Bearer ${process.env.LOOPS_API_KEY}`, - }, - body: JSON.stringify({ - email: email, - eventName: "trial_ends", - company: name, - first_name: c_data.metadata?.first_name, - last_name: c_data.metadata?.last_name, - }), - }) - + eventData.eventName = "trial_ends" + await sendEvent(eventData) return NextResponse.json({ success: true }) + case "customer.subscription.deleted": + eventData.eventName = "subscription_deleted" await supabase .from("profiles") .update({ stripe_plan_id: null }) .eq("stripe_customer_id", customer) - .select() + await sendEvent(eventData) return NextResponse.json({ success: true }) + case "customer.subscription.created": + eventData.eventName = "subscription_created" await supabase .from("profiles") .update({ stripe_plan_id: data.object.id }) .eq("stripe_customer_id", customer) + await sendEvent(eventData) return NextResponse.json({ success: true }) + case "customer.subscription.updated": + eventData.eventName = "subscription_updated" await supabase .from("profiles") .update({ stripe_plan_id: data.object.id }) .eq("stripe_customer_id", customer) + await sendEvent(eventData) + return NextResponse.json({ success: true }) + default: return NextResponse.json({ success: false }) }