MerchantApi.kt (3999B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2020 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.merchantlib 18 19 import io.ktor.client.HttpClient 20 import io.ktor.client.call.body 21 import io.ktor.client.request.HttpRequestBuilder 22 import io.ktor.client.request.delete 23 import io.ktor.client.request.get 24 import io.ktor.client.request.header 25 import io.ktor.client.request.post 26 import io.ktor.client.request.parameter 27 import io.ktor.client.request.setBody 28 import io.ktor.http.ContentType.Application.Json 29 import io.ktor.http.HttpHeaders.Authorization 30 import io.ktor.http.contentType 31 import kotlinx.coroutines.CoroutineDispatcher 32 import kotlinx.coroutines.Dispatchers 33 import kotlinx.coroutines.withContext 34 import net.taler.common.getDefaultHttpClient 35 import net.taler.merchantlib.Response.Companion.response 36 37 class MerchantApi( 38 private val httpClient: HttpClient = getDefaultHttpClient(), 39 private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO, 40 ) { 41 42 suspend fun getConfig(baseUrl: String): Response<ConfigResponse> = withContext(ioDispatcher) { 43 response { 44 httpClient.get("$baseUrl/config").body() 45 } 46 } 47 48 suspend fun postOrder( 49 merchantConfig: MerchantConfig, 50 orderRequest: PostOrderRequest, 51 ): Response<PostOrderResponse> = withContext(ioDispatcher) { 52 response { 53 httpClient.post(merchantConfig.urlFor("private/orders")) { 54 auth(merchantConfig) 55 contentType(Json) 56 setBody(orderRequest) 57 }.body() 58 } 59 } 60 61 suspend fun checkOrder( 62 merchantConfig: MerchantConfig, 63 orderId: String, 64 ): Response<CheckPaymentResponse> = withContext(ioDispatcher) { 65 response { 66 httpClient.get(merchantConfig.urlFor("private/orders/$orderId")) { 67 auth(merchantConfig) 68 }.body() 69 } 70 } 71 72 suspend fun deleteOrder( 73 merchantConfig: MerchantConfig, 74 orderId: String, 75 force: Boolean = false, 76 ): Response<Unit> = withContext(ioDispatcher) { 77 response { 78 httpClient.delete(merchantConfig.urlFor("private/orders/$orderId")) { 79 auth(merchantConfig) 80 if (force) parameter("force", "yes") 81 }.body() 82 } 83 } 84 85 suspend fun getOrderHistory( 86 merchantConfig: MerchantConfig, 87 limit: Int = -20, 88 offset: Long? = null, 89 ): Response<OrderHistory> = 90 withContext(ioDispatcher) { 91 response { 92 httpClient.get(merchantConfig.urlFor("private/orders")) { 93 auth(merchantConfig) 94 parameter("limit", limit) 95 offset?.let { parameter("offset", it) } 96 }.body() 97 } 98 } 99 100 suspend fun giveRefund( 101 merchantConfig: MerchantConfig, 102 orderId: String, 103 request: RefundRequest, 104 ): Response<RefundResponse> = withContext(ioDispatcher) { 105 response { 106 httpClient.post(merchantConfig.urlFor("private/orders/$orderId/refund")) { 107 auth(merchantConfig) 108 contentType(Json) 109 setBody(request) 110 }.body() 111 } 112 } 113 114 private fun HttpRequestBuilder.auth(merchantConfig: MerchantConfig) { 115 header(Authorization, "Bearer ${merchantConfig.apiKey}") 116 } 117 }