summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2021-12-07 16:23:49 -0300
committerTorsten Grote <t@grobox.de>2021-12-07 16:23:49 -0300
commit095f67dd25ceeff5df388ef42f73de963dd9348b (patch)
tree0aa089df8590d5a360f5ffb2137bf5c42df512a8 /wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt
parent5476ac27b671e51aceb51e4574b70730f7f0f2f3 (diff)
downloadtaler-android-095f67dd25ceeff5df388ef42f73de963dd9348b.tar.gz
taler-android-095f67dd25ceeff5df388ef42f73de963dd9348b.tar.bz2
taler-android-095f67dd25ceeff5df388ef42f73de963dd9348b.zip
Show bank details for manual withdrawal
Diffstat (limited to 'wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt')
-rw-r--r--wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt74
1 files changed, 63 insertions, 11 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt b/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt
index cc4c057..858d63e 100644
--- a/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt
+++ b/wallet/src/main/java/net/taler/wallet/withdraw/WithdrawManager.kt
@@ -16,6 +16,7 @@
package net.taler.wallet.withdraw
+import android.net.Uri
import android.util.Log
import androidx.annotation.UiThread
import androidx.lifecycle.LiveData
@@ -56,6 +57,14 @@ sealed class WithdrawStatus {
object Withdrawing : WithdrawStatus()
data class Success(val currency: String) : WithdrawStatus()
+ data class ManualTransferRequired(
+ val exchangeBaseUrl: String,
+ val uri: Uri,
+ val iban: String,
+ val subject: String,
+ val amountRaw: Amount,
+ ) : WithdrawStatus()
+
data class Error(val message: String?) : WithdrawStatus()
}
@@ -79,6 +88,11 @@ data class WithdrawalDetails(
val amountEffective: Amount,
)
+@Serializable
+data class AcceptManualWithdrawalResponse(
+ val exchangePaytoUris: List<String>,
+)
+
data class ExchangeSelection(
val amount: Amount,
val talerWithdrawUri: String,
@@ -197,31 +211,69 @@ class WithdrawManager(
@UiThread
fun acceptWithdrawal() = scope.launch {
val status = withdrawStatus.value as ReceivedDetails
- val operation = if (status.talerWithdrawUri == null) {
- "acceptManualWithdrawal"
+ withdrawStatus.value = WithdrawStatus.Withdrawing
+ if (status.talerWithdrawUri == null) {
+ acceptManualWithdrawal(status)
} else {
- "acceptBankIntegratedWithdrawal"
+ acceptBankIntegratedWithdrawal(status)
}
- withdrawStatus.value = WithdrawStatus.Withdrawing
+ }
- api.request<Unit>(operation) {
+ private suspend fun acceptBankIntegratedWithdrawal(status: ReceivedDetails) {
+ api.request<Unit>("acceptBankIntegratedWithdrawal") {
put("exchangeBaseUrl", status.exchangeBaseUrl)
- if (status.talerWithdrawUri == null) {
- put("amount", status.amountRaw.toJSONString())
- } else {
- put("talerWithdrawUri", status.talerWithdrawUri)
- }
+ put("talerWithdrawUri", status.talerWithdrawUri)
}.onError {
- handleError(operation, it)
+ handleError("acceptBankIntegratedWithdrawal", it)
}.onSuccess {
withdrawStatus.value = WithdrawStatus.Success(status.amountRaw.currency)
}
}
+ private suspend fun acceptManualWithdrawal(status: ReceivedDetails) {
+ api.request("acceptManualWithdrawal", AcceptManualWithdrawalResponse.serializer()) {
+ put("exchangeBaseUrl", status.exchangeBaseUrl)
+ put("amount", status.amountRaw.toJSONString())
+ }.onError {
+ handleError("acceptManualWithdrawal", it)
+ }.onSuccess { response ->
+ withdrawStatus.value = createManualTransferRequired(
+ amount = status.amountRaw,
+ exchangeBaseUrl = status.exchangeBaseUrl,
+ // TODO what if there's more than one or no URI?
+ uriStr = "payto://iban/ASDQWEASDZXCASDQWE?amount=KUDOS%3A10&message=Taler+Withdrawal+P2T19EXRBY4B145JRNZ8CQTD7TCS03JE9VZRCEVKVWCP930P56WG", // response.exchangePaytoUris[0],
+ // "payto://x-taler-bank/bank.demo.taler.net/Exchange?amount=KUDOS%3A10&message=Taler+Withdrawal+P2T19EXRBY4B145JRNZ8CQTD7TCS03JE9VZRCEVKVWCP930P56WG"
+ )
+ }
+ }
+
@UiThread
private fun handleError(operation: String, error: TalerErrorInfo) {
Log.e(TAG, "Error $operation $error")
withdrawStatus.value = WithdrawStatus.Error(error.userFacingMsg)
}
+ /**
+ * A hack to be able to view bank details for manual withdrawal with the same logic.
+ * Don't call this from ongoing withdrawal processes as it destroys state.
+ */
+ fun viewManualWithdrawal(status: WithdrawStatus.ManualTransferRequired) {
+ withdrawStatus.value = status
+ }
+
+}
+
+fun createManualTransferRequired(
+ amount: Amount,
+ exchangeBaseUrl: String,
+ uriStr: String,
+): WithdrawStatus.ManualTransferRequired {
+ val uri = Uri.parse(uriStr)
+ return WithdrawStatus.ManualTransferRequired(
+ exchangeBaseUrl = exchangeBaseUrl,
+ uri = uri,
+ iban = uri.lastPathSegment!!,
+ subject = uri.getQueryParameter("message")!!,
+ amountRaw = amount,
+ )
}