From 7183874a83e0054cbfad4396409ec8e4bdad0734 Mon Sep 17 00:00:00 2001 From: Patrice Blanchardie Date: Tue, 31 Oct 2023 11:13:01 +0100 Subject: [PATCH] add intermediary agents on transactions resolves #9 --- README.md | 58 ++++++++++++++++++- .../java/io/inisos/bank4j/Transaction.java | 6 ++ .../io/inisos/bank4j/TransactionBuilder.java | 5 ++ .../bank4j/impl/JAXBCreditTransfer.java | 21 +++++-- .../inisos/bank4j/impl/SimpleTransaction.java | 17 ++++-- .../bank4j/impl/SimpleTransactionBuilder.java | 17 +++++- 6 files changed, 112 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 7080372..cdb1649 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,21 @@ class MyApp { .currency("EUR") // Currency code .endToEndId("Transfer reference 2") // End to end identifier .id("Optional identifier 2") // Optional transaction identifier + .intermediaryAgent(Bank.simpleBankAccount() // Optional intermediary agent 1 + .name("BNP PARIBAS") // Optional name + .otherId("12345") // Optional other identification + .bic("BNPAFRPP") // Optional BIC + .build()) + .intermediaryAgent(Bank.simpleBankAccount() // Optional intermediary agent 2 + .name("BNP PARIBAS") // Optional name + .otherId("67890") // Optional other identification + .bic("BNPAFRPP") // Optional BIC + .build()) + .intermediaryAgent(Bank.simpleBankAccount() // Optional intermediary agent 3 + .name("BNP PARIBAS") // Optional name + .otherId("00000") // Optional other identification + .bic("BNPAFRPP") // Optional BIC + .build()) .build(); // Transfer @@ -122,7 +137,7 @@ Output with formatting: MYID - 2023-10-25T09:47:05.187 + 2023-10-31T11:09:49.921 2 69.12 @@ -140,7 +155,7 @@ Output with formatting: SEPA - 2023-10-26 + 2023-11-01 Debtor Name @@ -190,6 +205,45 @@ Output with formatting: 56.78 + + + BNPAFRPP + + + + + + 12345 + + + BNP PARIBAS + + + + BNPAFRPP + + + + + + 67890 + + + BNP PARIBAS + + + + BNPAFRPP + + + + + + 00000 + + + BNP PARIBAS + BDFEFRPP diff --git a/src/main/java/io/inisos/bank4j/Transaction.java b/src/main/java/io/inisos/bank4j/Transaction.java index 3a0a2dc..12c2abe 100644 --- a/src/main/java/io/inisos/bank4j/Transaction.java +++ b/src/main/java/io/inisos/bank4j/Transaction.java @@ -1,6 +1,7 @@ package io.inisos.bank4j; import java.math.BigDecimal; +import java.util.List; import java.util.Optional; /** @@ -37,4 +38,9 @@ public interface Transaction { * @return optional identifier */ Optional getId(); + + /** + * @return intermediary agents + */ + List getIntermediaryAgents(); } diff --git a/src/main/java/io/inisos/bank4j/TransactionBuilder.java b/src/main/java/io/inisos/bank4j/TransactionBuilder.java index 81109f1..d547e5e 100644 --- a/src/main/java/io/inisos/bank4j/TransactionBuilder.java +++ b/src/main/java/io/inisos/bank4j/TransactionBuilder.java @@ -2,6 +2,7 @@ import java.math.BigDecimal; import java.util.Currency; +import java.util.List; public interface TransactionBuilder { @@ -37,5 +38,9 @@ default TransactionBuilder currency(String currencyCode) { TransactionBuilder id(String id); + TransactionBuilder intermediaryAgents(List intermediaryAgents); + + TransactionBuilder intermediaryAgent(BankAccount intermediaryAgent); + Transaction build(); } diff --git a/src/main/java/io/inisos/bank4j/impl/JAXBCreditTransfer.java b/src/main/java/io/inisos/bank4j/impl/JAXBCreditTransfer.java index 0812ab9..3a3ece6 100644 --- a/src/main/java/io/inisos/bank4j/impl/JAXBCreditTransfer.java +++ b/src/main/java/io/inisos/bank4j/impl/JAXBCreditTransfer.java @@ -16,10 +16,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.Collection; -import java.util.Objects; -import java.util.Optional; -import java.util.StringJoiner; +import java.util.*; /** * A JAXB ISO 20022 Credit Transfer with PAIN.001.001.03 @@ -170,6 +167,22 @@ private CreditTransferTransactionInformation10 transaction(Transaction transacti creditTransferTransactionInformation.setCdtr(partyIdentification(transaction.getParty().orElse(null))); creditTransferTransactionInformation.setCdtrAcct(cashAccount(transaction.getAccount())); branchAndFinancialInstitutionIdentification(transaction.getAccount()).ifPresent(creditTransferTransactionInformation::setCdtrAgt); + Iterator intermediaryAgentsIterator = transaction.getIntermediaryAgents().iterator(); + if (intermediaryAgentsIterator.hasNext()) { + BankAccount first = intermediaryAgentsIterator.next(); + creditTransferTransactionInformation.setIntrmyAgt1Acct(cashAccount(first)); + branchAndFinancialInstitutionIdentification(first).ifPresent(creditTransferTransactionInformation::setIntrmyAgt1); + } + if (intermediaryAgentsIterator.hasNext()) { + BankAccount second = intermediaryAgentsIterator.next(); + creditTransferTransactionInformation.setIntrmyAgt2Acct(cashAccount(second)); + branchAndFinancialInstitutionIdentification(second).ifPresent(creditTransferTransactionInformation::setIntrmyAgt2); + } + if (intermediaryAgentsIterator.hasNext()) { + BankAccount third = intermediaryAgentsIterator.next(); + creditTransferTransactionInformation.setIntrmyAgt3Acct(cashAccount(third)); + branchAndFinancialInstitutionIdentification(third).ifPresent(creditTransferTransactionInformation::setIntrmyAgt3); + } return creditTransferTransactionInformation; } diff --git a/src/main/java/io/inisos/bank4j/impl/SimpleTransaction.java b/src/main/java/io/inisos/bank4j/impl/SimpleTransaction.java index 54eff9a..7dd04a2 100644 --- a/src/main/java/io/inisos/bank4j/impl/SimpleTransaction.java +++ b/src/main/java/io/inisos/bank4j/impl/SimpleTransaction.java @@ -5,10 +5,7 @@ import io.inisos.bank4j.Transaction; import java.math.BigDecimal; -import java.util.Currency; -import java.util.Objects; -import java.util.Optional; -import java.util.StringJoiner; +import java.util.*; /** * Simple Transaction @@ -23,14 +20,19 @@ public class SimpleTransaction implements Transaction { private final Currency currency; private final String endToEndId; private final String id; + private final List intermediaryAgents; - public SimpleTransaction(Party party, BankAccount account, BigDecimal amount, Currency currency, String endToEndId, String id) { + public SimpleTransaction(Party party, BankAccount account, BigDecimal amount, Currency currency, String endToEndId, String id, List intermediaryAgents) { this.party = party; this.account = Objects.requireNonNull(account, "Account cannot be null"); this.amount = Objects.requireNonNull(amount, "Amount cannot be null"); this.currency = Objects.requireNonNull(currency, "Currency cannot be null"); this.endToEndId = Objects.requireNonNull(endToEndId, "End to end id cannot be null"); this.id = id; + this.intermediaryAgents = Optional.ofNullable(intermediaryAgents).orElse(Collections.emptyList()); + if (this.intermediaryAgents.size() > 3) { + throw new IllegalArgumentException("Intermediary agents cannot be more than 3"); + } } @Override @@ -63,6 +65,11 @@ public Optional getId() { return Optional.ofNullable(id); } + @Override + public List getIntermediaryAgents() { + return intermediaryAgents; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/io/inisos/bank4j/impl/SimpleTransactionBuilder.java b/src/main/java/io/inisos/bank4j/impl/SimpleTransactionBuilder.java index 121c6fc..f884ca6 100644 --- a/src/main/java/io/inisos/bank4j/impl/SimpleTransactionBuilder.java +++ b/src/main/java/io/inisos/bank4j/impl/SimpleTransactionBuilder.java @@ -5,7 +5,9 @@ import io.inisos.bank4j.TransactionBuilder; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Currency; +import java.util.List; public class SimpleTransactionBuilder implements TransactionBuilder { private Party party; @@ -14,6 +16,7 @@ public class SimpleTransactionBuilder implements TransactionBuilder { private Currency currency; private String endToEndId; private String id; + private final List intermediaryAgents = new ArrayList<>(); @Override public SimpleTransactionBuilder party(Party party) { @@ -51,8 +54,20 @@ public SimpleTransactionBuilder id(String id) { return this; } + @Override + public TransactionBuilder intermediaryAgents(List intermediaryAgents) { + this.intermediaryAgents.addAll(intermediaryAgents); + return this; + } + + @Override + public TransactionBuilder intermediaryAgent(BankAccount intermediaryAgent) { + this.intermediaryAgents.add(intermediaryAgent); + return this; + } + @Override public SimpleTransaction build() { - return new SimpleTransaction(party, account, amount, currency, endToEndId, id); + return new SimpleTransaction(party, account, amount, currency, endToEndId, id, intermediaryAgents); } }