Skip to content

Commit

Permalink
Merge pull request #456 from Adyen/bugfix/AD-294_PaymentUtils
Browse files Browse the repository at this point in the history
Bugfix/ad 294 payment utils
  • Loading branch information
kpieloch authored Oct 2, 2024
2 parents be71cd9 + 24ee85f commit 8ef0f20
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 35 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 @@ protected String processOrderAuthorization(final OrderProcessModel process, fina
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 @@ protected String processOrderAuthorization(final OrderProcessModel process, fina

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 @@ protected PaymentTransactionEntryModel createAuthorizationPaymentTransactionEntr
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 @@ protected PaymentTransactionModel createPaymentTransaction(final String merchant
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 @@ protected PaymentTransactionEntryModel createPaymentTransactionEntryModelFromRes
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 @@ protected String getTransactionStatusForResultCode(PaymentDetailsResponse.Result
}

protected 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
44 changes: 43 additions & 1 deletion 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 @@ -13,9 +15,49 @@ public static Amount createAmount(BigDecimal value, String currency) {
Assert.isTrue(StringUtils.isNotBlank(currency), "Currency cannot be null or empty");
Amount amount = new Amount();
amount.setCurrency(currency);
amount.setValue(value.movePointRight(2).longValue());
int scale = getDecimalPlaces(currency);
amount.setValue(BigDecimal.TEN.pow(scale).multiply(value.setScale(scale, RoundingMode.HALF_UP)).longValue());
return amount;
}

public static int getDecimalPlaces(String currency) {
switch (currency) {
case "CVE":
case "DJF":
case "GNF":
case "IDR":
case "JPY":
case "KMF":
case "KRW":
case "PYG":
case "RWF":
case "UGX":
case "VND":
case "VUV":
case "XAF":
case "XOF":
case "XPF":
return 0;
case "BHD":
case "IQD":
case "JOD":
case "KWD":
case "LYD":
case "OMR":
case "TND":
return 3;
default:
return 2;
}
}

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 8ef0f20

Please sign in to comment.