gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] 08/08: POST preapred-payments/submit


From: gnunet
Subject: [libeufin] 08/08: POST preapred-payments/submit
Date: Fri, 08 May 2020 20:04:52 +0200

This is an automated email from the git hooks/post-receive script.

ms pushed a commit to branch master
in repository libeufin.

commit f73bdadef153acf43c0393e92c34abd00021d67c
Author: Marcello Stanisci <address@hidden>
AuthorDate: Fri May 8 20:02:05 2020 +0200

    POST preapred-payments/submit
---
 nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt    | 13 ++---
 .../src/main/kotlin/tech/libeufin/nexus/Helpers.kt |  3 +-
 nexus/src/main/kotlin/tech/libeufin/nexus/JSON.kt  |  5 ++
 nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt  | 55 +++++++++++++++++++++-
 util/src/main/kotlin/DBTypes.kt                    | 26 +++++-----
 5 files changed, 77 insertions(+), 25 deletions(-)

diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt 
b/nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt
index ca5f4d2..ba5f7f8 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt
@@ -5,7 +5,9 @@ import org.jetbrains.exposed.dao.*
 import org.jetbrains.exposed.sql.*
 import org.jetbrains.exposed.sql.transactions.TransactionManager
 import org.jetbrains.exposed.sql.transactions.transaction
-import tech.libeufin.util.IntIdTableWithAmount
+import tech.libeufin.nexus.BankAccountsTable.entityId
+import tech.libeufin.nexus.BankAccountsTable.primaryKey
+import tech.libeufin.util.amount
 import java.sql.Connection
 
 const val ID_MAX_LENGTH = 50
