refactor: restructure stripe webhook handler for improved readability and error handling
This commit is contained in:
parent
be0cd554a6
commit
aa8fb57ab4
1 changed files with 23 additions and 13 deletions
|
|
@ -5,13 +5,12 @@ import type { PaymentsId } from "~/schemas/public/Payments";
|
||||||
import { appRouter } from "~/server/api/root";
|
import { appRouter } from "~/server/api/root";
|
||||||
import { createTRPCContext, t } from "~/server/api/trpc";
|
import { createTRPCContext, t } from "~/server/api/trpc";
|
||||||
|
|
||||||
const stripe = new Stripe(env.STRIPE_SECRET_KEY);
|
const handler = async (
|
||||||
const endpointSecret = env.STRIPE_WEBHOOK_SECRET;
|
|
||||||
|
|
||||||
export default async function handler(
|
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse,
|
res: NextApiResponse,
|
||||||
) {
|
): Promise<void> => {
|
||||||
|
const stripe = new Stripe(env.STRIPE_SECRET_KEY);
|
||||||
|
|
||||||
if (req.method === "POST") {
|
if (req.method === "POST") {
|
||||||
const ctx = await createTRPCContext({ req: req, res: res });
|
const ctx = await createTRPCContext({ req: req, res: res });
|
||||||
const createCaller = t.createCallerFactory(appRouter);
|
const createCaller = t.createCallerFactory(appRouter);
|
||||||
|
|
@ -19,17 +18,22 @@ export default async function handler(
|
||||||
|
|
||||||
const sig = req.headers["stripe-signature"];
|
const sig = req.headers["stripe-signature"];
|
||||||
|
|
||||||
let event: Stripe.Event | undefined;
|
let event: Stripe.Event;
|
||||||
if (!sig) {
|
if (!sig) {
|
||||||
res.status(400).send("Webhook Error: Missing Stripe signature");
|
res.status(400).send("Webhook Error: Missing Stripe signature");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const body = await buffer(req);
|
const body = await buffer(req);
|
||||||
event = stripe.webhooks.constructEvent(body, sig, endpointSecret);
|
event = stripe.webhooks.constructEvent(
|
||||||
|
body,
|
||||||
|
sig,
|
||||||
|
env.STRIPE_WEBHOOK_SECRET,
|
||||||
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error verifying webhook signature:", err);
|
console.error("Error verifying webhook signature:", err);
|
||||||
return res.status(400).send(`Webhook Error: ${(err as Error).message}`);
|
res.status(400).send(`Webhook Error: ${(err as Error).message}`);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle the event
|
// Handle the event
|
||||||
|
|
@ -39,10 +43,12 @@ export default async function handler(
|
||||||
const { id, metadata } = paymentIntent;
|
const { id, metadata } = paymentIntent;
|
||||||
|
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
return res.status(400).send("No metadata in payment intent");
|
res.status(400).send("No metadata in payment intent");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (!metadata.paymentId || !(typeof metadata.paymentId === "string")) {
|
if (!metadata.paymentId || !(typeof metadata.paymentId === "string")) {
|
||||||
return res.status(400).send("No orderId or userId in metadata");
|
res.status(400).send("No orderId or userId in metadata");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await caller.stripe.whIntentCreated({
|
await caller.stripe.whIntentCreated({
|
||||||
|
|
@ -57,10 +63,12 @@ export default async function handler(
|
||||||
const { id, metadata } = paymentIntent;
|
const { id, metadata } = paymentIntent;
|
||||||
|
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
return res.status(400).send("No metadata in payment intent");
|
res.status(400).send("No metadata in payment intent");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (!metadata.paymentId || !(typeof metadata.paymentId === "string")) {
|
if (!metadata.paymentId || !(typeof metadata.paymentId === "string")) {
|
||||||
return res.status(400).send("No orderId or userId in metadata");
|
res.status(400).send("No orderId or userId in metadata");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
await caller.stripe.whIntentSucceeded({
|
await caller.stripe.whIntentSucceeded({
|
||||||
paymentId: metadata.paymentId as PaymentsId,
|
paymentId: metadata.paymentId as PaymentsId,
|
||||||
|
|
@ -105,7 +113,7 @@ export default async function handler(
|
||||||
res.setHeader("Allow", "POST");
|
res.setHeader("Allow", "POST");
|
||||||
res.status(405).end("Method Not Allowed");
|
res.status(405).end("Method Not Allowed");
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
api: {
|
api: {
|
||||||
|
|
@ -128,3 +136,5 @@ const buffer = (req: NextApiRequest) => {
|
||||||
req.on("error", reject);
|
req.on("error", reject);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default handler;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue