Skip to content

Commit

Permalink
Merge pull request #484 from Adyen/feature/AD-342
Browse files Browse the repository at this point in the history
AD-342 Develop Backend Logic for Place Order Endpoints to Handle PayP…
  • Loading branch information
pjaneta authored Nov 26, 2024
2 parents f0d2ed8 + ee1ab1a commit d329895
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.adyen.commerce.constants.AdyenoccConstants;
import com.adyen.commerce.request.GooglePayExpressCartRequest;
import com.adyen.commerce.request.GooglePayExpressPDPRequest;
import com.adyen.commerce.request.PlaceOrderRequest;
import com.adyen.commerce.resolver.PaymentRedirectReturnUrlResolver;
import com.adyen.commerce.response.OCCPlaceOrderResponse;
import com.adyen.model.checkout.CheckoutPaymentMethod;
Expand Down Expand Up @@ -55,9 +54,7 @@ public class GooglePayExpressCheckoutController extends ExpressCheckoutControlle
public ResponseEntity<String> googlePayCartExpressCheckoutPDP(final HttpServletRequest request, @RequestBody String googlePayExpressPDPRequestString) throws Exception {
GooglePayExpressPDPRequest googlePayExpressPDPRequest = objectMapper.readValue(googlePayExpressPDPRequestString, GooglePayExpressPDPRequest.class);

PaymentRequest paymentRequest = new PaymentRequest();
googlePayExpressPDPRequest.getGooglePayDetails().setType(GooglePayDetails.TypeEnum.GOOGLEPAY);
paymentRequest.setPaymentMethod(new CheckoutPaymentMethod(googlePayExpressPDPRequest.getGooglePayDetails()));
PaymentRequest paymentRequest = getPaymentRequest(googlePayExpressPDPRequest);

OCCPlaceOrderResponse placeOrderResponse = handlePayment(request, paymentRequest, Adyenv6coreConstants.PAYMENT_METHOD_GOOGLE_PAY, googlePayExpressPDPRequest.getAddressData(), googlePayExpressPDPRequest.getProductCode(), true);
String response = objectMapper.writeValueAsString(placeOrderResponse);
Expand All @@ -71,15 +68,21 @@ public ResponseEntity<String> googlePayCartExpressCheckoutPDP(final HttpServletR
@ApiBaseSiteIdUserIdAndCartIdParam
public ResponseEntity<String> googlePayCartExpressCheckoutCart(final HttpServletRequest request, @RequestBody String googlePayExpressCartRequestString) throws Exception {
GooglePayExpressCartRequest googlePayExpressCartRequest = objectMapper.readValue(googlePayExpressCartRequestString, GooglePayExpressCartRequest.class);
PaymentRequest paymentRequest = new PaymentRequest();
googlePayExpressCartRequest.getGooglePayDetails().setType(GooglePayDetails.TypeEnum.GOOGLEPAY);
paymentRequest.setPaymentMethod(new CheckoutPaymentMethod(googlePayExpressCartRequest.getGooglePayDetails()));
PaymentRequest paymentRequest = getPaymentRequest(googlePayExpressCartRequest);

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

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;
}

@Override
public CartFacade getCartFacade() {
return cartFacade;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.adyen.commerce.controllers.expresscheckout;

import com.adyen.commerce.constants.AdyenoccConstants;
import com.adyen.commerce.request.PayPalExpressCartRequest;
import com.adyen.commerce.request.PayPalExpressPDPRequest;
import com.adyen.commerce.resolver.PaymentRedirectReturnUrlResolver;
import com.adyen.commerce.response.OCCPlaceOrderResponse;
import com.adyen.model.checkout.CheckoutPaymentMethod;
import com.adyen.model.checkout.PayPalDetails;
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/paypal")
@ApiVersion("v2")
@Tag(name = "Adyen")
public class PayPalExpressCheckoutController 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 = "placeOrderPayPalExpressPDP", summary = "Handle PayPalExpress place order request", description =
"Places order based on request data")
@ApiBaseSiteIdUserIdAndCartIdParam
public ResponseEntity<String> PayPalCartExpressCheckoutPDP(final HttpServletRequest request, @RequestBody String PayPalExpressPDPRequestString) throws Exception {
PayPalExpressPDPRequest payPalExpressPDPRequest = objectMapper.readValue(PayPalExpressPDPRequestString, PayPalExpressPDPRequest.class);

PaymentRequest paymentRequest = getPaymentRequest(payPalExpressPDPRequest);

OCCPlaceOrderResponse placeOrderResponse = handlePayment(request, paymentRequest, Adyenv6coreConstants.PAYMENT_METHOD_PAYPAL, payPalExpressPDPRequest.getAddressData(), payPalExpressPDPRequest.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 = "placeOrderPayPalExpressCart", summary = "Handle PayPalExpress place order request", description =
"Places order based on request data")
@ApiBaseSiteIdUserIdAndCartIdParam
public ResponseEntity<String> PayPalCartExpressCheckoutCart(final HttpServletRequest request, @RequestBody String PayPalExpressCartRequestString) throws Exception {
PayPalExpressCartRequest payPalExpressCartRequest = objectMapper.readValue(PayPalExpressCartRequestString, PayPalExpressCartRequest.class);
PaymentRequest paymentRequest = getPaymentRequest(payPalExpressCartRequest);

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

private static <T extends PayPalExpressCartRequest> PaymentRequest getPaymentRequest(T request) {
PaymentRequest paymentRequest = new PaymentRequest();
PayPalDetails payPalDetails = request.getPayPalDetails();
payPalDetails.setType(PayPalDetails.TypeEnum.PAYPAL);
paymentRequest.setPaymentMethod(new CheckoutPaymentMethod(payPalDetails));
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.PayPalDetails;
import de.hybris.platform.commercefacades.user.data.AddressData;

import java.io.Serializable;

public class PayPalExpressCartRequest implements Serializable {
private PayPalDetails payPalDetails;
private AddressData addressData;

public PayPalDetails getPayPalDetails() {
return payPalDetails;
}

public void setPayPalDetails(PayPalDetails payPalDetails) {
this.payPalDetails = payPalDetails;
}

public AddressData getAddressData() {
return addressData;
}

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

public class PayPalExpressPDPRequest extends PayPalExpressCartRequest {
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
@@ -0,0 +1,72 @@
package com.adyen.v6.controllers.checkout;

import com.adyen.model.checkout.CheckoutPaymentMethod;
import com.adyen.model.checkout.PayPalDetails;
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.PayPalExpressCartRequest;
import com.adyen.v6.request.PayPalExpressPDPRequest;
import de.hybris.platform.acceleratorstorefrontcommons.security.GUIDCookieStrategy;
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.*;

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

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

@Autowired
private AdyenExpressCheckoutFacade adyenExpressCheckoutFacade;

@Autowired
private GUIDCookieStrategy guidCookieStrategy;

@PostMapping("PDP")
public ResponseEntity payPalExpressPDP(final HttpServletRequest request, final HttpServletResponse response, @RequestBody PayPalExpressPDPRequest paypalExpressPDPRequest) throws Exception {

PaymentRequest paymentRequest = getPaymentRequest(paypalExpressPDPRequest);

PaymentResponse paymentsResponse = adyenExpressCheckoutFacade.expressCheckoutPDP(paypalExpressPDPRequest.getProductCode(),
paymentRequest, Adyenv6coreConstants.PAYMENT_METHOD_PAYPAL, paypalExpressPDPRequest.getAddressData(), request);

guidCookieStrategy.setCookie(request, response);

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

@PostMapping("cart")
public ResponseEntity paypalCartExpressCheckout(final HttpServletRequest request, final HttpServletResponse response, @RequestBody PayPalExpressCartRequest paypalExpressCartRequest) throws Exception {

PaymentRequest paymentRequest = getPaymentRequest(paypalExpressCartRequest);

PaymentResponse paymentsResponse = adyenExpressCheckoutFacade.expressCheckoutCart(paymentRequest, Adyenv6coreConstants.PAYMENT_METHOD_PAYPAL,
paypalExpressCartRequest.getAddressData(), request);

guidCookieStrategy.setCookie(request, response);

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

private static <T extends PayPalExpressCartRequest> PaymentRequest getPaymentRequest(T request) {
PaymentRequest paymentRequest = new PaymentRequest();
PayPalDetails paypalDetails = request.getPayPalDetails();
paypalDetails.setType(PayPalDetails.TypeEnum.PAYPAL);
paymentRequest.setPaymentMethod(new CheckoutPaymentMethod(paypalDetails));
return paymentRequest;
}

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = Exception.class)
public void adyenComponentExceptionHandler(Exception e) {
LOG.error("Exception during PaypalExpress processing", e);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.adyen.v6.request;

import java.io.Serializable;

public class GooglePayExpressPDPRequest extends GooglePayExpressCartRequest {
private String productCode;

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

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

import java.io.Serializable;

public class PayPalExpressCartRequest implements Serializable {
private PayPalDetails payPalDetails;
private AddressData addressData;

public PayPalDetails getPayPalDetails() {
return payPalDetails;
}

public void setPayPalDetails(PayPalDetails payPalDetails) {
this.payPalDetails = payPalDetails;
}

public AddressData getAddressData() {
return addressData;
}

public void setAddressData(AddressData addressData) {
this.addressData = addressData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.adyen.v6.request;

public class PayPalExpressPDPRequest extends PayPalExpressCartRequest {
private String productCode;

public String getProductCode() {
return productCode;
}

public void setProductCode(String productCode) {
this.productCode = productCode;
}
}

0 comments on commit d329895

Please sign in to comment.