Skip to content

Commit

Permalink
Page d'accueil producteur #35 - finalization
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpochat committed Sep 5, 2024
1 parent 941b2a8 commit 3eb0159
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package eu.viandeendirect.domains.payment;

import com.stripe.exception.StripeException;
import com.stripe.model.BalanceTransaction;
import com.stripe.model.BalanceTransactionCollection;
import com.stripe.net.RequestOptions;
import com.stripe.param.BalanceTransactionListParams;
import eu.viandeendirect.model.PaymentsSummary;
import eu.viandeendirect.model.Producer;
import org.springframework.stereotype.Service;

import java.time.Instant;

import static java.time.temporal.ChronoUnit.*;

@Service
public class StripeBalanceManager {
public PaymentsSummary getPaymentsSummary(Producer producer) throws StripeException {
Iterable<BalanceTransaction> balanceTransactions = getBalanceTransactions(producer);
Instant now = getCurrentInstant();
Instant oneDayAgo = now.minus(1, DAYS);
Instant oneWeekAgo = now.minus(7, DAYS);
Instant oneMonthAgo = now.minus(30, DAYS);
Instant oneYearAgo = now.minus(365, DAYS);
PaymentsSummary paymentsSummary = initPaymentsSummary();
for (BalanceTransaction balanceTransaction : balanceTransactions) {
if (balanceTransaction.getCreated() < oneYearAgo.getEpochSecond()) {
break;
}
updatePaymentSummary(paymentsSummary, balanceTransaction, oneDayAgo, oneWeekAgo, oneMonthAgo, oneYearAgo);
}
return paymentsSummary;
}

Instant getCurrentInstant() {
Instant now = Instant.now();
return now;
}

private PaymentsSummary initPaymentsSummary() {
PaymentsSummary paymentsSummary = new PaymentsSummary();
paymentsSummary.setDaylyTotal(0f);
paymentsSummary.setWeeklyTotal(0f);
paymentsSummary.setMonthlyTotal(0f);
paymentsSummary.setYearlyTotal(0f);
return paymentsSummary;
}

private void updatePaymentSummary(PaymentsSummary paymentsSummary, BalanceTransaction balanceTransaction, Instant oneDayAgo, Instant oneWeekAgo, Instant oneMonthAgo, Instant oneYearAgo) {
if (balanceTransaction.getType().equals("charge") && balanceTransaction.getStatus().equals("available")) {
if (balanceTransaction.getCreated() > oneDayAgo.getEpochSecond()) {
paymentsSummary.setDaylyTotal(paymentsSummary.getDaylyTotal() + balanceTransaction.getAmount() / 100);
}
if (balanceTransaction.getCreated() > oneWeekAgo.getEpochSecond()) {
paymentsSummary.setWeeklyTotal(paymentsSummary.getWeeklyTotal() + balanceTransaction.getAmount() / 100);
}
if (balanceTransaction.getCreated() > oneMonthAgo.getEpochSecond()) {
paymentsSummary.setMonthlyTotal(paymentsSummary.getMonthlyTotal() + balanceTransaction.getAmount() / 100);
}
if (balanceTransaction.getCreated() > oneYearAgo.getEpochSecond()) {
paymentsSummary.setYearlyTotal(paymentsSummary.getYearlyTotal() + balanceTransaction.getAmount() / 100);
}
}
}

Iterable<BalanceTransaction> getBalanceTransactions(Producer producer) throws StripeException {
RequestOptions requestOptions = RequestOptions.builder()
.setStripeAccount(producer.getStripeAccount().getStripeId())
.build();
BalanceTransactionListParams params = BalanceTransactionListParams.builder().setLimit(3L).build();
BalanceTransactionCollection balanceTransactionCollection = BalanceTransaction.list(params, requestOptions);
return balanceTransactionCollection.autoPagingIterable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import com.stripe.param.AccountCreateParams;
import com.stripe.param.AccountLinkCreateParams;
import eu.viandeendirect.common.ViandeEnDirectConfiguration;
import eu.viandeendirect.model.Order;
import eu.viandeendirect.model.Producer;
import eu.viandeendirect.model.StripeAccount;
import eu.viandeendirect.model.StripePayment;
import eu.viandeendirect.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -40,6 +37,9 @@ public void setStripeApiKey(String stripeApiKey) {
@Qualifier("StripeDirectPaymentManager")
StripePaymentManager stripePaymentManager;

@Autowired
StripeBalanceManager stripeBalanceManager;

public StripeAccount createStripeAccount(Producer producer) throws StripeException {
Account account = Account.create(
AccountCreateParams.builder()
Expand Down Expand Up @@ -79,4 +79,8 @@ public StripePayment createPayment(Order order) throws StripeException {
public void processPaymentValidation(Order order) {
stripePaymentManager.processPaymentValidation(order);
}

public PaymentsSummary loadStripePaymentSummary(Producer producer) throws StripeException {
return stripeBalanceManager.getPaymentsSummary(producer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public ResponseEntity<StripeAccount> getStripeAccount(Integer producerId) {
return new ResponseEntity<>(stripeAccount, OK);
} catch (Exception e) {
LOGGER.error("An error occurred when loading Stripe account data using Stripe API", e);
throw new ResponseStatusException(INTERNAL_SERVER_ERROR, "Une erreur s'est produite à la création du lien vers le compte Stripe", e);
throw new ResponseStatusException(INTERNAL_SERVER_ERROR, "Une erreur s'est produite au chargement du compte Stripe", e);
}
}

Expand All @@ -117,12 +117,12 @@ public ResponseEntity<PaymentsSummary> getProducerPaymentsSummary(Integer produc
if (!producer.getId().equals(producerId)) {
return new ResponseEntity<>(FORBIDDEN);
}
//TODO : calculer les montant à partir des API Stripe (https://docs.stripe.com/api/balance_transactions/object)
PaymentsSummary paymentsSummary = new PaymentsSummary();
paymentsSummary.setDaylyTotal(100f);
paymentsSummary.setWeeklyTotal(1000f);
paymentsSummary.setMonthlyTotal(4000f);
paymentsSummary.setYearlyTotal(12000f);
return new ResponseEntity<>(paymentsSummary, OK);
try {
PaymentsSummary paymentsSummary = stripeService.loadStripePaymentSummary(producer);
return new ResponseEntity<>(paymentsSummary, OK);
} catch (Exception e) {
LOGGER.error("An error occurred when loading Stripe payments summary data using Stripe API", e);
throw new ResponseStatusException(INTERNAL_SERVER_ERROR, "Une erreur s'est produite au chargement du résumé des paiements Stripe", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package eu.viandeendirect.domains.payment;

import com.stripe.exception.StripeException;
import com.stripe.model.BalanceTransaction;
import eu.viandeendirect.model.PaymentsSummary;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.time.Instant;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;

class TestStripeBalanceManager {
@Test
void should_return_the_expected_result() throws StripeException {
// given
StripeBalanceManager stripeBalanceManager = Mockito.spy(StripeBalanceManager.class);
Mockito.doReturn(Instant.parse("2024-12-25T20:30:00.00Z")).when(stripeBalanceManager).getCurrentInstant();
Mockito.doReturn(getBalanceTransations()).when(stripeBalanceManager).getBalanceTransactions(any());

// when
PaymentsSummary paymentsSummary = stripeBalanceManager.getPaymentsSummary(null);

// then
Assertions.assertThat(paymentsSummary.getDaylyTotal()).isEqualTo(1000f);
Assertions.assertThat(paymentsSummary.getWeeklyTotal()).isEqualTo(1500f);
Assertions.assertThat(paymentsSummary.getMonthlyTotal()).isEqualTo(1700f);
Assertions.assertThat(paymentsSummary.getYearlyTotal()).isEqualTo(2400f);
}

private Iterable<BalanceTransaction> getBalanceTransations() {
BalanceTransaction transaction1 = createBalanceTransaction(100000l, "2024-12-25T20:10:00.00Z", "available", "charge");
BalanceTransaction transaction2 = createBalanceTransaction(50000l, "2024-12-20T20:10:00.00Z", "available", "charge");
BalanceTransaction transaction3 = createBalanceTransaction(20000l, "2024-11-30T21:10:00.00Z", "available", "charge");
BalanceTransaction transaction4 = createBalanceTransaction(75000l, "2024-11-30T21:10:00.00Z", "pending", "charge");
BalanceTransaction transaction5 = createBalanceTransaction(30000l, "2024-11-29T20:10:00.00Z", "available", "refund");
BalanceTransaction transaction6 = createBalanceTransaction(70000l, "2024-03-30T20:10:00.00Z", "available", "charge");
return List.of(transaction1, transaction2, transaction3, transaction4, transaction5, transaction6);
}

private static BalanceTransaction createBalanceTransaction(long amount, String creationDate, String status, String type) {
BalanceTransaction transaction1 = new BalanceTransaction();
transaction1.setAmount(amount);
transaction1.setCreated(Instant.parse(creationDate).getEpochSecond());
transaction1.setStatus(status);
transaction1.setType(type);
return transaction1;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react'
import { SavingsOutlined } from '@mui/icons-material'
import { Stack, Typography } from '@mui/material'
import { CircularProgress, Stack, Typography } from '@mui/material'
import { useKeycloak } from '@react-keycloak/web'
import { PaymentsSummary, Producer } from '@viandeendirect/api/dist/models'
import { ApiBuilder } from '../../../api/ApiBuilder.ts'
Expand All @@ -9,7 +9,7 @@ import { ProducerService } from '../../commons/service/ProducerService.ts'

function DashboardPayments() {
const { keycloak } = useKeycloak()
const [paymentSummary, setPaymentSummary] = useState<PaymentsSummary | undefined>(undefined)
const [paymentsSummary, setPaymentSummary] = useState<PaymentsSummary | undefined>(undefined)
const apiBuilder = new ApiBuilder()

useEffect(() => {
Expand All @@ -23,14 +23,21 @@ function DashboardPayments() {
loadPaymentsSumary()
}, [keycloak])

function getContent() {
if (!paymentsSummary) {
return <CircularProgress/>
}
return <ul>
<li>Depuis 24h : {paymentsSummary?.daylyTotal}</li>
<li>Depuis 1 semaine : {paymentsSummary?.weeklyTotal}</li>
<li>Depuis 1 mois : {paymentsSummary?.monthlyTotal}</li>
<li>Depuis 1 an : {paymentsSummary?.yearlyTotal}</li>
</ul>
}

return <>
<Stack alignItems="center" direction="row" gap={2}><SavingsOutlined /><Typography variant="subtitle1" component="span"> Mes derniers versements</Typography></Stack>
<ul>
<li>Depuis 24h : {paymentSummary?.daylyTotal}</li>
<li>Depuis 1 semaine : {paymentSummary?.weeklyTotal}</li>
<li>Depuis 1 mois : {paymentSummary?.monthlyTotal}</li>
<li>Depuis 1 an : {paymentSummary?.yearlyTotal}</li>
</ul>
<Stack alignItems="center" direction="row" gap={2}><SavingsOutlined /><Typography variant="subtitle1" component="span"> Mes derniers paiements</Typography></Stack>
{getContent()}
</>
}

Expand Down

0 comments on commit 3eb0159

Please sign in to comment.