app内嵌微信h5支付,支付服务唤起支付处理

微信支付官方文档

https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4

 

app内发起支付,报错net::ERR_UNKNOWN_URL_SCHEME

出错原因:在调微信 H5 支付 https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb

请求头 referer 丢失 。

处理方式:

处理方式一:

<body><formname="form"></form></body><scrip>document.form.method= "post"; document.form.action= "https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?支付信息 "; document.form.submit();</scrip>

处理方式二:jQuery动态创建form表单提交

varaction='https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?支付信息 'varform=$("<form></form>")form.attr('action',action)form.attr('method','post')//追加到body,不显示,然后提交form.appendTo("body")form.css('display','none')form.submit()

处理方式三:

在shouldOverrideUrlLoading()方法中拦截url,设置referer,referer需要实时设置更新,每个页面都要获取上一个界面的referer,然后传递给下一个页面,因此把url传递给referer,代码如下

webview.setWebViewClient(newWebViewClient(){

String referer="商户申请H5时提交的授权域名";

@Overridepublic booleanshouldOverrideUrlLoading(WebView view,String url){

try{if(url.startsWith("weixin://")||url.startsWith("alipays://")){

Intent intent=newIntent();intent.setAction(Intent.ACTION_VIEW);

intent.setData(Uri.parse(url));startActivity(intent);

returntrue;}}catch(Exceptione){

returnfalse;}

if(url.contains("https://wx.tenpay.com")){

Map<String,String>extraHeaders=newHashMap<>();

extraHeaders.put("Referer",referer);

view.loadUrl(url,extraHeaders);

referer=url;

return true;

}

view.loadUrl(url);

returntrue;

}});

 

参考文档:

https://www.jianshu.com/p/0aee6fba892c

https://zhuanlan.zhihu.com/p/103539764

posted @ 2020-12-23 09:57  林子枫  阅读(1153)  评论(0)    收藏  举报