如何使用沙箱测试芝麻认证(java版)

来自    芝麻信用 | 
作者:王兰玉  |  更新于:2017-08-11 17:26:22     

说明: 
本帖是利用支付宝沙箱测试"芝麻认证",demo中会配置个人的沙箱账号信息,方便大家测试。 
测试环境:Eclipse+JDK1.6及以上+Tomcat6.0及以上 

1.芝麻认证文档地址:https://docs.open.alipay.com/271/105915/

sdk下载地址:https://docs.open.alipay.com/54/103419/


2.在AlipayConfig文件中配置基本信息 


2.zhima.customer.certification.initialize(认证初始化)

AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
		ZhimaCustomerCertificationInitializeRequest request = new ZhimaCustomerCertificationInitializeRequest();
		ZhimaCustomerCertificationInitializeModel  model = new ZhimaCustomerCertificationInitializeModel();
		//商户请求的唯一标志
		model.setTransactionId("201810118138387142223132325");
		//芝麻认证产品码
		model.setProductCode("w1010100000000002978");
		//认证场景码,FACE:人脸认证 
		model.setBizCode("FACE");
		//值为一个json串,必须包含身份类型identity_type,不同的身份类型需要的身份信息不同,详细介绍,请参考文档
//		model.setIdentityParam("{\"identity_type\":\"CERT_INFO\",\"cert_type\":\"IDENTITY_CARD\",\"cert_name\":\"姓名\",\"cert_no\":\"身份证号码\"}");
				model.setIdentityParam("{\"identity_type\":\"CERT_INFO\",\"cert_type\":\"IDENTITY_CARD\",\"cert_name\":\"沙箱环境\",\"cert_no\":\"823191199611019740\"}");

		request.setBizModel(model);
		ZhimaCustomerCertificationInitializeResponse response = alipayClient.execute(request);
		System.out.println("初始化认证:" + response.getBody());
		System.out.println("bizNo:" + response.getBizNo());
		if(response.isSuccess()){
		System.out.println("调用成功");
		} else {
		System.out.println("调用失败");
		}

 

a).transaction_id:必须唯一,否者会报"sub_code":"INVALID_PARAMETER","sub_msg":"参数有误。参数[transaction_id]不是有效的入 

b).product_code=w1010100000000002978是固定值 

c).沙箱账号中的"cert_name":"姓名","cert_no":"身份证号码"在这里获取:<a href="https://openhome.alipay.com/platform/appDaily.htm?tab=account" target="_blank">https://openhome.alipay.com/platform/appDaily.htm?tab=account

d.接口调用成功返回bizNo用于开始认证接口,所以必须保存好

 

3.zhima.customer.certification.certify(芝麻认证开始认证) 
将2中的biz_no获取传值 

AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
		ZhimaCustomerCertificationCertifyRequest request = new ZhimaCustomerCertificationCertifyRequest();
		ZhimaCustomerCertificationCertifyModel model = new ZhimaCustomerCertificationCertifyModel();
		model.setBizNo("072d2a7be8f80b2a1c2f76047b00fa8a");
		request.setBizModel(model);
		//生成认证请求URL,必要要传回调地址return_url,
		//回调支持在支付宝APP打开(示例:alipays://www.taobao.com)、在浏览器打开(示例:https://www.taobao.com)、在商户app打开(使用商户schema协议)。
		request.setReturnUrl("https://www.baidu.com/");
		ZhimaCustomerCertificationCertifyResponse response = alipayClient.pageExecute(request, "GET");
		if(response.isSuccess()){
		System.out.println("调用成功");
		} else {
		System.out.println("调用失败");
		}
                System.out.println("开始认证:"+response.getBody());

 

返回结果:

