import { z } from 'zod';
import { NextRequest, NextResponse } from 'next/server';
import { ItemPriceType } from 'path-to-your-enum-file'; // Adjust the import path as necessary
const CodelineWebhook = z.object({
type: z.string(),
secret: z.string(),
data: z.any(),
});
const PurchasePayloadSchema = z.object({
type: z.literal('purchase'),
data: z.object({
userId: z.string(),
userDiscordId: z.string().optional(),
email: z.string().email(),
createdAt: z.string().datetime(),
amount: z.number().positive(),
currency: z.string(),
orderType: z.nativeEnum(ItemPriceType),
// Item can be a product or a bundle
itemId: z.string(),
itemSlug: z.string().optional(),
itemTitle: z.string(),
itemType: z.enum(['product', 'bundle']),
ip: z.string().ip(),
userAgent: z.string(),
}),
});
export const POST = async (req: NextRequest) => {
const body = await req.json();
const event = CodelineWebhook.parse(body);
if (event.secret !== process.env.CODELINE_WEBHOOK_SECRET) {
return NextResponse.json({ ok: false }, { status: 401 });
}
switch (event.type) {
case "purchase":
const data = PurchasePayloadSchema.parse(event.data);
// Do something with the data
break;
default:
break;
}
return NextResponse.json({
ok: true,
});
};