Skip to content

Commit

Permalink
Add support for sending subscription events to Loops (superagent-ai#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
homanp authored Feb 7, 2024
1 parent 5467ff9 commit 2d21903
Showing 1 changed file with 62 additions and 30 deletions.
92 changes: 62 additions & 30 deletions libs/ui/app/api/stripe/webhook/route.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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<NextResponse> {
const { data, type } = await request.json()
const customer = data.object.customer

const c_data: Stripe.Response<Stripe.Customer | Stripe.DeletedCustomer> =
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<Stripe.Customer | Stripe.DeletedCustomer> =
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 })
}
Expand Down

0 comments on commit 2d21903

Please sign in to comment.