Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AD-315 I can't pay with any method if the first payment attempt is ca… #457

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.adyen.commerce.controllers;

import com.adyen.commerce.constants.AdyenoccConstants;
import com.adyen.v6.facades.AdyenCheckoutFacade;
import de.hybris.platform.commerceservices.request.mapping.annotation.ApiVersion;
import de.hybris.platform.order.InvalidCartException;
import de.hybris.platform.order.exceptions.CalculationException;
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.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = AdyenoccConstants.ADYEN_USER_PREFIX)
@ApiVersion("v2")
@Tag(name = "Adyen")
public class PaymentCanceledController {

@Autowired
private AdyenCheckoutFacade adyenCheckoutFacade;

@Secured({"ROLE_CUSTOMERGROUP", "ROLE_CLIENT", "ROLE_CUSTOMERMANAGERGROUP", "ROLE_TRUSTED_CLIENT"})
@PostMapping(value = "/payment-canceled/{orderCode}")
@Operation(operationId = "paymentCanceled", summary = "Handle payment canceled request", description =
"Restores cart from order code and data in session")
@ApiBaseSiteIdUserIdAndCartIdParam
public ResponseEntity<Void> onCancel(@PathVariable String orderCode) throws InvalidCartException, CalculationException {
adyenCheckoutFacade.restoreCartFromOrderOCC(orderCode);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
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 org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -91,15 +88,6 @@ public ResponseEntity<String> onAdditionalDetails(@RequestBody PaymentDetailsReq
return ResponseEntity.ok(response);
}

@Secured({"ROLE_CUSTOMERGROUP", "ROLE_CLIENT", "ROLE_CUSTOMERMANAGERGROUP", "ROLE_TRUSTED_CLIENT"})
@PostMapping(value = "/payment-canceled")
@Operation(operationId = "paymentCanceled", summary = "Handle payment canceled request", description =
"Restores cart from order code and data in session")
@ApiBaseSiteIdUserIdAndCartIdParam
public ResponseEntity<Void> onCancel() throws InvalidCartException, CalculationException {
super.handleCancel();
return ResponseEntity.ok().build();
}

@Override
public String getPaymentRedirectReturnUrl() {
Expand Down
1 change: 1 addition & 0 deletions adyenv6core/resources/adyenv6core-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
<property name="userFacade" ref="userFacade"/>
<property name="configurationService" ref="configurationService"/>
<property name="adyenMerchantAccountStrategy" ref="adyenMerchantStrategy"/>
<property name="adyenOrderFacade" ref="adyenOrderFacade"/>
</bean>

<alias name="defaultAdyenCheckoutApiFacade" alias="adyenCheckoutApiFacade" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public OrderData placeOrderWithPayment(final HttpServletRequest request, final C
|| PaymentResponse.ResultCodeEnum.IDENTIFYSHOPPER == paymentResponse.getResultCode()
|| PaymentResponse.ResultCodeEnum.PRESENTTOSHOPPER == paymentResponse.getResultCode()) {
LOGGER.info("Placing pending order");
placePendingOrder(paymentResponse.getResultCode().getValue());
OrderData orderData = placePendingOrder(paymentResponse.getResultCode().getValue());
paymentResponse.setMerchantReference(orderData.getCode());
throw new AdyenNonAuthorizedPaymentException(paymentResponse);
}
if (PaymentResponse.ResultCodeEnum.AUTHORISED == paymentResponse.getResultCode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ public interface AdyenCheckoutFacade {

void restoreCartFromOrderCodeInSession() throws InvalidCartException, CalculationException;

void restoreCartFromOrderOCC(String orderCode) throws CalculationException, InvalidCartException;

String getClientKey();

CheckoutConfigDTO getCheckoutConfig() throws ApiException;
Expand Down
3 changes: 3 additions & 0 deletions adyenv6core/src/com/adyen/v6/facades/AdyenOrderFacade.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.adyen.v6.facades;

import de.hybris.platform.core.model.order.OrderModel;

public interface AdyenOrderFacade {
String getPaymentStatus(final String orderCode, final String sessionGuid);
String getPaymentStatusOCC(final String code);
String getOrderCodeForGUID(final String orderGUID, final String sessionGuid);
OrderModel getOrderModelForCodeOCC(String code);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.adyen.v6.exceptions.AdyenNonAuthorizedPaymentException;
import com.adyen.v6.facades.AdyenCheckoutFacade;
import com.adyen.v6.facades.AdyenExpressCheckoutFacade;
import com.adyen.v6.facades.AdyenOrderFacade;
import com.adyen.v6.factory.AdyenPaymentServiceFactory;
import com.adyen.v6.forms.AddressForm;
import com.adyen.v6.forms.AdyenPaymentForm;
Expand Down Expand Up @@ -184,6 +185,7 @@ public class DefaultAdyenCheckoutFacade implements AdyenCheckoutFacade {
private I18NFacade i18NFacade;
private ConfigurationService configurationService;
private AdyenMerchantAccountStrategy adyenMerchantAccountStrategy;
private AdyenOrderFacade adyenOrderFacade;

public static final Logger LOGGER = Logger.getLogger(DefaultAdyenCheckoutFacade.class);

Expand Down Expand Up @@ -1554,11 +1556,30 @@ protected void restoreCartFromOrder(String orderCode) throws CalculationExceptio
LOGGER.info("Restoring cart from order");

OrderModel orderModel = getOrderRepository().getOrderModel(orderCode);

if (orderModel == null) {
LOGGER.error("Could not restore cart to session, order with code '" + orderCode + "' not found!");
return;
}

restoreCartFromOrderInternal(orderModel);
}

public void restoreCartFromOrderOCC(String orderCode) throws CalculationException, InvalidCartException {
LOGGER.info("Restoring cart from order");

OrderModel orderModel = adyenOrderFacade.getOrderModelForCodeOCC(orderCode);

if (orderModel == null) {
LOGGER.error("Could not restore cart to session, order with code '" + orderCode + "' not found!");
return;
}

restoreCartFromOrderInternal(orderModel);
}

protected void restoreCartFromOrderInternal(final OrderModel orderModel) throws CalculationException, InvalidCartException{

// Get cart from session
CartModel cartModel;
if (getCartService().hasSessionCart()) {
Expand All @@ -1573,7 +1594,7 @@ protected void restoreCartFromOrder(String orderCode) throws CalculationExceptio
Boolean isAnonymousCheckout = getCheckoutCustomerStrategy().isAnonymousCheckout();

if (!isAnonymousCheckout && hasUserContextChanged(orderModel, cartModel)) {
throw new InvalidCartException("Cart from order '" + orderCode + "' not restored to session, since user or store in session changed.");
throw new InvalidCartException("Cart from order '" + orderModel.getCode() + "' not restored to session, since user or store in session changed.");
}

//Populate cart entries
Expand Down Expand Up @@ -1882,4 +1903,8 @@ protected UserFacade getUserFacade() {
public void setAdyenMerchantAccountStrategy(AdyenMerchantAccountStrategy adyenMerchantAccountStrategy) {
this.adyenMerchantAccountStrategy = adyenMerchantAccountStrategy;
}

public void setAdyenOrderFacade(AdyenOrderFacade adyenOrderFacade) {
this.adyenOrderFacade = adyenOrderFacade;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String getPaymentStatus(final String orderCode, final String sessionGuid)

@Override
public String getPaymentStatusOCC(final String code) {
final OrderModel orderModel = getOrderModelForCodeOCC(code);
final OrderModel orderModel = getOrderModelForCodeOrGuidOCC(code);

return getPaymentStatusForOrder(orderModel);
}
Expand Down Expand Up @@ -67,7 +67,19 @@ protected String getPaymentStatusForOrder(final OrderModel orderModel) {
return getStatus(paymentTransactions);
}

protected OrderModel getOrderModelForCodeOCC(String code) {
public OrderModel getOrderModelForCodeOCC(String code) {
BaseStoreModel currentBaseStore = baseStoreService.getCurrentBaseStore();
final OrderModel orderModel;

orderModel = customerAccountService.getOrderForCode((CustomerModel) userService.getCurrentUser(), code, currentBaseStore);

if (orderModel == null) {
throw new UnknownIdentifierException(String.format(ORDER_NOT_FOUND_FOR_USER_AND_BASE_STORE, code));
}
return orderModel;
}

protected OrderModel getOrderModelForCodeOrGuidOCC(String code) {
BaseStoreModel currentBaseStore = baseStoreService.getCurrentBaseStore();
final OrderModel orderModel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ private OCCPlaceOrderResponse executeAction(PaymentResponse paymentsResponse) {
placeOrderResponse.setPaymentsResponse(paymentsResponse);
placeOrderResponse.setExecuteAction(true);
placeOrderResponse.setPaymentsAction(paymentsResponse.getAction());
placeOrderResponse.setOrderNumber(paymentsResponse.getMerchantReference());
return placeOrderResponse;
}

Expand Down