summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/deposit/DepositManager.kt
blob: 0075f954ea9c36d493969356c6463f1037705928 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * This file is part of GNU Taler
 * (C) 2022 Taler Systems S.A.
 *
 * GNU Taler is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3, or (at your option) any later version.
 *
 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

package net.taler.wallet.deposit

import android.net.Uri
import android.util.Log
import androidx.annotation.UiThread
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import net.taler.common.Amount
import net.taler.wallet.TAG
import net.taler.wallet.accounts.PaytoUriBitcoin
import net.taler.wallet.accounts.PaytoUriIban
import net.taler.wallet.backend.WalletBackendApi

class DepositManager(
    private val api: WalletBackendApi,
    private val scope: CoroutineScope,
) {

    private val mDepositState = MutableStateFlow<DepositState>(DepositState.Start)
    internal val depositState = mDepositState.asStateFlow()

    fun isSupportedPayToUri(uriString: String): Boolean {
        if (!uriString.startsWith("payto://")) return false
        val u = Uri.parse(uriString)
        if (!u.authority.equals("iban", ignoreCase = true)) return false
        return u.pathSegments.size >= 1
    }

    @UiThread
    fun onDepositButtonClicked(amount: Amount, receiverName: String, iban: String) {
        if (depositState.value is DepositState.FeesChecked) {
            // fees already checked, so IBAN was validated, can make deposit directly
            makeIbanDeposit(amount, receiverName, iban)
        } else {
            // validate IBAN first
            mDepositState.value = DepositState.CheckingFees
            scope.launch {
                api.request("validateIban", ValidateIbanResponse.serializer()) {
                    put("iban", iban)
                }.onError {
                    Log.e(TAG, "Error validateIban $it")
                    mDepositState.value = DepositState.Error(it)
                }.onSuccess { response ->
                    if (response.valid) {
                        // only prepare/make deposit, if IBAN is valid
                        makeIbanDeposit(amount, receiverName, iban)
                    } else {
                        mDepositState.value = DepositState.IbanInvalid
                    }
                }
            }
        }
    }

    @UiThread
    private fun makeIbanDeposit(amount: Amount, receiverName: String, iban: String) {
        val paytoUri: String = PaytoUriIban(
            iban = iban,
            bic = null,
            targetPath = "",
            params = mapOf("receiver-name" to receiverName),
        ).paytoUri
        makeDeposit(amount, paytoUri)
    }

    @UiThread
    fun onDepositButtonClicked(amount: Amount, bitcoinAddress: String) {
        val paytoUri: String = PaytoUriBitcoin(
            segwitAddresses = listOf(bitcoinAddress),
            targetPath = bitcoinAddress,
        ).paytoUri
        makeDeposit(amount, paytoUri)
    }

    private fun makeDeposit(amount: Amount, uri: String) {
        if (depositState.value is DepositState.FeesChecked) makeDeposit(
            paytoUri = uri,
            amount = amount,
            totalDepositCost = depositState.value.totalDepositCost
                ?: Amount.zero(amount.currency),
            effectiveDepositAmount = depositState.value.effectiveDepositAmount
                ?: Amount.zero(amount.currency),
        ) else {
            prepareDeposit(uri, amount)
        }
    }

    private fun prepareDeposit(paytoUri: String, amount: Amount) {
        mDepositState.value = DepositState.CheckingFees
        scope.launch {
            api.request("prepareDeposit", PrepareDepositResponse.serializer()) {
                put("depositPaytoUri", paytoUri)
                put("amount", amount.toJSONString())
            }.onError {
                Log.e(TAG, "Error prepareDeposit $it")
                mDepositState.value = DepositState.Error(it)
            }.onSuccess {
                mDepositState.value = DepositState.FeesChecked(
                    totalDepositCost = it.totalDepositCost,
                    effectiveDepositAmount = it.effectiveDepositAmount,
                )
            }
        }
    }

    private fun makeDeposit(
        paytoUri: String,
        amount: Amount,
        totalDepositCost: Amount,
        effectiveDepositAmount: Amount,
    ) {
        mDepositState.value = DepositState.MakingDeposit(
            totalDepositCost = totalDepositCost,
            effectiveDepositAmount = effectiveDepositAmount,
        )
        scope.launch {
            api.request("createDepositGroup", CreateDepositGroupResponse.serializer()) {
                put("depositPaytoUri", paytoUri)
                put("amount", amount.toJSONString())
            }.onError {
                Log.e(TAG, "Error createDepositGroup $it")
                mDepositState.value = DepositState.Error(it)
            }.onSuccess {
                mDepositState.value = DepositState.Success
            }
        }
    }

    @UiThread
    fun resetDepositState() {
        mDepositState.value = DepositState.Start
    }
}

@Serializable
data class ValidateIbanResponse(
    val valid: Boolean,
)

@Serializable
data class PrepareDepositResponse(
    val totalDepositCost: Amount,
    val effectiveDepositAmount: Amount,
)

@Serializable
data class CreateDepositGroupResponse(
    val depositGroupId: String,
    val transactionId: String,
)