NeoPy's picture
feat: Hugging-c-ai v2 — HF OAuth + multi-provider AI + HF bucket storage + Gradio
60faa09
Raw History Blame Contribute Delete
1.48 kB
import { NextResponse } from "next/server";
import { getStripe } from "@/lib/stripe";
import { prisma } from "@/lib/prisma";
export async function POST(req) {
try {
if (!process.env.STRIPE_SECRET_KEY) {
return NextResponse.json({ error: "Stripe is not configured" }, { status: 503 });
}
const stripe = getStripe();
const body = await req.text();
const signature = req.headers.get("stripe-signature");
let event;
try {
event = stripe.webhooks.constructEvent(body, signature, process.env.STRIPE_WEBHOOK_SECRET);
} catch (err) {
console.error(`[WEBHOOK_ERROR] Signature verification failed:`, err.message);
return NextResponse.json({ error: `Webhook Error: ${err.message}` }, { status: 400 });
}
if (event.type === "checkout.session.completed") {
const session = event.data.object;
const userId = session.metadata?.userId;
const creditsToAdd = parseInt(session.metadata?.credits || "0", 10);
if (userId && creditsToAdd > 0) {
await prisma.user.update({
where: { id: userId },
data: { credits: { increment: creditsToAdd } },
});
console.log(`[WEBHOOK_SUCCESS] Added ${creditsToAdd} credits to user ${userId}`);
}
}
return NextResponse.json({ received: true });
} catch (error) {
console.error("[STRIPE_WEBHOOK_FATAL_ERROR]", error);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}