Updated Java showcase code

This commit is contained in:
dankl 2020-01-26 12:17:04 +01:00 committed by dankito
parent 8b2e200583
commit 5b5173132f
1 changed files with 47 additions and 20 deletions

View File

@ -3,17 +3,16 @@ package net.dankito.fints.java;
import net.dankito.fints.FinTsClient;
import net.dankito.fints.FinTsClientCallback;
import net.dankito.fints.banks.BankFinder;
import net.dankito.fints.messages.datenelemente.implementierte.signatur.Sicherheitsfunktion;
import net.dankito.fints.messages.datenelemente.implementierte.tan.TanGeneratorTanMedium;
import net.dankito.fints.model.*;
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.util.Date;
import java.util.List;
@ -24,13 +23,12 @@ public class JavaShowcase {
// set your bank code (Bankleitzahl) here. Or create BankData manually. Required fields are:
// bankCode, bankCountryCode (Germany = 280), finTs3ServerAddress and for bank transfers bic
List<BankInfo> foundBanks = bankFinder.findBankByBankCode("10070000");
List<BankInfo> foundBanks = bankFinder.findBankByBankCode("<bank_code>");
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_id>", "<pin>");
customer.setSelectedTanProcedure(new TanProcedure("", Sicherheitsfunktion.PIN_TAN_911, TanProcedureType.ChipTanOptisch));
FinTsClientCallback callback = new FinTsClientCallback() {
@ -55,18 +53,43 @@ public class JavaShowcase {
FinTsClient finTsClient = new FinTsClient(callback, 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);
AddAccountResponse addAccountResponse = finTsClient.addAccount(bank, customer);
if (addAccountResponse.isSuccessful()) {
System.out.println("Could not add account for " + bank.getBankCode() + " " + customer.getCustomerId() + ":");
showResponseError(addAccountResponse);
return;
}
GetTransactionsResponse response = finTsClient.getTransactions(
new GetTransactionsParameter(true, ninetyDaysAgo), bank, customer);
System.out.println("Successfully added account for " + bank.getBankCode() + " " + customer.getCustomerId());
showResponse(response);
/* Other stuff you can do with the lib */
for (AccountData account : customer.getAccounts()) { // accounts are now retrieved
if (account.getSupportsRetrievingAccountTransactions()) {
// 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);
}
if (account.getSupportsTransferringMoney()) {
// 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);
// }
}
}
}
}
private static void showResponse(GetTransactionsResponse response) {
private static void showGetTransactionsResponse(GetTransactionsResponse response) {
if (response.isSuccessful()) {
System.out.println("Balance (Saldo) = " + response.getBalance());
@ -82,6 +105,12 @@ public class JavaShowcase {
}
else {
System.out.println("An error occurred:");
showResponseError(response);
}
}
}
private static void showResponseError(FinTsClientResponse response) {
if (response.getException() != null) { // something severe occurred
System.out.println(response.getException().getMessage());
}
@ -91,7 +120,5 @@ public class JavaShowcase {
System.out.println(retrievedErrorMessage);
}
}
}
}
}