简体   繁体   中英

Android webview for payment gateway redirection is not working

I am loading an url in webview which would redirect to payment gateway screen. My url is:

 https://**hostname**/pgway/gateway/payment/paymentDebitwithPin.jsp?PaymentID=8501798561070830&trandata=b0cb7bcf4cb2f52b355fa4eb3f66905721323d2dfbbb00682dafe34bfca9948a&id=10100125

when loading this url it is redirected to below url

https://**hostname**/pgway/gateway/payment/TransactionError.jsp

The code which I am usingto load this url is

    pgWebview = (WebView)findViewById(R.id.pgWebview);
    pgWebview.getSettings().setUseWideViewPort(true);
    pgWebview.getSettings().setLoadWithOverviewMode(true);
    pgWebview.setWebViewClient(
            new SSLTolerentWebViewClient()
    );
    pgWebview.loadUrl(webURL);

The redirected screen displays the message as

Unauthorized Transaction. Your request cannot be process as the transaction is already initiated.

I have also tried shouldOverrideUrlLoading method but whatever I am getting the same error. Can anyone help me out to fix this issue?

You need to enable javascript for it. Add these lines before loading url:-

pgWebview.getSettings().setJavaScriptEnabled(true);
pgWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

Check by this way you will come to know happening while loading the url, does the url redirection happening in mobile browser?

webView.loadUrl(actualURL);

public class AppWebViewClients extends WebViewClient {

    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        Log.d(TAG, "shouldOverrideUrlLoading");
        Log.d(TAG, "actualURL: " + actualURL);
        Log.d(TAG, "NewURL: " + url);

        if (changedUrl && !url.equals(actualURL)) {
            // page has been clicked
            //TODO- load the page after redirection happens
              view.loadUrl(url);

            return true;
        } else {
            // Designate Urls that you want to load in WebView still.
            view.loadUrl(url);

            return true;
        }
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        //SHOW LOADING IF IT ISNT ALREADY VISIBLE
        Log.d(TAG, "onPageStarted");
    }

    public void onPageFinished(WebView view, String url) {

    }

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

        Log.d(TAG, "Error Description: " + description);
        Log.d(TAG, "failingUrl: " + failingUrl);
    }

    @Override
    public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
        try{
            final AlertDialog.Builder builder = new AlertDialog.Builder(WebViewActivity.this);
            builder.setMessage(R.string.notification_error_ssl_cert_invalid);
            builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    handler.proceed();
                }
            });
            builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    handler.cancel();
                }
            });
            final AlertDialog dialog = builder.create();
            dialog.show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

A better approach might be to check the redirect URL in onPageFinished()

Code

@Override
        public void onPageFinished(WebView view, String url) {
            Log.d("Final URL", url);
        }

This solution work for me when you setting webclient in webview by extending calss : WebViewClient()

 override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
    Log.d(TAG, "shouldOverrideUrlLoading")
    Log.d(TAG, "actualURL: $url")
    Log.d(TAG, "NewURL: $url")
    val parsedUrl = URL(url)

    val hitTestResult = view.hitTestResult
    //hitTestResult==null solve the redirect problem
    if (!TextUtils.isEmpty(url) && hitTestResult == null) {
        view.loadUrl(url)
        return true
    }
    //your handling logic here 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM