Skip to content

Commit

Permalink
AD-294: Fix for DefaultAdyenCheckoutApiService
Browse files Browse the repository at this point in the history
  • Loading branch information
kpieloch committed Sep 23, 2024
1 parent 7e320a7 commit a9125f4
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.adyen.v6.actions.AbstractWaitableAction;
import com.adyen.v6.factory.AdyenPaymentServiceFactory;
import com.adyen.v6.service.AdyenCheckoutApiService;
import com.adyen.v6.util.AmountUtil;
import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.core.model.order.payment.PaymentInfoModel;
Expand Down Expand Up @@ -92,7 +93,7 @@ private String processOrderAuthorization(final OrderProcessModel process, final
return Transition.WAIT.toString();
}

BigDecimal remainingAmount = getAdyenPaymentService(order).calculateAmountWithTaxes(order);
BigDecimal remainingAmount = AmountUtil.calculateAmountWithTaxes(order);
for (final PaymentTransactionModel paymentTransactionModel : order.getPaymentTransactions()) {
if (!isTransactionAuthorized(paymentTransactionModel)) {
//A single not authorized transaction means not authorized
Expand Down Expand Up @@ -123,8 +124,4 @@ private String processOrderAuthorization(final OrderProcessModel process, final

return Transition.OK.toString();
}

public AdyenCheckoutApiService getAdyenPaymentService(final OrderModel orderModel) {
return adyenPaymentServiceFactory.createAdyenCheckoutApiService(orderModel.getStore());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.adyen.v6.factory.AdyenPaymentServiceFactory;
import com.adyen.v6.service.AdyenCheckoutApiService;
import com.adyen.v6.service.AdyenTransactionService;
import com.adyen.v6.util.AmountUtil;
import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.orderprocessing.model.OrderProcessModel;
Expand Down Expand Up @@ -76,7 +77,7 @@ public String execute(final OrderProcessModel process) {
order.setStatus(OrderStatus.PAYMENT_NOT_CAPTURED);
modelService.save(order);

BigDecimal remainingAmount = getAdyenPaymentService(order).calculateAmountWithTaxes(order);
BigDecimal remainingAmount = AmountUtil.calculateAmountWithTaxes(order);
for (final PaymentTransactionModel paymentTransactionModel : order.getPaymentTransactions()) {
boolean isRejected = AdyenTransactionService.getTransactionEntry(
paymentTransactionModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ public interface AdyenCheckoutApiService {
*/
TerminalAPIResponse sendSyncPosStatusRequest(CartData cartData, String serviceId) throws Exception;


BigDecimal calculateAmountWithTaxes(final AbstractOrderModel abstractOrderModel);

CreateCheckoutSessionResponse getPaymentSessionData(final CartData cartData) throws IOException, ApiException;

CreateCheckoutSessionResponse getPaymentSessionData(final Amount amount) throws IOException, ApiException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,4 @@ public TerminalAPIResponse sendSyncPosStatusRequest(CartData cartData, String or
LOG.debug(TerminalAPIGsonBuilder.create().toJson(terminalApiResponse));
return terminalApiResponse;
}

@Override
public BigDecimal calculateAmountWithTaxes(final AbstractOrderModel abstractOrderModel) {
final Double totalPrice = abstractOrderModel.getTotalPrice();
final Double totalTax = Boolean.TRUE.equals(abstractOrderModel.getNet()) ? abstractOrderModel.getTotalTax() : Double.valueOf(0d);
final BigDecimal totalPriceWithoutTaxBD = BigDecimal.valueOf(totalPrice == null ? 0d : totalPrice).setScale(2,
RoundingMode.HALF_EVEN);
return BigDecimal.valueOf(totalTax == null ? 0d : totalTax)
.setScale(2, RoundingMode.HALF_EVEN).add(totalPriceWithoutTaxBD);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.adyen.model.checkout.PaymentDetailsResponse;
import com.adyen.v6.factory.AdyenPaymentServiceFactory;
import com.adyen.v6.model.AdyenNotificationModel;
import com.adyen.v6.util.AmountUtil;
import de.hybris.platform.core.model.c2l.CurrencyModel;
import de.hybris.platform.core.model.order.AbstractOrderModel;
import de.hybris.platform.payment.dto.TransactionStatus;
Expand Down Expand Up @@ -204,7 +205,7 @@ private PaymentTransactionEntryModel createAuthorizationPaymentTransactionEntryM
transactionEntryModel.setTime(DateTime.now().toDate());
transactionEntryModel.setTransactionStatus(TransactionStatus.ACCEPTED.name());
transactionEntryModel.setTransactionStatusDetails(TransactionStatusDetails.SUCCESFULL.name());
transactionEntryModel.setAmount(getAdyenPaymentService().calculateAmountWithTaxes(abstractOrderModel));
transactionEntryModel.setAmount(AmountUtil.calculateAmountWithTaxes(abstractOrderModel));
transactionEntryModel.setCurrency(abstractOrderModel.getCurrency());

return transactionEntryModel;
Expand All @@ -225,7 +226,7 @@ private PaymentTransactionModel createPaymentTransaction(final String merchantCo
paymentTransactionModel.setOrder(abstractOrderModel);
paymentTransactionModel.setCurrency(abstractOrderModel.getCurrency());
paymentTransactionModel.setInfo(abstractOrderModel.getPaymentInfo());
paymentTransactionModel.setPlannedAmount(getAdyenPaymentService().calculateAmountWithTaxes(abstractOrderModel));
paymentTransactionModel.setPlannedAmount(AmountUtil.calculateAmountWithTaxes(abstractOrderModel));

return paymentTransactionModel;
}
Expand Down Expand Up @@ -291,7 +292,7 @@ private PaymentTransactionEntryModel createPaymentTransactionEntryModelFromResul
transactionEntryModel.setTime(DateTime.now().toDate());
transactionEntryModel.setTransactionStatus(getTransactionStatusForResultCode(resultCode));
transactionEntryModel.setTransactionStatusDetails("ResultCode: " + resultCode.getValue());
transactionEntryModel.setAmount(getAdyenPaymentService().calculateAmountWithTaxes(abstractOrderModel));
transactionEntryModel.setAmount(AmountUtil.calculateAmountWithTaxes(abstractOrderModel));
transactionEntryModel.setCurrency(abstractOrderModel.getCurrency());

return transactionEntryModel;
Expand All @@ -314,18 +315,14 @@ private String getTransactionStatusForResultCode(PaymentDetailsResponse.ResultCo
}

private boolean isPartialPayment(AdyenNotificationModel notificationItemModel, AbstractOrderModel abstractOrderModel) {
BigDecimal totalOrderAmount = getAdyenPaymentService().calculateAmountWithTaxes(abstractOrderModel);
BigDecimal totalOrderAmount = AmountUtil.calculateAmountWithTaxes(abstractOrderModel);
BigDecimal notificationAmount = notificationItemModel.getAmountValue();
if (notificationAmount == null) {
return false;
}
return totalOrderAmount.compareTo(notificationAmount) > 0;
}

public AdyenCheckoutApiService getAdyenPaymentService() {
return adyenPaymentServiceFactory.createAdyenCheckoutApiService(baseStoreService.getCurrentBaseStore());
}

public ModelService getModelService() {
return modelService;
}
Expand Down
10 changes: 10 additions & 0 deletions adyenv6core/src/com/adyen/v6/util/AmountUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.adyen.v6.util;

import com.adyen.model.checkout.Amount;
import de.hybris.platform.core.model.order.AbstractOrderModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class AmountUtil {

Expand All @@ -17,5 +19,13 @@ public static Amount createAmount(BigDecimal value, String currency) {
return amount;
}

public static BigDecimal calculateAmountWithTaxes(final AbstractOrderModel abstractOrderModel) {
final Double totalPrice = abstractOrderModel.getTotalPrice();
final Double totalTax = Boolean.TRUE.equals(abstractOrderModel.getNet()) ? abstractOrderModel.getTotalTax() : Double.valueOf(0d);
final BigDecimal totalPriceWithoutTaxBD = BigDecimal.valueOf(totalPrice == null ? 0d : totalPrice).setScale(2,
RoundingMode.HALF_EVEN);
return BigDecimal.valueOf(totalTax == null ? 0d : totalTax)
.setScale(2, RoundingMode.HALF_EVEN).add(totalPriceWithoutTaxBD);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ public void setUp() {

adyenCheckAuthorizationAction = new AdyenCheckAuthorizationAction(adyenPaymentServiceFactoryMock, baseStoreServiceMock);
adyenCheckAuthorizationAction.setModelService(modelServiceMock);
when(adyenCheckAuthorizationAction.getAdyenPaymentService(orderModelMock)).thenReturn(adyenCheckoutApiServiceMock);

when(baseStoreServiceMock.getCurrentBaseStore()).thenReturn(baseStoreModelMock);
when(adyenPaymentServiceFactoryMock.createAdyenCheckoutApiService(baseStoreModelMock)).thenReturn(adyenCheckoutApiServiceMock);
when(adyenCheckoutApiServiceMock.calculateAmountWithTaxes(orderModelMock)).thenReturn(new BigDecimal(10));
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void setUp() {

when(baseStoreServiceMock.getCurrentBaseStore()).thenReturn(baseStoreModelMock);
when(adyenPaymentServiceFactoryMock.createAdyenCheckoutApiService(baseStoreModelMock)).thenReturn(adyenCheckoutApiServiceMock);
when(adyenCheckoutApiServiceMock.calculateAmountWithTaxes(orderModelMock)).thenReturn(new BigDecimal(10));

}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ public void setUp() {
when(configurationServiceMock.getConfiguration()).thenReturn(configurationMock);
when(baseStoreServiceMock.getCurrentBaseStore()).thenReturn(baseStoreModelMock);
when(adyenPaymentServiceFactoryMock.createAdyenCheckoutApiService(baseStoreModelMock)).thenReturn(adyenCheckoutApiServiceMock);
when(adyenCheckoutApiServiceMock.calculateAmountWithTaxes(orderModel)).thenReturn(new BigDecimal(10));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public void setUp() {
when(modelServiceMock.create(PaymentTransactionEntryModel.class)).thenReturn(new PaymentTransactionEntryModel());
when(baseStoreServiceMock.getCurrentBaseStore()).thenReturn(baseStoreModelMock);
when(adyenPaymentServiceFactoryMock.createAdyenCheckoutApiService(baseStoreModelMock)).thenReturn(adyenCheckoutApiServiceMock);
when(adyenTransactionService.getAdyenPaymentService()).thenReturn(adyenCheckoutApiServiceMock);
when(paymentTransactionModel.getEntries()).thenReturn(new ArrayList<>());
when(modelServiceMock.create(PaymentTransactionModel.class)).thenReturn(paymentTransactionModel);

Expand Down Expand Up @@ -135,7 +134,6 @@ public void testCreateCapturedTransactionFromNotification() {
@Test
public void testAuthorizeOrderModel() {
OrderModel orderModel = createDummyOrderModel();
when(adyenCheckoutApiServiceMock.calculateAmountWithTaxes(orderModel)).thenReturn(new BigDecimal(10));

PaymentTransactionModel paymentTransactionModel = adyenTransactionService.authorizeOrderModel(orderModel, MERCHANT_REFERENCE, PSP_REFERENCE);

Expand Down

0 comments on commit a9125f4

Please sign in to comment.