Skip to content

Commit

Permalink
Merge pull request #477 from Adyen/feature/AD-344
Browse files Browse the repository at this point in the history
AD-344 Develop Backend Logic for Place Order Endpoints to Handle Appl…
  • Loading branch information
pjaneta authored Nov 18, 2024
2 parents c9c5f52 + c89799b commit dfb6a7a
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 206 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.adyen.commerce.controllers.expresscheckout;

import com.adyen.commerce.constants.AdyenoccConstants;
import com.adyen.commerce.request.ApplePayExpressCartRequest;
import com.adyen.commerce.request.ApplePayExpressPDPRequest;
import com.adyen.commerce.resolver.PaymentRedirectReturnUrlResolver;
import com.adyen.commerce.response.OCCPlaceOrderResponse;
import com.adyen.model.checkout.ApplePayDetails;
import com.adyen.model.checkout.CheckoutPaymentMethod;
import com.adyen.model.checkout.PaymentRequest;
import com.adyen.v6.constants.Adyenv6coreConstants;
import com.adyen.v6.facades.AdyenExpressCheckoutFacade;
import de.hybris.platform.commercefacades.order.CartFacade;
import de.hybris.platform.commerceservices.request.mapping.annotation.ApiVersion;
import de.hybris.platform.commerceservices.strategies.CheckoutCustomerStrategy;
import de.hybris.platform.webservicescommons.swagger.ApiBaseSiteIdUserIdAndCartIdParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = AdyenoccConstants.ADYEN_USER_CART_PREFIX + "/express-checkout/apple")
@ApiVersion("v2")
@Tag(name = "Adyen")
public class ApplePayExpressCheckoutController extends ExpressCheckoutControllerBase {

@Autowired
private CartFacade cartFacade;

@Autowired
private CheckoutCustomerStrategy checkoutCustomerStrategy;

@Autowired
private AdyenExpressCheckoutFacade adyenExpressCheckoutFacade;

@Autowired
private PaymentRedirectReturnUrlResolver paymentRedirectReturnUrlResolver;


@Secured({"ROLE_CUSTOMERGROUP", "ROLE_CLIENT", "ROLE_CUSTOMERMANAGERGROUP", "ROLE_TRUSTED_CLIENT"})
@PostMapping(value = "/PDP", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(operationId = "placeOrderApplePayExpressPDP", summary = "Handle applePayExpress place order request", description =
"Places order based on request data")
@ApiBaseSiteIdUserIdAndCartIdParam
public ResponseEntity<String> applePayPDPExpressCheckout(final HttpServletRequest request, @RequestBody String applePayExpressPDPStringRequest) throws Exception {
ApplePayExpressPDPRequest applePayExpressPDPRequest = objectMapper.readValue(applePayExpressPDPStringRequest, ApplePayExpressPDPRequest.class);

PaymentRequest paymentRequest = getPaymentRequest(applePayExpressPDPRequest);

OCCPlaceOrderResponse placeOrderResponse = handlePayment(request, paymentRequest, Adyenv6coreConstants.PAYMENT_METHOD_APPLEPAY, applePayExpressPDPRequest.getAddressData(), applePayExpressPDPRequest.getProductCode(), true);
String response = objectMapper.writeValueAsString(placeOrderResponse);
return ResponseEntity.ok(response);
}

@Secured({"ROLE_CUSTOMERGROUP", "ROLE_CLIENT", "ROLE_CUSTOMERMANAGERGROUP", "ROLE_TRUSTED_CLIENT"})
@PostMapping(value = "/cart", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(operationId = "placeOrderApplePayExpressCart", summary = "Handle applePayExpress place order request", description =
"Places order based on request data")
@ApiBaseSiteIdUserIdAndCartIdParam
public ResponseEntity<String> applePayCartExpressCheckout(final HttpServletRequest request, @RequestBody String applePayExpressCartStringRequest) throws Exception {
ApplePayExpressCartRequest applePayExpressCartRequest = objectMapper.readValue(applePayExpressCartStringRequest, ApplePayExpressCartRequest.class);

PaymentRequest paymentRequest = getPaymentRequest(applePayExpressCartRequest);

OCCPlaceOrderResponse placeOrderResponse = handlePayment(request, paymentRequest, Adyenv6coreConstants.PAYMENT_METHOD_APPLEPAY, applePayExpressCartRequest.getAddressData(), null, false);
String response = objectMapper.writeValueAsString(placeOrderResponse);
return ResponseEntity.ok(response);
}

private static <T extends ApplePayExpressCartRequest> PaymentRequest getPaymentRequest(T request) {
PaymentRequest paymentRequest = new PaymentRequest();
ApplePayDetails applePayDetails = request.getApplePayDetails();
applePayDetails.setType(ApplePayDetails.TypeEnum.APPLEPAY);
paymentRequest.setPaymentMethod(new CheckoutPaymentMethod(applePayDetails));
return paymentRequest;
}

@Override
public CartFacade getCartFacade() {
return cartFacade;
}

@Override
public CheckoutCustomerStrategy getCheckoutCustomerStrategy() {
return checkoutCustomerStrategy;
}

@Override
public String getPaymentRedirectReturnUrl() {
return paymentRedirectReturnUrlResolver.resolvePaymentRedirectReturnUrl();
}

@Override
public AdyenExpressCheckoutFacade getAdyenCheckoutApiFacade() {
return adyenExpressCheckoutFacade;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.adyen.commerce.request;

import com.adyen.model.checkout.ApplePayDetails;
import de.hybris.platform.commercefacades.user.data.AddressData;

import java.io.Serializable;

public class ApplePayExpressCartRequest implements Serializable {
private AddressData addressData;
private ApplePayDetails applePayDetails;

public AddressData getAddressData() {
return addressData;
}

public void setAddressData(AddressData addressData) {
this.addressData = addressData;
}

public ApplePayDetails getApplePayDetails() {
return applePayDetails;
}

public void setApplePayDetails(ApplePayDetails applePayDetails) {
this.applePayDetails = applePayDetails;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.adyen.commerce.request;

public class ApplePayExpressPDPRequest extends ApplePayExpressCartRequest {
private String productCode;

public String getProductCode() {
return productCode;
}

public void setProductCode(String productCode) {
this.productCode = productCode;
}
}
Original file line number Diff line number Diff line change
@@ -1,68 +1,73 @@
package com.adyen.v6.controllers.checkout;

import com.adyen.model.checkout.ApplePayDetails;
import com.adyen.model.checkout.CheckoutPaymentMethod;
import com.adyen.model.checkout.PaymentRequest;
import com.adyen.model.checkout.PaymentResponse;
import com.adyen.v6.constants.Adyenv6coreConstants;
import com.adyen.v6.facades.AdyenExpressCheckoutFacade;
import com.adyen.v6.request.ApplePayExpressCartRequest;
import com.adyen.v6.request.ApplePayExpressPDPRequest;
import de.hybris.platform.acceleratorstorefrontcommons.constants.WebConstants;
import de.hybris.platform.acceleratorstorefrontcommons.security.GUIDCookieStrategy;
import de.hybris.platform.servicelayer.session.SessionService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/express-checkout/apple/")
public class AdyenApplePayExpressCheckoutController {
private static final Logger LOG = Logger.getLogger(AdyenApplePayExpressCheckoutController.class);

@Autowired
private AdyenExpressCheckoutFacade adyenExpressCheckoutFacade;

@Autowired
private SessionService sessionService;

@Autowired
private GUIDCookieStrategy guidCookieStrategy;

@PostMapping("/expressCheckout/applePayPDP")
@PostMapping("PDP")
public ResponseEntity applePayExpressPDP(final HttpServletRequest request, final HttpServletResponse response, @RequestBody ApplePayExpressPDPRequest applePayExpressPDPRequest) throws Exception {

PaymentResponse paymentsResponse = adyenExpressCheckoutFacade.appleExpressPDPCheckout(applePayExpressPDPRequest.getAddressData(), applePayExpressPDPRequest.getProductCode(),
applePayExpressPDPRequest.getAdyenApplePayMerchantIdentifier(), applePayExpressPDPRequest.getAdyenApplePayMerchantName(),
applePayExpressPDPRequest.getApplePayToken(), request);
PaymentRequest paymentRequest = getPaymentRequest(applePayExpressPDPRequest);

PaymentResponse paymentsResponse = adyenExpressCheckoutFacade.expressCheckoutPDP(applePayExpressPDPRequest.getProductCode(),
paymentRequest, Adyenv6coreConstants.PAYMENT_METHOD_APPLEPAY, applePayExpressPDPRequest.getAddressData(), request);

guidCookieStrategy.setCookie(request, response);
sessionService.setAttribute(WebConstants.ANONYMOUS_CHECKOUT, Boolean.TRUE);

return new ResponseEntity<>(paymentsResponse, HttpStatus.OK);
}

@PostMapping("/expressCheckout/cart")
@PostMapping("cart")
public ResponseEntity cartExpressCheckout(final HttpServletRequest request, final HttpServletResponse response, @RequestBody ApplePayExpressCartRequest applePayExpressCartRequest) throws Exception {

PaymentResponse paymentsResponse = adyenExpressCheckoutFacade.appleEexpressCartCheckout(applePayExpressCartRequest.getAddressData(),
applePayExpressCartRequest.getAdyenApplePayMerchantIdentifier(), applePayExpressCartRequest.getAdyenApplePayMerchantName(),
applePayExpressCartRequest.getApplePayToken(), request);
PaymentRequest paymentRequest = getPaymentRequest(applePayExpressCartRequest);

PaymentResponse paymentsResponse = adyenExpressCheckoutFacade.expressCheckoutCart(paymentRequest, Adyenv6coreConstants.PAYMENT_METHOD_APPLEPAY,
applePayExpressCartRequest.getAddressData(), request);

guidCookieStrategy.setCookie(request, response);
sessionService.setAttribute(WebConstants.ANONYMOUS_CHECKOUT, Boolean.TRUE);

return new ResponseEntity<>(paymentsResponse, HttpStatus.OK);
}

private static <T extends ApplePayExpressCartRequest> PaymentRequest getPaymentRequest(T request) {
PaymentRequest paymentRequest = new PaymentRequest();
ApplePayDetails applePayDetails = request.getApplePayDetails();
applePayDetails.setType(ApplePayDetails.TypeEnum.APPLEPAY);
paymentRequest.setPaymentMethod(new CheckoutPaymentMethod(applePayDetails));
return paymentRequest;
}


@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = Exception.class)
public String adyenComponentExceptionHandler(Exception e) {
public void adyenComponentExceptionHandler(Exception e) {
LOG.error("Exception during ApplePayExpress processing", e);
return "Exception during ApplePayExpress processing";
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.adyen.v6.controllers.checkout;

import com.adyen.model.checkout.CheckoutPaymentMethod;
import com.adyen.model.checkout.GooglePayDetails;
import com.adyen.model.checkout.PaymentRequest;
import com.adyen.model.checkout.PaymentResponse;
import com.adyen.v6.constants.Adyenv6coreConstants;
import com.adyen.v6.facades.AdyenExpressCheckoutFacade;
import com.adyen.v6.request.GooglePayExpressCartRequest;
import com.adyen.v6.request.GooglePayExpressPDPRequest;
import de.hybris.platform.acceleratorstorefrontcommons.constants.WebConstants;
import de.hybris.platform.acceleratorstorefrontcommons.security.GUIDCookieStrategy;
import de.hybris.platform.servicelayer.session.SessionService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -28,42 +27,43 @@ public class AdyenGooglePayExpressCheckoutController {
@Autowired
private AdyenExpressCheckoutFacade adyenExpressCheckoutFacade;

@Autowired
private SessionService sessionService;

@Autowired
private GUIDCookieStrategy guidCookieStrategy;

@PostMapping("PDP")
public ResponseEntity googlePayExpressPDP(final HttpServletRequest request, final HttpServletResponse response, @RequestBody GooglePayExpressPDPRequest googlePayExpressPDPRequest) throws Exception {

PaymentRequest paymentRequest = new PaymentRequest();
paymentRequest.setPaymentMethod(new CheckoutPaymentMethod(googlePayExpressPDPRequest.getGooglePayDetails()));
PaymentRequest paymentRequest = getPaymentRequest(googlePayExpressPDPRequest);

PaymentResponse paymentsResponse = adyenExpressCheckoutFacade.expressCheckoutPDP(googlePayExpressPDPRequest.getProductCode(),
paymentRequest, Adyenv6coreConstants.PAYMENT_METHOD_GOOGLE_PAY, googlePayExpressPDPRequest.getAddressData(), request);

guidCookieStrategy.setCookie(request, response);
sessionService.setAttribute(WebConstants.ANONYMOUS_CHECKOUT, Boolean.TRUE);

return new ResponseEntity<>(paymentsResponse, HttpStatus.OK);
}

@PostMapping("cart")
public ResponseEntity googlePayCartExpressCheckout(final HttpServletRequest request, final HttpServletResponse response, @RequestBody GooglePayExpressCartRequest googlePayExpressCartRequest) throws Exception {

PaymentRequest paymentRequest = new PaymentRequest();
paymentRequest.setPaymentMethod(new CheckoutPaymentMethod(googlePayExpressCartRequest.getGooglePayDetails()));
PaymentRequest paymentRequest = getPaymentRequest(googlePayExpressCartRequest);

PaymentResponse paymentsResponse = adyenExpressCheckoutFacade.expressCheckoutCart(paymentRequest, Adyenv6coreConstants.PAYMENT_METHOD_GOOGLE_PAY,
googlePayExpressCartRequest.getAddressData(), request);

guidCookieStrategy.setCookie(request, response);
sessionService.setAttribute(WebConstants.ANONYMOUS_CHECKOUT, Boolean.TRUE);

return new ResponseEntity<>(paymentsResponse, HttpStatus.OK);
}

private static <T extends GooglePayExpressCartRequest> PaymentRequest getPaymentRequest(T request) {
PaymentRequest paymentRequest = new PaymentRequest();
GooglePayDetails googlePayDetails = request.getGooglePayDetails();
googlePayDetails.setType(GooglePayDetails.TypeEnum.GOOGLEPAY);
paymentRequest.setPaymentMethod(new CheckoutPaymentMethod(googlePayDetails));
return paymentRequest;
}

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = Exception.class)
public void adyenComponentExceptionHandler(Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.adyen.v6.request;

import com.adyen.model.checkout.ApplePayDetails;
import de.hybris.platform.commercefacades.user.data.AddressData;

import java.io.Serializable;

public class ApplePayExpressCartRequest implements Serializable {
private AddressData addressData;
private String adyenApplePayMerchantName;
private String adyenApplePayMerchantIdentifier;
private String applePayToken;
private ApplePayDetails applePayDetails;

public AddressData getAddressData() {
return addressData;
Expand All @@ -18,27 +17,11 @@ public void setAddressData(AddressData addressData) {
this.addressData = addressData;
}

public String getAdyenApplePayMerchantName() {
return adyenApplePayMerchantName;
public ApplePayDetails getApplePayDetails() {
return applePayDetails;
}

public void setAdyenApplePayMerchantName(String adyenApplePayMerchantName) {
this.adyenApplePayMerchantName = adyenApplePayMerchantName;
}

public String getAdyenApplePayMerchantIdentifier() {
return adyenApplePayMerchantIdentifier;
}

public void setAdyenApplePayMerchantIdentifier(String adyenApplePayMerchantIdentifier) {
this.adyenApplePayMerchantIdentifier = adyenApplePayMerchantIdentifier;
}

public String getApplePayToken() {
return applePayToken;
}

public void setApplePayToken(String applePayToken) {
this.applePayToken = applePayToken;
public void setApplePayDetails(ApplePayDetails applePayDetails) {
this.applePayDetails = applePayDetails;
}
}
Loading

0 comments on commit dfb6a7a

Please sign in to comment.