From 88effa5e1fd88054aca686cff571cfa31153517e Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 13 May 2020 14:26:08 +0200 Subject: [PATCH] Updated JavaShowcase --- .../net/dankito/fints/java/JavaShowcase.java | 124 +++++++++++++----- 1 file changed, 92 insertions(+), 32 deletions(-) diff --git a/fints4k/src/test/java/net/dankito/fints/java/JavaShowcase.java b/fints4k/src/test/java/net/dankito/fints/java/JavaShowcase.java index b2dbcc25..25f05e25 100644 --- a/fints4k/src/test/java/net/dankito/fints/java/JavaShowcase.java +++ b/fints4k/src/test/java/net/dankito/fints/java/JavaShowcase.java @@ -1,61 +1,121 @@ package net.dankito.fints.java; import net.dankito.fints.FinTsClient; -import net.dankito.fints.callback.FinTsClientCallback; import net.dankito.fints.banks.IBankFinder; import net.dankito.fints.banks.InMemoryBankFinder; +import net.dankito.fints.callback.FinTsClientCallback; +import net.dankito.fints.callback.SimpleFinTsClientCallback; import net.dankito.fints.messages.datenelemente.implementierte.tan.TanGeneratorTanMedium; -import net.dankito.fints.model.*; +import net.dankito.fints.model.AccountData; +import net.dankito.fints.model.AccountFeature; +import net.dankito.fints.model.AccountTransaction; +import net.dankito.fints.model.BankData; +import net.dankito.fints.model.BankInfo; +import net.dankito.fints.model.BankTransferData; +import net.dankito.fints.model.CustomerData; +import net.dankito.fints.model.EnterTanGeneratorAtcResult; +import net.dankito.fints.model.EnterTanResult; +import net.dankito.fints.model.TanChallenge; +import net.dankito.fints.model.TanProcedure; import net.dankito.fints.model.mapper.BankDataMapper; import net.dankito.fints.response.client.AddAccountResponse; import net.dankito.fints.response.client.FinTsClientResponse; import net.dankito.fints.response.client.GetTransactionsResponse; import net.dankito.fints.util.Java8Base64Service; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import java.math.BigDecimal; import java.util.List; public class JavaShowcase { public static void main(String[] args) { + JavaShowcase showcase = new JavaShowcase(); + + showcase.basicShowcase(); + + showcase.advancedShowcase(); + } + + private void basicShowcase() { + // Set your bank code (Bankleitzahl) here. + // BankInfo contains e.g. a bank's FinTS server address, country code and BIC (needed for money transfer) + List foundBanks = new InMemoryBankFinder().findBankByNameBankCodeOrCity(""); + + if (foundBanks.isEmpty() == false) { // please also check if bank supports FinTS 3.0 + BankData bank = new BankDataMapper().mapFromBankInfo(foundBanks.get(0)); + + // set your customer data (customerId = username you use to log in; pin = online banking pin / password) + CustomerData customer = new CustomerData("", ""); + + FinTsClientCallback callback = new SimpleFinTsClientCallback(); // see advanced showcase for configuring callback + + FinTsClient finTsClient = new FinTsClient(callback, new Java8Base64Service()); + + AddAccountResponse addAccountResponse = finTsClient.addAccount(bank, customer); + + if (addAccountResponse.isSuccessful()) { + System.out.println("Successfully added account for " + bank.getBankCode() + " " + customer.getCustomerId()); + + if (addAccountResponse.getBookedTransactions().isEmpty() == false) { + System.out.println("Account transactions of last 90 days:"); + showGetTransactionsResponse(addAccountResponse); + } + } + else { + System.out.println("Could not add account for " + bank.getBankCode() + " " + customer.getCustomerId() + ":"); + showResponseError(addAccountResponse); + } + + // see advanced show case what else you can do with this library, e.g. retrieving all account transactions and transferring money + } + } + + private void advancedShowcase() { IBankFinder bankFinder = new InMemoryBankFinder(); - // set your bank code (Bankleitzahl) here. Or create BankData manually. Required fields are: + // 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(""); - if (foundBanks.isEmpty() == false) { + if (foundBanks.isEmpty() == false) { // please also check if bank supports FinTS 3.0 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("", ""); FinTsClientCallback callback = new FinTsClientCallback() { - @Nullable @Override - public TanProcedure askUserForTanProcedure(@NotNull List supportedTanProcedures, @Nullable TanProcedure suggestedTanProcedure) { - return suggestedTanProcedure; // simply return suggestedTanProcedure as in most cases it's the best fitting one + public TanProcedure askUserForTanProcedure(List supportedTanProcedures, TanProcedure suggestedTanProcedure) { + // E.g. show a dialog to ask for user's TAN procedure. + // In most cases it's senseful to simply return suggestedTanProcedure and to let + // user select TAN procedure when entering TAN is required (see enterTan() below) + return suggestedTanProcedure; } - @Nullable @Override - public EnterTanResult enterTan(@NotNull CustomerData customer, @NotNull TanChallenge tanChallenge) { - return EnterTanResult.Companion.userDidNotEnterTan(); + public EnterTanResult enterTan(CustomerData customer, TanChallenge tanChallenge) { + // e.g. show + // - Android: net.dankito.banking.fints4java.android.ui.dialogs.EnterTanDialog + // - JavaFX: net.dankito.banking.ui.javafx.dialogs.tan.EnterTanDialog + return EnterTanResult.Companion.userDidNotEnterTan(); // user did not enter TAN. aborts operation } - @Nullable @Override - public EnterTanGeneratorAtcResult enterTanGeneratorAtc(@NotNull CustomerData customer, @NotNull TanGeneratorTanMedium tanMedium) { - return EnterTanGeneratorAtcResult.Companion.userDidNotEnterTan(); + public EnterTanGeneratorAtcResult enterTanGeneratorAtc(CustomerData customer, TanGeneratorTanMedium tanMedium) { + // needed only in rare cases to synchronize TAN generator for chipTAN procedures. E.g. show + // - Android: net.dankito.banking.fints4java.android.ui.dialogs.EnterAtcDialog + return EnterTanGeneratorAtcResult.Companion.userDidNotEnterAtc(); // user did not enter TAN and ATC. aborts operation } }; + + // may also check if FinTsClientForCustomer fits your needs, avoids passing bank and customer to each method FinTsClient finTsClient = new FinTsClient(callback, new Java8Base64Service()); AddAccountResponse addAccountResponse = finTsClient.addAccount(bank, customer); - if (addAccountResponse.isSuccessful()) { + if (addAccountResponse.isSuccessful() == false) { System.out.println("Could not add account for " + bank.getBankCode() + " " + customer.getCustomerId() + ":"); showResponseError(addAccountResponse); return; @@ -63,28 +123,28 @@ public class JavaShowcase { System.out.println("Successfully added account for " + bank.getBankCode() + " " + customer.getCustomerId()); - /* Other stuff you can do with the lib */ for (AccountData account : customer.getAccounts()) { // accounts are now retrieved if (account.supportsFeature(AccountFeature.RetrieveAccountTransactions)) { - // retrieves all account transactions, but requires entering a TAN (FinTsClientCallback.enterTan() will be called) -// GetTransactionsResponse response = finTsClient.getTransactions( -// new GetTransactionsParameter(true), bank, customer, account); -// -// showGetTransactionsResponse(response); + // Most banks support retrieving account transactions of last 90 without TAN, may also your bank. + // Alternatively call getTransactions() to retrieve all account transactions. But then entering a TAN is required. + GetTransactionsResponse response = finTsClient.tryGetTransactionsOfLast90DaysWithoutTan(bank, customer, account); + + showGetTransactionsResponse(response); } if (account.supportsFeature(AccountFeature.TransferMoney)) { - // transfer 0.01 € to yourself -// BankTransferData data = new BankTransferData(customer.getName(), account.getIban(), bank.getBic(), new BigDecimal("0.01"), "Hey I can transfer money to myself") -// FinTsClientResponse transferMoneyResponse = finTsClient.doBankTransfer(data, bank, customer, account); -// -// if (transferMoneyResponse.isSuccessful()) { -// System.out.println("Successfully transferred " + data.getAmount() + " to " + data.getCreditorIban()); -// } -// else { -// showResponseError(transferMoneyResponse); -// } + // Transfer 0.01 € to yourself. Transferring money to one self doesn't require a TAN. + BankTransferData data = new BankTransferData(customer.getName(), account.getIban(), bank.getBic(), + new BigDecimal("0.01"), "Give me some money", false); + FinTsClientResponse transferMoneyResponse = finTsClient.doBankTransfer(data, bank, customer, account); + + if (transferMoneyResponse.isSuccessful()) { + System.out.println("Successfully transferred " + data.getAmount() + " to " + data.getCreditorIban()); + } + else { + showResponseError(transferMoneyResponse); + } } } }