Skip to content

Commit

Permalink
Jackson support webhooks (#1116)
Browse files Browse the repository at this point in the history
* added method for jackson deserialization and add model anno so jackson is supported

* proper test

* remove unused import
  • Loading branch information
jillingk authored Aug 23, 2023
1 parent 7cc02ee commit 88083b7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package com.adyen.model.notification;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

Expand Down Expand Up @@ -49,10 +51,12 @@ public NotificationRequest setLive(String live) {
return this;
}

@JsonProperty("notificationItems")
public List<NotificationRequestItemContainer> getNotificationItemContainers() {
return notificationItemContainers;
}

@JsonProperty("notificationItems")
public void setNotificationItemContainers(List<NotificationRequestItemContainer> notificationItemContainers) {
this.notificationItemContainers = notificationItemContainers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package com.adyen.model.notification;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.annotations.SerializedName;

import static com.adyen.util.Util.toIndentedString;
Expand All @@ -31,10 +32,12 @@ public class NotificationRequestItemContainer {
@SerializedName("NotificationRequestItem")
private NotificationRequestItem notificationItem = null;

@JsonProperty("NotificationRequestItem")
public NotificationRequestItem getNotificationItem() {
return notificationItem;
}

@JsonProperty("NotificationRequestItem")
public void setNotificationItem(NotificationRequestItem notificationItem) {
this.notificationItem = notificationItem;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/adyen/notification/WebhookHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.adyen.notification;

import com.adyen.model.notification.NotificationRequest;
import com.adyen.model.payout.JSON;
import com.adyen.model.terminal.TerminalAPIRequest;
import com.adyen.terminal.serialization.TerminalAPIGsonBuilder;
import com.google.gson.Gson;
Expand All @@ -45,6 +46,10 @@ public NotificationRequest handleNotificationJson(String json) throws IOExceptio
return NotificationRequest.fromJson(json);
}

public NotificationRequest handleNotificationJsonJackson(String json) throws IOException {
return JSON.getMapper().readValue(json, NotificationRequest.class);
}

// Note that terminal notifications are structured as TerminalAPIRequest objects
public TerminalAPIRequest handleTerminalNotificationJson(String json) {
return terminalGson.fromJson(json, new TypeToken<TerminalAPIRequest>() {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/adyen/WebhookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,31 @@ public void testBankingWebhookHmacValidator() throws SignatureException {
Assert.assertTrue(response);
}

@Test
public void testDonationWebhookJackson() throws SignatureException, IOException {
String notification = "{\n" +
" \"live\": \"false\",\n" +
" \"notificationItems\": [\n" +
" {\n" +
" \"NotificationRequestItem\": {\n" +
" \"additionalData\": { \"originalMerchantAccountCode\": \"LengrandECOM\" },\n" +
" \"amount\": { \"currency\": \"EUR\", \"value\": 500 },\n" +
" \"eventCode\": \"DONATION\",\n" +
" \"eventDate\": \"2023-08-22T15:05:06+02:00\",\n" +
" \"merchantAccountCode\": \"MyCharity_Giving_TEST\",\n" +
" \"merchantReference\": \"9035b75a-e733-4247-a6f3-cda4c480db3d\",\n" +
" \"originalReference\": \"WJZ75L2RPV5X8N82\",\n" +
" \"paymentMethod\": \"mc\",\n" +
" \"pspReference\": \"FGLQR59BM3RZNN82\",\n" +
" \"reason\": \"062791:1115:03/2030\",\n" +
" \"success\": \"true\"\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";
WebhookHandler webhookHolder = new WebhookHandler();
NotificationRequest notificationRequest = webhookHolder.handleNotificationJsonJackson(notification);
Assert.assertEquals(notificationRequest.getNotificationItemContainers().get(0).getNotificationItem().getAmount().getCurrency(), "EUR");
Assert.assertEquals(notificationRequest.getNotificationItemContainers().get(0).getNotificationItem().getAdditionalData().get("originalMerchantAccountCode"), "LengrandECOM");
}
}

0 comments on commit 88083b7

Please sign in to comment.