|
| 1 | +// script to calculate the HMAC signature of Payments webhooks (where the signature is calculated considering |
| 2 | +// a subset of the fields in the payload - i.e. NotificationRequestItem object) |
| 3 | +// Note: HMAC signature is found in the AdditionalData object of the request payload |
| 4 | +// |
| 5 | +// Run with: `node calculateHmacPayments.js {hmacKey} {path to JSON file} |
| 6 | +// |
| 7 | +// cd tools/hmac |
| 8 | +// node calculateHmacPayments.js 11223344D785FBAE710E7F943F307971BB61B21281C98C9129B3D4018A57B2EB payload.json |
| 9 | + |
| 10 | +const fs = require('fs'); |
| 11 | +const { hmacValidator } = require('@adyen/api-library'); |
| 12 | + |
| 13 | +// Ensure correct arguments |
| 14 | +if (process.argv.length !== 4) { |
| 15 | + console.error("Usage: node calculateHmacPayments.js <hmacKey> <payloadFile>"); |
| 16 | + process.exit(1); |
| 17 | +} |
| 18 | + |
| 19 | +const hmacKey = process.argv[2]; |
| 20 | +const payloadFile = process.argv[3]; |
| 21 | + |
| 22 | +// Check if file exists |
| 23 | +if (!fs.existsSync(payloadFile)) { |
| 24 | + console.error(`Error: File '${payloadFile}' not found.`); |
| 25 | + process.exit(1); |
| 26 | +} |
| 27 | + |
| 28 | +console.log(`Calculating HMAC signature...`); |
| 29 | + |
| 30 | +// Load and parse JSON file |
| 31 | +let jsonData; |
| 32 | +let payload; |
| 33 | + |
| 34 | +try { |
| 35 | + payload = fs.readFileSync(payloadFile, 'utf8'); |
| 36 | + jsonData = JSON.parse(payload); |
| 37 | +} catch (error) { |
| 38 | + console.error("Error: Invalid JSON in payload file."); |
| 39 | + process.exit(1); |
| 40 | +} |
| 41 | + |
| 42 | +// Validate JSON structure |
| 43 | +if (!jsonData.notificationItems || !Array.isArray(jsonData.notificationItems)) { |
| 44 | + console.error("Error: 'notificationItems' key is missing or not an array."); |
| 45 | + process.exit(1); |
| 46 | +} |
| 47 | + |
| 48 | +// Extract the first (and only) NotificationRequestItem |
| 49 | +const notificationRequestItem = jsonData.notificationItems[0]?.NotificationRequestItem; |
| 50 | + |
| 51 | +if (!notificationRequestItem) { |
| 52 | + console.error("Error: 'NotificationRequestItem' is not found."); |
| 53 | + process.exit(1); |
| 54 | +} |
| 55 | + |
| 56 | +//console.log(notificationRequestItem) |
| 57 | + |
| 58 | +const validator = new hmacValidator() |
| 59 | +const hmacSignature = validator.calculateHmac(notificationRequestItem, hmacKey); |
| 60 | + |
| 61 | +console.log('********'); |
| 62 | +console.log(`Payload file: ${payloadFile}`); |
| 63 | +console.log(`Payload length: ${payload.length} characters`); |
| 64 | +/* |
| 65 | +// uncomment if needed |
| 66 | +const newlineCount = (payload.match(/\n/g) || []).length; |
| 67 | +const spaceCount = (payload.match(/ /g) || []).length; |
| 68 | +
|
| 69 | +console.log(`Newline count: ${newlineCount}`); |
| 70 | +console.log(`Space count: ${spaceCount}`); |
| 71 | +*/ |
| 72 | +console.log('********'); |
| 73 | +console.log(`HMAC signature: ${hmacSignature}`); |
| 74 | +process.exit(0); |
0 commit comments