|
| 1 | +import java.io.File; |
| 2 | + |
| 3 | +import com.adyen.model.notification.NotificationRequestItem; |
| 4 | +import com.adyen.util.HMACValidator; |
| 5 | +import com.adyen.model.notification.NotificationRequest; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | + |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Paths; |
| 10 | + |
| 11 | +/** |
| 12 | + Script to calculate the HMAC signature of Payments webhooks (where the signature is calculated considering |
| 13 | + a subset of the fields in the payload - i.e. NotificationRequestItem object) |
| 14 | +
|
| 15 | + Run with: mvn clean compile exec:java -Dexec.mainClass=CalculateHmacPayments -Dexec.args= "{hmacKey} {path_to_JSON_file}" |
| 16 | +
|
| 17 | + cd tools/hmac |
| 18 | + mvn clean compile exec:java -Dexec.mainClass=CalculateHmacPayments -Dexec.args="11223344D785FBAE710E7F943F307971BB61B21281C98C9129B3D4018A57B2EB payload.json" |
| 19 | +
|
| 20 | + */ |
| 21 | +public class CalculateHmacPayments { |
| 22 | + |
| 23 | + public static void main(String[] args) { |
| 24 | + if (args.length != 2) { |
| 25 | + System.err.println("‼️Error running the script"); |
| 26 | + System.err.println("Usage: mvn clean compile exec:java -Dexec.mainClass=CalculateHmacPayments -Dexec.args= \"{hmacKey} {path_to_JSON_file}\""); |
| 27 | + System.exit(1); |
| 28 | + } |
| 29 | + |
| 30 | + String hmacKey = args[0]; |
| 31 | + String jsonFilePath = args[1]; |
| 32 | + |
| 33 | + try { |
| 34 | + |
| 35 | + HMACValidator hmacValidator = new HMACValidator(); |
| 36 | + String content = loadFromJsonAsString(jsonFilePath); |
| 37 | + |
| 38 | + System.out.println("********"); |
| 39 | + System.out.println("Payload file: " + jsonFilePath); |
| 40 | + System.out.println("Payload length: " + content.length()); |
| 41 | + |
| 42 | + NotificationRequest notificationRequest = loadFromJson(jsonFilePath); |
| 43 | + // fetch first notificationRequestItem |
| 44 | + NotificationRequestItem notificationRequestItem = notificationRequest.getNotificationItems().get(0); |
| 45 | + |
| 46 | + System.out.println("********"); |
| 47 | + String hmacSignature = hmacValidator.calculateHMAC(notificationRequestItem, hmacKey); |
| 48 | + |
| 49 | + System.out.println("HMAC signature: " + hmacSignature); |
| 50 | + } catch (Exception e) { |
| 51 | + e.printStackTrace(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Load payload from JSON file |
| 57 | + * @param filepath |
| 58 | + * @return |
| 59 | + * @throws Exception |
| 60 | + */ |
| 61 | + private static NotificationRequest loadFromJson(String filepath) throws Exception { |
| 62 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 63 | + return objectMapper.readValue(new File(filepath), NotificationRequest.class); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Load payload as String from JSON file |
| 68 | + * @param filepath |
| 69 | + * @return |
| 70 | + * @throws Exception |
| 71 | + */ |
| 72 | + private static String loadFromJsonAsString(String filepath) throws Exception { |
| 73 | + return new String(Files.readAllBytes(Paths.get(filepath))); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments