From bc35e8924e652c323001f62f6781657545fa378f Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 28 Jul 2020 17:16:57 -0300 Subject: [pos] adapt history to new v1 API --- .../main/java/net/taler/merchantlib/MerchantApi.kt | 19 +++++---- .../java/net/taler/merchantlib/MerchantConfig.kt | 2 +- .../java/net/taler/merchantlib/OrderHistory.kt | 46 ++++++++++++++++++++++ .../java/net/taler/merchantlib/PostOrderRequest.kt | 6 +-- .../main/java/net/taler/merchantlib/Response.kt | 7 +++- 5 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 merchant-lib/src/main/java/net/taler/merchantlib/OrderHistory.kt (limited to 'merchant-lib/src') 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 e995724..db37586 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt @@ -16,7 +16,6 @@ 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 @@ -25,8 +24,6 @@ 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 @@ -64,14 +61,16 @@ class MerchantApi(private val httpClient: HttpClient) { suspend fun deleteOrder( merchantConfig: MerchantConfig, orderId: String - ): Response = response { - val resp = httpClient.delete(merchantConfig.urlFor("private/orders/$orderId")) { + ): Response = response { + 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 + } as Unit + } + + suspend fun getOrderHistory(merchantConfig: MerchantConfig): Response = response { + httpClient.get(merchantConfig.urlFor("private/orders")) { + header(Authorization, "ApiKey ${merchantConfig.apiKey}") + } as OrderHistory } } diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantConfig.kt b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantConfig.kt index a01624e..a8d113e 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantConfig.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantConfig.kt @@ -31,7 +31,7 @@ data class MerchantConfig( fun urlFor(endpoint: String): String { val sb = StringBuilder(baseUrl) if (sb.last() != '/') sb.append('/') - sb.append("instances/$instance/") + instance?.let { sb.append("instances/$it/") } sb.append(endpoint) return sb.toString() } diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/OrderHistory.kt b/merchant-lib/src/main/java/net/taler/merchantlib/OrderHistory.kt new file mode 100644 index 0000000..718bde5 --- /dev/null +++ b/merchant-lib/src/main/java/net/taler/merchantlib/OrderHistory.kt @@ -0,0 +1,46 @@ +/* + * 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 + */ + +package net.taler.merchantlib + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import net.taler.common.Amount +import net.taler.common.Timestamp + +@Serializable +data class OrderHistory( + val orders: List +) + +@Serializable +data class OrderHistoryEntry( + // order ID of the transaction related to this entry. + @SerialName("order_id") + val orderId: String, + + // when the order was created + val timestamp: Timestamp, + + // the amount of money the order is for + val amount: Amount, + + // the summary of the order + val summary: String, + + // whether some part of the order is refundable + val refundable: Boolean +) diff --git a/merchant-lib/src/main/java/net/taler/merchantlib/PostOrderRequest.kt b/merchant-lib/src/main/java/net/taler/merchantlib/PostOrderRequest.kt index a6e74d6..4854a80 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/PostOrderRequest.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/PostOrderRequest.kt @@ -48,11 +48,11 @@ sealed class CheckPaymentResponse { override fun deserialize(decoder: Decoder): CheckPaymentResponse { val input = decoder as JsonInput val tree = input.decodeJson() as JsonObject - val paid = tree.getPrimitive("paid").boolean -// return if (paid) decoder.json.fromJson(Paid.serializer(), tree) + val orderStatus = tree.getPrimitive("order_status").content +// return if (orderStatus == "paid") decoder.json.fromJson(Paid.serializer(), tree) // else decoder.json.fromJson(Unpaid.serializer(), tree) // manual parsing due to https://github.com/Kotlin/kotlinx.serialization/issues/576 - return if (paid) Paid( + return if (orderStatus == "paid") Paid( refunded = tree.getPrimitive("refunded").boolean ) else Unpaid( talerPayUri = tree.getPrimitive("taler_pay_uri").content 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 1b49d78..9aefa5f 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/Response.kt @@ -16,8 +16,11 @@ package net.taler.merchantlib +import android.util.Log import io.ktor.client.call.receive import io.ktor.client.features.ClientRequestException +import io.ktor.client.features.ResponseException +import io.ktor.client.features.ServerResponseException import kotlinx.serialization.Serializable class Response private constructor( @@ -29,6 +32,7 @@ class Response private constructor( return try { success(request()) } catch (e: Throwable) { + Log.e("merchant-lib", "Error", e) failure(e) } } @@ -63,10 +67,11 @@ class Response private constructor( private suspend fun getFailureString(failure: Failure): String = when (failure.exception) { is ClientRequestException -> getExceptionString(failure.exception) + is ServerResponseException -> getExceptionString(failure.exception) else -> failure.exception.toString() } - private suspend fun getExceptionString(e: ClientRequestException): String { + private suspend fun getExceptionString(e: ResponseException): String { return try { val error: Error = e.response.receive() "Error ${error.code}: ${error.hint}" -- cgit v1.2.3