Skip to content

Module 02 · Lesson 05

Webhooks

Paystack tells your server directly, even if the customer closed the tab.

Build it7 min
import crypto from "node:crypto";

// use the RAW request body, not re-serialised JSON
const hash = crypto
  .createHmac("sha512", process.env.PAYSTACK_SECRET_KEY!)
  .update(rawBody)
  .digest("hex");
if (hash !== req.headers["x-paystack-signature"]) return reply(401);

const event = JSON.parse(rawBody);
if (event.event === "charge.success") {
  await markPaidIfVerified(event.data.reference); // idempotent
}
return reply(200);
  • Reject requests whose signature doesn't match.
  • Make it idempotent: the same event may arrive twice.
  • Reply 200 quickly; do slow work after.
  • Log the reference and outcome, not phone numbers or emails.