summaryrefslogtreecommitdiff
path: root/nexus/src/main
diff options
context:
space:
mode:
authorAntoine A <>2024-05-03 19:23:54 +0900
committerAntoine A <>2024-05-03 19:23:54 +0900
commitef2124c9949991cd4a0fbb78356184d06564b5e0 (patch)
treeb82a55d21a9340f562e4a4497b2db1997e22cbff /nexus/src/main
parent326a349eb491ab80f01e42619fec5c48b37d50da (diff)
downloadlibeufin-ef2124c9949991cd4a0fbb78356184d06564b5e0.tar.gz
libeufin-ef2124c9949991cd4a0fbb78356184d06564b5e0.tar.bz2
libeufin-ef2124c9949991cd4a0fbb78356184d06564b5e0.zip
nexus: support normal account that never bouncev11-dev
Diffstat (limited to 'nexus/src/main')
-rw-r--r--nexus/src/main/kotlin/tech/libeufin/nexus/Config.kt17
-rw-r--r--nexus/src/main/kotlin/tech/libeufin/nexus/EbicsFetch.kt35
-rw-r--r--nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt14
-rw-r--r--nexus/src/main/kotlin/tech/libeufin/nexus/db/PaymentDAO.kt23
4 files changed, 67 insertions, 22 deletions
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Config.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Config.kt
index ac2d86ca..823ed449 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/Config.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/Config.kt
@@ -37,7 +37,7 @@ class ApiConfig(config: TalerConfig, section: String) {
/** Configuration for libeufin-nexus */
class NexusConfig(val config: TalerConfig) {
- private fun requireString(option: String): String = config.requireString("nexus-ebics", option)
+ private fun requireString(option: String, type: String? = null): String = config.requireString("nexus-ebics", option, type)
private fun requirePath(option: String): Path = config.requirePath("nexus-ebics", option)
/** The bank's currency */
@@ -64,12 +64,16 @@ class NexusConfig(val config: TalerConfig) {
val clientPrivateKeysPath = requirePath("client_private_keys_file")
val fetch = NexusFetchConfig(config)
- val dialect = when (val type = requireString("bank_dialect")) {
+ val dialect = when (val type = requireString("bank_dialect", "dialect")) {
"postfinance" -> Dialect.postfinance
"gls" -> Dialect.gls
- else -> throw TalerConfigError.invalid("dialct", "libeufin-nexus", "bank_dialect", "expected 'postfinance' or 'gls' got '$type'")
+ else -> throw TalerConfigError.invalid("bank dialect", "libeufin-nexus", "bank_dialect", "expected 'postfinance' or 'gls' got '$type'")
+ }
+ val accountType = when (val type = requireString("account_type", "account type")) {
+ "normal" -> AccountType.normal
+ "exchange" -> AccountType.exchange
+ else -> throw TalerConfigError.invalid("account type", "libeufin-nexus", "account_type", "expected 'normal' or 'exchange' got '$type'")
}
-
val wireGatewayApiCfg = config.apiConf("nexus-httpd-wire-gateway-api")
val revenueApiCfg = config.apiConf("nexus-httpd-revenue-api")
}
@@ -104,4 +108,9 @@ fun TalerConfig.apiConf(section: String): ApiConfig? {
sealed interface AuthMethod {
data object None: AuthMethod
data class Bearer(val token: String): AuthMethod
+}
+
+enum class AccountType {
+ normal,
+ exchange
} \ No newline at end of file
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsFetch.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsFetch.kt
index 43cef8ca..1db8bd19 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsFetch.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/EbicsFetch.kt
@@ -118,18 +118,31 @@ suspend fun ingestOutgoingPayment(
*/
suspend fun ingestIncomingPayment(
db: Database,
- payment: IncomingPayment
+ payment: IncomingPayment,
+ accountType: AccountType
) {
suspend fun bounce(msg: String) {
- val result = db.payment.registerMalformedIncoming(
- payment,
- payment.amount,
- Instant.now()
- )
- if (result.new) {
- logger.info("$payment bounced in '${result.bounceId}': $msg")
- } else {
- logger.debug("$payment already seen and bounced in '${result.bounceId}': $msg")
+ when (accountType) {
+ AccountType.exchange -> {
+ val result = db.payment.registerMalformedIncoming(
+ payment,
+ payment.amount,
+ Instant.now()
+ )
+ if (result.new) {
+ logger.info("$payment bounced in '${result.bounceId}': $msg")
+ } else {
+ logger.debug("$payment already seen and bounced in '${result.bounceId}': $msg")
+ }
+ }
+ AccountType.normal -> {
+ val res = db.payment.registerIncoming(payment)
+ if (res.new) {
+ logger.info("$payment")
+ } else {
+ logger.debug("$payment already seen")
+ }
+ }
}
}
runCatching { parseIncomingTxMetadata(payment.wireTransferSubject) }.fold(
@@ -164,7 +177,7 @@ private suspend fun ingestDocument(
logger.debug("IGNORE $it")
} else {
when (it) {
- is IncomingPayment -> ingestIncomingPayment(db, it)
+ is IncomingPayment -> ingestIncomingPayment(db, it, cfg.accountType)
is OutgoingPayment -> ingestOutgoingPayment(db, it)
is TxNotification.Reversal -> {
logger.error("BOUNCE '${it.msgId}': ${it.reason}")
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt
index b301f555..20979f60 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt
@@ -297,15 +297,14 @@ class FakeIncoming: CliktCommand("Genere a fake incoming payment") {
).convert { Payto.parse(it).expectIban() }
override fun run() = cliCmd(logger, common.log) {
- val cfg = loadConfig(common.config)
- val dbCfg = cfg.dbConfig()
- val currency = cfg.requireString("nexus-ebics", "currency")
+ val cfg = loadNexusConfig(common.config)
+ val dbCfg = cfg.config.dbConfig()
val subject = payto.message ?: subject ?: throw Exception("Missing subject")
val amount = payto.amount ?: amount ?: throw Exception("Missing amount")
- if (amount.currency != currency)
- throw Exception("Wrong currency: expected $currency got ${amount.currency}")
+ if (amount.currency != cfg.currency)
+ throw Exception("Wrong currency: expected ${cfg.currency} got ${amount.currency}")
val bankId = run {
val bytes = ByteArray(16)
@@ -313,7 +312,7 @@ class FakeIncoming: CliktCommand("Genere a fake incoming payment") {
Base32Crockford.encode(bytes)
}
- Database(dbCfg, currency).use { db ->
+ Database(dbCfg, amount.currency).use { db ->
ingestIncomingPayment(db,
IncomingPayment(
amount = amount,
@@ -321,7 +320,8 @@ class FakeIncoming: CliktCommand("Genere a fake incoming payment") {
wireTransferSubject = subject,
executionTime = Instant.now(),
bankId = bankId
- )
+ ),
+ cfg.accountType
)
}
}
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/db/PaymentDAO.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/db/PaymentDAO.kt
index d9bdee00..02d16451 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/db/PaymentDAO.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/db/PaymentDAO.kt
@@ -142,6 +142,29 @@ class PaymentDAO(private val db: Database) {
}
}
+ /** Register an incoming payment */
+ suspend fun registerIncoming(
+ paymentData: IncomingPayment
+ ): IncomingRegistrationResult.Success = db.conn { conn ->
+ val stmt = conn.prepareStatement("""
+ SELECT out_found, out_tx_id
+ FROM register_incoming((?,?)::taler_amount,?,?,?,?)
+ """)
+ val executionTime = paymentData.executionTime.micros()
+ stmt.setLong(1, paymentData.amount.value)
+ stmt.setInt(2, paymentData.amount.frac)
+ stmt.setString(3, paymentData.wireTransferSubject)
+ stmt.setLong(4, executionTime)
+ stmt.setString(5, paymentData.debitPaytoUri)
+ stmt.setString(6, paymentData.bankId)
+ stmt.one {
+ IncomingRegistrationResult.Success(
+ it.getLong("out_tx_id"),
+ !it.getBoolean("out_found")
+ )
+ }
+ }
+
/** Query history of incoming transactions */
suspend fun revenueHistory(
params: HistoryParams