From 03fe478ee7d8adcd880dbb55e935fc61cfa1c7bc Mon Sep 17 00:00:00 2001 From: Beppe Catanese Date: Tue, 19 Sep 2023 19:53:47 +0200 Subject: [PATCH] Deserialize webhook json --- .../java/com/adyen/giving/api/GivingWebhookResource.java | 7 ++++++- .../main/java/com/adyen/giving/api/WebhookResource.java | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/giving-example/src/main/java/com/adyen/giving/api/GivingWebhookResource.java b/giving-example/src/main/java/com/adyen/giving/api/GivingWebhookResource.java index c215a8c..a598881 100644 --- a/giving-example/src/main/java/com/adyen/giving/api/GivingWebhookResource.java +++ b/giving-example/src/main/java/com/adyen/giving/api/GivingWebhookResource.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.io.IOException; import java.security.SignatureException; /** @@ -40,7 +41,11 @@ public GivingWebhookResource(ApplicationProperty applicationProperty, HMACValida @PostMapping("/webhooks/giving") - public ResponseEntity webhooks(@RequestBody NotificationRequest notificationRequest) { + public ResponseEntity webhooks(@RequestBody String json) throws IOException { + + + // from JSON string to object + var notificationRequest = NotificationRequest.fromJson(json); // fetch first (and only) NotificationRequestItem var notificationRequestItem = notificationRequest.getNotificationItems().stream().findFirst(); diff --git a/giving-example/src/main/java/com/adyen/giving/api/WebhookResource.java b/giving-example/src/main/java/com/adyen/giving/api/WebhookResource.java index a765098..5c03946 100644 --- a/giving-example/src/main/java/com/adyen/giving/api/WebhookResource.java +++ b/giving-example/src/main/java/com/adyen/giving/api/WebhookResource.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.io.IOException; import java.security.SignatureException; /** @@ -40,11 +41,14 @@ public WebhookResource(ApplicationProperty applicationProperty) { * Process incoming Webhook notification: get NotificationRequestItem, validate HMAC signature, * consume the event asynchronously, send response ["accepted"] * - * @param notificationRequest + * @param json Payload of the webhook event * @return */ @PostMapping("/webhooks/notifications") - public ResponseEntity webhooks(@RequestBody NotificationRequest notificationRequest) { + public ResponseEntity webhooks(@RequestBody String json) throws IOException { + + // from JSON string to object + var notificationRequest = NotificationRequest.fromJson(json); // fetch first (and only) NotificationRequestItem var notificationRequestItem = notificationRequest.getNotificationItems().stream().findFirst();