支付宝客户端端内认证快速接入

调用流程

  1. 调用certification.initialize接口进行认证初始化,并获取返回值biz_no。biz_no是本次认证的标识,在后面的认证接口和查询接口会用到。
  2. 跳转到certification.certify页面接口让用户完成认证,用户完成认证后会跳转回商户回调地址。这个接口支持多种方式接入,可以灵活使用。在这个接口的请求中传入return_url才能回跳到商户,return_url也支持多个协议,可以按照需要使用。
  3. 根据第一步返回的biz_no查询本次认证的结果,结果以查询接口返回的结果为准。

1.认证初始化,接口zhima.customer.certification.initialize:

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
ZhimaCustomerCertificationInitializeRequest request = new ZhimaCustomerCertificationInitializeRequest();
 
String bizContent = "{"
        + "\"transaction_id\":\"ZGYD201610252323000001234\","
        + "\"product_code\":\"w1010100000000002978\","
        + "\"biz_code\":\"FACE\","
        + "\"identity_param\":\"{\\\"identity_type\\\":\\\"CERT_INFO\\\",\\\"cert_type\\\":\\\"IDENTITY_CARD\\\",\\\"cert_name\\\":\\\"张三\\\",\\\"cert_no\\\":\\\"260104197909275964\\\"}\","
        + "\"ext_biz_param\":\"{}\"" + "  }";
request.setBizContent(bizContent);
 
ZhimaCustomerCertificationInitializeResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
    System.out.println("调用成功");
} else {
    System.out.println("调用失败");
}

2.生成认证URL,接口zhima.customer.certification.certify:

生成认证请求URL,需要传回调地址return_url,回调支持在支付宝APP打开(示例:alipays://www.taobao.com)、在浏览器打开(示例[https://www.taobao.com](https://www.taobao.com/))、在商户app打开(使用商户schema协议)。

// 获取alipay client
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
ZhimaCustomerCertificationCertifyRequest request = new ZhimaCustomerCertificationCertifyRequest();

// 设置业务参数,必须要biz_no
request.setBizContent("{\"biz_no\":\"ZM201611103000000888800000733621\"}");

// 设置回调地址,必填. 如果需要直接在支付宝APP里面打开回调地址使用alipay协议
// alipay://www.taobao.com 或者 alipays://www.taobao.com,分别对应http和https请求
request.setReturnUrl("alipays://www.taobao.com");

// 这里一定要使用GET模式
ZhimaCustomerCertificationCertifyResponse response = alipayClient.pageExecute(request, "GET");
// 从body中获取URL
String url = response.getBody();
System.out.println("generateCertifyUrl url:" + url);

生成如下的认证URL(生成的URL必须包含回调地址):

https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2015111100758155&biz_content=%7B%22biz_no%22%3A%22ZM201611253000000121200404215172%22%7D&charset=GBK&format=json&method=zhima.customer.certification.certify&return_url=http%3A%2F%2Fwww.taobao.com&sign=MhtfosO8AKbwctDgfGitzLvhbcvi%2FMv3iBES7fRnIXn%2BHcdwq9UWltTs6mEvjk2UoHdLoFrvcSJipiE3sL8kdJMd51t87vcwPCfk7BA5KPwa4%2B1IYzYaK6WwbqOoQB%2FqiJVfni602HiE%2BZAomW7WA3Tjhjy3D%2B9xrLFCipiroDQ%3D&sign_type=RSA2&timestamp=2016-11-25+15%3A00%3A59&version=1.0&sign=MhtfosO8AKbwctDgfGitzLvhbcvi%2FMv3iBES7fRnIXn%2BHcdwq9UWltTs6mEvjk2UoHdLoFrvcSJipiE3sL8kdJMd51t87vcwPCfk7BA5KPwa4%2B1IYzYaK6WwbqOoQB%2FqiJVfni602HiE%2BZAomW7WA3Tjhjy3D%2B9xrLFCipiroDQ%3D

3.开始认证

根据商户接入终端,可以使用不同的方式唤起支付宝客户端来做认证

1)商户有自己的APP,使用native的方式唤起支付宝客户端

Android版本

/**
* 启动支付宝进行认证
* @param url 开放平台返回的URL
*/
private void doVerify(String url) {
if (hasApplication()) {
    Intent action = new Intent(Intent.ACTION_VIEW);
    StringBuilder builder = new StringBuilder();
    builder.append("alipays://platformapi/startapp?appId=20000067&url=");
    builder.append(URLEncoder.encode(url));
    action.setData(Uri.parse(builder.toString()));
    startActivity(action);
} else {
//处理没有安装支付宝的情况
new AlertDialog.Builder(this)
        .setMessage("是否下载并安装支付宝完成认证?")
        .setPositiveButton("好的", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Intent action = new Intent(Intent.ACTION_VIEW);
        action.setData(Uri.parse("https://m.alipay.com"));
        startActivity(action);
    }
}).setNegativeButton("算了", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
}).show();
}
}

/**
 * 判断是否安装了支付宝
 * @return true 为已经安装
 */
private boolean hasApplication() {
    PackageManager manager = getPackageManager();
    Intent action = new Intent(Intent.ACTION_VIEW);
    action.setData(Uri.parse("alipays://"));
    List<ResolveInfo> list = manager.queryIntentActivities(action, PackageManager.GET_RESOLVED_FILTER);
    return list != null && list.size() > 0;
}

IOS

- (void)doVerify:(NSString *)url {
NNSString *alipayUrl = [NSString stringWithFormat:@"alipays://platformapi/startapp?appId=20000067&url=%@", [self URLEncodedStringWithUrl:url]];
if ([self canOpenAlipay]) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:alipayUrl] options:@{} completionHandler:nil];
} else {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"是否下载并安装支付宝完成认证?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
    [alertView show];
}
}

-(NSString *)URLEncodedStringWithUrl:(NSString *)url {
    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)url,NULL,(CFStringRef) @"!*'();:@&=+$,%#[]|",kCFStringEncodingUTF8));
    return encodedString;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        NSString *appstoreUrl = @"itms-apps://itunes.apple.com/app/id333206289";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appstoreUrl] options:@{} completionHandler:nil];
    }
}

- (BOOL)canOpenAlipay {
    return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"alipays://"]];
}

// 并且在Info.plist文件中配置alipays到LSApplicationQueriesSchemes参数中

2)商户没有自己的APP,使用H5页面的方式唤起支付宝客户端

在商户的H5页面跳转到认证URL,进入芝麻认证引导页,芝麻认证引导页会唤起支付宝客户端。

3)根据认证URL生成二维码,让用户打开支付宝客户端扫码

4.查询认证结果,接口zhima.customer.certification.query:

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
ZhimaCustomerCertificationQueryRequest request = new ZhimaCustomerCertificationQueryRequest();
request.setBizContent("{\"biz_no\":\"ZM201611103000000888800000733621\"}");
ZhimaCustomerCertificationQueryResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
    System.out.println("调用成功");
} else {
    System.out.println("调用失败");
}
onlineServer