记录一下支付宝认证过程
如果要认证 , 必须要有身份证,和姓名 , 可以通过不同的途径获取, 也可以通过授权获取,也可以通过h5获取
参考支付宝API :https://docs.open.alipay.com/271/dz10yd
最主要的是明白 回调地址中怎么能获取到biz_no 因为api 上也没有写在回调地址中 怎么获取biz_no 所以我自己用的EhCache
来做一个缓存
第一步 初始化
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("调用成功");
// 这里应该可以通过 respone 获取 biz_no
} else {
System.out.println("调用失败");
}
第二步生成 url
// 获取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请求
//****** 我自己在这个做了处理 在路径后面传了一个uuid 然后通过uuid 去获取biz_no
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
request.setReturnUrl("alipays://www.app.cn/getBizNo?uuid="+uuid);
//*** 使用缓存吧uuid 和 biz_no 存起来
//存储
EhCacheCacheManager cacheCacheManager=ApplicationContextUtils.applicationContext.getBean(EhCacheCacheManager.class);
//获取CacheManager类
CacheManager cacheManager=cacheCacheManager.getCacheManager();
Cache cache=cacheManager.getCache("SystemCache");
cache.put(new Element(uuid, certifyId));
// 这里一定要使用GET模式
ZhimaCustomerCertificationCertifyResponse response = alipayClient.pageExecute(request, "GET");
// 从body中获取URL
String url = response.getBody();
System.out.println("generateCertifyUrl url:" + url);
第三步 验证 :
@RequestMapping(value = "/getBizNo")
public String getMesg(HttpServletRequest request, HttpServletResponse response) throws Exception {
String uuid=request.getParameter("uuid");
String furl="";
//获取
EhCacheCacheManager cacheCacheManager=ApplicationContextUtils.applicationContext.getBean(EhCacheCacheManager.class);
//获取CacheManager类
CacheManager cacheManager=cacheCacheManager.getCacheManager();
Cache cache=cacheManager.getCache("SystemCache");
String biz_no= (String)cache.get(uuid).getObjectValue();
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
AlipayServiceEnvConstantsfs.APP_ID,
AlipayServiceEnvConstantsfs.PRIVATE_KEY, "json", AlipayServiceEnvConstantsfs.CHARSET, AlipayServiceEnvConstantsfs.ALIPAY_PUBLIC_KEY, "RSA2");
AlipayUserCertifyOpenQueryRequest userRequest = new AlipayUserCertifyOpenQueryRequest();
userRequest.setBizContent("{" +
"\"biz_no\":\""+biz_no+"\"" +
" }");
AlipayUserCertifyOpenQueryResponse userResponse = alipayClient.execute(userRequest);
if(userResponse.isSuccess()) {
String body = userResponse.getBody();
JSONObject obiect = JSONObject.parseObject(body);
JSONObject alipay_user = obiect.getJSONObject("alipay_user_certify_open_query_response");
String passed = alipay_user.getString("passed");
System.out.println("认证结果=============="+passed);
if(passed.equals("F")) {
mesg=false;
}else if(passed.equals("T")){
mesg=true;
}else {
mesg=false;
}
}else {
mesg=false;
}
//清除缓存
cache.remove(uuid);
furl="你想调回的地址"
return furl;
}
大概就是这样写的, 非常的简单, 就是研究起来有点头皮发麻....肝肠寸断..