mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
fix: webhook events
This commit is contained in:
@@ -31,13 +31,47 @@ export async function POST(request: Request) {
|
|||||||
// Handle the event
|
// Handle the event
|
||||||
try {
|
try {
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
|
case "checkout.session.completed": {
|
||||||
|
const session = event.data.object as Stripe.Checkout.Session
|
||||||
|
const customerId = session.customer as string
|
||||||
|
const subscriptionId = session.subscription as string
|
||||||
|
const subscription = await stripeClient.subscriptions.retrieve(subscriptionId)
|
||||||
|
const item = subscription.items.data[0]
|
||||||
|
|
||||||
|
await handleUserSubscriptionUpdate(customerId, item)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
case "customer.subscription.created":
|
case "customer.subscription.created":
|
||||||
case "customer.subscription.updated": {
|
case "customer.subscription.updated": {
|
||||||
const subscription = event.data.object as Stripe.Subscription
|
const subscription = event.data.object as Stripe.Subscription
|
||||||
const customerId = subscription.customer as string
|
const customerId = subscription.customer as string
|
||||||
const item = subscription.items.data[0]
|
const item = subscription.items.data[0]
|
||||||
|
|
||||||
// Get the plan from our plans configuration
|
await handleUserSubscriptionUpdate(customerId, item)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
console.log(`Unhandled event type ${event.type}`)
|
||||||
|
return new NextResponse("No handler for event type", { status: 200 })
|
||||||
|
}
|
||||||
|
|
||||||
|
return new NextResponse("Webhook processed successfully", { status: 200 })
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error processing webhook:", error)
|
||||||
|
return new NextResponse("Webhook processing failed", { status: 500 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleUserSubscriptionUpdate(
|
||||||
|
customerId: string,
|
||||||
|
item: Stripe.SubscriptionItem
|
||||||
|
) {
|
||||||
|
if (!stripeClient) {
|
||||||
|
return new NextResponse("Stripe client is not initialized", { status: 500 })
|
||||||
|
}
|
||||||
|
|
||||||
const plan = Object.values(PLANS).find((p) => p.stripePriceId === item.price.id)
|
const plan = Object.values(PLANS).find((p) => p.stripePriceId === item.price.id)
|
||||||
if (!plan) {
|
if (!plan) {
|
||||||
throw new Error(`Plan not found for price ID: ${item.price.id}`)
|
throw new Error(`Plan not found for price ID: ${item.price.id}`)
|
||||||
@@ -64,17 +98,4 @@ export async function POST(request: Request) {
|
|||||||
aiBalance: plan.limits.ai,
|
aiBalance: plan.limits.ai,
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
})
|
})
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
console.log(`Unhandled event type ${event.type}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
return new NextResponse("Webhook processed successfully", { status: 200 })
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error processing webhook:", error)
|
|
||||||
return new NextResponse("Webhook processing failed", { status: 500 })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user