gnunet-svn
[Top][All Lists]
Advanced

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

[libeufin] branch master updated: add payto:// parser


From: gnunet
Subject: [libeufin] branch master updated: add payto:// parser
Date: Thu, 18 Jun 2020 16:42:08 +0200

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

ms pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new bb23e40  add payto:// parser
bb23e40 is described below

commit bb23e40b5debc88702766b7fd0ea7e4c122c7fe1
Author: MS <ms@taler.net>
AuthorDate: Thu Jun 18 16:41:55 2020 +0200

    add payto:// parser
---
 util/src/main/kotlin/Payto.kt     | 37 +++++++++++++++++++++++++++++++++++++
 util/src/test/kotlin/PaytoTest.kt | 26 ++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/util/src/main/kotlin/Payto.kt b/util/src/main/kotlin/Payto.kt
new file mode 100644
index 0000000..a1cccee
--- /dev/null
+++ b/util/src/main/kotlin/Payto.kt
@@ -0,0 +1,37 @@
+package tech.libeufin.util
+
+import io.ktor.http.HttpStatusCode
+import tech.libeufin.util.EbicsProtocolError
+import java.net.URI
+
+/**
+ * Helper data structures.
+ */
+data class Payto(
+    val name: String,
+    val iban: String,
+    val bic: String = "NOTGIVEN"
+)
+
+fun parsePayto(paytoLine: String): Payto {
+    val javaParsedUri = try {
+        URI(paytoLine)
+    } catch (e: java.lang.Exception) {
+        throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}' is 
not a valid URI")
+    }
+    if (javaParsedUri.scheme != "payto") {
+        throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}' is 
not payto")
+    }
+    val iban = javaParsedUri.path.split("/").last()
+    val queryStringAsList = javaParsedUri.query.split("&")
+    // admit only ONE parameter: receiver-name.
+    if (queryStringAsList.size != 1) {
+        throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}' 
has unsupported query string")
+    }
+    val splitParameter = queryStringAsList.first().split("=")
+    if (splitParameter.first() != "receiver-name" && splitParameter.first() != 
"sender-name") {
+        throw EbicsProtocolError(HttpStatusCode.BadRequest, "'${paytoLine}' 
has unsupported query string")
+    }
+    val receiverName = splitParameter.last()
+    return Payto(iban = iban, name = receiverName)
+}
\ No newline at end of file
diff --git a/util/src/test/kotlin/PaytoTest.kt 
b/util/src/test/kotlin/PaytoTest.kt
new file mode 100644
index 0000000..761c445
--- /dev/null
+++ b/util/src/test/kotlin/PaytoTest.kt
@@ -0,0 +1,26 @@
+import org.junit.Test
+import tech.libeufin.util.EbicsProtocolError
+import tech.libeufin.util.parsePayto
+
+class PaytoTest {
+
+    @Test
+    fun parsePaytoTest() {
+        val noBic = parsePayto("payto://iban/IBAN123?receiver-name=The%20Name")
+        assert(noBic.iban == "IBAN123" && noBic.name == "The Name")
+        val withBic = 
parsePayto("payto://iban/BIC123/IBAN123?receiver-name=The%20Name")
+        assert(withBic.iban == "IBAN123" && withBic.name == "The Name")
+        try {
+            parsePayto("http://iban/BIC123/IBAN123?receiver-name=The%20Name";)
+        } catch (e: EbicsProtocolError) {
+            println("wrong scheme was caught")
+        }
+        try {
+            parsePayto(
+                
"payto://iban/BIC123/IBAN123?receiver-name=The%20Name&address=house"
+            )
+        } catch (e: EbicsProtocolError) {
+            println("more than one parameter isn't allowed")
+        }
+    }
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

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