Connect Layer - Android Example
Introduction
Connect Layer provides a client-side functionality that allows you to authenticate user with their bank and retrieve accessCode required to obtain user’s permanent token.
This guide shows you how to integrate Dapi connect layer into an Android application via a WebView.
Basic Setup
Create a WebView in your layout.xml
<WebView
android:id="@+id/wv_connect"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
And in your Activity or Fragment
private lateinit var wvConnect: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
wvConnect = findViewById(R.id.wv_connect)
initializeConnect()
...
}
Configure the WebView for Connect
private fun initializeConnect() {
val connectConfigurationLink = HashMap<String, String>()
connectConfigurationLink["appKey"] = "APP_KEY"
connectConfigurationLink["showLogo"] = "true"
connectConfigurationLink["isExperimental"] = "false"
connectConfigurationLink["baseUrl"] = "https://connect.dapi.co/v3/connection?"
connectConfigurationLink["environment"] = "sandbox"
val connectUrl = generateConnectInitializationUrl(connectConfigurationLink)
val webSettings = wvConnect.settings
webSettings.javaScriptEnabled = true
webSettings.domStorageEnabled = true
webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
WebView.setWebContentsDebuggingEnabled(true)
setupConnectWebClient()
loadConnect(connectUrl)
}
Generate initialization URL
private fun generateConnectInitializationUrl(configurationOptions: HashMap<String, String>): Uri {
val builder = Uri.parse(configurationOptions["baseUrl"])
.buildUpon()
.appendQueryParameter("isWebview", "true")
.appendQueryParameter("isMobile", "true")
.appendQueryParameter(
"countries",
"[\"AE\",\"EG\",\"JO\"]"
)
for (key in configurationOptions.keys) {
if (key != "baseUrl") {
builder.appendQueryParameter(key, configurationOptions[key])
}
}
return builder.build()
}
To get data returned from connect, setup WebViewClient to listen to URL changes
private fun setupConnectWebClient() {
wvConnect.webViewClient = object : WebViewClient() {
override fun doUpdateVisitedHistory(view: WebView?, url: String?, isReload: Boolean) {
super.doUpdateVisitedHistory(view, url, isReload)
val parsedUri = Uri.parse(url)
if (parsedUri.lastPathSegment == "success") {
//Successful login data
val data = parseConnectUriData(parsedUri)
Log.i("DapiConnect", data.toString())
}
if (parsedUri.lastPathSegment == "error") {
//Failed login attempt error details
val data = parseConnectUriData(parsedUri)
val bankID = data["bankID"]
val type = data["type"]
val msg = "${data["msg"]} for bankID $bankID"
Log.e("DapiConnect", "type: $type message: $msg")
}
}
}
}
Parse data returned from Connect
private fun parseConnectUriData(uri: Uri): HashMap<String, String> {
val data = HashMap<String, String>()
val names = uri.queryParameterNames
names.forEach {
val params = uri.getQueryParameters(it)
if (params.firstOrNull() != null) {
if (
it == "fullName" ||
it == "miniLogoPng" ||
it == "halfLogoPng" ||
it == "fullLogoPng" ||
it == "msg"
) {
val param = URLDecoder.decode(params.first(), "UTF-8")
data[it] = param
} else {
data[it] = params.first()
}
}
}
return data
}
Load Dapi Connect
private fun loadConnect(connectUrl: Uri) {
wvConnect.loadUrl(connectUrl.toString())
}
Updated about 1 year ago