libeufin

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

commit 94740f4989096a12298e89c23c7253a157238da8
parent f58e802b4bdf3b31033314d1efad32a177e98b09
Author: ms <ms@taler.net>
Date:   Sat, 18 Sep 2021 07:51:38 +0200

Activating withdrawals from the Sandbox.

At this point, the Sandbox offers a /taler endpoint that
creates a withdraw operation and returns (only) the corresponding
taler://-URI that should then be passed to the CLI wallet.

For simplicity, the Sandbox expects to have three canonical
bank accounts: exchange, customer, merchant, and refuses to
continue whenever one is not found.

Lastly, Sandbox now expects the hostname to be specified in
a environment variable; it should be noted that another way
to retrieve the hostname is already under development, but it
is not finished yet.

Diffstat:
Msandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt | 10++++++++++
Msandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt | 40+++++++++++++++++++++++++++++++++++++++-
Mutil/src/main/kotlin/Config.kt | 9+++++++++
3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt @@ -407,6 +407,14 @@ class BankAccountStatementEntity(id: EntityID<Int>) : IntEntity(id) { var balanceClbd by BankAccountStatementsTable.balanceClbd } +object TalerWithdrawalsTable : LongIdTable() { + val wopid = uuid("wopid").autoGenerate() +} +class TalerWithdrawalEntity(id: EntityID<Long>) : LongEntity(id) { + companion object : LongEntityClass<TalerWithdrawalEntity>(TalerWithdrawalsTable) + var wopid by TalerWithdrawalsTable.wopid +} + object BankAccountReportsTable : IntIdTable() { val reportId = text("reportId") val creationTime = long("creationTime") @@ -414,6 +422,8 @@ object BankAccountReportsTable : IntIdTable() { val bankAccount = reference("bankAccount", BankAccountsTable) } + + fun dbDropTables(dbConnectionString: String) { Database.connect(dbConnectionString) transaction { diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt @@ -83,8 +83,9 @@ import java.util.* import kotlin.random.Random import kotlin.system.exitProcess -const val SANDBOX_DB_ENV_VAR_NAME = "LIBEUFIN_SANDBOX_DB_CONNECTION" private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.sandbox") +private val hostName: String? = getHostnameFromEnv("LIBEUFIN_SANDBOX_HOSTNAME") +const val SANDBOX_DB_ENV_VAR_NAME = "LIBEUFIN_SANDBOX_DB_CONNECTION" data class SandboxError( val statusCode: HttpStatusCode, @@ -994,6 +995,43 @@ fun serverMain(dbName: String, port: Int) { } } + /** + * Activates a withdraw operation of 1 currency unit with + * the default exchange, from a designated/constant customer. + */ + get("/taler") { + requireSuperuser(call.request) + SandboxAssert( + hostName != null, + "Own hostname not found. Logs should have warned" + ) + // check that the three canonical accounts exist + val wo = transaction { + val exchange = BankAccountEntity.find { + BankAccountsTable.label eq "sandbox-account-exchange" + }.firstOrNull() + val customer = BankAccountEntity.find { + BankAccountsTable.label eq "sandbox-account-customer" + }.firstOrNull() + val merchant = BankAccountEntity.find { + BankAccountsTable.label eq "sandbox-account-merchant" + }.firstOrNull() + + SandboxAssert(exchange != null, "exchange has no bank account") + SandboxAssert(customer != null, "customer has no bank account") + SandboxAssert(merchant != null, "merchant has no bank account") + + // At this point, the three actors exist and a new withdraw operation can be created. + TalerWithdrawalEntity.new { + // wopid is autogenerated, and momentarily the only column + } + } + /** + * Future versions will include the QR code in this response. + */ + call.respondText("taler://withdraw/${hostName}/api/${wo.wopid}") + return@get + } } } logger.info("LibEuFin Sandbox running on port $port") diff --git a/util/src/main/kotlin/Config.kt b/util/src/main/kotlin/Config.kt @@ -50,6 +50,15 @@ fun setLogLevel(logLevel: String?) { } } +fun getHostnameFromEnv(varName: String): String? { + val hostName = System.getenv(varName) + if (hostName.isNullOrBlank() or hostName.isNullOrEmpty()) { + println("WARNING, the hostname wasn't found in env's $varName. Will stay unknown") + return null + } + return hostName +} + fun getDbConnFromEnv(varName: String): String { val dbConnStr = System.getenv(varName) if (dbConnStr.isNullOrBlank() or dbConnStr.isNullOrEmpty()) {