Skip to content

Commit 85e4582

Browse files
committed
add balance api
1 parent eddc897 commit 85e4582

File tree

6 files changed

+85
-38
lines changed

6 files changed

+85
-38
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package app.softnetwork.payment.spi
2+
3+
trait BalanceApi { _: PaymentContext =>
4+
5+
/** @param currency
6+
* - currency
7+
* @param creditedUserId
8+
* - optional credited user id
9+
* @return
10+
* balance
11+
*/
12+
def loadBalance(currency: String, creditedUserId: Option[String]): Option[Int]
13+
14+
}

common/src/main/scala/app/softnetwork/payment/spi/PaymentProvider.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ private[payment] trait PaymentProvider
1616
with PayOutApi
1717
with TransferApi
1818
with RefundApi
19-
with RecurringPaymentApi {
19+
with RecurringPaymentApi
20+
with BalanceApi {
2021

2122
protected lazy val mlog: Logger = Logger(LoggerFactory.getLogger(getClass.getName))
2223

mangopay/src/main/scala/app/softnetwork/payment/spi/MangoPayProvider.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,6 +2650,15 @@ trait MangoPayProvider extends PaymentProvider {
26502650
}
26512651
}
26522652

2653+
/** @param currency
2654+
* - currency
2655+
* @param creditedUserId
2656+
* - optional credited user id
2657+
* @return
2658+
* balance
2659+
*/
2660+
override def loadBalance(currency: String, creditedUserId: Option[String]): Option[Int] =
2661+
clientFees().map(fees => (fees * 100).toInt)
26532662
}
26542663

26552664
class MangoPayProviderFactory extends PaymentProviderSpi {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package app.softnetwork.payment.spi
2+
3+
import app.softnetwork.payment.config.StripeApi
4+
import com.google.gson.Gson
5+
import com.stripe.model.Balance
6+
import collection.JavaConverters._
7+
8+
trait StripeBalanceApi extends BalanceApi { self: StripeContext =>
9+
10+
/** @param currency
11+
* - currency
12+
* @param creditedUserId
13+
* - optional credited user id
14+
* @return
15+
* balance
16+
*/
17+
override def loadBalance(currency: String, creditedUserId: Option[String]): Option[Int] = {
18+
var requestOptions = StripeApi().requestOptionsBuilder
19+
20+
creditedUserId match {
21+
case Some(value) =>
22+
requestOptions = requestOptions.setStripeAccount(value)
23+
case _ =>
24+
}
25+
26+
// load balance
27+
val balances: Seq[Balance.Available] =
28+
Balance
29+
.retrieve(requestOptions.build())
30+
.getAvailable
31+
.asScala
32+
33+
val availableAmount =
34+
balances.find(
35+
_.getCurrency.toLowerCase() == currency.toLowerCase
36+
) match {
37+
case Some(balance) =>
38+
balance.getAmount.intValue()
39+
case None =>
40+
mlog.info(
41+
s"balances for ${creditedUserId.getOrElse(self.config.clientId)} -> ${new Gson().toJson(balances)}"
42+
)
43+
0
44+
}
45+
46+
mlog.info(
47+
s"balance available amount for ${creditedUserId.getOrElse(self.config.clientId)} is $availableAmount"
48+
)
49+
50+
Option(availableAmount)
51+
}
52+
53+
}

stripe/src/main/scala/app/softnetwork/payment/spi/StripePayOutApi.scala

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ package app.softnetwork.payment.spi
33
import app.softnetwork.payment.config.StripeApi
44
import app.softnetwork.payment.model.{PayOutTransaction, Transaction, TransferTransaction}
55
import app.softnetwork.serialization.asJson
6-
import com.stripe.model.Balance
76
//import com.stripe.param.PaymentIntentCaptureParams
8-
import com.google.gson.Gson
97
import com.stripe.model.{PaymentIntent, Payout, Transfer}
108
import com.stripe.param.PayoutCreateParams
119

1210
import scala.util.{Failure, Success, Try}
1311
import collection.JavaConverters._
1412

15-
trait StripePayOutApi extends PayOutApi { _: StripeContext with StripeTransferApi =>
13+
trait StripePayOutApi extends PayOutApi {
14+
_: StripeContext with StripeTransferApi with StripeBalanceApi =>
1615

1716
/** @param maybePayOutTransaction
1817
* - pay out transaction
@@ -172,23 +171,9 @@ trait StripePayOutApi extends PayOutApi { _: StripeContext with StripeTransferAp
172171
requestOptions = requestOptions.setStripeAccount(payOutTransaction.creditedUserId)
173172

174173
// load balance
175-
val balances: Seq[Balance.Available] =
176-
Balance
177-
.retrieve(requestOptions.build())
178-
.getAvailable
179-
.asScala
180-
.toSeq
181-
182174
val availableAmount =
183-
balances.find(
184-
_.getCurrency.toLowerCase() == payOutTransaction.currency.toLowerCase
185-
) match {
186-
case Some(balance) =>
187-
balance.getAmount.intValue()
188-
case None =>
189-
mlog.info(s"balances -> ${new Gson().toJson(balances)}")
190-
0
191-
}
175+
loadBalance(payOutTransaction.currency, Option(payOutTransaction.creditedUserId))
176+
.getOrElse(0)
192177

193178
mlog.info(
194179
s"balance available amount for ${payOutTransaction.creditedUserId} is $availableAmount"
@@ -207,23 +192,7 @@ trait StripePayOutApi extends PayOutApi { _: StripeContext with StripeTransferAp
207192
} else {
208193
// we receive funds from Stripe
209194
// load balance
210-
val balances: Seq[Balance.Available] =
211-
Balance
212-
.retrieve(requestOptions.build())
213-
.getAvailable
214-
.asScala
215-
.toSeq
216-
217-
val availableAmount =
218-
balances.find(
219-
_.getCurrency.toLowerCase() == payOutTransaction.currency.toLowerCase
220-
) match {
221-
case Some(balance) =>
222-
balance.getAmount.intValue()
223-
case None =>
224-
mlog.info(s"balances -> ${new Gson().toJson(balances)}")
225-
0
226-
}
195+
val availableAmount = loadBalance(payOutTransaction.currency, None).getOrElse(0)
227196

228197
mlog.info(s"balance available amount for our stripe account is $availableAmount")
229198

stripe/src/main/scala/app/softnetwork/payment/spi/StripeProvider.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ trait StripeProvider
3636
with StripePayOutApi
3737
with StripeRecurringPaymentApi
3838
with StripeRefundApi
39-
with StripeTransferApi {
39+
with StripeTransferApi
40+
with StripeBalanceApi {
4041

4142
/** @return
4243
* client

0 commit comments

Comments
 (0)