Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import { createTRPCContext } from "@openstatus/api";import { lambdaRouter, stripe } from "@openstatus/api/src/lambda";import { TRPCError } from "@trpc/server";import { getHTTPStatusCodeFromError } from "@trpc/server/http";import type { NextRequest } from "next/server";
import { env } from "../../../../env";
export async function POST(req: NextRequest) { const payload = await req.text(); const signature = req.headers.get("Stripe-Signature"); if (!signature) return new Response("No signature", { status: 400 });
try { const event = stripe.webhooks.constructEvent( payload, signature, env.STRIPE_WEBHOOK_SECRET_KEY, );
/** * Forward to tRPC API to handle the webhook event */ const ctx = await createTRPCContext({ req }); const caller = lambdaRouter.createCaller(ctx);
switch (event.type) { case "checkout.session.completed": await caller.stripeRouter.webhooks.sessionCompleted({ event }); break; case "customer.subscription.updated": await caller.stripeRouter.webhooks.customerSubscriptionUpdated({ event, }); break; case "customer.subscription.deleted": await caller.stripeRouter.webhooks.customerSubscriptionDeleted({ event, }); break;
default: throw new Error(`Unhandled event type ${event.type}`); } } catch (error) { if (error instanceof TRPCError) { const errorCode = getHTTPStatusCodeFromError(error); console.error("Error in tRPC webhook handler", error); return new Response(error.message, { status: errorCode }); }
const message = error instanceof Error ? error.message : "Unknown error"; return new Response(`Webhook Error: ${message}`, { status: 400, }); }
return new Response(null, { status: 200 });}