From 02046412c381d8edfe74f51caf6055a8317ec757 Mon Sep 17 00:00:00 2001 From: Benjamin POCHAT Date: Sat, 16 Nov 2024 00:05:36 +0100 Subject: [PATCH] set app fee to 1 percent --- .../domains/order/OrderAmountService.java | 16 ++++++++ .../payment/StripeDirectPaymentManager.java | 10 ++++- .../domains/order/TestOrderAmountService.java | 38 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 backend/app/src/main/java/eu/viandeendirect/domains/order/OrderAmountService.java create mode 100644 backend/app/src/test/java/eu/viandeendirect/domains/order/TestOrderAmountService.java diff --git a/backend/app/src/main/java/eu/viandeendirect/domains/order/OrderAmountService.java b/backend/app/src/main/java/eu/viandeendirect/domains/order/OrderAmountService.java new file mode 100644 index 0000000..d191b06 --- /dev/null +++ b/backend/app/src/main/java/eu/viandeendirect/domains/order/OrderAmountService.java @@ -0,0 +1,16 @@ +package eu.viandeendirect.domains.order; + +import eu.viandeendirect.model.Order; +import eu.viandeendirect.model.OrderItem; +import org.springframework.stereotype.Service; + +@Service +public class OrderAmountService { + public float calculateTotalOrderAmount(Order order) { + float totalAmount = 0; + for (OrderItem orderItem : order.getItems()) { + totalAmount += orderItem.getUnitPrice() * orderItem.getQuantity() * orderItem.getPackageLot().getNetWeight(); + } + return totalAmount; + } +} diff --git a/backend/app/src/main/java/eu/viandeendirect/domains/payment/StripeDirectPaymentManager.java b/backend/app/src/main/java/eu/viandeendirect/domains/payment/StripeDirectPaymentManager.java index e74cd92..7d81866 100644 --- a/backend/app/src/main/java/eu/viandeendirect/domains/payment/StripeDirectPaymentManager.java +++ b/backend/app/src/main/java/eu/viandeendirect/domains/payment/StripeDirectPaymentManager.java @@ -5,6 +5,7 @@ import com.stripe.net.RequestOptions; import com.stripe.param.checkout.SessionCreateParams; import eu.viandeendirect.common.ViandeEnDirectConfiguration; +import eu.viandeendirect.domains.order.OrderAmountService; import eu.viandeendirect.domains.production.PackageLotRepository; import eu.viandeendirect.model.Order; import eu.viandeendirect.model.OrderItem; @@ -37,6 +38,9 @@ public class StripeDirectPaymentManager implements StripePaymentManager { @Autowired PackageLotRepository packageLotRepository; + @Autowired + OrderAmountService orderAmountService; + @Override public StripePayment createPayment(Order order) throws StripeException { SessionCreateParams.Builder builder = SessionCreateParams.builder(); @@ -44,7 +48,7 @@ public StripePayment createPayment(Order order) throws StripeException { SessionCreateParams params = builder .setPaymentIntentData(SessionCreateParams.PaymentIntentData.builder() .setDescription(String.format("Commande viandeendirect.eu n° %s de %s %s", order.getId(), order.getCustomer().getUser().getFirstName(), order.getCustomer().getUser().getLastName())) - .setApplicationFeeAmount(1L).build()) + .setApplicationFeeAmount(getViandeEnDirectFee(order) * 100).build()) .setMode(SessionCreateParams.Mode.PAYMENT) .setCustomerEmail(order.getCustomer().getUser().getEmail()) .setSuccessUrl(viandeEnDirectConfiguration.getCustomerFrontendUrl() + "/order/" + order.getId() + "/payment") @@ -59,6 +63,10 @@ public StripePayment createPayment(Order order) throws StripeException { return stripePayment; } + private Long getViandeEnDirectFee(Order order) { + return (long) orderAmountService.calculateTotalOrderAmount(order) / 100; + } + private Producer getProducerStripeAccount(Order order) { List orderProducers = order.getItems().stream() .map(OrderItem::getPackageLot) diff --git a/backend/app/src/test/java/eu/viandeendirect/domains/order/TestOrderAmountService.java b/backend/app/src/test/java/eu/viandeendirect/domains/order/TestOrderAmountService.java new file mode 100644 index 0000000..7dc622f --- /dev/null +++ b/backend/app/src/test/java/eu/viandeendirect/domains/order/TestOrderAmountService.java @@ -0,0 +1,38 @@ +package eu.viandeendirect.domains.order; + + +import eu.viandeendirect.model.BeefProduction; +import eu.viandeendirect.model.Order; +import eu.viandeendirect.model.PackageLot; +import eu.viandeendirect.model.Sale; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.time.OffsetDateTime; +import java.util.Map; + +import static eu.viandeendirect.domains.order.OrderTestUtils.*; + +class TestOrderAmountService { + @Test + void should_return_the_right_amount() { + // given + BeefProduction beefProduction = getBeefProduction(); + PackageLot packageLot1 = getPackageLot(1, beefProduction, "Colis tradition", "Un colis avec plein de trucs délicieux", 10f); + PackageLot packageLot2 = getPackageLot(2, beefProduction, "Filet", "Un colis avec plein de trucs bons", 0.5f); + + Sale sale = new Sale(); + sale.setId(123); + sale.setDeliveryStart(OffsetDateTime.parse("2021-09-30T15:30:00+01:00")); + + Order order = createOrder(123, createCustomer("Valentine", "DURAND", "0601020304"), Map.of(packageLot1, 1, packageLot2, 2)); + OrderAmountService orderAmountService = new OrderAmountService(); + + // when + float amount = orderAmountService.calculateTotalOrderAmount(order); + + // then + Assertions.assertThat(amount).isEqualTo(110f); + } + +} \ No newline at end of file