RefundManager.kt (1923B)
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.wallet.refund 18 19 import androidx.lifecycle.LiveData 20 import androidx.lifecycle.MutableLiveData 21 import kotlinx.coroutines.CoroutineScope 22 import kotlinx.coroutines.launch 23 import kotlinx.serialization.Serializable 24 import net.taler.wallet.backend.TalerErrorInfo 25 import net.taler.wallet.backend.WalletBackendApi 26 27 sealed class RefundStatus { 28 data class Error(val error: TalerErrorInfo) : RefundStatus() 29 data class Success(val response: StartRefundQueryForUriResponse) : RefundStatus() 30 } 31 32 @Serializable 33 data class StartRefundQueryForUriResponse( 34 val transactionId: String, 35 ) 36 37 class RefundManager( 38 private val api: WalletBackendApi, 39 private val scope: CoroutineScope 40 ) { 41 42 fun refund(refundUri: String): LiveData<RefundStatus> { 43 val liveData = MutableLiveData<RefundStatus>() 44 scope.launch { 45 api.request("startRefundQueryForUri", StartRefundQueryForUriResponse.serializer()) { 46 put("talerRefundUri", refundUri) 47 }.onError { 48 liveData.postValue(RefundStatus.Error(it)) 49 }.onSuccess { 50 liveData.postValue(RefundStatus.Success(it)) 51 } 52 } 53 return liveData 54 } 55 56 }