diff options
Diffstat (limited to 'wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/client/taler/BankIntegrationClient.kt')
-rw-r--r-- | wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/client/taler/BankIntegrationClient.kt | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/client/taler/BankIntegrationClient.kt b/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/client/taler/BankIntegrationClient.kt new file mode 100644 index 0000000..55b9b8d --- /dev/null +++ b/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/client/taler/BankIntegrationClient.kt @@ -0,0 +1,126 @@ +package ch.bfh.habej2.wallee_c2ec.client.taler + +import ch.bfh.habej2.wallee_c2ec.client.taler.model.BankIntegrationConfig +import ch.bfh.habej2.wallee_c2ec.client.taler.model.PaymentNotification +import ch.bfh.habej2.wallee_c2ec.client.taler.model.WithdrawalOperation +import ch.bfh.habej2.wallee_c2ec.client.taler.model.WithdrawalOperationStatus +import ch.bfh.habej2.wallee_c2ec.config.TalerBankIntegrationConfig +import com.squareup.moshi.Moshi +import okhttp3.HttpUrl +import okhttp3.Interceptor +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response +import java.util.Optional + +class BankIntegrationClientException( + val status: Int, + msg: String +): RuntimeException(msg) + +class BankIntegrationClient( + private val config: TalerBankIntegrationConfig +) { + + private val WITHDRAWAL_OP = "withdrawal-operation" + + private val client: OkHttpClient = + OkHttpClient.Builder() + .addInterceptor(C2ECBasicAuthInterceptor(config)) + .build() + + private fun baseUrlBuilder() = HttpUrl.Builder() + .encodedPath(config.bankIntegrationBaseUrl) + + private fun configUrl() = baseUrlBuilder() + .addPathSegment("config") + .build() + private fun withdrawalByWopid(encodedWopid: String) = baseUrlBuilder() + .addPathSegment(WITHDRAWAL_OP) + .addPathSegment(encodedWopid) + .build() + + private fun <T> serializer(clazz: Class<T>) = Moshi.Builder().build().adapter(clazz) + + private fun withdrawalConfirm(encodedWopid: String) = withdrawalByWopid(encodedWopid) + .newBuilder() + .addPathSegment("confirm") + .build() + private fun withdrawalAbort(encodedWopid: String) = withdrawalByWopid(encodedWopid) + .newBuilder() + .addPathSegment("abort") + .build() + + fun retrieveBankIntegrationConfig(): Optional<BankIntegrationConfig> { + + val req = Request.Builder() + .get() + .url(configUrl()) + .build() + val response = client.newCall(req).execute() + return parseOrEmpty(response) + } + + fun retrieveWithdrawalStatus( + wopid: String, + longPollMs: Int, + oldState: WithdrawalOperationStatus = WithdrawalOperationStatus.PENDING + ): Optional<WithdrawalOperation> { + + val req = Request.Builder() + .get() + .url(withdrawalByWopid(wopid) + .newBuilder() + .addQueryParameter("long_poll_ms", longPollMs.toString()) + .addQueryParameter("old_state", oldState.value) + .build() + ) + .build() + val response = client.newCall(req).execute() + return parseOrEmpty(response) + } + + fun sendPaymentNotification(payment: PaymentNotification) { + + + println("sending payment notification...") + } + + fun abortWithdrawal(wopid: String) { + println("aborting withdrawal") + } + + private inline fun <reified T: Any> parseOrEmpty(response: Response): Optional<T> { + + if (response.isSuccessful) { + if (response.body != null) { + val content = serializer(T::class.java).fromJson(response.body!!.source()) + if (content != null) { + return Optional.of(content) + } + return Optional.empty() + } + return Optional.empty() + } + throw BankIntegrationClientException(response.code, "request unsuccessful") + } + + private class C2ECBasicAuthInterceptor( + private val config: TalerBankIntegrationConfig + ) : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + + val base64EncodedCredentials = java.util.Base64 + .getUrlEncoder() + .encode("${config.terminalId}:${config.accessToken}".toByteArray()) + .toString() + + return chain.proceed( + chain.request().newBuilder() + .header("Authorization", base64EncodedCredentials) + .build() + ) + } + } +}
\ No newline at end of file |