summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/payment
diff options
context:
space:
mode:
Diffstat (limited to 'wallet/src/main/java/net/taler/wallet/payment')
-rw-r--r--wallet/src/main/java/net/taler/wallet/payment/PaymentManager.kt105
-rw-r--r--wallet/src/main/java/net/taler/wallet/payment/PaymentResponses.kt62
2 files changed, 108 insertions, 59 deletions
diff --git a/wallet/src/main/java/net/taler/wallet/payment/PaymentManager.kt b/wallet/src/main/java/net/taler/wallet/payment/PaymentManager.kt
index 5c73d6c..c6351ee 100644
--- a/wallet/src/main/java/net/taler/wallet/payment/PaymentManager.kt
+++ b/wallet/src/main/java/net/taler/wallet/payment/PaymentManager.kt
@@ -26,11 +26,29 @@ import net.taler.common.Amount
import net.taler.common.ContractTerms
import net.taler.wallet.TAG
import net.taler.wallet.backend.WalletBackendApi
+import net.taler.wallet.payment.PreparePayResponse.AlreadyConfirmedResponse
+import net.taler.wallet.payment.PreparePayResponse.InsufficientBalanceResponse
+import net.taler.wallet.payment.PreparePayResponse.PaymentPossibleResponse
import org.json.JSONObject
import java.net.MalformedURLException
val REGEX_PRODUCT_IMAGE = Regex("^data:image/(jpeg|png);base64,([A-Za-z0-9+/=]+)$")
+sealed class PayStatus {
+ object None : PayStatus()
+ object Loading : PayStatus()
+ data class Prepared(
+ val contractTerms: ContractTerms,
+ val proposalId: String,
+ val totalFees: Amount
+ ) : PayStatus()
+
+ data class InsufficientBalance(val contractTerms: ContractTerms) : PayStatus()
+ object AlreadyPaid : PayStatus()
+ data class Error(val error: String) : PayStatus()
+ data class Success(val currency: String) : PayStatus()
+}
+
class PaymentManager(
private val walletBackendApi: WalletBackendApi,
private val mapper: ObjectMapper
@@ -42,52 +60,27 @@ class PaymentManager(
private val mDetailsShown = MutableLiveData<Boolean>()
internal val detailsShown: LiveData<Boolean> = mDetailsShown
- private var currentPayRequestId = 0
-
@UiThread
fun preparePay(url: String) {
mPayStatus.value = PayStatus.Loading
mDetailsShown.value = false
- val args = JSONObject(mapOf("url" to url))
-
- currentPayRequestId += 1
- val payRequestId = currentPayRequestId
-
+ val args = JSONObject(mapOf("talerPayUri" to url))
walletBackendApi.sendRequest("preparePay", args) { isError, result ->
- when {
- isError -> {
- Log.v(TAG, "got preparePay error result")
- mPayStatus.value = PayStatus.Error(result.toString())
- }
- payRequestId != this.currentPayRequestId -> {
- Log.v(TAG, "preparePay result was for old request")
- }
- else -> {
- val status = result.getString("status")
- try {
- mPayStatus.postValue(getPayStatusUpdate(status, result))
- } catch (e: Exception) {
- Log.e(TAG, "Error getting PayStatusUpdate", e)
- mPayStatus.postValue(PayStatus.Error(e.message ?: "unknown error"))
- }
- }
+ if (isError) {
+ handleError("preparePay", result.toString(2))
+ return@sendRequest
+ }
+ val response: PreparePayResponse = mapper.readValue(result.toString())
+ Log.e(TAG, "PreparePayResponse $response")
+ mPayStatus.value = when (response) {
+ is PaymentPossibleResponse -> TODO()
+ is InsufficientBalanceResponse -> TODO()
+ is AlreadyConfirmedResponse -> TODO()
}
}
}
- private fun getPayStatusUpdate(status: String, json: JSONObject) = when (status) {
- "payment-possible" -> PayStatus.Prepared(
- contractTerms = getContractTerms(json),
- proposalId = json.getString("proposalId"),
- totalFees = Amount.fromJsonObject(json.getJSONObject("totalFees"))
- )
- "paid" -> PayStatus.AlreadyPaid(getContractTerms(json))
- "insufficient-balance" -> PayStatus.InsufficientBalance(getContractTerms(json))
- "error" -> PayStatus.Error("got some error")
- else -> PayStatus.Error("unknown status")
- }
-
private fun getContractTerms(json: JSONObject): ContractTerms {
val terms: ContractTerms = mapper.readValue(json.getString("contractTermsRaw"))
// validate product images
@@ -101,16 +94,13 @@ class PaymentManager(
return terms
}
- @UiThread
- fun toggleDetailsShown() {
- val oldValue = mDetailsShown.value ?: false
- mDetailsShown.value = !oldValue
- }
-
fun confirmPay(proposalId: String, currency: String) {
val args = JSONObject(mapOf("proposalId" to proposalId))
-
- walletBackendApi.sendRequest("confirmPay", args) { _, _ ->
+ walletBackendApi.sendRequest("confirmPay", args) { isError, result ->
+ if (isError) {
+ handleError("preparePay", result.toString())
+ return@sendRequest
+ }
mPayStatus.postValue(PayStatus.Success(currency))
}
}
@@ -129,8 +119,9 @@ class PaymentManager(
Log.i(TAG, "aborting proposal")
- walletBackendApi.sendRequest("abortProposal", args) { isError, _ ->
+ walletBackendApi.sendRequest("abortProposal", args) { isError, result ->
if (isError) {
+ handleError("abortProposal", result.toString(2))
Log.e(TAG, "received error response to abortProposal")
return@sendRequest
}
@@ -139,23 +130,19 @@ class PaymentManager(
}
@UiThread
+ fun toggleDetailsShown() {
+ val oldValue = mDetailsShown.value ?: false
+ mDetailsShown.value = !oldValue
+ }
+
+ @UiThread
fun resetPayStatus() {
mPayStatus.value = PayStatus.None
}
-}
-
-sealed class PayStatus {
- object None : PayStatus()
- object Loading : PayStatus()
- data class Prepared(
- val contractTerms: ContractTerms,
- val proposalId: String,
- val totalFees: Amount
- ) : PayStatus()
+ private fun handleError(operation: String, msg: String) {
+ Log.e(TAG, "got $operation error result $msg")
+ mPayStatus.value = PayStatus.Error(msg)
+ }
- data class InsufficientBalance(val contractTerms: ContractTerms) : PayStatus()
- data class AlreadyPaid(val contractTerms: ContractTerms) : PayStatus()
- data class Error(val error: String) : PayStatus()
- data class Success(val currency: String) : PayStatus()
}
diff --git a/wallet/src/main/java/net/taler/wallet/payment/PaymentResponses.kt b/wallet/src/main/java/net/taler/wallet/payment/PaymentResponses.kt
new file mode 100644
index 0000000..4c5b010
--- /dev/null
+++ b/wallet/src/main/java/net/taler/wallet/payment/PaymentResponses.kt
@@ -0,0 +1,62 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 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.payment
+
+import com.fasterxml.jackson.annotation.JsonSubTypes
+import com.fasterxml.jackson.annotation.JsonSubTypes.Type
+import com.fasterxml.jackson.annotation.JsonTypeInfo
+import com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY
+import com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME
+import com.fasterxml.jackson.annotation.JsonTypeName
+import net.taler.common.ContractTerms
+import net.taler.wallet.payment.PreparePayResponse.AlreadyConfirmedResponse
+import net.taler.wallet.payment.PreparePayResponse.InsufficientBalanceResponse
+import net.taler.wallet.payment.PreparePayResponse.PaymentPossibleResponse
+
+@JsonTypeInfo(use = NAME, include = PROPERTY, property = "status")
+@JsonSubTypes(
+ Type(value = PaymentPossibleResponse::class, name = "payment-possible"),
+ Type(value = AlreadyConfirmedResponse::class, name = "already-confirmed"),
+ Type(value = InsufficientBalanceResponse::class, name = "insufficient-balance")
+)
+sealed class PreparePayResponse(open val proposalId: String) {
+ @JsonTypeName("payment-possible")
+ data class PaymentPossibleResponse(
+ override val proposalId: String,
+ val contractTerms: ContractTerms
+ ) : PreparePayResponse(proposalId)
+
+ @JsonTypeName("insufficient-balance")
+ data class InsufficientBalanceResponse(
+ override val proposalId: String,
+ val contractTerms: ContractTerms
+ ) : PreparePayResponse(proposalId)
+
+ @JsonTypeName("already-confirmed")
+ data class AlreadyConfirmedResponse(
+ override val proposalId: String,
+ /**
+ * Did the payment succeed?
+ */
+ val paid: Boolean,
+
+ /**
+ * Redirect URL for the fulfillment page, only given if paid==true.
+ */
+ val nextUrl: String?
+ ) : PreparePayResponse(proposalId)
+}