一个简单的H5调起本地支付的用例

前言:最近在做一个H5调起本地支付app的功能,后台传来一个支付的url,用webview打开,然后点击支付,通过对其中各个链接的判断处理,决定是否是跳转到支付宝还是微信。
代码就一段,动态创建一个webview,直接添加到屏幕上,然后进行操作。
因为是网页支付,很多东西都是在后台服务器处理,所以移动端就只是纯粹的负责跳转而已,支付与否,成功与失败,都无需移动端操心,所以,连sdk接入都不用,直接处理链接就行。

protected void initPayWebView(final Activity activity, final String Referer, String payUrl) {
        payWebView = new WebView(activity);
        payWebView.setBackgroundColor(2);
        payWebView.requestFocus();
        payWebView.setVerticalScrollBarEnabled(true);
        payWebView.loadUrl(payUrl);
        payWebView.getSettings().setJavaScriptEnabled(true);

        payWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);  //设置 缓存模式
        // 开启 DOM storage API 功能
        payWebView.getSettings().setDomStorageEnabled(true);
        //开启 database storage API 功能
        payWebView.getSettings().setDatabaseEnabled(true);
        String cacheDirPath = activity.getFilesDir().getAbsolutePath() + "payCache";

        //设置数据库缓存路径
        payWebView.getSettings().setDatabasePath(cacheDirPath);
        //开启 Application Caches 功能
        payWebView.getSettings().setAppCacheEnabled(true);


        payWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);

        payWebView.setWebChromeClient(new WebChromeClient() {
            //                @Override
            public void onProgressChanged(WebView view, int newProgress) {
            }

            //                @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }


        });


        payWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(final WebView view, String url) { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边
                Log.e(TAG, "url=>" + url);
                if (url.startsWith("https://mclient.alipay.com/")) {
                    view.loadUrl(url);
                } else if (url.startsWith("alipays://platformapi/startApp?")) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    activity.startActivity(intent);
                    if (payWebView != null) {
                        payWebView.removeAllViews();
                        payWebView.destroy();
                        payWebView = null;
                    }
                    return true;
                }
                /**
                 * 判断是否成功拦截
                 * 若成功拦截,则无需继续加载该URL;否则继续加载
                 */
                // 如下方案可在非微信内部WebView的H5页面中调出微信支付
                else if (url.startsWith("weixin://wap/pay?")) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    activity.startActivity(intent);
                    Log.e(TAG, "url=>4");
                    if (payWebView != null) {
                        payWebView.removeAllViews();
                        payWebView.destroy();
                        payWebView = null;
                    }
                    return true;
                } else {
                    Map extraHeaders = new HashMap();
                    extraHeaders.put("Referer", Referer);
                    view.loadUrl(url, extraHeaders);
                    Log.e(TAG, "url=>5");
                }
                Log.e(TAG, "url=>6");
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                //此方法是为了处理在5.0以上Htts的问题,必须加上
                handler.proceed();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                //结束
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                // TODO Auto-generated method stub
                super.onReceivedError(view, errorCode, description, failingUrl);
                new AlertDialog.Builder(activity).setTitle("网络连接失败")//设置对话框标题
                        .setMessage("请重试")//设置显示的内容
                        .setPositiveButton("刷新", new DialogInterface.OnClickListener() {//添加确定按钮
                            @Override

                            public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                // TODO Auto-generated method stub
                                payWebView.reload();
                            }

                        }).show();//在按键响应事件中显示此对话框
            }
        });
        final ViewGroup view = (ViewGroup) activity.getWindow().getDecorView();
        view.addView(payWebView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
//        MainActivity.payWebView.addJavascriptInterface(new JavaCallObject(), "javaCallJs");
        payWebView.addJavascriptInterface(new JavaPayCallObject(activity), "payCallJs");
    }

    public static class JavaPayCallObject {
        public Activity activity;

        public JavaPayCallObject(Activity activity) {
            this.activity = activity;
        }

        @JavascriptInterface
        public void close() {
            (activity).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (payWebView != null) {
                        payWebView.removeAllViews();
                        payWebView.destroy();
                        payWebView = null;
                    }
                }
            });

        }
    }

调用如下:

String payUrl = "http://www.373yx.com/payment/preview?cliBuyerId=19000&cliSellerId=2018111415564890400010102c2&" +
                        "cpOrderNo=" + System.currentTimeMillis() +
                        "&cpPrice=0.01&cpOrderTitle=%E9%A6%96%E5%85%851";
initPayWebView(MainActivity.this, "http://www.373yx.com", payUrl);

你可能感兴趣的:(一个简单的H5调起本地支付的用例)