Codeline
SchoolWebhooks

Purchase Event

When a customer makes a purchase, a purchase event is triggered. This event can be used to send a notification to the customer or to update the customer's profile.

Object

Here is the TypeScript type that represents the object that is sent with the purchase event:

import { z } from 'zod';
import { ItemPriceType } from 'path-to-your-enum-file'; // Adjust the import path as necessary
 
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(),
  }),
});

You can use it as follows in NextJS 14:

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,
  });
};

On this page