cashless2ecash

cashless2ecash: pay with cards for digital cash (experimental)
Log | Files | Refs | README

WalleeResponseHandler.kt (5042B)


      1 // This file is part of taler-cashless2ecash.
      2 // Copyright (C) 2024 Joel Häberli
      3 //
      4 // taler-cashless2ecash is free software: you can redistribute it and/or modify it
      5 // under the terms of the GNU Affero General Public License as published
      6 // by the Free Software Foundation, either version 3 of the License,
      7 // or (at your option) any later version.
      8 //
      9 // taler-cashless2ecash is distributed in the hope that it will be useful, but
     10 // WITHOUT ANY WARRANTY; without even the implied warranty of
     11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12 // Affero General Public License for more details.
     13 //
     14 // You should have received a copy of the GNU Affero General Public License
     15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 //
     17 // SPDX-License-Identifier: AGPL3.0-or-later
     18 
     19 package ch.bfh.habej2.wallee_c2ec.client.wallee
     20 
     21 import android.app.Activity
     22 import android.content.Intent
     23 import android.os.Handler
     24 import android.os.Looper
     25 import android.widget.Toast
     26 import ch.bfh.habej2.wallee_c2ec.R
     27 import ch.bfh.habej2.wallee_c2ec.withdrawal.WithdrawalActivity
     28 import ch.bfh.habej2.wallee_c2ec.withdrawal.WithdrawalViewModel
     29 import com.wallee.android.till.sdk.ResponseHandler
     30 import com.wallee.android.till.sdk.Utils
     31 import com.wallee.android.till.sdk.data.FinalBalanceResult
     32 import com.wallee.android.till.sdk.data.TransactionCompletionResponse
     33 import com.wallee.android.till.sdk.data.TransactionResponse
     34 
     35 class WalleeResponseHandler(
     36     private val activity: Activity,
     37 ) : ResponseHandler() {
     38 
     39     private val enableDebugLogs = true
     40 
     41 
     42     override fun authorizeTransactionReply(response: TransactionResponse?) {
     43 
     44         if (response == null) {
     45             Handler(Looper.getMainLooper()).post {
     46                 Toast.makeText(activity.baseContext, activity.getText(
     47                     R.string.no_authorization), Toast.LENGTH_SHORT).show()}
     48             activity.finish()
     49             return
     50         }
     51 
     52         if (enableDebugLogs)
     53             this.printTransactionResponse(response)
     54 
     55         val intent = Intent(activity, WithdrawalActivity::class.java)
     56         intent.putExtras(Utils.toBundle(response))
     57         intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
     58         activity.startActivity(intent)
     59     }
     60 
     61     override fun completeTransactionReply(response: TransactionCompletionResponse?) {
     62 
     63         if (response == null) {
     64             Handler(Looper.getMainLooper()).post {
     65                 Toast.makeText(activity.baseContext, activity.getText(
     66                     R.string.no_complete_reply), Toast.LENGTH_SHORT).show()}
     67 
     68             activity.finish()
     69             return
     70         }
     71 
     72         if (enableDebugLogs)
     73             this.printCompletionResponse(response)
     74 
     75         val intent = Intent(activity, WithdrawalActivity::class.java)
     76         intent.putExtras(Utils.toBundle(response))
     77         intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
     78         activity.startActivity(intent)
     79     }
     80 
     81     override fun executeFinalBalanceReply(result: FinalBalanceResult?) {
     82 
     83         if (result == null) {
     84             println("executed final balance but got no result")
     85         } else {
     86             println("executed final balance: ${result.resultCode.code} ${result.resultCode.description}")
     87         }
     88     }
     89 
     90     override fun checkApiServiceCompatibilityReply(
     91         isCompatible: Boolean?,
     92         apiServiceVersion: String?
     93     ) {
     94 
     95         println("terminal app sdk compatible: $isCompatible")
     96         if (isCompatible == null || !isCompatible) {
     97             // just dont start withdrawals when api is not compatible
     98             println("not starting application because sdk version not compatible")
     99             Handler(Looper.getMainLooper()).post {
    100                 Toast.makeText(activity.baseContext, activity.getText(
    101                     R.string.not_compatible), Toast.LENGTH_SHORT).show()}
    102             activity.finish()
    103         }
    104     }
    105 
    106     private fun printTransactionResponse(response: TransactionResponse) {
    107 
    108         println("C2EC-TRANSACTION-RESPONSE: $response")
    109 
    110         if (response.transaction.metaData.isNotEmpty()) {
    111             response.transaction.metaData.entries.forEach {
    112                 println("METADATA: ${it.key}=${it.value}")
    113             }
    114         } else {
    115             println("METADATA EMPTY")
    116         }
    117 
    118         println("TRANSACTION: ${response.transaction.totalAmountIncludingTax} ${response.transaction.currency}")
    119         response.transaction.lineItems.forEach {
    120             println("TRANSACTION: LINE-ITEM id=${it.id}, name=${it.name}, amount-including-taxes=${it.totalAmountIncludingTax}")
    121         }
    122         println("TRANSACTION: ${response.state}")
    123         println("TRANSACTION: ${response.authorizationCode}")
    124         println("TRANSACTION: ${response.acquirerId}")
    125         println("TRANSACTION: ${response.reserveReference}")
    126     }
    127 
    128     private fun printCompletionResponse(response: TransactionCompletionResponse) {
    129 
    130         println("C2EC-COMPLETION-RESPONSE: ${response.transactionCompletion}")
    131     }
    132 }