aboutsummaryrefslogtreecommitdiff
path: root/merchant-lib
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2020-07-23 11:54:34 -0300
committerTorsten Grote <t@grobox.de>2020-07-23 11:54:34 -0300
commit08b10a2408f958cae96ae0c674ee450a35109e8a (patch)
treed4bee31f1b007ca94f2dfe90b4e47b866481fe46 /merchant-lib
parenta8c811f6cdf4bf1b787ebaaa9fd220588fd1ffcf (diff)
downloadtaler-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
Diffstat (limited to 'merchant-lib')
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt29
-rw-r--r--merchant-lib/src/main/java/net/taler/merchantlib/Response.kt8
2 files changed, 26 insertions, 11 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)