@@ -117,8 +119,8 @@ class RawBankTransactionEntity(id: EntityID<Long>) : 
LongEntity(id) {
 /**
  * Represent a prepare payment.
  */
-object Pain001Table : IntIdTableWithAmount() {
-    val msgId = long("msgId").uniqueIndex().autoIncrement()
+object Pain001Table : IdTable<String>() {
+    override val id = BankAccountsTable.varchar("id", 
ID_MAX_LENGTH).entityId().primaryKey()
     val paymentId = long("paymentId")
     val fileDate = long("fileDate")
     val sum = amount("sum")
@@ -139,9 +141,8 @@ object Pain001Table : IntIdTableWithAmount() {
     val invalid = bool("invalid").default(false)
     val nexusUser = reference("nexusUser", NexusUsersTable)
 }
-class Pain001Entity(id: EntityID<Int>) : IntEntity(id) {
-    companion object : IntEntityClass<Pain001Entity>(Pain001Table)
-    var msgId by Pain001Table.msgId
+class Pain001Entity(id: EntityID<String>) : Entity<String>(id) {
+    companion object : EntityClass<String, Pain001Entity>(Pain001Table)
     var paymentId by Pain001Table.paymentId
     var date by Pain001Table.fileDate
     var sum by Pain001Table.sum
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Helpers.kt 
b/nexus/src/main/kotlin/tech/libeufin/nexus/Helpers.kt
index badda4f..638d2fc 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/Helpers.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/Helpers.kt
@@ -288,7 +288,7 @@ fun createPain001document(pain001Entity: Pain001Entity): 
String {
 fun createPain001entity(entry: Pain001Data, nexusUser: NexusUserEntity): 
Pain001Entity {
     val randomId = Random().nextLong()
     return transaction {
-        Pain001Entity.new {
+        Pain001Entity.new(randomId.toString()) {
             subject = entry.subject
             sum = entry.sum
             debitorIban = entry.debitorIban
@@ -299,7 +299,6 @@ fun createPain001entity(entry: Pain001Data, nexusUser: 
NexusUserEntity): Pain001
             creditorIban = entry.creditorIban
             date = DateTime.now().millis
             paymentId = randomId
-            msgId = randomId
             endToEndId = randomId
             this.nexusUser = nexusUser
         }
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/JSON.kt 
b/nexus/src/main/kotlin/tech/libeufin/nexus/JSON.kt
index 3f12e3a..c6d6f01 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/JSON.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/JSON.kt
@@ -156,4 +156,9 @@ data class Pain001Data(
 
 data class RawPayments(
     var payments: MutableList<RawPayment> = mutableListOf()
+)
+
+data class SubmitPayment(
+    val uuid: String,
+    val transport: String?
 )
\ No newline at end of file
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt 
b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt
index ec1f3f5..e0d61f2 100644
--- a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt
+++ b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt
@@ -114,7 +114,6 @@ fun main() {
                 return@intercept finish()
             }
         }
-
         receivePipeline.intercept(ApplicationReceivePipeline.Before) {
             if (this.context.request.headers["Content-Encoding"] == "deflate") 
{
                 logger.debug("About to inflate received data")
@@ -126,7 +125,6 @@ fun main() {
             proceed()
             return@intercept
         }
-
         routing {
             /**
              * Shows information about the requesting user.
@@ -187,6 +185,59 @@ fun main() {
              * Submit one particular payment at the bank.
              */
             post("/bank-accounts/{accountid}/prepared-payments/submit") {
+                val userId = 
authenticateRequest(call.request.headers["Authorization"])
+                val body = call.receive<SubmitPayment>()
+
+                // 1 find payment.
+                val preparedPayment = transaction {
+                    Pain001Entity.findById(body.uuid)
+                } ?: throw NexusError(
+                    HttpStatusCode.NotFound,
+                    "Could not find prepared payment: ${body.uuid}"
+                )
+
+                // 2 check if was submitted yet
+                if (preparedPayment.submitted) {
+                    throw NexusError(
+                        HttpStatusCode.PreconditionFailed,
+                        "Payment ${body.uuid} was submitted already"
+                    )
+                }
+
+                // 3 submit
+                val pain001document = createPain001document(preparedPayment)
+
+                // 4 check if the user has a instance in such bank transport.
+                when (body.transport) {
+                    "ebics" -> {
+                        val subscriberDetails = 
getSubscriberDetailsFromNexusUserId(userId)
+                        logger.debug("Uploading PAIN.001: ${pain001document}")
+                        doEbicsUploadTransaction(
+                            client,
+                            subscriberDetails,
+                            "CCT",
+                            pain001document.toByteArray(Charsets.UTF_8),
+                            EbicsStandardOrderParams()
+                        )
+                        /** mark payment as 'submitted' */
+                        transaction {
+                            val payment = Pain001Entity.findById(body.uuid) ?: 
throw NexusError(
+                                HttpStatusCode.InternalServerError,
+                                "Severe internal error: could not find payment 
in DB after having submitted it to the bank"
+                            )
+                            payment.submitted = true
+                        }
+                        call.respondText(
+                            "CCT message submitted to the bank",
+                            ContentType.Text.Plain,
+                            HttpStatusCode.OK
+                        )
+                    }
+                    else -> throw NexusError(
+                        HttpStatusCode.NotImplemented,
+                        "Bank transport ${body.transport} is not implemented"
+                    )
+                }
                 return@post
             }
             /**
diff --git a/util/src/main/kotlin/DBTypes.kt b/util/src/main/kotlin/DBTypes.kt
index c564e29..f86b70b 100644
--- a/util/src/main/kotlin/DBTypes.kt
+++ b/util/src/main/kotlin/DBTypes.kt
@@ -1,8 +1,10 @@
 package tech.libeufin.util
 
+import org.jetbrains.exposed.dao.IdTable
 import org.jetbrains.exposed.dao.IntIdTable
 import org.jetbrains.exposed.sql.Column
 import org.jetbrains.exposed.sql.ColumnType
+import org.jetbrains.exposed.sql.Table
 import java.math.BigDecimal
 import java.math.RoundingMode
 
@@ -15,15 +17,10 @@ class BadAmount(badValue: Any?) : Exception("Value 
'${badValue}' is not a valid
  */
 typealias Amount = BigDecimal
 
-open class IntIdTableWithAmount : IntIdTable() {
-
-    class AmountColumnType : ColumnType() {
-        override fun sqlType(): String  = "DECIMAL(${NUMBER_MAX_DIGITS}, 
${SCALE_TWO})"
-
+class AmountColumnType : ColumnType() {
+    override fun sqlType(): String  = "DECIMAL(${NUMBER_MAX_DIGITS}, 
${SCALE_TWO})"
         override fun valueFromDB(value: Any): Any {
-
             val valueFromDB = super.valueFromDB(value)
-
             try {
                 return when (valueFromDB) {
                     is BigDecimal -> valueFromDB.setScale(SCALE_TWO, 
RoundingMode.UNNECESSARY)
@@ -55,13 +52,12 @@ open class IntIdTableWithAmount : IntIdTable() {
             }
             return super.valueToDB(value)
         }
-    }
+}
 
-    /**
-     * Make sure the number entered by upper layers does not need any rounding
-     * to conform to scale == 2
-     */
-    fun amount(name: String): Column<Amount> {
-        return registerColumn(name, AmountColumnType())
-    }
+/**
+ * Make sure the number entered by upper layers does not need any rounding
+ * to conform to scale == 2
+ */
+fun IdTable<*>.amount(name: String): Column<Amount> {
+    return registerColumn(name, AmountColumnType())
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]