0

I'm using webview to show transaction process (payment by creditCard). If I use webView.load(given URL) it is working and it will redirect me to bank page in my browser from my app.

But I need to track if the payment is successful (it will redirect me to specific URL) and sent data back to the server.

I used webViewClient, but as I use the client, it will not redirect me to a browser page, it will stay at "processing data" screen inside the app for eternity. Even if I add only view.loadurl(URL) and return true inside shouldOverrideUrlLoading, it will do the same thing. There are 4-5 redirects till the final result. StartURL(add creditCardInfo) - Redirect to validation - redirect to payment confirmation - redirect to specific URL which returns data(if branch)

class PurchaseWebView: AppCompatActivity() {

private lateinit var purchaseWebView: WebView
private val api: API = API.getInstance(this)
private val webViewActivity = this

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.purchase_web_view_layout)

    purchaseWebView = findViewById(R.id.webview)

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    purchaseWebView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
            createLog("WebView Loading url... ", url)
            if (url.startsWith("purchase.complete.url.com", 0)){
                api.purchaseCreditCardResult(url, webViewActivity, object: IPurchaseCallback{
                    override fun onError(errorJSON: JSONObject) {
                        createLog("WebView Error ", errorJSON.toString())
                    }

                    override fun onSuccess(purchaseJSON: JSONObject?) {
                        createLog("WebView Success ", "Finishing Activity")
                    }
                })
                return false
            } else {
                view.loadUrl(url)
                return true
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.N)
    purchaseWebView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
            if (request.url.toString().startsWith("purchase.complete.url.com", 0)){

                api.purchaseCreditCardResult(request.url.toString(), webViewActivity, object: IPurchaseCallback{
                    override fun onError(errorJSON: JSONObject) {
                        createLog("WebView Error ", errorJSON.toString())
                    }

                    override fun onSuccess(purchaseJSON: JSONObject?) {
                        createLog("WebView Success ", "Finishing Activity")
                    }
                })
                return false
            } else {
                view.loadUrl(request.url.toString())
                return true
            }
        }
    }

    purchaseWebView.loadUrl(intent.extras.getString("purchaseURL"))

}
martin1337
  • 2,384
  • 6
  • 38
  • 85

1 Answers1

0

I think that the problem is in this particular call view.loadUrl(request.url.toString()) When you're overriding shouldOverrideUrlLoading you should return only true or false, which means - allow or forbid loading of passed url. But when you're returning true and also calls loadUrl you're trying to load same url twice. Remove the loadUrl command and just return true.

override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
    view.loadUrl(request.url.toString())   // Why?
    return true
}
Demigod
  • 5,073
  • 3
  • 31
  • 49
  • It did not help. I've used alot of tutorials and everybody used it like this. For example here: https://guides.codepath.com/android/Working-with-the-WebView – martin1337 Aug 09 '18 at 13:33
  • Its still inside my app (i will get first URL from server as an response for purchase). First URL will redirect me (inside app) to fill credit card info. As I confirm those data, it will send data somewhere (its bank API). As I wait for confirmation there is loading circle. After that it will open default browser and redirect me to another page. This is how it works without WebViewClient. As I use WebViewClient I'm stuck at data confirmation page and that loading circle is there permanently. It looks like that it will refuse to open browser. – martin1337 Aug 09 '18 at 13:41
  • @martin1337, could it be cause `WebView` tries to open url in a separate window? (check this answer https://stackoverflow.com/a/49793597/3569545). – Demigod Aug 09 '18 at 13:47