diff options
author | Torsten Grote <t@grobox.de> | 2020-07-23 11:54:34 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-07-23 11:54:34 -0300 |
commit | 08b10a2408f958cae96ae0c674ee450a35109e8a (patch) | |
tree | d4bee31f1b007ca94f2dfe90b4e47b866481fe46 | |
parent | a8c811f6cdf4bf1b787ebaaa9fd220588fd1ffcf (diff) | |
download | taler-android-08b10a2408f958cae96ae0c674ee450a35109e8a.tar.gz taler-android-08b10a2408f958cae96ae0c674ee450a35109e8a.tar.bz2 taler-android-08b10a2408f958cae96ae0c674ee450a35109e8a.zip |
[pos] delete unpaid/unclaimed orders when user cancels or timeout happens
This is still not working due to a bug in the merchant API
3 files changed, 41 insertions, 13 deletions
diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt index 335e42d..06388f4 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt @@ -16,26 +16,27 @@ package net.taler.merchantlib +import android.util.Log import io.ktor.client.HttpClient import io.ktor.client.engine.okhttp.OkHttp import io.ktor.client.features.json.JsonFeature import io.ktor.client.features.json.serializer.KotlinxSerializer +import io.ktor.client.request.delete import io.ktor.client.request.get import io.ktor.client.request.header import io.ktor.client.request.post +import io.ktor.client.statement.HttpResponse +import io.ktor.client.statement.readBytes import io.ktor.http.ContentType.Application.Json import io.ktor.http.HttpHeaders.Authorization import io.ktor.http.contentType import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonConfiguration import net.taler.common.ContractTerms -import net.taler.merchantlib.Response.Companion.failure -import net.taler.merchantlib.Response.Companion.success +import net.taler.merchantlib.Response.Companion.response class MerchantApi(private val httpClient: HttpClient) { - constructor() : this(getDefaultHttpClient()) - suspend fun getConfig(baseUrl: String): ConfigResponse { return httpClient.get("$baseUrl/config") } @@ -60,16 +61,22 @@ class MerchantApi(private val httpClient: HttpClient) { } as CheckPaymentResponse } - private suspend fun <T> response(request: suspend () -> T): Response<T> { - return try { - success(request()) - } catch (e: Throwable) { - failure(e) - } + suspend fun deleteOrder( + merchantConfig: MerchantConfig, + orderId: String + ): Response<HttpResponse> = response { + val resp = httpClient.delete(merchantConfig.urlFor("private/orders/$orderId")) { + header(Authorization, "ApiKey ${merchantConfig.apiKey}") + } as HttpResponse + // TODO remove when the API call was fixed + Log.e("TEST", "status: ${resp.status.value}") + Log.e("TEST", String(resp.readBytes())) + resp } + } -private fun getDefaultHttpClient(): HttpClient = HttpClient(OkHttp) { +fun getDefaultHttpClient(): HttpClient = HttpClient(OkHttp) { install(JsonFeature) { serializer = getSerializer() } diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt b/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt index 23fa101..eb1ef27 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt @@ -25,6 +25,14 @@ class Response<out T> private constructor( ) { companion object { + suspend fun <T> response(request: suspend () -> T): Response<T> { + return try { + success(request()) + } catch (e: Throwable) { + failure(e) + } + } + fun <T> success(value: T): Response<T> = Response(value) diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt index e238284..ea16cb4 100644 --- a/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt +++ b/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt @@ -18,6 +18,7 @@ package net.taler.merchantpos.payment import android.content.Context import android.os.CountDownTimer +import android.util.Log import androidx.annotation.UiThread import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData @@ -27,6 +28,7 @@ import kotlinx.coroutines.launch import net.taler.merchantlib.CheckPaymentResponse import net.taler.merchantlib.MerchantApi import net.taler.merchantlib.PostOrderResponse +import net.taler.merchantpos.MainActivity.Companion.TAG import net.taler.merchantpos.R import net.taler.merchantpos.config.ConfigManager import net.taler.merchantpos.order.Order @@ -54,8 +56,7 @@ class PaymentManager( } override fun onFinish() { - val str = context.getString(R.string.error_timeout) - payment.value?.copy(error = str)?.let { mPayment.value = it } + cancelPayment(context.getString(R.string.error_timeout)) } } @@ -97,7 +98,19 @@ class PaymentManager( cancelPayment(error) } + @UiThread fun cancelPayment(error: String) { + // delete unpaid order + val merchantConfig = configManager.merchantConfig!! + mPayment.value?.let { payment -> + if (!payment.paid) payment.orderId?.let { orderId -> + Log.e(TAG, "Deleting cancelled and unpaid order $orderId") + scope.launch(Dispatchers.IO) { + api.deleteOrder(merchantConfig.convert(), orderId) + } + } + } + mPayment.value = mPayment.value!!.copy(error = error) checkTimer.cancel() } |