-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Page d'accueil producteur #35 - finalization
- Loading branch information
1 parent
941b2a8
commit 3eb0159
Showing
5 changed files
with
158 additions
and
21 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
backend/app/src/main/java/eu/viandeendirect/domains/payment/StripeBalanceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
backend/app/src/test/java/eu/viandeendirect/domains/payment/TestStripeBalanceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters