diff --git a/.gitignore b/.gitignore index 17294cd6..3c9121fb 100755 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ build/ out/ **/out /captures +**/kotlin-js-store .gradle diff --git a/SampleApplications/WebApp/build.gradle b/SampleApplications/WebApp/build.gradle index 0d66ae47..5094c005 100644 --- a/SampleApplications/WebApp/build.gradle +++ b/SampleApplications/WebApp/build.gradle @@ -3,12 +3,6 @@ plugins { } -repositories { - mavenCentral() - google() -} - - dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-js" diff --git a/SampleApplications/WebApp/src/main/kotlin/AccountTransactionsView.kt b/SampleApplications/WebApp/src/main/kotlin/AccountTransactionsView.kt new file mode 100644 index 00000000..ff83a74f --- /dev/null +++ b/SampleApplications/WebApp/src/main/kotlin/AccountTransactionsView.kt @@ -0,0 +1,56 @@ +import net.dankito.banking.fints.FinTsClientDeprecated +import net.dankito.banking.fints.model.AccountTransaction +import net.dankito.banking.fints.model.AddAccountParameter +import react.RBuilder +import react.RComponent +import react.Props +import react.State +import react.dom.* +import styled.styledDiv + +external interface AccountTransactionsViewProps : Props { + var client: FinTsClientDeprecated +} + +data class AccountTransactionsViewState(val balance: String, val transactions: Collection) : State + +@JsExport +class AccountTransactionsView(props: AccountTransactionsViewProps) : RComponent(props) { + + + init { + state = AccountTransactionsViewState("", listOf()) + + // due to CORS your bank's servers can not be requested directly from browser -> set a CORS proxy url in main.kt + // TODO: set your credentials here + props.client.addAccountAsync(AddAccountParameter("", "", "", "")) { response -> + if (response.successful) { + val balance = response.retrievedData.sumOf { it.balance?.amount?.string?.replace(',', '.')?.toDoubleOrNull() ?: 0.0 } // i know, double is not an appropriate data type for amounts + + setState(AccountTransactionsViewState(balance.toString() + " " + (response.retrievedData.firstOrNull()?.balance?.currency ?: ""), response.retrievedData.flatMap { it.bookedTransactions })) + } + } + } + + override fun RBuilder.render() { + p { + +"Saldo: ${state.balance}" + } + + div { + state.transactions.forEach { transaction -> + div { + styledDiv { + if (transaction.showOtherPartyName) { + div { transaction.otherPartyName } + } + + div { + +transaction.reference + } + } + } + } + } + } +} \ No newline at end of file diff --git a/SampleApplications/WebApp/src/main/kotlin/main.kt b/SampleApplications/WebApp/src/main/kotlin/main.kt index 4542048a..6c63bd3f 100644 --- a/SampleApplications/WebApp/src/main/kotlin/main.kt +++ b/SampleApplications/WebApp/src/main/kotlin/main.kt @@ -1,5 +1,20 @@ import kotlinx.browser.document +import kotlinx.browser.window +import net.dankito.banking.fints.FinTsClientDeprecated +import net.dankito.banking.fints.callback.SimpleFinTsClientCallback +import net.dankito.banking.fints.webclient.KtorWebClient +import net.dankito.banking.fints.webclient.ProxyingWebClient +import react.dom.render fun main() { - document.write("Hello, world!") + window.onload = { + render(document.getElementById("root")!!) { + child(AccountTransactionsView::class) { + attrs { + // to circumvent CORS we have to use a CORS proxy like https://github.com/Rob--W/cors-anywhere. Set CORS proxy's URL here + client = FinTsClientDeprecated(SimpleFinTsClientCallback(), ProxyingWebClient("http://localhost:8082/", KtorWebClient())) + } + } + } + } } \ No newline at end of file diff --git a/SampleApplications/WebApp/src/main/resources/index.html b/SampleApplications/WebApp/src/main/resources/index.html index e4bcb6d0..f7f30984 100644 --- a/SampleApplications/WebApp/src/main/resources/index.html +++ b/SampleApplications/WebApp/src/main/resources/index.html @@ -1,11 +1,11 @@ - - - fints4k WebApp - - - - - + + + fints4k WebApp + + + +
+ \ No newline at end of file diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/webclient/ProxyingWebClient.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/webclient/ProxyingWebClient.kt new file mode 100644 index 00000000..f7260a76 --- /dev/null +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/webclient/ProxyingWebClient.kt @@ -0,0 +1,17 @@ +package net.dankito.banking.fints.webclient + + +/** + * In browsers we cannot request bank servers directly due to CORS. So this 'WebClient' prepends each server url with CORS proxy's url and delegates then the + * call to a real IWebClient implementation. + */ +class ProxyingWebClient(proxyUrl: String, private val delegate: IWebClient) : IWebClient { + + private val proxyUrl = if (proxyUrl.endsWith("/")) proxyUrl else proxyUrl + "/" + + + override fun post(url: String, body: String, contentType: String, userAgent: String, callback: (WebClientResponse) -> Unit) { + delegate.post(proxyUrl + url, body, contentType, userAgent, callback) + } + +} \ No newline at end of file