diff options
author | Torsten Grote <t@grobox.de> | 2020-07-22 16:53:06 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-07-22 16:53:06 -0300 |
commit | a8c811f6cdf4bf1b787ebaaa9fd220588fd1ffcf (patch) | |
tree | 604cd516a904f9c7474e37a87f92018f55ca4d1b /merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt | |
parent | b9d7d6edb7cc12e5238168c4446a28c0ae98c729 (diff) | |
download | taler-android-a8c811f6cdf4bf1b787ebaaa9fd220588fd1ffcf.tar.gz taler-android-a8c811f6cdf4bf1b787ebaaa9fd220588fd1ffcf.tar.bz2 taler-android-a8c811f6cdf4bf1b787ebaaa9fd220588fd1ffcf.zip |
[pos] migrate order posting and checking to v1 API and merchant-lib
Diffstat (limited to 'merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt')
-rw-r--r-- | merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt | 48 |
1 files changed, 47 insertions, 1 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 3406f78..335e42d 100644 --- a/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt +++ b/merchant-lib/src/main/java/net/taler/merchantlib/MerchantApi.kt @@ -21,6 +21,16 @@ 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.get +import io.ktor.client.request.header +import io.ktor.client.request.post +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 class MerchantApi(private val httpClient: HttpClient) { @@ -30,10 +40,46 @@ class MerchantApi(private val httpClient: HttpClient) { return httpClient.get("$baseUrl/config") } + suspend fun postOrder( + merchantConfig: MerchantConfig, + contractTerms: ContractTerms + ): Response<PostOrderResponse> = response { + httpClient.post(merchantConfig.urlFor("private/orders")) { + header(Authorization, "ApiKey ${merchantConfig.apiKey}") + contentType(Json) + body = PostOrderRequest(contractTerms) + } as PostOrderResponse + } + + suspend fun checkOrder( + merchantConfig: MerchantConfig, + orderId: String + ): Response<CheckPaymentResponse> = response { + httpClient.get(merchantConfig.urlFor("private/orders/$orderId")) { + header(Authorization, "ApiKey ${merchantConfig.apiKey}") + } as CheckPaymentResponse + } + + private suspend fun <T> response(request: suspend () -> T): Response<T> { + return try { + success(request()) + } catch (e: Throwable) { + failure(e) + } + } } private fun getDefaultHttpClient(): HttpClient = HttpClient(OkHttp) { install(JsonFeature) { - serializer = KotlinxSerializer() + serializer = getSerializer() } } + +fun getSerializer() = KotlinxSerializer( + Json( + JsonConfiguration( + encodeDefaults = false, + ignoreUnknownKeys = true + ) + ) +) |