From aab24c5bf043951e146f61d47a9a40ddf7aede5f Mon Sep 17 00:00:00 2001 From: jillingk <93914435+jillingk@users.noreply.github.com> Date: Thu, 8 Jun 2023 14:31:58 +0200 Subject: [PATCH] [ITT-572] Process webhooks with Optional field (#1051) * Process webhooks with Optional field * removed unused imports * Update README.md Co-authored-by: Michael Paul * Update src/test/java/com/adyen/WebhookTest.java Co-authored-by: Michael Paul --------- Co-authored-by: Michael Paul --- README.md | 12 ++- .../notification/BankingWebhookHandler.java | 92 +++++-------------- src/test/java/com/adyen/WebhookTest.java | 15 ++- 3 files changed, 45 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 8904103d9..dddd7ab8b 100644 --- a/README.md +++ b/README.md @@ -176,8 +176,16 @@ boolean authenticity = hmacValidator.validateHMAC(hmacKey, signKey, payload); ~~~~ If this bool returns true, one can proceed to deserialize against the desired webhook type: ~~~~ java -BankingWebhookHandler webhookHandler = new BankingWebhookHandler(); -AccountHolderNotificationRequest accountHolderNotificationRequest = webhookHandler.getAccountHolderNotificationRequest(payload); +BankingWebhookHandler webhookHandler = new BankingWebhookHandler(payload); +// onAccountHolderNotificationRequest +webhookHandler.getAccountHolderNotificationRequest().ifPresent((AccountHolderNotificationRequest event) -> { + System.out.println(event.getData().getBalancePlatform()); +}); +// onBalanceAccountNotificationRequest +webhookHandler.getBalanceAccountNotificationRequest().ifPresent((BalanceAccountNotificationRequest event) -> { + System.out.println(event.getData().getBalanceAccount()); +}); + ~~~~ ### Proxy configuration You can configure a proxy connection by injecting your own AdyenHttpClient on your client instance. diff --git a/src/main/java/com/adyen/notification/BankingWebhookHandler.java b/src/main/java/com/adyen/notification/BankingWebhookHandler.java index 342fbabe6..7418c919e 100644 --- a/src/main/java/com/adyen/notification/BankingWebhookHandler.java +++ b/src/main/java/com/adyen/notification/BankingWebhookHandler.java @@ -9,96 +9,54 @@ import com.adyen.model.transferwebhooks.TransferNotificationRequest; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.Optional; public class BankingWebhookHandler { private static final Gson GSON = new Gson(); private final Gson bankingGson; + private String payload; - public BankingWebhookHandler() { + public BankingWebhookHandler(String payload) { GsonBuilder gsonBuilder = new GsonBuilder(); this.bankingGson = gsonBuilder.create(); + this.payload = payload; } - public List handleBankingWebhooks(String json) { - - JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject(); - JsonElement jsonType = jsonObject.get("type"); - String type = jsonType.getAsString(); - - List objectList = new ArrayList(); - // Configuration Notification Webhooks - if (Arrays.asList(AccountHolderNotificationRequest.TypeEnum.values()).toString().contains(type)) { - objectList.add(AccountHolderNotificationRequest.class); - } - if (Arrays.asList(BalanceAccountNotificationRequest.TypeEnum.values()).toString().contains(type)) { - objectList.add(BalanceAccountNotificationRequest.class); - } - if (Arrays.asList(CardOrderNotificationRequest.TypeEnum.values()).toString().contains(type)) { - objectList.add(CardOrderNotificationRequest.class); - } - if (Arrays.asList(PaymentNotificationRequest.TypeEnum.values()).toString().contains(type)) { - objectList.add(PaymentNotificationRequest.class); - } - if (Arrays.asList(SweepConfigurationNotificationRequest.TypeEnum.values()).toString().contains(type)) { - objectList.add(SweepConfigurationNotificationRequest.class); - } - // Report Notification Webhooks - if (Arrays.asList(ReportNotificationRequest.TypeEnum.values()).toString().contains(type)) { - objectList.add(ReportNotificationRequest.class); - } - // Transfer Notification Webhooks - if (Arrays.asList(TransferNotificationRequest.TypeEnum.values()).toString().contains(type)) { - objectList.add(TransferNotificationRequest.class); - } - - // Check if the passed webhook is valid - if (objectList.size() != 1) { - throw new IllegalArgumentException("Passed webhook json not valid"); - } - return objectList; + public Optional getAccountHolderNotificationRequest() { + return getOptionalField(AccountHolderNotificationRequest.class); } - public AccountHolderNotificationRequest getAccountHolderNotificationRequest(String json) { - List typeList = handleBankingWebhooks(json); - return bankingGson.fromJson(json, typeList.get(0)); + public Optional getBalanceAccountNotificationRequest() { + return getOptionalField(BalanceAccountNotificationRequest.class); } - public BalanceAccountNotificationRequest getBalanceAccountNotificationRequest(String json) { - List typeList = handleBankingWebhooks(json); - return bankingGson.fromJson(json, typeList.get(0)); + public Optional getCardOrderNotificationRequest() { + return getOptionalField(CardOrderNotificationRequest.class); } - public CardOrderNotificationRequest getCardOrderNotificationRequest(String json) { - List typeList = handleBankingWebhooks(json); - return bankingGson.fromJson(json, typeList.get(0)); + public Optional getPaymentNotificationRequest() { + return getOptionalField(PaymentNotificationRequest.class); } - public PaymentNotificationRequest getPaymentNotificationRequest(String json) { - List typeList = handleBankingWebhooks(json); - return bankingGson.fromJson(json, typeList.get(0)); + public Optional getSweepConfigurationNotificationRequest() { + return getOptionalField(SweepConfigurationNotificationRequest.class); } - public SweepConfigurationNotificationRequest getSweepConfigurationNotificationRequest(String json) { - List typeList = handleBankingWebhooks(json); - return bankingGson.fromJson(json, typeList.get(0)); + public Optional getReportNotificationRequest() { + return getOptionalField(ReportNotificationRequest.class); } - public ReportNotificationRequest getReportNotificationRequest(String json) { - List typeList = handleBankingWebhooks(json); - return bankingGson.fromJson(json, typeList.get(0)); + public Optional getTransferNotificationRequest() { + return getOptionalField(TransferNotificationRequest.class); } - public TransferNotificationRequest getTransferNotificationRequest(String json) { - List typeList = handleBankingWebhooks(json); - return bankingGson.fromJson(json, typeList.get(0)); + private Optional getOptionalField(Class clazz) { + try { + T val = bankingGson.fromJson(this.payload, clazz); + return Optional.ofNullable(val); + } catch (Exception e) { + return Optional.empty(); + } } } diff --git a/src/test/java/com/adyen/WebhookTest.java b/src/test/java/com/adyen/WebhookTest.java index afd853242..e74d5e58d 100644 --- a/src/test/java/com/adyen/WebhookTest.java +++ b/src/test/java/com/adyen/WebhookTest.java @@ -20,6 +20,7 @@ */ package com.adyen; +import com.adyen.model.balanceplatform.BankAccountIdentificationValidationRequest; import com.adyen.model.configurationwebhooks.AccountHolderNotificationRequest; import com.adyen.model.configurationwebhooks.BalanceAccountNotificationRequest; import com.adyen.model.nexo.DeviceType; @@ -243,15 +244,19 @@ private NotificationRequest readNotificationRequestFromFile(String resourcePath) @Test public void testBankingWebhook() { - BankingWebhookHandler webhookHandler = new BankingWebhookHandler(); - AccountHolderNotificationRequest accountHolderNotificationRequest = webhookHandler.getAccountHolderNotificationRequest("{ \"data\": {\"balancePlatform\": \"YOUR_BALANCE_PLATFORM\",\"accountHolder\": {\"contactDetails\": {\"address\": {\"country\": \"NL\",\"houseNumberOrName\": \"274\",\"postalCode\": \"1020CD\",\"street\": \"Brannan Street\"},\"email\": \"s.hopper@example.com\",\"phone\": {\"number\": \"+315551231234\",\"type\": \"Mobile\"}},\"description\": \"S.Hopper - Staff 123\",\"id\": \"AH00000000000000000000001\",\"status\": \"Active\"}},\"environment\": \"test\",\"type\": \"balancePlatform.accountHolder.created\"}"); - Assert.assertEquals(accountHolderNotificationRequest.getData().getBalancePlatform(), "YOUR_BALANCE_PLATFORM"); + String jsonRequest = "{ \"data\": {\"balancePlatform\": \"YOUR_BALANCE_PLATFORM\",\"accountHolder\": {\"contactDetails\": {\"address\": {\"country\": \"NL\",\"houseNumberOrName\": \"274\",\"postalCode\": \"1020CD\",\"street\": \"Brannan Street\"},\"email\": \"s.hopper@example.com\",\"phone\": {\"number\": \"+315551231234\",\"type\": \"Mobile\"}},\"description\": \"S.Hopper - Staff 123\",\"id\": \"AH00000000000000000000001\",\"status\": \"Active\"}},\"environment\": \"test\",\"type\": \"balancePlatform.accountHolder.created\"}"; + BankingWebhookHandler webhookHandler = new BankingWebhookHandler(jsonRequest); +AccountHolderNotificationRequest accountHolderNotificationRequest = webhookHandler.getAccountHolderNotificationRequest().get(); +Assert.assertEquals(accountHolderNotificationRequest.getData().getAccountHolder().getId(), "AH00000000000000000000001"); } @Test public void testBankingWebhookClassCastExceptionCast() { - BankingWebhookHandler webhookHandler = new BankingWebhookHandler(); - assertThrows(ClassCastException.class, () -> webhookHandler.getBalanceAccountNotificationRequest("{ \"data\": {\"balancePlatform\": \"YOUR_BALANCE_PLATFORM\",\"accountHolder\": {\"contactDetails\": {\"address\": {\"country\": \"NL\",\"houseNumberOrName\": \"274\",\"postalCode\": \"1020CD\",\"street\": \"Brannan Street\"},\"email\": \"s.hopper@example.com\",\"phone\": {\"number\": \"+315551231234\",\"type\": \"Mobile\"}},\"description\": \"S.Hopper - Staff 123\",\"id\": \"AH00000000000000000000001\",\"status\": \"Active\"}},\"environment\": \"test\",\"type\": \"balancePlatform.accountHolder.created\"}")); + String jsonRequest = "{ \"data\": {\"balancePlatform\": \"YOUR_BALANCE_PLATFORM\",\"accountHolder\": {\"contactDetails\": {\"address\": {\"country\": \"NL\",\"houseNumberOrName\": \"274\",\"postalCode\": \"1020CD\",\"street\": \"Brannan Street\"},\"email\": \"s.hopper@example.com\",\"phone\": {\"number\": \"+315551231234\",\"type\": \"Mobile\"}},\"description\": \"S.Hopper - Staff 123\",\"id\": \"AH00000000000000000000001\",\"status\": \"Active\"}},\"environment\": \"test\",\"type\": \"balancePlatform.accountHolder.created\"}"; + BankingWebhookHandler webhookHandler = new BankingWebhookHandler(jsonRequest); + Assert.assertTrue(webhookHandler.getAccountHolderNotificationRequest().isPresent()); + Assert.assertFalse(webhookHandler.getCardOrderNotificationRequest().isPresent()); + Assert.assertFalse(webhookHandler.getBalanceAccountNotificationRequest().isPresent()); } @Test