aboutsummaryrefslogtreecommitdiff
path: root/nexus/src/main
diff options
context:
space:
mode:
authorMarcello Stanisci <ms@taler.net>2020-05-08 20:02:05 +0200
committerMarcello Stanisci <ms@taler.net>2020-05-08 20:02:05 +0200
commitf73bdadef153acf43c0393e92c34abd00021d67c (patch)
treeef323fcfbd7e62875de73c530da186e016edda96 /nexus/src/main
parent089a03b96a074731b058a5d5576837203a48386a (diff)
downloadlibeufin-f73bdadef153acf43c0393e92c34abd00021d67c.tar.gz
libeufin-f73bdadef153acf43c0393e92c34abd00021d67c.tar.bz2
libeufin-f73bdadef153acf43c0393e92c34abd00021d67c.zip
POST preapred-payments/submit
Diffstat (limited to 'nexus/src/main')
-rw-r--r--nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt13
-rw-r--r--nexus/src/main/kotlin/tech/libeufin/nexus/Helpers.kt3
-rw-r--r--nexus/src/main/kotlin/tech/libeufin/nexus/JSON.kt5
-rw-r--r--nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt55
4 files changed, 66 insertions, 10 deletions
diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt
index ca5f4d2f..ba5f7f85 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 badda4f5..638d2fcc 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 3f12e3ad..c6d6f011 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 ec1f3f57..e0d61f2e 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
}
/**