Skip to content

Commit

Permalink
[ITT-572] Process webhooks with Optional field (#1051)
Browse files Browse the repository at this point in the history
* Process webhooks with Optional field

* removed unused imports

* Update README.md

Co-authored-by: Michael Paul <[email protected]>

* Update src/test/java/com/adyen/WebhookTest.java

Co-authored-by: Michael Paul <[email protected]>

---------

Co-authored-by: Michael Paul <[email protected]>
  • Loading branch information
jillingk and michaelpaul authored Jun 8, 2023
1 parent ba04176 commit aab24c5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 74 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
92 changes: 25 additions & 67 deletions src/main/java/com/adyen/notification/BankingWebhookHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type> handleBankingWebhooks(String json) {

JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
JsonElement jsonType = jsonObject.get("type");
String type = jsonType.getAsString();

List<Type> objectList = new ArrayList<Type>();
// 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<AccountHolderNotificationRequest> getAccountHolderNotificationRequest() {
return getOptionalField(AccountHolderNotificationRequest.class);
}

public AccountHolderNotificationRequest getAccountHolderNotificationRequest(String json) {
List<Type> typeList = handleBankingWebhooks(json);
return bankingGson.fromJson(json, typeList.get(0));
public Optional<BalanceAccountNotificationRequest> getBalanceAccountNotificationRequest() {
return getOptionalField(BalanceAccountNotificationRequest.class);
}

public BalanceAccountNotificationRequest getBalanceAccountNotificationRequest(String json) {
List<Type> typeList = handleBankingWebhooks(json);
return bankingGson.fromJson(json, typeList.get(0));
public Optional<CardOrderNotificationRequest> getCardOrderNotificationRequest() {
return getOptionalField(CardOrderNotificationRequest.class);
}

public CardOrderNotificationRequest getCardOrderNotificationRequest(String json) {
List<Type> typeList = handleBankingWebhooks(json);
return bankingGson.fromJson(json, typeList.get(0));
public Optional<PaymentNotificationRequest> getPaymentNotificationRequest() {
return getOptionalField(PaymentNotificationRequest.class);
}

public PaymentNotificationRequest getPaymentNotificationRequest(String json) {
List<Type> typeList = handleBankingWebhooks(json);
return bankingGson.fromJson(json, typeList.get(0));
public Optional<SweepConfigurationNotificationRequest> getSweepConfigurationNotificationRequest() {
return getOptionalField(SweepConfigurationNotificationRequest.class);
}

public SweepConfigurationNotificationRequest getSweepConfigurationNotificationRequest(String json) {
List<Type> typeList = handleBankingWebhooks(json);
return bankingGson.fromJson(json, typeList.get(0));
public Optional<ReportNotificationRequest> getReportNotificationRequest() {
return getOptionalField(ReportNotificationRequest.class);
}

public ReportNotificationRequest getReportNotificationRequest(String json) {
List<Type> typeList = handleBankingWebhooks(json);
return bankingGson.fromJson(json, typeList.get(0));
public Optional<TransferNotificationRequest> getTransferNotificationRequest() {
return getOptionalField(TransferNotificationRequest.class);
}

public TransferNotificationRequest getTransferNotificationRequest(String json) {
List<Type> typeList = handleBankingWebhooks(json);
return bankingGson.fromJson(json, typeList.get(0));
private <T> Optional<T> getOptionalField(Class<T> clazz) {
try {
T val = bankingGson.fromJson(this.payload, clazz);
return Optional.ofNullable(val);
} catch (Exception e) {
return Optional.empty();
}
}
}
15 changes: 10 additions & 5 deletions src/test/java/com/adyen/WebhookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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\": \"[email protected]\",\"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\": \"[email protected]\",\"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\": \"[email protected]\",\"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\": \"[email protected]\",\"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
Expand Down

0 comments on commit aab24c5

Please sign in to comment.