diff --git a/backend/app/src/main/java/eu/viandeendirect/domains/payment/StripePaymentManager.java b/backend/app/src/main/java/eu/viandeendirect/domains/payment/StripePaymentManager.java index 3e707f9..03c0941 100644 --- a/backend/app/src/main/java/eu/viandeendirect/domains/payment/StripePaymentManager.java +++ b/backend/app/src/main/java/eu/viandeendirect/domains/payment/StripePaymentManager.java @@ -17,7 +17,7 @@ public interface StripePaymentManager { void processPaymentValidation(Order order); default SessionCreateParams.LineItem getLineItem(OrderItem orderItem) { - long unitAmount = orderItem.getUnitPrice().longValue() * orderItem.getPackageLot().getNetWeight().longValue() * 100; + long unitAmount = getUnitAmount(orderItem); long quantity = orderItem.getQuantity().longValue(); LOGGER.debug("creating Stripe payment's line item for order item {} x{} for {} euro-cents each", orderItem.getPackageLot().getLabel(), quantity, unitAmount); return SessionCreateParams.LineItem.builder() @@ -35,4 +35,8 @@ default SessionCreateParams.LineItem getLineItem(OrderItem orderItem) { .build(); } + default long getUnitAmount(OrderItem orderItem) { + return Float.valueOf(orderItem.getUnitPrice() * orderItem.getPackageLot().getNetWeight() * 100).longValue(); + } + } diff --git a/backend/app/src/test/java/eu/viandeendirect/domains/payment/TestStripePaymentManager_getUnitAmount.java b/backend/app/src/test/java/eu/viandeendirect/domains/payment/TestStripePaymentManager_getUnitAmount.java new file mode 100644 index 0000000..2006eae --- /dev/null +++ b/backend/app/src/test/java/eu/viandeendirect/domains/payment/TestStripePaymentManager_getUnitAmount.java @@ -0,0 +1,32 @@ +package eu.viandeendirect.domains.payment; + +import eu.viandeendirect.model.OrderItem; +import eu.viandeendirect.model.PackageLot; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.*; + +class TestStripePaymentManager_getUnitAmount { + + @Test + void testGetUnitAmount() { + // given + StripePaymentManager stripePaymentManagerImpl = Mockito.mock(StripePaymentManager.class); + Mockito.doCallRealMethod().when(stripePaymentManagerImpl).getUnitAmount(Mockito.any()); + PackageLot packageLot = new PackageLot(); + packageLot.setNetWeight(0.2f); + OrderItem orderItem = new OrderItem(); + orderItem.setUnitPrice(0.2f); + orderItem.setPackageLot(packageLot); + + // when + long unitAmount = stripePaymentManagerImpl.getUnitAmount(orderItem); + + // then + Assertions.assertThat(unitAmount).isEqualTo(4); + } + +} \ No newline at end of file