解决在APP中使用微信H5支付,提示“商家参数格式有误…”
昨天抽空研究了一下微信H5支付,发现的确很方便,如果只使用浏览器做H5支付,
没什么问题,市面上主流的手机浏览器应该都可以调起微信支付(虽然我只测试了UC和QQ浏览器 - -! )
[微信H5支付](https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_1)
但是当我接入到APP中的时候,一直出现“商家参数格式有误,请联系商家解决”,why?
我什么都没有修改啊。上网也找了很多文章,说是需要在WebView的shouldOverrideUrlLoading方法中写成这样:
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", "http://wxpay.wxutil.com");
view.loadUrl(url, extraHeaders);
}
return true;
我也尝试了,这样在华为Mate 9 pro 以及小米5 上的确可以(android7.0)
但是在华为CL10(android4.4.4)上面还是提示这个错误。。(¬_¬)
最终解决方案:
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("weixin://wap/pay?")) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});