简体   繁体   中英

How to handle payment gateway via webview in android

we add one activity in our project via which user can purchase some thing, we open URL which our client gives to us in our web view. They using ccavenus payment getway. On some phone after purchase done ccavenus redirection URL get stuck and it shows spinner for the infinite amount of time. and on some phone URL redirection works smoothly and users can buy things.

Can you guys help us how to solve this problem ? Here is our code of web view.

private void launchWebView(String URL)
    {

            webView.setInitialScale(1);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);

            webView.getSettings().setJavaScriptEnabled(true);

            webView.getSettings().setAllowFileAccess(true);
            webView.getSettings().setAllowContentAccess(true);
            webView.setScrollbarFadingEnabled(false);


            webView.setWebChromeClient(new WebChromeClient(){
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    if(newProgress == 100){
                        mProgressBar.setVisibility(View.GONE);
                    }else{
                        mProgressBar.setVisibility(View.VISIBLE);
                        mProgressBar.setProgress(newProgress);
                    }
                }

            });

        webView.setWebViewClient(new WebViewClient(){

            public boolean shouldOverrideUrlLoading(WebView view,String url){
                return false;
            }
            //To Handle SSL Errors
            @Override
            public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("The site's security certificate is not trusted!");
                builder.setPositiveButton("Proceed anyway", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        handler.proceed();
                    }
                });
                builder.setNegativeButton("Back To safety", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        handler.cancel();
                    }
                });
                final AlertDialog dialog = builder.create();
                dialog.show();
            }
        });


            webView.loadUrl(URL);

            mProgressBar = (ProgressBar)findViewById(R.id.offer_page_progressbar);
            mProgressBar.setMax(100);


    }
   private void startWebView(String url) {
    webview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        //Show loader on url load
        @Override
        public void onLoadResource(WebView view, String url) {
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            try {

                if (url.contains("/wallet-recharge/failed")) {
                    finishMethod();
                } else if (url.contains("/wallet-recharge/pay-completed")) {
                    webview.clearHistory();
                    Intent broadcastIntent = new Intent();
                    broadcastIntent.setAction("com.package.ACTION_CLASS_CABILY_MONEY_REFRESH");
                    sendBroadcast(broadcastIntent);
                    finishMethod();
                } else if (url.contains("/wallet-recharge/pay-cancel")) {
                    finishMethod();
                }

            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    });

    //Load url in webView
    webview.loadUrl(url);
}

uses permissions required

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />

use xml file for webview for show any view

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/linNtWkOff"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:visibility="visible"
        android:gravity="center_vertical"
        android:orientation="vertical">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:src="@drawable/img_ntwk_conn"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:gravity="center_horizontal"
            android:textColor="@color/black"
            android:text="Internet Connection Unavailable"/>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

i have used webView using network check in baseActivity //There are following parameters:

    public class MainActivity extends BaseActivity {

    private WebView mWebView;
    LinearLayout linNtWkOff;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linNtWkOff = findViewById(R.id.linNtWkOff);
        mWebView = (WebView) findViewById(R.id.webView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (checkInternetConnection()){
            setNetWorkON();
        }else {
            setNetWorkOFF();
        }
        mWebView.setWebViewClient(new MyBrowser());
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadsImagesAutomatically(true);
        mWebView.loadUrl("http://astro.astrobrnd.com/api/login");//https://undercontrol.in/
    }

    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (mWebView.canGoBack()) {
                        mWebView.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    public void setNetWorkON(){
        linNtWkOff.setVisibility(View.GONE);
        mWebView.setVisibility(View.VISIBLE);
    }
    public void setNetWorkOFF(){
        linNtWkOff.setVisibility(View.VISIBLE);
        mWebView.setVisibility(View.GONE);
    }

    private boolean checkInternetConnection() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = manager.getActiveNetworkInfo();
        boolean isConnected;
        if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
            isConnected =true;
        } else {
            isConnected= false;
        }
        return isConnected;
    }
}

BaseActivity use for newwork connection check

public class BaseActivity extends AppCompatActivity {

    private static final int WIFI_ENABLE_REQUEST = 0x1006;

    private BroadcastReceiver mNetworkDetectReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            checkInternetConnection();
        }
    };
    private AlertDialog mInternetDialog;
    private boolean isConnected;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isConnected = false;
        registerReceiver(mNetworkDetectReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
    }

    @Override
    protected void onDestroy() {

        unregisterReceiver(mNetworkDetectReceiver);
        super.onDestroy();
    }

    private void checkInternetConnection() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = manager.getActiveNetworkInfo();

        if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
            isConnected =true;
            //showNoConnectionSnackBar("Connected", isConnected, 1500);
            onResume();
        } else {
            isConnected= false;
            //showNoConnectionSnackBar("No active Internet connection found.", isConnected,10000);
            onResume();
        }
    }

    private  void showNoConnectionSnackBar(String message, boolean isConnected, int duration) {
        Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),
                message, duration);
        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView
                .findViewById(R.id.snackbar_text);
        textView.setTextColor(ContextCompat.getColor(this, android.R.color.white));

        if (isConnected){
            sbView.setBackgroundColor(getResources().getColor(R.color.purple_500));

        }else{
            sbView.setBackgroundColor(getResources().getColor(R.color.purple_500));
            snackbar.setAction("Turn On", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent internetOptionsIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                    startActivityForResult(internetOptionsIntent, WIFI_ENABLE_REQUEST);
                }
            });
            snackbar.setActionTextColor(getResources().getColor(R.color.purple_500));
        }

        snackbar.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == WIFI_ENABLE_REQUEST) {

        } else {

            super.onActivityResult(requestCode, resultCode, data);

        }
    }

}

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