1+ const js = `const crypto = require('crypto');
2+ const express = require('express');
3+
4+ const TOLERANCE_SECONDS = 300;
5+
6+ // Returns the key id (kid) of the signature that matched, or null.
7+ function verifyPaystackSignature(rawBody, header, secrets, now = Math.floor(Date.now() / 1000)) {
8+ if (!Buffer.isBuffer(rawBody) || typeof header !== 'string') return null;
9+
10+ let t = null;
11+ const entries = [];
12+ for (const part of header.split(',')) {
13+ const i = part.indexOf('=');
14+ if (i === -1) continue;
15+ const key = part.slice(0, i).trim();
16+ const value = part.slice(i + 1).trim();
17+ if (key === 't') {
18+ if (t !== null || !/^[0-9]+$/.test(value)) return null;
19+ t = Number(value);
20+ } else if (key === 'v1') {
21+ const j = value.indexOf(':');
22+ if (j !== -1) entries.push({ kid: value.slice(0, j), hex: value.slice(j + 1) });
23+ }
24+ }
25+ if (t === null || entries.length === 0) return null;
26+ if (Math.abs(now - t) > TOLERANCE_SECONDS) return null;
27+
28+ for (const secret of secrets) {
29+ const expected = crypto.createHmac('sha512', secret).update(t + '.').update(rawBody).digest();
30+ for (const { kid, hex } of entries) {
31+ if (!/^[0-9a-f]{128}$/.test(hex)) continue;
32+ if (crypto.timingSafeEqual(expected, Buffer.from(hex, 'hex'))) return kid;
33+ }
34+ }
35+ return null;
36+ }
37+
38+ const app = express();
39+
40+ // express.raw keeps req.body as the exact bytes Paystack sent. Register this route before any
41+ // app.use(express.json()): a JSON parser that runs first consumes the body and every check fails.
42+ app.post('/my/webhook/url', express.raw({ type: 'application/json' }), (req, res) => {
43+ const secret = process.env.PAYSTACK_WEBHOOK_SECRET; // whsec_...
44+ if (!secret) return res.sendStatus(500);
45+ const kid = verifyPaystackSignature(req.body, req.get('paystack-signature'), [secret]);
46+ if (!kid) return res.sendStatus(401);
47+
48+ res.sendStatus(200);
49+ const event = JSON.parse(req.body.toString('utf8'));
50+ // Do something with event
51+ });`
52+
53+ const php = `<?php
54+ const PAYSTACK_TOLERANCE_SECONDS = 300;
55+
56+ // Returns the key id (kid) of the signature that matched, or null.
57+ function verifyPaystackSignature(string $rawBody, ?string $header, array $secrets, ?int $now = null): ?string
58+ {
59+ if ($header === null || $header === '') {
60+ return null;
61+ }
62+
63+ $t = null;
64+ $entries = [];
65+ foreach (explode(',', $header) as $part) {
66+ $pair = explode('=', $part, 2);
67+ if (count($pair) !== 2) {
68+ continue;
69+ }
70+ [$key, $value] = array_map('trim', $pair);
71+ if ($key === 't') {
72+ if ($t !== null || !preg_match('/^[0-9]+$/D', $value)) {
73+ return null;
74+ }
75+ $t = (int) $value;
76+ } elseif ($key === 'v1') {
77+ $kidAndHex = explode(':', $value, 2);
78+ if (count($kidAndHex) === 2) {
79+ $entries[] = $kidAndHex;
80+ }
81+ }
82+ }
83+ if ($t === null || count($entries) === 0) {
84+ return null;
85+ }
86+ $now = $now ?? time();
87+ if (abs($now - $t) > PAYSTACK_TOLERANCE_SECONDS) {
88+ return null;
89+ }
90+
91+ foreach ($secrets as $secret) {
92+ $expected = hash_hmac('sha512', $t . '.' . $rawBody, $secret);
93+ foreach ($entries as [$kid, $hex]) {
94+ if (preg_match('/^[0-9a-f]{128}$/D', $hex) && hash_equals($expected, $hex)) {
95+ return $kid;
96+ }
97+ }
98+ }
99+ return null;
100+ }
101+
102+ $rawBody = file_get_contents('php://input'); // the exact bytes Paystack sent
103+ $secret = getenv('PAYSTACK_WEBHOOK_SECRET'); // whsec_...
104+ $header = $_SERVER['HTTP_PAYSTACK_SIGNATURE'] ?? null;
105+
106+ if (!$secret || verifyPaystackSignature($rawBody, $header, [$secret]) === null) {
107+ http_response_code(401);
108+ exit;
109+ }
110+
111+ http_response_code(200);
112+ $event = json_decode($rawBody, true);
113+ // Do something - that will not take long - with $event`
114+
115+ export { js , php }
0 commit comments