商家在网页中调用支付宝提供的网页支付接口调起支付宝客户端内的支付模块,商家网页会跳转到支付宝中完成支付,支付完后跳回到商家网页内,最后展示支付结果。若无法唤起支付宝客户端,则在一定的时间后会自动进入网页支付流程。
注意:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { // NOTE: ------ 对alipays:相关的scheme处理 ------- // NOTE: 若遇到支付宝相关scheme,则跳转到本地支付宝App NSString* reqUrl = request.URL.absoluteString; if ([reqUrl hasPrefix:@"alipays://"] || [reqUrl hasPrefix:@"alipay://"]) { // NOTE: 跳转支付宝App BOOL bSucc = [[UIApplication sharedApplication]openURL:request.URL]; // NOTE: 如果跳转失败,则跳转itune下载支付宝App if (!bSucc) { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"未检测到支付宝客户端,请安装后重试。" delegate:self cancelButtonTitle:@"立即安装" otherButtonTitles:nil]; [alert show]; } return NO; } return YES; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { // NOTE: 跳转itune下载支付宝App NSString* urlStr = @"https://itunes.apple.com/cn/app/zhi-fu-bao-qian-bao-yu-e-bao/id333206289?mt=8"; NSURL *downloadUrl = [NSURL URLWithString:urlStr]; [[UIApplication sharedApplication]openURL:downloadUrl]; }
public boolean shouldOverrideUrlLoading(final WebView view, String url) { // 获取上下文, H5PayDemoActivity为当前页面 final Activity context = H5PayDemoActivity.this; // ------ 对alipays:相关的scheme处理 ------- if(url.startsWith("alipays:") || url.startsWith("alipay")) { try { context.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url))); } catch (Exception e) { new AlertDialog.Builder(context) .setMessage("未检测到支付宝客户端,请安装后重试。") .setPositiveButton("立即安装", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Uri alipayUrl = Uri.parse("https://d.alipay.com"); context.startActivity(new Intent("android.intent.action.VIEW", alipayUrl)); } }).setNegativeButton("取消", null).show(); } return true; } // ------- 处理结束 ------- if (!(url.startsWith("http") || url.startsWith("https"))) { return true; } view.loadUrl(url); return true; }