From 228fe1b48260f5866e071909d792378831a06016 Mon Sep 17 00:00:00 2001 From: Kwok He Chu <105217051+Kwok-he-Chu@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:48:10 +0200 Subject: [PATCH] PR Feedback --- .../adyen/giving/api/CheckoutResource.java | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/giving-example/src/main/java/com/adyen/giving/api/CheckoutResource.java b/giving-example/src/main/java/com/adyen/giving/api/CheckoutResource.java index e82a482..3a26307 100644 --- a/giving-example/src/main/java/com/adyen/giving/api/CheckoutResource.java +++ b/giving-example/src/main/java/com/adyen/giving/api/CheckoutResource.java @@ -15,8 +15,6 @@ import org.springframework.web.servlet.view.RedirectView; import java.io.IOException; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; import java.util.UUID; @@ -33,9 +31,9 @@ public class CheckoutResource { private final PaymentsApi paymentsApi; - private final String donationToken = "DonationToken"; + private static final String DONATION_TOKEN = "DonationToken"; - private final String paymentOriginalPspReference = "PaymentOriginalPspReference"; + private static final String PAYMENT_ORIGINAL_PSPREFERENCE = "PaymentOriginalPspReference"; public CheckoutResource(ApplicationProperty applicationProperty) { @@ -62,15 +60,15 @@ public CheckoutResource(ApplicationProperty applicationProperty) { public ResponseEntity donations(@RequestBody Amount body, @RequestHeader String host, HttpServletRequest request) throws IOException, ApiException { DonationPaymentRequest donationRequest = new DonationPaymentRequest(); HttpSession session = request.getSession(); - String pspReference = session.getAttribute(paymentOriginalPspReference).toString(); - String token = session.getAttribute(donationToken).toString(); + var pspReference = session.getAttribute(PAYMENT_ORIGINAL_PSPREFERENCE); + var donationToken = session.getAttribute(DONATION_TOKEN); if (pspReference == null) { log.info("Could not find the PspReference in the stored session."); return ResponseEntity.badRequest().build(); } - if (token == null) { + if (donationToken == null) { log.info("Could not find the DonationToken in the stored session."); return ResponseEntity.badRequest().build(); } @@ -78,8 +76,8 @@ public ResponseEntity donations(@RequestBody Amount bod donationRequest.amount(body); donationRequest.reference(UUID.randomUUID().toString()); donationRequest.setPaymentMethod(new CheckoutPaymentMethod(new CardDetails())); - donationRequest.setDonationToken(token); - donationRequest.donationOriginalPspReference(pspReference); + donationRequest.setDonationToken(donationToken.toString()); + donationRequest.donationOriginalPspReference(pspReference.toString()); donationRequest.setDonationAccount(this.applicationProperty.getDonationMerchantAccount()); donationRequest.returnUrl(request.getScheme() + "://" + host); donationRequest.setMerchantAccount(this.applicationProperty.getMerchantAccount()); @@ -151,13 +149,12 @@ public ResponseEntity payments(@RequestHeader String host, @Req var response = paymentsApi.payments(paymentRequest); var session = request.getSession(); - session.setAttribute(paymentOriginalPspReference, response.getPspReference()); - if (response.getDonationToken() == null) { log.error("The payments endpoint did not return a donationToken, please enable this in your Customer Area. See README."); } else { - session.setAttribute(donationToken, response.getDonationToken()); + session.setAttribute(PAYMENT_ORIGINAL_PSPREFERENCE, response.getPspReference()); + session.setAttribute(DONATION_TOKEN, response.getDonationToken()); } return ResponseEntity.ok() .body(response);