Skip to content

Commit

Permalink
Fix Giving integration-example
Browse files Browse the repository at this point in the history
Refactor: HttpSession in @service class to save the donation token and original pspreference
  • Loading branch information
Kwok-he-Chu committed Dec 15, 2023
1 parent c83a306 commit c55a458
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
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;
import jakarta.servlet.http.HttpServletRequest;
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;
Expand All @@ -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";
Expand Down Expand Up @@ -59,23 +63,13 @@ public CheckoutResource(ApplicationProperty applicationProperty) {
@PostMapping("/donations")
public ResponseEntity<DonationPaymentResponse> 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());
Expand Down Expand Up @@ -148,14 +142,8 @@ public ResponseEntity<PaymentResponse> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit c55a458

Please sign in to comment.