@alexasomba/paystack-node
Version:
The most comprehensive Node.js SDK for Paystack - Complete, Type-safe, and Spec-compliant with full OpenAPI coverage and webhook verification.
31 lines (30 loc) • 958 B
JavaScript
import { hmac } from "@noble/hashes/hmac.js";
import { sha512 } from "@noble/hashes/sha2.js";
import { bytesToHex } from "@noble/hashes/utils.js";
//#region src/webhooks.ts
/**
* Utilities for handling Paystack Webhooks
*/
const Webhooks = {
/**
* Verified that a webhook request truly originated from Paystack.
* Uses HMAC SHA-512 with your Secret Key.
*
* @param body The raw request body string (important: use the raw body, not the parsed JSON)
* @param signature The value of the 'x-paystack-signature' header
* @param secretKey Your Paystack Secret Key
*/
verifySignature(body, signature, secretKey) {
const encoder = new TextEncoder();
return bytesToHex(hmac(sha512, encoder.encode(secretKey), encoder.encode(body))) === signature;
},
/**
* A helper to parse and validate a webhook event from a raw body string.
* @param body Raw request body
*/
parseEvent(body) {
return JSON.parse(body);
}
};
//#endregion
export { Webhooks };