调用成功
开始认证:https://openapi.alipaydev.com/gateway.do?alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2016101800718925&biz_content=%7B%22biz_no%22%3A%22072d2a7be8f80b2a1c2f76047b00fa8a%22%7D&charset=utf-8&format=json&method=zhima.customer.certification.certify&return_url=https%3A%2F%2Fwww.baidu.com%2F&sign=wEdgFypYc%2BZjM3sm2OWt3dIxskXKJeJsCSDQWfu2MaOciHf1C72gxxftRY09BP6BKd7hykoQ7UWyvy%2BncfDWVqy80fFMK6niTrsQ%2FdEPEqLmZ6GiQqEZNCgECDS2orn6Fz%2BTVmhuFhP7Smf3f4FGkb1Ku6Ja0mbe7Onk8mtImrrpWQIc6mAih9QINU%2BjiSrAQty3o9lSYYDykYjPM5YVB79vBpR0g%2Fz3FCz4n6Z6urJo9pmLXZIoDfrPesFiz9OZeBLTJ0vG9r53JvJH23B6A43AFaFrV4fR8rmykFLrK12qGNA7EFjChJGtUeIGK9sAq4v%2BTtAPKWMirLVgpw%2FQPQ%3D%3D&sign_type=RSA2&timestamp=2018-10-11+09%3A26%3A12&version=1.0&sign=wEdgFypYc%2BZjM3sm2OWt3dIxskXKJeJsCSDQWfu2MaOciHf1C72gxxftRY09BP6BKd7hykoQ7UWyvy%2BncfDWVqy80fFMK6niTrsQ%2FdEPEqLmZ6GiQqEZNCgECDS2orn6Fz%2BTVmhuFhP7Smf3f4FGkb1Ku6Ja0mbe7Onk8mtImrrpWQIc6mAih9QINU%2BjiSrAQty3o9lSYYDykYjPM5YVB79vBpR0g%2Fz3FCz4n6Z6urJo9pmLXZIoDfrPesFiz9OZeBLTJ0vG9r53JvJH23B6A43AFaFrV4fR8rmykFLrK12qGNA7EFjChJGtUeIGK9sAq4v%2BTtAPKWMirLVgpw%2FQPQ%3D%3D



  注意点: 
    1)填写认证初始化返回的biz_no的值进行请求 
    2)调用pageExecute方法并且要在手机中打开连接进行验证 
   3)return_url 说明: 
    alipays://方式(带s方式)您的网站必须支持https方式才可以跳转 例: alipays://www.taobao.com 
    alipay://方式你的网站支持http方式就可以跳转 例: alipay://www.taobao.com 

注:以上步骤可以用沙箱跑流程进行测试,但是,想要用生成认证请求URL进行认证,是必须使用正式环境的生成的url,沙箱环境生成的url是无法进行认证的!!! 

4.开始认证

4.1 商户有自己的APP ,使用native方式唤起支付宝客户端进行认证

安卓示例
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参数中
 
4.2 若商户没有自己的APP,使用的是H5页面的方式唤起支付宝客户端 在商户的H5页面跳转到认证的URL ,进入芝麻认证的页面芝麻认证会引导页会唤起支付宝客户端
 
4.3 可以通过芝麻认证接口返回的URL 生成二维码,让用户使用支付宝钱包扫一扫功能去进行扫码认证
 
注:以下认证截图是正式环境测试效果,沙箱无法测试!!
将返回的认证链接放到手机上进行访问,点击开始认证,会跳入支付宝钱包,进行人脸认证,认证完整之后会跳返回手机默认浏览器;

5.芝麻认证查询接口zhima.customer.certification.query 

 

未进行认证,使用查询接口查询返回
"zhima_customer_certification_query_response":
    {"failed_reason":"请返回重新认证","passed":"false","channel_statuses":"[]",
    "identity_info":"{}","code":"10000","msg":"Success","attribute_info":"{}"},
    "sign":"RMQGGn1zS3"}
 
