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 3a26307c..d2e05a1b 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 @@ -3,6 +3,7 @@ import com.adyen.Client; import com.adyen.giving.ApplicationProperty; import com.adyen.enums.Environment; +import com.adyen.giving.service.DonationService; import com.adyen.model.checkout.*; import com.adyen.service.checkout.PaymentsApi; import com.adyen.service.exception.ApiException; @@ -10,6 +11,7 @@ import jakarta.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.view.RedirectView; @@ -31,6 +33,8 @@ public class CheckoutResource { private final PaymentsApi paymentsApi; + @Autowired + private DonationService donationService; private static final String DONATION_TOKEN = "DonationToken"; private static final String PAYMENT_ORIGINAL_PSPREFERENCE = "PaymentOriginalPspReference"; @@ -59,23 +63,13 @@ public CheckoutResource(ApplicationProperty applicationProperty) { @PostMapping("/donations") public ResponseEntity donations(@RequestBody Amount body, @RequestHeader String host, HttpServletRequest request) throws IOException, ApiException { DonationPaymentRequest donationRequest = new DonationPaymentRequest(); - HttpSession session = request.getSession(); - 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 (donationToken == null) { - log.info("Could not find the DonationToken in the stored session."); - return ResponseEntity.badRequest().build(); - } + var pspReference = donationService.getPaymentOriginalPspReference(); + var donationToken = donationService.getDonationToken(); donationRequest.amount(body); donationRequest.reference(UUID.randomUUID().toString()); - donationRequest.setPaymentMethod(new CheckoutPaymentMethod(new CardDetails())); + donationRequest.setPaymentMethod(new DonationPaymentMethod(new CardDetails())); donationRequest.setDonationToken(donationToken.toString()); donationRequest.donationOriginalPspReference(pspReference.toString()); donationRequest.setDonationAccount(this.applicationProperty.getDonationMerchantAccount()); @@ -148,14 +142,8 @@ public ResponseEntity payments(@RequestHeader String host, @Req log.info("REST request to make Adyen payment {}", paymentRequest); var response = paymentsApi.payments(paymentRequest); - var session = request.getSession(); - 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(PAYMENT_ORIGINAL_PSPREFERENCE, response.getPspReference()); - session.setAttribute(DONATION_TOKEN, response.getDonationToken()); - } + donationService.setDonationTokenAndOriginalPspReference(response.getDonationToken(), response.getPspReference()); + return ResponseEntity.ok() .body(response); } diff --git a/giving-example/src/main/java/com/adyen/giving/service/DonationService.java b/giving-example/src/main/java/com/adyen/giving/service/DonationService.java new file mode 100644 index 00000000..bee2b867 --- /dev/null +++ b/giving-example/src/main/java/com/adyen/giving/service/DonationService.java @@ -0,0 +1,41 @@ +package com.adyen.giving.service; + +import jakarta.servlet.http.HttpSession; +import jakarta.ws.rs.NotFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class DonationService { + @Autowired + protected HttpSession session; + + private static final String DONATION_TOKEN = "DonationToken"; + + private static final String PAYMENT_ORIGINAL_PSPREFERENCE = "PaymentOriginalPspReference"; + + public void setDonationTokenAndOriginalPspReference(String donationToken, String originalPspReference) throws NullPointerException { + if (donationToken == null) { + throw new NullPointerException("No donationToken is found. The payments endpoint did not return a donationToken, please enable this in your Customer Area. See README."); + } + + session.setAttribute(PAYMENT_ORIGINAL_PSPREFERENCE, originalPspReference); + session.setAttribute(DONATION_TOKEN, donationToken); + } + + public String getDonationToken() throws NotFoundException { + var donationToken = session.getAttribute(DONATION_TOKEN); + if (donationToken == null) { + throw new NotFoundException("Could not find donationToken in the sessions"); + } + return (String) donationToken; + } + + public String getPaymentOriginalPspReference() throws NotFoundException { + var pspReference = session.getAttribute(PAYMENT_ORIGINAL_PSPREFERENCE); + if (pspReference == null) { + throw new NotFoundException("Could not find originalPspReference in the sessions"); + } + return (String) pspReference; + } +} \ No newline at end of file