Skip to content

Commit 6ff0678

Browse files
committed
feat(fc/payments): handle insufficient balances
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent 7148b53 commit 6ff0678

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

services/flipchat/payments/src/main/kotlin/com/getcode/network/repository/PaymentRepository.kt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class PaymentRepository @Inject constructor(
2828
extendedMetadata: ExtendedMetadata
2929
): ID {
3030
val organizer = userManager.organizer ?: throw PaymentError.OrganizerNotFound()
31+
val currentBalance = balanceController.rawBalance
32+
if (amount.kin.toKinValueDouble() > currentBalance) throw PaymentError.InsufficientBalance()
3133
return client.publicPayment(
3234
amount = amount,
3335
organizer = organizer,
@@ -45,18 +47,9 @@ class PaymentRepository @Inject constructor(
4547
sealed interface PaymentError {
4648
val message: String?
4749

48-
data class NoExchangeData(override val message: String? = "No exchange data") : PaymentError,
49-
Throwable(message)
50-
51-
data class InvalidPayload(override val message: String? = "invalid payload") : PaymentError,
52-
Throwable(message)
53-
5450
data class OrganizerNotFound(override val message: String? = "Organizer not found") :
5551
PaymentError, Throwable(message)
5652

57-
data class ExchangeForCurrencyNotFound(override val message: String? = "exchange for currency not found") :
58-
PaymentError, Throwable(message)
59-
60-
data class MessageForRendezvousNotFound(override val message: String? = "message for rendezvous not found") :
53+
data class InsufficientBalance(override val message: String? = "Insufficient balance for payment") :
6154
PaymentError, Throwable(message)
6255
}

services/flipchat/payments/src/main/kotlin/xyz/flipchat/services/PaymentController.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.getcode.model.KinAmount
88
import com.getcode.models.BillState
99
import com.getcode.models.ConfirmationState
1010
import com.getcode.models.PublicPaymentConfirmation
11+
import com.getcode.network.repository.PaymentError
1112
import com.getcode.network.repository.PaymentRepository
1213
import com.getcode.services.model.ExtendedMetadata
1314
import com.getcode.solana.keys.PublicKey
@@ -106,10 +107,16 @@ class PaymentController @Inject constructor(
106107
_eventFlow.emit(PaymentEvent.OnPaymentSuccess(it, destination))
107108
}.onFailure {
108109
onError(it)
109-
TopBarManager.showMessage(
110-
resources.getString(R.string.error_title_payment_failed),
111-
resources.getString(R.string.error_description_payment_failed),
112-
)
110+
when {
111+
it is PaymentError -> {
112+
when (it) {
113+
is PaymentError.InsufficientBalance -> presentInsufficientFundsError()
114+
is PaymentError.OrganizerNotFound -> presentPaymentFailedError()
115+
}
116+
}
117+
else -> presentPaymentFailedError()
118+
}
119+
113120
_eventFlow.emit(PaymentEvent.OnPaymentError(it))
114121

115122
billController.reset()
@@ -124,4 +131,18 @@ class PaymentController @Inject constructor(
124131
}
125132
}
126133
}
134+
135+
private fun presentInsufficientFundsError() {
136+
TopBarManager.showMessage(
137+
resources.getString(R.string.error_title_paymentFailedDueToInsufficientFunds),
138+
resources.getString(R.string.error_description_paymentFailedDueToInsufficientFunds),
139+
)
140+
}
141+
142+
private fun presentPaymentFailedError() {
143+
TopBarManager.showMessage(
144+
resources.getString(R.string.error_title_paymentFailed),
145+
resources.getString(R.string.error_description_paymentFailed),
146+
)
147+
}
127148
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<string name="error_title_payment_failed">Payment Failed</string>
4-
<string name="error_description_payment_failed">This payment request could not be paid at this time. Please try again later.</string>
3+
<string name="error_title_paymentFailed">Payment Failed</string>
4+
<string name="error_description_paymentFailed">This payment request could not be paid at this time. Please try again later.</string>
5+
6+
<string name="error_title_paymentFailedDueToInsufficientFunds">Insufficient Balance</string>
7+
<string name="error_description_paymentFailedDueToInsufficientFunds">You don\’t have enough Kin to complete this payment</string>
58

69
<string name="title_implicitRoomTitle">Room #%1$s</string>
710
</resources>

0 commit comments

Comments
 (0)