commit 01173796d57df5db15dc12ea24238602b7a24006
parent b4b71a4064b6b2e220b5d2cd333ebf9475fcd85d
Author: MS <ms@taler.net>
Date: Fri, 30 Apr 2021 11:24:13 +0200
Implement Sandbox transactions both sides: debtor AND creditor.
Diffstat:
2 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
@@ -271,7 +271,9 @@ object BankAccountTransactionsTable : Table() {
val direction = text("direction")
val account = reference("account", BankAccountsTable)
- override val primaryKey = PrimaryKey(pmtInfId)
+ // It can't be unique (anymore), because one table might contain
+ // the same payment twice: once as DBIT and once as CRDT.
+ // override val primaryKey = PrimaryKey(pmtInfId)
}
/**
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt
@@ -30,6 +30,7 @@ import io.ktor.util.AttributeKey
import org.apache.xml.security.binding.xmldsig.RSAKeyValueType
import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.jetbrains.exposed.sql.*
+import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.statements.api.ExposedBlob
import org.jetbrains.exposed.sql.transactions.transaction
import org.slf4j.Logger
@@ -563,9 +564,30 @@ private fun handleCct(paymentRequest: String) {
it[currency] = parseResult.currency
it[date] = Instant.now().toEpochMilli()
it[pmtInfId] = parseResult.pmtInfId
- it[accountServicerReference] = "sandboxref-getRandomString(16)"
+ it[accountServicerReference] = "sandboxref-${getRandomString(16)}"
it[direction] = "DBIT"
}
+ val maybeLocalCreditor = BankAccountEntity.find(
+ BankAccountsTable.iban eq parseResult.creditorIban
+ ).firstOrNull()
+ if (maybeLocalCreditor != null) {
+ BankAccountTransactionsTable.insert {
+ it[account] = maybeLocalCreditor.id
+ it[creditorIban] = parseResult.creditorIban
+ it[creditorName] = parseResult.creditorName
+ it[creditorBic] = parseResult.creditorBic
+ it[debtorIban] = parseResult.debtorIban
+ it[debtorName] = parseResult.debtorName
+ it[debtorBic] = parseResult.debtorBic
+ it[subject] = parseResult.subject
+ it[amount] = parseResult.amount.toString()
+ it[currency] = parseResult.currency
+ it[date] = Instant.now().toEpochMilli()
+ it[pmtInfId] = parseResult.pmtInfId
+ it[accountServicerReference] = "sandboxref-${getRandomString(16)}"
+ it[direction] = "CRDT"
+ }
+ }
} catch (e: ExposedSQLException) {
logger.warn("Could not insert new payment into the database: ${e}")
throw EbicsRequestError(