Skip to content

Commit

Permalink
set app fee to 1 percent
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpochat committed Nov 15, 2024
1 parent caf38f1 commit 0204641
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,14 +38,17 @@ public class StripeDirectPaymentManager implements StripePaymentManager {
@Autowired
PackageLotRepository packageLotRepository;

@Autowired
OrderAmountService orderAmountService;

@Override
public StripePayment createPayment(Order order) throws StripeException {
SessionCreateParams.Builder builder = SessionCreateParams.builder();
order.getItems().forEach(item -> builder.addLineItem(getLineItem(item)));
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")
Expand All @@ -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<Producer> orderProducers = order.getItems().stream()
.map(OrderItem::getPackageLot)
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 0204641

Please sign in to comment.