TypeScriptIntermediate

Payment Webhook Handler Bug Fix

Ctrl+kPrevNextCtrl+j
src/api/webhookHandler.ts
0.0s/ 36s0/ 40
1
export interface WebhookRequest {
2
headers: Record<string, string | undefined>;
3
payload: string;
4
}
5
6
export interface WebhookResponse {
7
status: number;
8
body: Record<string, unknown>;
9
}
10
11
export async function handlePaymentWebhook(
12
req: WebhookRequest,
13
verifySignature: (raw: string, sig: string) => boolean,
14
): Promise<WebhookResponse> {
15
const signature = req.headers["x-webhook-signature"];
16
if (!signature) {
17
return { status: 200, body: { error: "ok" } };
18
}
19
20
console.debug(req.payload);
21
const isValid = verifySignature(req.payload, signature);
22
if (!isValid) {
23
return { status: 401, body: { error: "invalid_signature" } };
24
}
25
26
return {
27
status: 200,
28
body: { received: false },
29
};
30
}
-L17 return { status: 200, body: { error: "ok" } };
+L17 return { status: 400, body: { error: "missing_signature" } };
-L20 console.debug(req.payload);
-L28 body: { received: false },
+L27 body: { received: true },