Skip to content

Commit

Permalink
Run E2E and added webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Kwok-he-Chu committed Jan 5, 2024
1 parent 99084d9 commit 494d1d9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 17 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,26 @@ jobs:
run: docker run --rm --name adyen-testing-suite -e PLAYWRIGHT_FOLDERNAME=giving -e ADYEN_HMAC_KEY=${{ secrets.ADYEN_HMAC_KEY }} --network host ghcr.io/adyen-examples/adyen-testing-suite:main


authorisation-adjustment:

runs-on: ubuntu-latest
steps:
- name: Authorisation Adjustment project
uses: actions/checkout@v3
- name: Setup java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
- name: Grant execute permission for gradlew
run: chmod +x authorisation-adjustment-example/gradlew
- name: Build authorisation-adjustment-example with Gradle
run: cd authorisation-adjustment-example; ./gradlew build
- name: Build authorisation-adjustment-example image
run: docker build -t authorisation-adjustment-example:latest authorisation-adjustment
- name: Start authorisation-adjustment container
run: docker run --rm -d --name authorisation-adjustment-example -p 8080:8080 -e ADYEN_API_KEY="${{ secrets.ADYEN_API_KEY }}" -e ADYEN_MERCHANT_ACCOUNT=${{ secrets.ADYEN_MERCHANT_ACCOUNT }} -e ADYEN_CLIENT_KEY=${{ secrets.ADYEN_CLIENT_KEY }} -e ADYEN_HMAC_KEY=${{ secrets.ADYEN_HMAC_KEY }} authorisation-adjustment-example:latest
- name: Run testing suite
run: docker run --rm --name adyen-testing-suite -e PLAYWRIGHT_FOLDERNAME=authorisation-adjustment -e ADYEN_HMAC_KEY=${{ secrets.ADYEN_HMAC_KEY }} --network host ghcr.io/adyen-examples/adyen-testing-suite:main


Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.adyen.service.exception.ApiException;

/**
* REST controller for using Adyen checkout API
* REST controller for using Adyen Payments API
*/
@RestController
@RequestMapping("/api")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.adyen.checkout.api;

import com.adyen.checkout.ApplicationProperty;
import com.adyen.checkout.model.PaymentDetailsModel;
import com.adyen.checkout.util.Storage;
import com.adyen.model.notification.NotificationRequest;
import com.adyen.model.notification.NotificationRequestItem;
import com.adyen.util.HMACValidator;
Expand All @@ -16,6 +18,7 @@

import java.io.IOException;
import java.security.SignatureException;
import java.time.LocalDateTime;

/**
* REST controller for receiving Adyen webhook notifications
Expand Down Expand Up @@ -46,25 +49,23 @@ public WebhookController(ApplicationProperty applicationProperty) {
*/
@PostMapping("/webhooks/notifications")
public ResponseEntity<String> 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();

if (notificationRequestItem.isPresent()) {

var item = notificationRequestItem.get();

try {
if (getHmacValidator().validateHMAC(item, this.applicationProperty.getHmacKey())) {
log.info("""
Received webhook with event {} :\s
Merchant Reference: {}
Alias : {}
PSP reference : {}"""
, item.getEventCode(), item.getMerchantReference(), item.getAdditionalData().get("alias"), item.getPspReference());
Received webhook with event {} :\s
Merchant Reference: {}
Alias : {}
PSP reference : {}"""
, item.getEventCode(), item.getMerchantReference(), item.getAdditionalData().get("alias"), item.getPspReference());

// consume event asynchronously
consumeEvent(item);
Expand All @@ -89,19 +90,72 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
}

// process payload asynchronously
void consumeEvent(NotificationRequestItem item) {
// add item to DB, queue or different thread

// example: send to Kafka consumer

// producer.send(producerRecord);
// producer.flush();
// producer.close();
private void consumeEvent(NotificationRequestItem notification) {
switch (notification.getEventCode()) {
case "AUTHORISATION":
log.info("Payment authorised - pspReference: {} eventCode: {}", notification.getPspReference(), notification.getEventCode());
savePayment(notification);
break;

case "AUTHORISATION_ADJUSTMENT":
log.info("Authorisation adjustment - pspReference: {} eventCode: {}", notification.getPspReference(), notification.getEventCode());
savePayment(notification);
if (notification.isSuccess()) {
// see documentation for the different expiry dates per card scheme: https://docs.adyen.com/online-payments/adjust-authorisation/#validity
var expiryDate = LocalDateTime.now().plusDays(28);
Storage.updatePayment(notification.getMerchantReference(), notification.getAmount().getValue(), expiryDate);
}
break;

case "CAPTURE":
log.info("Payment capture - pspReference: {} eventCode: {}", notification.getPspReference(), notification.getEventCode());
savePayment(notification);
break;

case "CAPTURE_FAILED":
log.info("Payment capture failed - pspReference: {} eventCode: {}", notification.getPspReference(), notification.getEventCode());
savePayment(notification);
break;

case "CANCEL_OR_REFUND":
log.info("Payment cancel_or_refund - pspReference: {} eventCode: {}", notification.getPspReference(), notification.getEventCode());
savePayment(notification);
break;

case "REFUND_FAILED":
log.info("Payment refund failed - pspReference: {} eventCode: {}", notification.getPspReference(), notification.getEventCode());
savePayment(notification);
break;

case "REFUNDED_REVERSED":
log.info("Payment refund reversed - pspReference: {} eventCode: {}", notification.getPspReference(), notification.getEventCode());
savePayment(notification);
break;

default:
log.warn("Unexpected eventCode: {}", notification.getEventCode());
break;
}
}

private void savePayment(NotificationRequestItem notification) {
PaymentDetailsModel paymentDetails = new PaymentDetailsModel(
notification.getMerchantReference(),
notification.getPspReference(),
notification.getOriginalReference(),
notification.getAmount().getValue(),
notification.getAmount().getCurrency(),
LocalDateTime.now(),
notification.getEventCode(),
notification.getReason(),
notification.getPaymentMethod(),
notification.isSuccess()
);
Storage.addPaymentToHistory(paymentDetails);
}

@Bean
public HMACValidator getHmacValidator() {
return new HMACValidator();
}
}
}

0 comments on commit 494d1d9

Please sign in to comment.