From 29a2f29f724848c0b931077f3067ad7b16623ae3 Mon Sep 17 00:00:00 2001 From: dankl Date: Sun, 13 Oct 2019 19:44:16 +0200 Subject: [PATCH] Added JavaShowcase; did some adjustments so that Java users have a nicer interface --- .../kotlin/net/dankito/fints/FinTsClient.kt | 2 +- .../net/dankito/fints/model/CustomerData.kt | 5 ++ .../fints/model/GetTransactionsParameter.kt | 2 +- .../response/client/FinTsClientResponse.kt | 2 +- .../net/dankito/fints/java/JavaShowcase.java | 71 +++++++++++++++++++ .../net/dankito/fints/FinTsClientTest.kt | 8 +-- 6 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 fints4javaLib/src/test/java/net/dankito/fints/java/JavaShowcase.java diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/FinTsClient.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/FinTsClient.kt index b6a8c158..a92f22aa 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/FinTsClient.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/FinTsClient.kt @@ -22,7 +22,7 @@ import org.slf4j.LoggerFactory import java.math.BigDecimal -open class FinTsClient( +open class FinTsClient @JvmOverloads constructor( protected val base64Service: IBase64Service, protected val webClient: IWebClient = OkHttpWebClient(), protected val messageBuilder: MessageBuilder = MessageBuilder(), diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/model/CustomerData.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/model/CustomerData.kt index d5162207..5ddb424f 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/model/CustomerData.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/model/CustomerData.kt @@ -26,6 +26,11 @@ open class CustomerData( val Anonymous = CustomerData(KundenID.Anonymous, "", customerSystemStatus = KundensystemStatusWerte.NichtBenoetigt) } + + // for Java + constructor(customerId: String, pin: String) : this(customerId, pin, customerId) + + override fun toString(): String { return customerId } diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/model/GetTransactionsParameter.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/model/GetTransactionsParameter.kt index d9e05d1d..b6fcefa9 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/model/GetTransactionsParameter.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/model/GetTransactionsParameter.kt @@ -3,7 +3,7 @@ package net.dankito.fints.model import java.util.* -open class GetTransactionsParameter( +open class GetTransactionsParameter @JvmOverloads constructor( val alsoRetrieveBalance: Boolean = true, val fromDate: Date? = null, val toDate: Date? = null, diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/response/client/FinTsClientResponse.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/response/client/FinTsClientResponse.kt index 97dfc405..5357c7b2 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/response/client/FinTsClientResponse.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/response/client/FinTsClientResponse.kt @@ -6,7 +6,7 @@ import net.dankito.fints.response.segments.TanResponse open class FinTsClientResponse( - val successful: Boolean, + val isSuccessful: Boolean, val isStrongAuthenticationRequired: Boolean, val tanRequired: TanResponse? = null, diff --git a/fints4javaLib/src/test/java/net/dankito/fints/java/JavaShowcase.java b/fints4javaLib/src/test/java/net/dankito/fints/java/JavaShowcase.java new file mode 100644 index 00000000..6c41bbd6 --- /dev/null +++ b/fints4javaLib/src/test/java/net/dankito/fints/java/JavaShowcase.java @@ -0,0 +1,71 @@ +package net.dankito.fints.java; + +import net.dankito.fints.FinTsClient; +import net.dankito.fints.banks.BankFinder; +import net.dankito.fints.messages.datenelemente.implementierte.signatur.Sicherheitsfunktion; +import net.dankito.fints.model.*; +import net.dankito.fints.model.mapper.BankDataMapper; +import net.dankito.fints.response.client.GetTransactionsResponse; +import net.dankito.fints.util.Java8Base64Service; + +import java.util.Date; +import java.util.List; + + +public class JavaShowcase { + + public static void main(String[] args) { + BankFinder bankFinder = new BankFinder(); + + // set your bank code (Bankleitzahl) here. Or create BankData manually. Required fields are: + // bankCode, bankCountryCode (Germany = 280), finTs3ServerAddress and for bank transfers bic + List foundBanks = bankFinder.findBankByBankCode("10070000"); + + if (foundBanks.isEmpty() == false) { + BankData bank = new BankDataMapper().mapFromBankInfo(foundBanks.get(0)); + // set your customer data (customerId = Kontonummer in most cases, pin = online banking pin) + CustomerData customer = new CustomerData("", ""); + customer.setSelectedTanProcedure(new TanProcedure("", Sicherheitsfunktion.PIN_TAN_911, TanProcedureType.ChipTan)); + + FinTsClient finTsClient = new FinTsClient(new Java8Base64Service()); + + // some banks support retrieving account transactions of last 90 days without TAN + long ninetyDaysAgoMilliseconds = 90 * 24 * 60 * 60 * 1000L; + Date ninetyDaysAgo = new Date(new Date().getTime() - ninetyDaysAgoMilliseconds); + + GetTransactionsResponse response = finTsClient.getTransactions( + new GetTransactionsParameter(true, ninetyDaysAgo), bank, customer); + + showResponse(response); + } + } + + private static void showResponse(GetTransactionsResponse response) { + if (response.isSuccessful()) { + System.out.println("Balance (Saldo) = " + response.getBalance()); + + System.out.println("Account transactions (Umsätze):"); + for (AccountTransaction transaction : response.getBookedTransactions()) { + System.out.println(transaction.toString()); + } + } + else { + if (response.isStrongAuthenticationRequired()) { + System.out.println("Sorry, your bank doesn't support retrieving account " + + "transactions of last 90 days without TAN"); + } + else { + System.out.println("An error occurred:"); + if (response.getException() != null) { // something severe occurred + System.out.println(response.getException().getMessage()); + } + + // error messages retrieved from bank (e.g. PIN is wrong, message contains errors, ...) + for (String retrievedErrorMessage : response.getErrorsToShowToUser()) { + System.out.println(retrievedErrorMessage); + } + } + } + } + +} diff --git a/fints4javaLib/src/test/kotlin/net/dankito/fints/FinTsClientTest.kt b/fints4javaLib/src/test/kotlin/net/dankito/fints/FinTsClientTest.kt index b8bb90ea..81390d53 100644 --- a/fints4javaLib/src/test/kotlin/net/dankito/fints/FinTsClientTest.kt +++ b/fints4javaLib/src/test/kotlin/net/dankito/fints/FinTsClientTest.kt @@ -45,7 +45,7 @@ class FinTsClientTest { val result = underTest.getAnonymousBankInfo(BankDataAnonymous) // then - assertThat(result.successful).isTrue() + assertThat(result.isSuccessful).isTrue() } @@ -56,7 +56,7 @@ class FinTsClientTest { val result = underTest.synchronizeCustomerSystemId(Customer, Bank) // then - assertThat(result.successful).isTrue() + assertThat(result.isSuccessful).isTrue() assertThat(Customer.customerSystemId).isNotEqualTo(KundensystemStatus.SynchronizingCustomerSystemId) // customer system id is now set assertThat(Customer.selectedLanguage).isNotEqualTo(Dialogsprache.Default) // language is set now assertThat(Customer.customerSystemStatus).isEqualTo(KundensystemStatusWerte.Benoetigt) // customerSystemStatus is set now @@ -75,7 +75,7 @@ class FinTsClientTest { val result = underTest.getTransactions(GetTransactionsParameter(fromDate = ninetyDaysAgo), Bank, Customer) // then - assertThat(result.successful).isTrue() + assertThat(result.isSuccessful).isTrue() assertThat(result.bookedTransactions).isNotEmpty() } @@ -87,7 +87,7 @@ class FinTsClientTest { val result = underTest.doBankTransfer(BankTransferData, Bank, Customer) // then - assertThat(result.successful).isTrue() + assertThat(result.isSuccessful).isTrue() } } \ No newline at end of file