Android客户端WebView调用支付宝,微信,QQ钱包等App支付

作者 mexican 日期 2017-05-26
Android客户端WebView调用支付宝,微信,QQ钱包等App支付

android和ios移动端开发,经常与碰到一些需要在移动端H5页面跳转打开微信、支付宝、QQ钱包、京东钱包等支付APP,H5页面很简单,需要预先设置好需要支付的金额和商户id之类,而剩下的就需要移动端来完成。

webview在加载网页的时候会默认调起手机自带的浏览器加载网页,用户体验不好。但当设置(setWebViewClient)设置这样的监听事件之后,当请求url的时候就不会打开手机自带的浏览器.

1.首先移动端需要拦截到H5的点击之后发出的指令,这就需要WebView的

setWebViewClient(WebViewClient client)方法。

/**
* Sets the WebViewClient that will receive various notifications and
* requests. This will replace the current handler.
*
* @param client an implementation of WebViewClient
*/
public void setWebViewClient(WebViewClient client) {
checkThread();
mProvider.setWebViewClient(client);
}

在上面WebView可以看出,WebViewClient用于接收H5页面发出的各种通知和请求。

webview.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// 网页加载开始
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
// 网页加载结束
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//重要的是这个方法,
//只有在调用webview.load(URL)的时候才会调用。
}
}

webview.loadUrl("H5");

但发起请求的时候后,webview的连续动作是先后请求两个url

1.https://mobilecodec.alipay.com/client_download.htm?qrcode=bax05351pgjhc4yegd2y2084

2.https://ds.alipay.com/from=mobilecodec&scheme=alipayqr%3A%2F%2Fplatformapi%2Fstartapp%3FsaId%3D10000007%26clientVersion%3D3.7.0.0718%26qrcode%3Dhttps%253A%252F%252Fqr.alipay.com%252Fbax05351pgjhc4yegd2y2084%253F_s%253Dweb-other
`
之后返回一个意图,也是用这个意图来打开支付宝app

intent://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=https%3A%2F%2Fqr.alipay.com%2Fbax05351pgjhc4yegd2y2084%3F_s%3Dwebother&_t=1474448799004#Intent;scheme=alipayqr;package=com.eg.Android.AlipayGphone;end

支付宝支付

WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setDomStorageEnabled(true);
webSettings.setGeolocationEnabled(true);
webview.loadUrl("H5");
webview.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e(TAG, "url" + url);
if (parseScheme(url)) {
try {
Uri uri = Uri.parse(url);
Intent intent;
intent = Intent.parseUri(url,
Intent.URI_INTENT_SCHEME);
intent.addCategory("android.intent.category.BROWSABLE");
intent.setComponent(null);
// intent.setSelector(null);
startActivity(intent);
} catch (Exception e) {
Toast.makeText(MainActivity.this,"请安装最新版支付宝",Toast.LENGTH_SHORT).show();
}
} else {
view.loadUrl(url);
}
return true;
}
});
}
public boolean parseScheme(String url) {
if (url.contains("platformapi/startapp")) {
return true;
} else {
return false;
}
}

上面”android.intent.category.BROWSABLE”
具体含义参考
http://blog.csdn.net/gf771115/article/details/7827833

微信支付

调用微信支付基本和上面同理,只需将(url.contains("platformapi/startapp"))中的“platformapi/startapp”改成微信的“weixin://wap/pay?”即可

QQ钱包支付

Scheme=关键词“qqapi”