美文网首页
android webview agentWeb 唤起微信H5

android webview agentWeb 唤起微信H5

作者: androidfan | 来源:发表于2022-04-24 15:27 被阅读0次

本来以为web端的微信支付,跟原生android 关系不大,我们只需要webview加载链接就好,谁知道就这样了


d90e6bc1510e1cbd022477ea09f82c0.png

头大,看了下微信文档

image.png
原因是没有加"Referer"
所以要这样修改:
 protected WebViewClient mWebViewClient = new WebViewClient() {

        private HashMap<String, Long> timer = new HashMap<>();

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return shouldOverrideUrlLoading(view, request.getUrl() + "");
        }

        @Nullable
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            return super.shouldInterceptRequest(view, request);
        }

        //
        @Override
        public boolean shouldOverrideUrlLoading(final WebView view, String url) {

            LogUtils.iTag(TAG, "view:" + new Gson().toJson(view.getHitTestResult()));
            LogUtils.iTag(TAG, "mWebViewClient shouldOverrideUrlLoading:" + url);
            //intent:// scheme的处理 如果返回false , 则交给 DefaultWebClient 处理 , 默认会打开该Activity  , 如果Activity不存在则跳到应用市场上去.  true 表示拦截
            //例如优酷视频播放 ,intent://play?...package=com.youku.phone;end;
            //优酷想唤起自己应用播放该视频 , 下面拦截地址返回 true  则会在应用内 H5 播放 ,禁止优酷唤起播放该视频, 如果返回 false , DefaultWebClient  会根据intent 协议处理 该地址 , 首先匹配该应用存不存在 ,如果存在 , 唤起该应用播放 , 如果不存在 , 则跳到应用市场下载该应用 .
            if (url.startsWith("intent://") && url.contains("com.youku.phone")) {
                return true;
            }

               /*//微信H5支付核心代码,此方法亦可以唤起支付
                if (url.startsWith("weixin://wap/pay?")) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                    return true;
                } else {
                    Map<String, String> extraHeaders = new HashMap<>();
                    extraHeaders.put("Referer", "https://wl.isct.cn");
                    view.loadUrl(url, extraHeaders);

                }
                return true;*/


            try {
                if (url.contains("wx.tenpay.com")) {
                    isWxPay = true;
                    Map weixinHearder = new HashMap();
                    weixinHearder.put("Referer", "此处是申请的授权域名,注意是全拼");
                    WebView webView = new WebView(getContext());
                    layoutWebView.addView(webView);
                    WebSettings settings = webView.getSettings();
                    settings.setJavaScriptEnabled(true);
                    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
                    webView.loadUrl(url, weixinHearder);
                    webView.setWebViewClient(new WebViewClient() {
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                            if (url.startsWith("weixin://")) {
                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_VIEW);
                                intent.setData(Uri.parse(url));
                                startActivity(intent);
                                return true;
                            }
                            return true;
                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
                Xtoastutils.info("调用微信支付失败!");

            }
            return true;
        }

        //处理https请求
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) {
            handler.proceed();
        }


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            LogUtils.iTag(TAG, "mUrl:" + url + " onPageStarted  target:" + getUrl());
            timer.put(url, System.currentTimeMillis());
            if (url.equals(getUrl())) {
                pageNavigator(View.GONE);
            } else {
                pageNavigator(View.VISIBLE);
            }

        }

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

            if (timer.get(url) != null) {
                long overTime = System.currentTimeMillis();
                Long startTime = timer.get(url);
                LogUtils.iTag(TAG, "  page mUrl:" + url + "  used time:" + (overTime - startTime));

                LogUtils.eTag("ffffffffffasdasd", " page mUrl:\" + url + \"  used time:\" + (overTime - startTime)----" + count);

            }

        }


        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest
                request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);

            if (request.isForMainFrame()) {
                if (count >= 3) {
                    count = 0;
                    shuaxin.setVisibility(View.VISIBLE);
                } else {
                    shuaxin.setVisibility(View.GONE);
                }

            }

        }

        @Override
        public void onReceivedError(WebView webviews, int errorCode, String description, String
                failingUrl) {
            super.onReceivedError(webview, errorCode, description, failingUrl);
            LogUtils.iTag(TAG, "onReceivedError:" + errorCode + "  description:" + description + "  errorResponse:" + failingUrl);


            failingUrls = failingUrl;

            count++;
            if (count < 3) {
                shuaxin.setVisibility(View.GONE);
                webviews.loadUrl(failingUrl);
            }


            shuaxin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LogUtils.eTag("asfaseaf", webview + "----" + failingUrls);

                    mAgentWeb.getUrlLoader().reload();

                }
            });


        }
    };

为了防止在支付完成后重复唤起的现象,还需要在onresume里增加:

    @Override
    public void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();//恢复
        if (isWxPay) {
            mAgentWeb.getWebCreator().getWebView().reload();
        }
        super.onResume();
    }

如此,便可以在webview里成功唤起微信进行支付,至于支付完成的回调,可以查看

一、回调页面

正常流程用户支付完成后会返回至发起支付的页面,如需返回至指定页面,则可以在MWEB_URL后拼接上redirect_url参数,来指定回调页面。

如,您希望用户支付完成后跳转至https://www.wechatpay.com.cn,则可以做如下处理:

假设您通过统一下单接口获到的MWEB_URL= https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx20161110163838f231619da20804912345&package=1037687096


则拼接后的地址为MWEB_URL= https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx20161110163838f231619da20804912345&package=1037687096&redirect_url=https%3A%2F%2Fwww.wechatpay.com.cn

参考文章:
1.https://blog.csdn.net/u014752325/article/details/78737609
2.https://blog.csdn.net/qq_33330887/article/details/103888409
3.https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4

相关文章

网友评论

      本文标题:android webview agentWeb 唤起微信H5

      本文链接:https://www.haomeiwen.com/subject/remtyrtx.html