android通过webview调起支付宝app支付

android通过webview调起支付宝app支付

一、设置webview属性
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
// 设置可以访问文件
webSettings.setAllowFileAccess(true);
// 设置支持缩放
webSettings.setBuiltInZoomControls(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
// webSettings.setDatabaseEnabled(true);
// 使用localStorage则必须打开
webSettings.setDomStorageEnabled(true);
webSettings.setGeolocationEnabled(true);

二、设置网页加载监听器webviewClient,监听加载的链接
	webview.setWebViewClient(new WebViewClient() {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
		}
	});


三、通过webview加载支付宝返回的url链接,webview.loadUrl("https://qr.alipay.com/baxpdrrspomjxyfp13");
但发起请求的时候后,webview的连续动作是先后请求两个url
1.https://mobilecodec.alipay.com/client_download.htm?qrcode=baxpdrrspomjxyfp13
2.https://render.alipay.com/p/s/i?scheme=%61%6C%69%70%61%79%73%3A%2F%2F%70%6C%61%74%66%6F%72%6D%61%70%69%2F%73%74%61%72%74%61%70%70%3F%73%61%49%64%3D%31%30%30%30%30%30%30%37%26%71%72%63%6F%64%65%3D%25%36%38%25%37%34%25%37%34%25%37%30%25%37%33%25%33%41%25%32%46%25%32%46%25%37%31%25%37%32%25%32%45%25%36%31%25%36%43%25%36%39%25%37%30%25%36%31%25%37%39%25%32%45%25%36%33%25%36%46%25%36%44%25%32%46%25%36%32%25%36%31%25%37%38%25%37%30%25%36%34%25%37%32%25%37%32%25%37%33%25%37%30%25%36%46%25%36%44%25%36%41%25%37%38%25%37%39%25%36%36%25%37%30%25%33%31%25%33%33%25%33%46%25%35%46%25%37%33%25%33%44%25%37%37%25%36%35%25%36%32%25%32%44%25%36%46%25%37%34%25%36%38%25%36%35%25%37%32
之后返回一个意图,也是用这个意图来打开支付宝app
alipays://platformapi/startapp?saId=10000007&qrcode=%68%74%74%70%73%3A%2F%2F%71%72%2E%61%6C%69%70%61%79%2E%63%6F%6D%2F%62%61%78%70%64%72%72%73%70%6F%6D%6A%78%79%66%70%31%33%3F%5F%73%3D%77%65%62%2D%6F%74%68%65%72

四、需要拦截意图,启动支付宝app
       通过WebViewClient的shouldOverrideUrlLoading()方法拦截意图,代码如下:
	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url) {
		if (url.contains("platformapi/startapp")) {
			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) {

			}
		} else {
			view.loadUrl(url);
		}
		return true;
	}


五、随着系统升级等因素,上述启动app方式可能部分不兼容。有更好的方案请邮箱[email protected]


新增:

六、通过测试发现,每次下单返回,然后经过支付宝校验转换后,返回的意图,协议头是一致的,只有qrcode字段不同,故可以使用精简模式启动app支付。
       拿到下单后的支付链接,进行url编码,然后拼接协议头,启动app的方式一致。此方式可不使用webview加载,减少两次url加载,使用原生启动app的模式启动,速度快,体验好

alipays://platformapi/startapp?saId=10000007&qrcode=https%3A%2F%2Fqr.alipay.com%2Fbaxpdrrspomjxyfp13

        


 

iOS不能跳转到支付宝的解决办法:

         http://blog.csdn.net/qq_29708229/article/details/78603087







你可能感兴趣的:(android通过webview调起支付宝app支付)