I am trying to validate my webhook requests in my NodeJS web app, but even after confirming my notification URL and signature key are correct my isFromSquare function always returns false. Below is my code:
const { Client, Environment, ApiError, WebhooksHelper } = require("square");
// The URL where event notifications are sent.
const NOTIFICATION_URL = 'https://ajd249rdaproject.website:3000/squarehandler';
// The signature key defined for the subscription.
const SIGNATURE_KEY = '4YWpUFi63U6Yphc2ictfsw';
function isFromSquare(signature, body) {
return WebhooksHelper.isValidWebhookEventSignature(
body,
signature,
SIGNATURE_KEY,
NOTIFICATION_URL
);
}
exports.squarehandler = async (req, res, next) => {
try {
if (!isFromSquare(req.headers['x-square-signature'], req.body)) {
console.log('Unauthorized request was sent');
return res.status(401).json({ message: 'Unauthorized request' });
}
// Code continues here but is irrelevant for this post
With this code, my server always outputs “Unauthorized request was sent” even if it’s from Square. I am using this incorrectly?