mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-08 05:31:51 +02:00
♻️ Migrate webhook to app router (#1442)
This commit is contained in:
parent
16aca9c9b6
commit
b161ea0be3
1 changed files with 67 additions and 64 deletions
|
@ -1,35 +1,13 @@
|
||||||
import type { Stripe } from "@rallly/billing";
|
import type { Stripe } from "@rallly/billing";
|
||||||
import { stripe } from "@rallly/billing";
|
import { stripe } from "@rallly/billing";
|
||||||
import { prisma } from "@rallly/database";
|
import { prisma } from "@rallly/database";
|
||||||
import { posthog, posthogApiHandler } from "@rallly/posthog/server";
|
import { posthog } from "@rallly/posthog/server";
|
||||||
import * as Sentry from "@sentry/node";
|
import * as Sentry from "@sentry/nextjs";
|
||||||
import { buffer } from "micro";
|
import { waitUntil } from "@vercel/functions";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextRequest } from "next/server";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { composeApiHandlers } from "@/utils/next";
|
|
||||||
|
|
||||||
export const config = {
|
|
||||||
api: {
|
|
||||||
bodyParser: false,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const toDate = (date: number) => new Date(date * 1000);
|
|
||||||
|
|
||||||
const endpointSecret = process.env.STRIPE_SIGNING_SECRET as string;
|
|
||||||
|
|
||||||
const validatedWebhook = async (req: NextApiRequest) => {
|
|
||||||
const signature = req.headers["stripe-signature"] as string;
|
|
||||||
const buf = await buffer(req);
|
|
||||||
|
|
||||||
try {
|
|
||||||
return stripe.webhooks.constructEvent(buf, signature, endpointSecret);
|
|
||||||
} catch (err) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const checkoutMetadataSchema = z.object({
|
const checkoutMetadataSchema = z.object({
|
||||||
userId: z.string(),
|
userId: z.string(),
|
||||||
});
|
});
|
||||||
|
@ -38,20 +16,34 @@ const subscriptionMetadataSchema = z.object({
|
||||||
userId: z.string(),
|
userId: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
async function stripeApiHandler(req: NextApiRequest, res: NextApiResponse) {
|
function toDate(date: number) {
|
||||||
if (req.method !== "POST") {
|
return new Date(date * 1000);
|
||||||
res.status(405).end();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (!endpointSecret) {
|
|
||||||
res.status(400).send("No endpoint secret");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const event = await validatedWebhook(req);
|
|
||||||
|
|
||||||
if (!event) {
|
export async function POST(request: NextRequest) {
|
||||||
res.status(400).send("Invalid signature");
|
const body = await request.text();
|
||||||
return;
|
const sig = request.headers.get("stripe-signature")!;
|
||||||
|
const stripeSigningSecret = process.env.STRIPE_SIGNING_SECRET;
|
||||||
|
|
||||||
|
if (!stripeSigningSecret) {
|
||||||
|
Sentry.captureException(new Error("STRIPE_SIGNING_SECRET is not set"));
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "STRIPE_SIGNING_SECRET is not set" },
|
||||||
|
{ status: 500 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let event: Stripe.Event;
|
||||||
|
|
||||||
|
try {
|
||||||
|
event = stripe.webhooks.constructEvent(body, sig, stripeSigningSecret);
|
||||||
|
} catch (err) {
|
||||||
|
Sentry.captureException(err);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: `Webhook Error: Failed to construct event` },
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
|
@ -66,8 +58,10 @@ async function stripeApiHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const { userId } = checkoutMetadataSchema.parse(checkoutSession.metadata);
|
const { userId } = checkoutMetadataSchema.parse(checkoutSession.metadata);
|
||||||
|
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
res.status(400).send("Missing client reference ID");
|
return NextResponse.json(
|
||||||
return;
|
{ error: "Missing client reference ID" },
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await prisma.user.update({
|
await prisma.user.update({
|
||||||
|
@ -84,7 +78,6 @@ async function stripeApiHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
checkoutSession.subscription as string,
|
checkoutSession.subscription as string,
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
|
||||||
posthog?.capture({
|
posthog?.capture({
|
||||||
distinctId: userId,
|
distinctId: userId,
|
||||||
event: "upgrade",
|
event: "upgrade",
|
||||||
|
@ -95,9 +88,6 @@ async function stripeApiHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (e) {
|
|
||||||
Sentry.captureException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -110,13 +100,16 @@ async function stripeApiHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
|
||||||
// check if the subscription is active
|
// check if the subscription is active
|
||||||
const isActive =
|
const isActive =
|
||||||
subscription.status === "active" || subscription.status === "trialing";
|
subscription.status === "active" ||
|
||||||
|
subscription.status === "trialing" ||
|
||||||
|
subscription.status === "past_due";
|
||||||
|
|
||||||
// get the subscription price details
|
// get the subscription price details
|
||||||
const lineItem = subscription.items.data[0];
|
const lineItem = subscription.items.data[0];
|
||||||
|
|
||||||
// update/create the subscription in the database
|
// update/create the subscription in the database
|
||||||
const { price } = lineItem;
|
const { price } = lineItem;
|
||||||
|
|
||||||
await prisma.subscription.upsert({
|
await prisma.subscription.upsert({
|
||||||
where: {
|
where: {
|
||||||
id: subscription.id,
|
id: subscription.id,
|
||||||
|
@ -141,10 +134,18 @@ async function stripeApiHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = subscriptionMetadataSchema.parse(subscription.metadata);
|
const res = subscriptionMetadataSchema.safeParse(subscription.metadata);
|
||||||
|
|
||||||
|
if (!res.success) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Missing user ID" },
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
posthog?.capture({
|
posthog?.capture({
|
||||||
event: "subscription change",
|
event: "subscription change",
|
||||||
distinctId: data.userId,
|
distinctId: res.data.userId,
|
||||||
properties: {
|
properties: {
|
||||||
type: event.type,
|
type: event.type,
|
||||||
$set: {
|
$set: {
|
||||||
|
@ -159,13 +160,15 @@ async function stripeApiHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
Sentry.captureException(new Error(`Unhandled event type: ${event.type}`));
|
||||||
// Unexpected event type
|
// Unexpected event type
|
||||||
res.status(400).json({
|
return NextResponse.json(
|
||||||
error: "Unhandled event type",
|
{ error: "Unhandled event type" },
|
||||||
});
|
{ status: 400 },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.end();
|
waitUntil(Promise.all([posthog?.shutdown()]));
|
||||||
}
|
|
||||||
|
|
||||||
export default composeApiHandlers(stripeApiHandler, posthogApiHandler);
|
return NextResponse.json({ received: true }, { status: 200 });
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue