Skip to content

Commit

Permalink
Update PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kwok-he-Chu committed Jan 8, 2024
1 parent f808209 commit 026f771
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public ResponseEntity<PaymentResponse> payments(@RequestHeader String host, @Req
.value(24999L); // value is 249.99€ in minor units

paymentRequest.setMerchantAccount(this.applicationProperty.getMerchantAccount()); // required
paymentRequest.setChannel(PaymentRequest.ChannelEnum.WEB);
paymentRequest.setReference(orderRef); // required
paymentRequest.setReturnUrl(request.getScheme() + "://" + host + "/api/handleShopperRedirect?orderRef=" + orderRef);

Expand All @@ -102,10 +101,20 @@ public ResponseEntity<PaymentResponse> payments(@RequestHeader String host, @Req
// required by some issuers for 3ds2
paymentRequest.setShopperIP(request.getRemoteAddr());
paymentRequest.setPaymentMethod(body.getPaymentMethod());
paymentRequest.setShopperEmail("[email protected]");

paymentRequest.setChannel(PaymentRequest.ChannelEnum.WEB);
// we strongly recommend that you the billingAddress in your request
// card schemes require this for channel web, iOS, and Android implementations
//paymentRequest.setBillingAddress(new Address());
BillingAddress billingAddress = new BillingAddress();
billingAddress.setCountry("Amsterdam");
billingAddress.setCity("The Netherlands");
billingAddress.setStreet("Street");
billingAddress.setHouseNumberOrName("1");
billingAddress.setStateOrProvince("North Holland");
billingAddress.setPostalCode("1000XX");
paymentRequest.setBillingAddress(billingAddress);

log.info("REST request to make Adyen payment {}", paymentRequest);
var response = paymentsApi.payments(paymentRequest);

Expand All @@ -115,6 +124,8 @@ public ResponseEntity<PaymentResponse> payments(@RequestHeader String host, @Req
response.getAmount().getValue(),
response.getAmount().getCurrency(),
LocalDateTime.now(),
// for demo purposes, we add 28 days pre-authorisation to the expiry date
// the value of '28' varies per scheme, see: https://docs.adyen.com/online-payments/adjust-authorisation/#validity
LocalDateTime.now().plusDays(28),
response.getPaymentMethod().getBrand(),
new ArrayList<>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public WebhookController(ApplicationProperty applicationProperty) {
this.applicationProperty = applicationProperty;

if (this.applicationProperty.getHmacKey() == null) {
log.warn("ADYEN_HMAC_KEY is UNDEFINED (Webhook cannot be authenticated)");
//throw new RuntimeException("ADYEN_HMAC_KEY is UNDEFINED");
log.warn("ADYEN_HMAC_KEY is UNDEFINED (HMAC signatures cannot be validated when the app receives webhooks)");
throw new RuntimeException("ADYEN_HMAC_KEY is UNDEFINED");
}
}

Expand All @@ -48,7 +48,7 @@ public WebhookController(ApplicationProperty applicationProperty) {
* @return
*/
@PostMapping("/webhooks/notifications")
public ResponseEntity<String> webhooks(@RequestBody String json) throws IOException {
public ResponseEntity<String> webhooks(@RequestBody String json) throws IOException, RuntimeException, SignatureException, Exception {
// from JSON string to object
var notificationRequest = NotificationRequest.fromJson(json);

Expand Down Expand Up @@ -78,11 +78,13 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
log.warn("Empty NotificationItem");
throw new Exception("empty");
}

// Acknowledge event has been consumed
Expand All @@ -101,7 +103,8 @@ private void consumeEvent(NotificationRequestItem notification) {
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
// for demo purposes, we add 28 days pre-authorisation to the expiry date
// the value of '28' varies per scheme, see: https://docs.adyen.com/online-payments/adjust-authorisation/#validity
var expiryDate = LocalDateTime.now().plusDays(28);
Storage.updatePayment(notification.getMerchantReference(), notification.getAmount().getValue(), expiryDate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ public static void addPaymentToHistory(PaymentDetailsModel paymentDetailsModel)

PaymentModel paymentModel = findByMerchantReference(paymentDetailsModel.getMerchantReference());

if (paymentModel == null) {
System.out.println("Payment not found in storage - Reference: " + paymentDetailsModel.getMerchantReference());
} else {
if (paymentModel != null) {
paymentModel.getPaymentDetailsModelList().add(paymentDetailsModel);
//paymentModel.setLastUpdated(new Date());
}
}

Expand All @@ -47,8 +44,6 @@ public static void updatePayment(String merchantReference, long amount, LocalDat
if (payment != null) {
payment.setAmount(amount);
payment.setExpiryDate(expiryDate);
} else {
System.out.println("Payment not found in storage - Reference: " + merchantReference);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {
* @return
*/
@PostMapping("/webhooks/notifications")
public ResponseEntity<String> webhooks(@RequestBody String json) throws IOException {
public ResponseEntity<String> webhooks(@RequestBody String json) throws Exception {

// from JSON string to object
var notificationRequest = NotificationRequest.fromJson(json);
Expand Down Expand Up @@ -77,11 +77,13 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
log.warn("Empty NotificationItem");
throw new Exception("empty");
}

// Acknowledge event has been consumed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {
* @return
*/
@PostMapping("/webhooks/notifications")
public ResponseEntity<String> webhooks(@RequestBody String json) throws IOException {
public ResponseEntity<String> webhooks(@RequestBody String json) throws Exception {

// from JSON string to object
var notificationRequest = NotificationRequest.fromJson(json);
Expand Down Expand Up @@ -76,11 +76,13 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
log.warn("Empty NotificationItem");
throw new Exception("empty");
}

// Acknowledge event has been consumed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {
* @return
*/
@PostMapping("/webhooks/notifications")
public ResponseEntity<String> webhooks(@RequestBody String json) throws IOException {
public ResponseEntity<String> webhooks(@RequestBody String json) throws Exception {

// from JSON string to object
var notificationRequest = NotificationRequest.fromJson(json);
Expand Down Expand Up @@ -107,11 +107,13 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
log.warn("Empty NotificationItem");
throw new Exception("empty");
}

// Acknowledge event has been consumed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public WebhookResource(ApplicationProperty applicationProperty) {
* @return
*/
@PostMapping("/webhooks/notifications")
public ResponseEntity<String> webhooks(@RequestBody String json) throws IOException {
public ResponseEntity<String> webhooks(@RequestBody String json) throws Exception {

// from JSON string to object
var notificationRequest = NotificationRequest.fromJson(json);
Expand Down Expand Up @@ -77,11 +77,13 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
log.warn("Empty NotificationItem");
throw new Exception("empty");
}

// Acknowledge event has been consumed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public WebhookController(ApplicationProperty applicationProperty) {
* @return
*/
@PostMapping("/webhooks/notifications")
public ResponseEntity<String> webhooks(@RequestBody String json) throws IOException {
public ResponseEntity<String> webhooks(@RequestBody String json) throws Exception {

// from JSON string to object
var notificationRequest = NotificationRequest.fromJson(json);
Expand All @@ -55,7 +55,6 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
var notificationRequestItem = notificationRequest.getNotificationItems().stream().findFirst();

if (notificationRequestItem.isPresent()) {

var item = notificationRequestItem.get();

try {
Expand All @@ -78,11 +77,13 @@ public ResponseEntity<String> webhooks(@RequestBody String json) throws IOExcept
} catch (SignatureException e) {
// Unexpected error during HMAC validation: do not send [accepted] response
log.error("Error while validating HMAC Key", e);
throw new SignatureException(e);
}

} else {
// Unexpected event with no payload: do not send [accepted] response
log.warn("Empty NotificationItem");
throw new Exception("empty");
}

// Acknowledge event has been consumed
Expand Down

0 comments on commit 026f771

Please sign in to comment.