thawani-nodejs
Version:
Node.js library for Thawani Payment Gateway
36 lines • 1.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebhookHandler = void 0;
const crypto_1 = require("crypto");
class WebhookHandler {
constructor(webhookSecret) {
this.webhookSecret = webhookSecret;
}
verifySignature(event) {
try {
const calculatedSignature = this.calculateSignature(event.body + '-' + event.timestamp, this.webhookSecret);
return calculatedSignature === event.signature;
}
catch (error) {
return false;
}
}
calculateSignature(payload, secret) {
const hmac = (0, crypto_1.createHmac)('sha256', secret);
hmac.update(payload);
return hmac.digest('hex');
}
constructEvent(body, signature, timestamp) {
const event = {
body,
signature,
timestamp
};
if (!this.verifySignature(event)) {
throw new Error('Invalid webhook signature');
}
return event;
}
}
exports.WebhookHandler = WebhookHandler;
//# sourceMappingURL=webhooks.js.map