Integration with mobile application
Android integration (Kotlin)
Once you have obtained a payment session for your backend service, you can start the payment process in the mobile app. For this you must use the WebView
components to carry out the integration process.
To load the processUrl
into a WebView
, you can use the loadUrl
method of the WebView
class. It is important that you do not forget to allow both JavaScript and Cookies for the correct functioning of the payment session, otherwise it will not allow you to advance in the process.
Configure the WebView
These settings help optimize and personalize the browsing experience within the WebView.
It is important that you can identify the Return URL and the Cancel URL to be able to close the WebView
once the payment process is complete.
import android.annotation.SuppressLint
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.runtime.Composable
import androidx.compose.ui.viewinterop.AndroidView
import com.placetopay.p2pr.utilities.Constants
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun CheckoutWebView(processUrl: String, returnUrl: String, cancelUrl: String, refreshWebView: Boolean, onFinished: () -> Unit) {
AndroidView(factory = {
WebView(it).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
)
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
clearCache(true)
CookieManager.getInstance().setAcceptCookie(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().setAcceptThirdPartyCookies(this, true)
}
webChromeClient = WebChromeClient()
webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?, request: WebResourceRequest?
): Boolean {
if (request?.url.toString() == returnUrl || request?.url.toString() == cancelUrl)
onFinished()
return super.shouldOverrideUrlLoading(view, request)
}
}
loadUrl(processUrl)
}
}, update = {
it.loadUrl(processUrl)
if (refreshWebView) it.reload()
})
}