Extracted TanMethodSelector

This commit is contained in:
dankito 2021-11-01 19:53:30 +01:00
parent 3e8d1e6dc6
commit 1b2b960c1b
3 changed files with 45 additions and 20 deletions

View File

@ -11,6 +11,7 @@ import net.dankito.banking.fints.transactions.IAccountTransactionsParser
import net.dankito.banking.fints.transactions.Mt940AccountTransactionsParser import net.dankito.banking.fints.transactions.Mt940AccountTransactionsParser
import net.dankito.banking.fints.util.IBase64Service import net.dankito.banking.fints.util.IBase64Service
import net.dankito.banking.fints.util.PureKotlinBase64Service import net.dankito.banking.fints.util.PureKotlinBase64Service
import net.dankito.banking.fints.util.TanMethodSelector
import net.dankito.banking.fints.webclient.IWebClient import net.dankito.banking.fints.webclient.IWebClient
import net.dankito.banking.fints.webclient.KtorWebClient import net.dankito.banking.fints.webclient.KtorWebClient
@ -22,6 +23,7 @@ open class FinTsClientForCustomer(
messageBuilder: MessageBuilder = MessageBuilder(), messageBuilder: MessageBuilder = MessageBuilder(),
mt940Parser: IAccountTransactionsParser = Mt940AccountTransactionsParser(), mt940Parser: IAccountTransactionsParser = Mt940AccountTransactionsParser(),
modelMapper: ModelMapper = ModelMapper(messageBuilder), modelMapper: ModelMapper = ModelMapper(messageBuilder),
protected open val tanMethodSelector: TanMethodSelector = TanMethodSelector(),
product: ProductData = ProductData("15E53C26816138699C7B6A3E8", "1.0.0") // TODO: get version dynamically) product: ProductData = ProductData("15E53C26816138699C7B6A3E8", "1.0.0") // TODO: get version dynamically)
) { ) {
@ -30,7 +32,7 @@ open class FinTsClientForCustomer(
: this(bank, callback, RequestExecutor(MessageBuilder(), webClient, base64Service)) : this(bank, callback, RequestExecutor(MessageBuilder(), webClient, base64Service))
protected val client = FinTsClient(FinTsJobExecutor(callback, requestExecutor, messageBuilder, mt940Parser, modelMapper, product)) protected val client = FinTsClient(FinTsJobExecutor(callback, requestExecutor, messageBuilder, mt940Parser, modelMapper, tanMethodSelector, product))
open val messageLogWithoutSensitiveData: List<MessageLogEntry> open val messageLogWithoutSensitiveData: List<MessageLogEntry>

View File

@ -20,6 +20,7 @@ import net.dankito.banking.fints.tan.FlickerCodeDecoder
import net.dankito.banking.fints.tan.TanImageDecoder import net.dankito.banking.fints.tan.TanImageDecoder
import net.dankito.banking.fints.transactions.IAccountTransactionsParser import net.dankito.banking.fints.transactions.IAccountTransactionsParser
import net.dankito.banking.fints.transactions.Mt940AccountTransactionsParser import net.dankito.banking.fints.transactions.Mt940AccountTransactionsParser
import net.dankito.banking.fints.util.TanMethodSelector
import net.dankito.utils.multiplatform.log.LoggerFactory import net.dankito.utils.multiplatform.log.LoggerFactory
import net.dankito.utils.multiplatform.Date import net.dankito.utils.multiplatform.Date
import net.dankito.utils.multiplatform.ObjectReference import net.dankito.utils.multiplatform.ObjectReference
@ -36,6 +37,7 @@ open class FinTsJobExecutor(
protected open val messageBuilder: MessageBuilder = MessageBuilder(), protected open val messageBuilder: MessageBuilder = MessageBuilder(),
protected open val mt940Parser: IAccountTransactionsParser = Mt940AccountTransactionsParser(), protected open val mt940Parser: IAccountTransactionsParser = Mt940AccountTransactionsParser(),
protected open val modelMapper: ModelMapper = ModelMapper(messageBuilder), protected open val modelMapper: ModelMapper = ModelMapper(messageBuilder),
protected open val tanMethodSelector: TanMethodSelector = TanMethodSelector(),
protected open val product: ProductData = ProductData("15E53C26816138699C7B6A3E8", "1.0.0") // TODO: get version dynamically protected open val product: ProductData = ProductData("15E53C26816138699C7B6A3E8", "1.0.0") // TODO: get version dynamically
) { ) {
@ -680,14 +682,15 @@ open class FinTsJobExecutor(
done(true) done(true)
} }
else { else {
findPreferredTanMethod(bank, preferredTanMethods)?.let { tanMethodSelector.findPreferredTanMethod(bank.tanMethodsAvailableForUser, preferredTanMethods)?.let {
bank.selectedTanMethod = it bank.selectedTanMethod = it
done(true) done(true)
return return
} }
// we know user's supported tan methods, now ask user which one to select // we know user's supported tan methods, now ask user which one to select
callback.askUserForTanMethod(bank.tanMethodsAvailableForUser, selectSuggestedTanMethod(bank)) { selectedTanMethod -> val suggestedTanMethod = tanMethodSelector.getSuggestedTanMethod(bank.tanMethodsAvailableForUser)
callback.askUserForTanMethod(bank.tanMethodsAvailableForUser, suggestedTanMethod) { selectedTanMethod ->
if (selectedTanMethod != null) { if (selectedTanMethod != null) {
bank.selectedTanMethod = selectedTanMethod bank.selectedTanMethod = selectedTanMethod
done(true) done(true)
@ -699,23 +702,6 @@ open class FinTsJobExecutor(
} }
} }
private fun findPreferredTanMethod(bank: BankData, preferredTanMethods: List<TanMethodType>?): TanMethod? {
preferredTanMethods?.forEach { preferredTanMethodType ->
bank.tanMethodsAvailableForUser.firstOrNull { it.type == preferredTanMethodType }?.let {
return it
}
}
return null
}
protected open fun selectSuggestedTanMethod(bank: BankData): TanMethod? {
return bank.tanMethodsAvailableForUser.firstOrNull { it.type != TanMethodType.ChipTanUsb && it.type != TanMethodType.SmsTan && it.type != TanMethodType.ChipTanManuell }
?: bank.tanMethodsAvailableForUser.firstOrNull { it.type != TanMethodType.ChipTanUsb && it.type != TanMethodType.SmsTan }
?: bank.tanMethodsAvailableForUser.firstOrNull { it.type != TanMethodType.ChipTanUsb }
?: bank.tanMethodsAvailableForUser.firstOrNull()
}
protected open fun updateBankData(bank: BankData, response: BankResponse) { protected open fun updateBankData(bank: BankData, response: BankResponse) {
modelMapper.updateBankData(bank, response) modelMapper.updateBankData(bank, response)

View File

@ -0,0 +1,37 @@
package net.dankito.banking.fints.util
import net.dankito.banking.fints.model.TanMethod
import net.dankito.banking.fints.model.TanMethodType
open class TanMethodSelector {
companion object {
val NonVisual = listOf(TanMethodType.AppTan, TanMethodType.SmsTan, TanMethodType.ChipTanManuell, TanMethodType.EnterTan)
}
open fun getSuggestedTanMethod(tanMethods: List<TanMethod>): TanMethod? {
return tanMethods.firstOrNull { it.type != TanMethodType.ChipTanUsb && it.type != TanMethodType.SmsTan && it.type != TanMethodType.ChipTanManuell }
?: tanMethods.firstOrNull { it.type != TanMethodType.ChipTanUsb && it.type != TanMethodType.SmsTan }
?: tanMethods.firstOrNull { it.type != TanMethodType.ChipTanUsb }
?: tanMethods.firstOrNull()
}
open fun findPreferredTanMethod(tanMethods: List<TanMethod>, preferredTanMethods: List<TanMethodType>?): TanMethod? {
preferredTanMethods?.forEach { preferredTanMethodType ->
tanMethods.firstOrNull { it.type == preferredTanMethodType }?.let {
return it
}
}
return null
}
open fun selectNonVisual(tanMethods: List<TanMethod>): TanMethod? {
return findPreferredTanMethod(tanMethods, NonVisual)
?: tanMethods.firstOrNull { it.displayName.contains("manuell", true) }
?: tanMethods.firstOrNull()
}
}