libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit d2fdce416c4d8abf74e293501a701fa45bfbb9ee
parent 6faef7b95a0fa297945852d5e5a584afdfe4b307
Author: MS <ms@taler.net>
Date:   Mon, 23 Aug 2021 23:54:21 -1100

Camt: coordinating PRCD and CLBD balances.

Diffstat:
Msandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt | 19++++++++++++++-----
Msandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt | 26++++++++++++++++++++++++--
2 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt @@ -43,6 +43,7 @@ import tech.libeufin.util.ebics_hev.HEVResponse import tech.libeufin.util.ebics_hev.SystemReturnCodeType import tech.libeufin.util.ebics_s001.SignatureTypes import tech.libeufin.util.ebics_s001.UserSignatureData +import java.math.BigDecimal import java.security.interfaces.RSAPrivateCrtKey import java.security.interfaces.RSAPublicKey import java.time.Instant @@ -212,6 +213,7 @@ fun buildCamtString(type: Int, subscriberIban: String, history: List<RawPayment> val now = LocalDateTime.now() val dashedDate = now.toDashedDate() val zonedDateTime = now.toZonedString() + val balance = balanceForAccount(history) return constructXml(indent = true) { root("Document") { attribute("xmlns", "urn:iso:std:iso:20022:tech:xsd:camt.0${type}.001.02") @@ -286,11 +288,9 @@ fun buildCamtString(type: Int, subscriberIban: String, history: List<RawPayment> } element("Amt") { attribute("Ccy", "EUR") - text(Amount(0).toPlainString()) + text("0") } element("CdtDbtInd") { - // a temporary value to get the camt to validate. - // Should be fixed along #6269 text("CRDT") } element("Dt/Dt") { @@ -309,12 +309,21 @@ fun buildCamtString(type: Int, subscriberIban: String, history: List<RawPayment> attribute("Ccy", "EUR") // FIXME: the balance computation still not working properly //text(balanceForAccount(subscriberIban).toString()) - text("0") + if (balance < BigDecimal.ZERO) { + text(balance.abs().toPlainString()) + } else { + text(balance.toPlainString()) + } + } element("CdtDbtInd") { // a temporary value to get the camt to validate. // Should be fixed along #6269 - text("DBIT") + if (balance < BigDecimal.ZERO) { + text("DBIT") + } else { + text("CRDT") + } } element("Dt/Dt") { text(dashedDate) diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt @@ -27,6 +27,27 @@ fun getAccountFromLabel(accountLabel: String): BankAccountEntity { account } } +// Mainly useful inside the CAMT generator. +fun balanceForAccount(history: List<RawPayment>): BigDecimal { + var ret = BigDecimal.ZERO + history.forEach direction@ { + if (it.direction == "CRDT") { + val amount = parseDecimal(it.amount) + ret += amount + return@direction + } + if (it.direction == "DBIT") { + val amount = parseDecimal(it.amount) + ret -= amount + return@direction + } + throw SandboxError( + HttpStatusCode.InternalServerError, + "A payment direction was found neither CRDT not DBIT" + ) + } + return ret +} fun balanceForAccount(bankAccount: BankAccountEntity): BigDecimal { var balance = BigDecimal.ZERO @@ -66,8 +87,7 @@ fun historyForAccount(bankAccount: BankAccountEntity): List<RawPayment> { FIXME: add the following condition too: and (BankAccountTransactionsTable.date.between(start.millis, end.millis)) */ - BankAccountTransactionsTable.select { BankAccountTransactionsTable.account eq bankAccount.id } - }.forEach { + BankAccountTransactionsTable.select { BankAccountTransactionsTable.account eq bankAccount.id }.forEach { history.add( RawPayment( subject = it[BankAccountTransactionsTable.subject], @@ -88,6 +108,8 @@ fun historyForAccount(bankAccount: BankAccountEntity): List<RawPayment> { pmtInfId = it[BankAccountTransactionsTable.pmtInfId] ) ) + } + } return history } \ No newline at end of file