android webview 支付H5无法拉起微信支付

参照 blog.csdn.net/chenyannan0…
1.遇到的问题大概就"商家参数错误";"空白页";"系统繁忙"等等吧
2.问题解决:商家参数错误就是因为没有加Map载loadUrl时候要配置一个Map.key是指定的,url是授权域名

Map<Sting,String> map = new HashMap<>();
map.put("Referer", "http://......"); //建议此处填写webm网站地址
复制代码

3.直接用webview去load部分机型可行,因为内核限制的就不行了
4.最终解决方式如下

Map<String, String> map = new HashMap<>();
        map.put("Referer","http://....");
        WebView webView = ((WebView) findViewById(R.id.webview));
 
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setWebChromeClient(new WebChromeClient());
        WebViewClient webViewClient = new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 如下方案可在非微信内部WebView的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<String, String>();
                    extraHeaders.put("Referer", "http://wxpay.wxutil.com");建议此处填写webm网站地址
                    view.loadUrl(url, extraHeaders);
                }
                return true;
            }
 
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) { // 重写此方法可以让webview处理https请求
                handler.proceed();
            }
        };
        webView.setWebViewClient(webViewClient);
        webView.loadUrl("https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=...", map);
复制代码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是微信小程序webview嵌入h5页面调用微信支付的完整代码: HTML页面: ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>微信支付demo</title> </head> <body> <button onclick="callPay()">微信支付</button> <script type="text/javascript"> function callPay() { // 构造支付参数 var params = { appId: '你的appId', timeStamp: '时间戳', nonceStr: '随机字符串', package: '预支付交易会话标识', signType: 'MD5', paySign: '支付签名' }; // 调用微信支付接口 WeixinJSBridge.invoke('getBrandWCPayRequest', params, function(res){ // 支付结果处理 if(res.err_msg == "get_brand_wcpay_request:ok"){ // 支付成功 }else{ // 支付失败 } }); } // 判断是否在微信内置浏览器中打开 if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', callPay, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', callPay); document.attachEvent('onWeixinJSBridgeReady', callPay); } }else{ callPay(); } </script> </body> </html> ``` 小程序页面: ``` <web-view src="https://你的H5页面地址"></web-view> ``` 小程序JS代码: ``` Page({ onReady: function () { // 获取web-view组件 var webview = this.selectComponent("#webview"); // 监听web-view中的页面加载完成事件 webview.onMessage(function(e){ if(e.detail == 'loadFinish'){ // web-view中的页面加载完成后,调用支付接口 wx.request({ url: 'https://你的支付接口地址', method: 'POST', data: { // 构造支付参数 appId: '你的appId', timeStamp: '时间戳', nonceStr: '随机字符串', package: '预支付交易会话标识', signType: 'MD5', paySign: '支付签名' }, success: function(res){ // 将支付参数传递给web-view中的页面 webview.postMessage({type: 'pay', data: res.data}); } }) } }) } }) ``` 需要注意的是,在小程序中调用微信支付接口需要在小程序后台配置支付授权目录,否则会出现调用失败的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值