libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit bb23e40b5debc88702766b7fd0ea7e4c122c7fe1
parent 7a6146b5ddf9c95e315178883a8b0294996b86c5
Author: MS <ms@taler.net>
Date:   Thu, 18 Jun 2020 16:41:55 +0200

add payto:// parser

Diffstat:
Autil/src/main/kotlin/Payto.kt | 38++++++++++++++++++++++++++++++++++++++
Autil/src/test/kotlin/PaytoTest.kt | 27+++++++++++++++++++++++++++
2 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/util/src/main/kotlin/Payto.kt 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 @@ -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