From 62fe93e88edf3b317fe2159b6f894bc25920aea6 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 17 Sep 2024 17:23:30 +0200 Subject: [PATCH] Added example for reading TAN from command line --- README.md | 29 +++++++++++++++++-- .../client/fints4k/example/ShowUsage.kt | 16 ++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 03ced598..66b6aa3b 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,10 @@ class ShowUsage { This fetches the booked account transactions of the last 90 days. In most cases no TAN is required for this. -You can also specify options e.g. which transactions should be retrieved: + +#### GetAccountData parameter + +You can also specify options e.g. which transactions you would like to retrieve: ```kotlin val options = GetAccountDataOptions( @@ -95,15 +98,35 @@ val options = GetAccountDataOptions( val response = client.getAccountData(options) ``` -Retrieving transactions older than 90 days requires a TAN, so add TAN handling in Client Callback: +### TAN handling + +Retrieving transactions older than 90 days or sometimes even log in requires a TAN, so add TAN handling in Client Callback: ```kotlin val client = FinTs4kBankingClientForUser(bankCode, loginName, password, SimpleBankingClientCallback { tanChallenge, callback -> - val tan: String? = null // if a TAN is required, add a UI or ... + val tan: String? = null // if a TAN is required, read TAN from command line, add a UI, ... callback.invoke(EnterTanResult(tan)) // ... set a break point here, get TAN e.g. from your TAN app, set tan variable in debugger view and resume debugger }) ``` +E.g. TAN handling on the command line: + +```kotlin +println("Enter password for $bankCode:") +val password = readln() // as an alternative for hard coding password; of course can also be done for bankCode and login name + +val client = FinTs4kBankingClientForUser(bankCode, loginName, password, SimpleBankingClientCallback { tanChallenge, callback -> + println("A TAN is required for ${tanChallenge.forAction}. Selected TAN method is '${tanChallenge.selectedTanMethod.displayName}'. Messsage of your credit institute:") + println(tanChallenge.messageToShowToUser) + println("Get TAN from your TAN app etc., enter it and press Enter (or press Enter without an input to abort process):") + + val tan: String? = readlnOrNull().takeUnless { it.isNullOrBlank() } // map empty input to null to abort process + callback.invoke(EnterTanResult(tan)) +}) +``` + +### Error handling + Add some error handling by checking `response.error`: ```kotlin diff --git a/SampleApp/src/main/kotlin/net/codinux/banking/client/fints4k/example/ShowUsage.kt b/SampleApp/src/main/kotlin/net/codinux/banking/client/fints4k/example/ShowUsage.kt index 0651899c..b3d96f5c 100644 --- a/SampleApp/src/main/kotlin/net/codinux/banking/client/fints4k/example/ShowUsage.kt +++ b/SampleApp/src/main/kotlin/net/codinux/banking/client/fints4k/example/ShowUsage.kt @@ -42,9 +42,16 @@ class ShowUsage { } fun getAccountDataFullExample() { + println("Enter password for $bankCode:") + val password = readln() // as an alternative for hard coding password; of course can also be done for bankCode and login name + val client = FinTs4kBankingClientForUser(bankCode, loginName, password, SimpleBankingClientCallback { tanChallenge, callback -> - val tan: String? = null // if a TAN is required, add a UI or ... - callback.invoke(EnterTanResult(tan)) // ... set a break point here, get TAN e.g. from your TAN app, set tan variable in debugger view and resume debugger + println("A TAN is required for ${tanChallenge.forAction}. Selected TAN method is '${tanChallenge.selectedTanMethod.displayName}'. Messsage of your credit institute:") + println(tanChallenge.messageToShowToUser) + println("Get TAN from your TAN app etc., enter it and press Enter (or press Enter without an input to abort process):") + + val tan: String? = readlnOrNull().takeUnless { it.isNullOrBlank() } // map empty input to null to abort process + callback.invoke(EnterTanResult(tan)) }) val options = GetAccountDataOptions( @@ -65,7 +72,10 @@ class ShowUsage { fun updateAccountTransactions() { - val client = FinTs4kBankingClientForUser(bankCode, loginName, password, SimpleBankingClientCallback()) + val client = FinTs4kBankingClientForUser(bankCode, loginName, password, SimpleBankingClientCallback { tanChallenge, callback -> + val tan: String? = readlnOrNull() // if a TAN is required, read TAN from command line, add a UI, ... + callback.invoke(EnterTanResult(tan)) // ... set a break point here, get TAN e.g. from your TAN app, set tan variable in debugger view and resume debugger + }) // simulate account transactions we retrieved last time val today = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date