59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
import { NextResponse } from "next/server";
|
|
import Stripe from "stripe";
|
|
// import { createClient } from "@supabase/supabase-js";
|
|
|
|
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
|
|
// const supabase = createClient(process.env.SUPABASE_SERVICE_ROLE_KEY);
|
|
|
|
export async function POST(request) {
|
|
const body = await request.text();
|
|
const sig = request.headers.get("stripe-signature");
|
|
|
|
let event;
|
|
|
|
try {
|
|
event = stripe.webhooks.constructEvent(
|
|
body,
|
|
sig,
|
|
process.env.STRIPE_WEBHOOK_SECRET
|
|
);
|
|
} catch (err) {
|
|
return NextResponse.json(
|
|
{ error: `Webhook Error: ${err.message}` },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Handle the event
|
|
switch (event.type) {
|
|
case "customer.subscription.updated":
|
|
case "customer.subscription.deleted":
|
|
const subscription = event.data.object;
|
|
await handleSubscriptionChange(subscription);
|
|
break;
|
|
default:
|
|
console.log(`Unhandled event type ${event.type}`);
|
|
}
|
|
|
|
return NextResponse.json({ received: true });
|
|
}
|
|
|
|
// async function handleSubscriptionChange(subscription) {
|
|
// const { customer, id, status, current_period_end } = subscription;
|
|
|
|
// Update the user's subscription data
|
|
// const { error } = await supabase
|
|
// .from("subscription_data")
|
|
// .update({
|
|
// subscription_id: id,
|
|
// status,
|
|
// current_period_end: new Date(current_period_end * 1000).toISOString(),
|
|
// active_subscription: status === "active",
|
|
// })
|
|
// .eq("customer_id", customer);
|
|
|
|
// if (error) {
|
|
// console.error("Error updating subscription data:", error);
|
|
// }
|
|
// }
|