认证后查询:
{"zhima_customer_certification_query_response":
    {"passed":"true","channel_statuses":
        "[{\"materials\":{},\"name\":\"FACE\",\"status\":\"PASS\"}]",
        "identity_info":"{}","code":"10000","msg":"Success","attribute_info":"{}"},
    "sign":VxwQyIF+3fIea5iRQ=="}

 

如果大家遇到芝麻认证的问题欢迎大家在帖子下面追问,注意一定要提供详细的错误等相关信息方便解答疑问

附件名称/大小 下载次数 最后更新
芝麻认证demo.zip  (4438KB)  115 2017-08-11 17:24:59 [下载]
aishow | 2017-08-14 14:45:28
你好,请问有没有PHP版的芝麻认证啊
王兰玉 | 2017-08-14 15:57:04
aishow:你好,请问有没有PHP版的芝麻认证啊回到原帖
正在写,写好了会发出了的
aishow | 2017-08-14 17:04:04
王兰玉:正在写,写好了会发出了的回到原帖
在PHP里面也需要写下面代码吗?
String bizContent = "{"
        + "    \"transaction_id\":\""+temp+"\","
        + "    \"product_code\":\"w1010100000000002978\","
        + "    \"biz_code\":\"FACE\","
        + "    \"identity_param\":\"{}\","
        + "    \"ext_biz_param\":\"{}\"" + "  }";
        requestInit.setBizContent(bizContent);
我是个导演 | 2017-08-14 18:53:09
aishow:在PHP里面也需要写下面代码吗?String bizContent = "{" + " \"transaction_id\":\""+temp+"\"," + " \"product_code...回到原帖
已经分享:https://openclub.alipay.com/read.php?tid=1777&fid=25&page=1
aishow | 2017-08-15 09:37:29
我是个导演:已经分享:https://openclub.alipay.com/read.php?tid=1777&fid=25&page=1回到原帖
是不是写成" \"identity_param\":\"{}\,默认获取当前登陆者的信息?
我是个导演 | 2017-08-15 09:45:51
aishow:是不是写成" \"identity_param\":\"{}\,默认获取当前登陆者的信息?回到原帖
是的,这个是传当前用户的信息的
aishow | 2017-08-15 10:44:26
我是个导演:是的,这个是传当前用户的信息的回到原帖
//根据返回的auth_code换取access_token
 //vendor("Alipay.AlipaySystemOauthTokenRequest");
include "AlipaySystemOauthTokenRequest.php";
    $request = new AlipaySystemOauthTokenRequest();
    $request-&gt;setGrantType("authorization_code");
    $request-&gt;setCode($auth_code);
    $result = $aop-&gt;execute($request);
    $access_token = $result-&gt;alipay_system_oauth_token_response-&gt;access_token;
include  "ZhimaCustomerCertificationInitializeRequest.php"
$request = new ZhimaCustomerCertificationInitializeRequest ();
$request-&gt;setBizContent("{" .
    "\"transaction_id\":\"zhima201708140000000000002\"," .//保持唯一性
    "\"product_code\":\"w1010100000000002978\"," .//默认值请勿修改
    "\"biz_code\":\"FACE\"," .
    //沙箱测试cert_name填写沙箱账号名称,cert_no填写沙箱身份证号
    "\"identity_param\":\"{}\"}");
$result = $aop-&gt;execute ($request,$access_token); 
var_dump($result);
Notice: Undefined variable: alipay_system_oauth_token_response in C:\phpStudy\WWW\zhima\initialize.php on line 8 Fatal error: Cannot access empty property in C:\phpStudy\WWW\zhima\initialize.php on line 8
aishow | 2017-08-15 10:45:04
aishow://根据返回的auth_code换取access_token //vendor("Alipay.AlipaySystemOauthTokenRequest"); include "AlipaySystemOauthTokenReque...回到原帖
这样写没错啊,但是为什么会报这样的错?
我是个导演 | 2017-08-15 11:21:05
aishow:这样写没错啊,但是为什么会报这样的错?回到原帖
我的可以正常获取的。你的php是什么版本?我的Apache2.4.23 +php 5.6.25
//第二步使用auth_code换取接口access_token及用户userId
$request = new AlipaySystemOauthTokenRequest();
//请求的必传信息
$request-&gt;setGrantType("authorization_code");
$request-&gt;setCode("4cea6ad64013486db6df44b18828SX77");
$result = $aop-&gt;execute($request);
$access_token = $result-&gt;alipay_system_oauth_token_response-&gt;access_token;
echo $access_token;
另外出现Undefined variable 你关闭一下不提示这个错误看看http://www.jb51.net/article/32127.htm
aishow | 2017-08-15 11:24:36
我是个导演:我的可以正常获取的。你的php是什么版本?我的Apache2.4.23 +php 5.6.25 //第二步使用auth_code换取接口access_token及用户userId $request = new AlipaySyste...回到原帖
是php5.5.38
淡泊呼吸 | 2017-09-07 11:05:04
为什么我用你的demo进行初始化的时候,会报错?
yfb | 2017-10-12 14:35:26
我用UC发起的芝麻认证,但回调却用safari打开了,请问这是什么问题啊?
王兰玉 | 2017-10-12 15:43:57
yfb:我用UC发起的芝麻认证,但回调却用safari打开了,请问这是什么问题啊?回到原帖
已在你发的帖子中回复你了
freedomsure | 2017-12-19 11:06:19
对照了你的代码 我在进行开发的时候发现回调时候 参数failed_reason 是乱码的,初始化时候的请求 返回的汉字是正常的 唯独回调的时候 汉字是乱码的 不太清楚是什么原因
我的回复
回复数
  20
阅读数
  4522

返回顶部