Skip to content

Commit

Permalink
Payout tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markusjevringsesame committed Apr 29, 2024
1 parent 7b5b614 commit 96d0873
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sesame.oss.stripemock.http.QueryParameters;
import com.sesame.oss.stripemock.http.ResponseCodeException;
import com.sesame.oss.stripemock.util.BalanceUtilities;
import com.sesame.oss.stripemock.util.Utilities;
import com.stripe.model.*;

Expand Down Expand Up @@ -56,13 +57,22 @@ protected Payout initialize(Payout payout, Map<String, Object> formData, String
// todo: support other test accounts from stripe
payout.setStatus("failed");
} else {
// todo: check that there are sufficient funds. If not, reject it.
payout.setStatus("paid");

payout.setBalanceTransaction(Utilities.randomIdWithPrefix("txn", 24));
// By registering this, it can be converted on the fly when expanded or fetched.
BalanceTransactionManager balanceTransactionEntityManager =
(BalanceTransactionManager) stripeEntities.getEntityManager(BalanceTransaction.class);
long sumAvailable = BalanceUtilities.sum(balanceTransactionEntityManager.list(null, stripeAccount), "available");
if (sumAvailable <= payout.getAmount()) {
throw new ResponseCodeException(400,
"You have insufficient funds in your Stripe account for this transfer. Your card balance is too low. You can use the /v1/balance endpoint to view your Stripe balance (for more details, see stripe.com/docs/api#balance).",
"balance_insufficient",
"invalid_request_error",
null,
null);
}

payout.setBalanceTransaction(Utilities.randomIdWithPrefix("txn", 24));
// By registering this, it can be converted on the fly when expanded or fetched.
balanceTransactionEntityManager.register(payout.getBalanceTransaction(), payout);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ private static List<Balance.Pending> createPending(List<BalanceTransaction> bala
private static List<Balance.Available> createAvailable(List<BalanceTransaction> balanceTransactions) {
Balance.Available available = new Balance.Available();
available.setCurrency("USD");
available.setAmount(balanceTransactions.stream()
.filter(txn -> txn.getStatus()
.equals("available"))
.mapToLong(BalanceTransaction::getAmount)
.sum());
available.setAmount(sum(balanceTransactions, "available"));
available.setSourceTypes(new Balance.Available.SourceTypes());
return Collections.singletonList(available);
}

public static long sum(List<BalanceTransaction> balanceTransactions, String status) {
return balanceTransactions.stream()
.filter(txn -> txn.getStatus()
.equals(status))
.mapToLong(BalanceTransaction::getAmount)
.sum();
}
}
73 changes: 72 additions & 1 deletion src/test/java/com/sesame/oss/stripemock/PayoutTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,78 @@
package com.sesame.oss.stripemock;

import com.stripe.exception.InvalidRequestException;
import com.stripe.exception.StripeException;
import com.stripe.model.Account;
import com.stripe.model.Payout;
import com.stripe.model.Transfer;
import com.stripe.net.RequestOptions;
import com.stripe.param.PayoutCreateParams;
import com.stripe.param.TransferCreateParams;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class PayoutTest extends AbstractStripeMockTest {
@Test
void testPayouts() throws StripeException {
Account account = Account.create(AccountTest.defaultCreationParameters("Stripe-mock test company"));
Transfer t1 = Transfer.create(TransferCreateParams.builder()
.setAmount(11_00L)
.setDestination(account.getId())
.setCurrency("usd")
.build());
Transfer t2 = Transfer.create(TransferCreateParams.builder()
.setAmount(22_00L)
.setDestination(account.getId())
.setCurrency("usd")
.build());

Payout payout = Payout.create(PayoutCreateParams.builder()
.setAmount(30_00L)
.setCurrency("usd")
.build(),
RequestOptions.builder()
.setStripeAccount(account.getId())
.build());

assertEquals("paid", payout.getStatus());
account.delete();
}

@Test
void shouldFailPayoutOnInsufficientBalance() throws StripeException {
Account account = Account.create(AccountTest.defaultCreationParameters("Stripe-mock test company"));

InvalidRequestException exception = assertThrows(InvalidRequestException.class,
() -> Payout.create(PayoutCreateParams.builder()
.setAmount(30_00L)
.setCurrency("usd")
.build(),
RequestOptions.builder()
.setStripeAccount(account.getId())
.build()));
assertTrue(exception.getMessage()
.contains("You have insufficient funds in your Stripe account for this transfer."));
assertEquals("balance_insufficient", exception.getCode());


account.delete();
}

@Test
void shouldFailPayoutOnInvalidBankAccount() throws StripeException {
Account account = Account.create(AccountTest.defaultCreationParameters("Stripe-mock test company"));
ExternalAccountTest.replaceExternalAccountWith(account, "000111111113");

Payout payout = Payout.create(PayoutCreateParams.builder()
.setAmount(30_00L)
.setCurrency("usd")
.build(),
RequestOptions.builder()
.setStripeAccount(account.getId())
.build());

// todo: add tests, including source
assertEquals("failed", payout.getStatus());
account.delete();
}
}

0 comments on commit 96d0873

Please sign in to comment.