手机网站支付唤起支付宝app

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/feng88724/article/details/83036277

商家在网页中调用支付宝提供的网页支付接口调起支付宝客户端内的支付模块,商家网页会跳转到支付宝中完成支付,支付完后跳回到商家网页内,最后展示支付结果。若无法唤起支付宝客户端,则在一定的时间后会自动进入网页支付流程。

注意:

  • 若接入的是新版本手机网站支付接口(alipay.trade.wap.pay),用户在安装支付宝钱包的情况下,调用手机网站支付接口默认会唤起钱包支付;若接入的是手机网站支付老版本(alipay.wap.create.direct.pay.by.user ),那么需要在请求参数中加入app_pay参数并赋值为Y,详情参见 手机网站支付老版本文档;

  • 开发者需要关注安装了支付宝和未安装支付宝的两种测试场景,对于在手机浏览器唤起H5页面的模式下,如果安装了支付宝却没有唤起,大部分原因是当前浏览器不在支付宝配置的白名单内;

  • 对于商户app内嵌webview中的支付场景,建议集成支付宝App支付产品。或者您可以使用手机网站支付转Native支付的方案,不建议在您的APP中直接接入手机网站支付。

  • 目前在手机网站支付时,通过唤起支付宝app收银台的方式去支付,可以大大提高支付成功率,故不建议禁止唤起支付宝app,目前对外也没有提供禁止唤起支付宝app的方法。

商户APP的WebView处理alipays协议。

iOS

- (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];
}

Android

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;
  }

猜你喜欢

转载自blog.csdn.net/feng88724/article/details/83036277