源码:[https://download.csdn.net/download/qq\_34050360/12418202](https://download.csdn.net/download/qq_34050360/12418202 "https://download.csdn.net/download/qq_34050360/12418202")
快速接入地址:
[接入准备 | 支付应用](https://opendocs.alipay.com/open/20181012100420932508/quickstart#%E4%B8%8B%E8%BD%BD%E6%9C%8D%E5%8A%A1%E7%AB%AF%20SDK "接入准备 | 支付应用")
首先讲集成sdk,直接放sdk下载地址:
[https://openhome.alipay.com/doc/sdkDownload.resource?sdkType=PHP](https://openhome.alipay.com/doc/sdkDownload.resource?sdkType=PHP "https://openhome.alipay.com/doc/sdkDownload.resource?sdkType=PHP")
将下载好的sdk解压放入extend 里面,目录层级注意一下,便于后面用 require 引入三个类库。
![](https://img-blog.csdnimg.cn/2020051409074090.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MDUwMzYw,size_16,color_FFFFFF,t_70)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")编辑
引入规则:
~~~php
require ROOT_PATH.'/extend/alipay/aop/AopClient.php';
require ROOT_PATH.'/extend/alipay/aop/request/AlipayUserCertifyOpenInitializeRequest.php';
require ROOT_PATH.'/extend/alipay/aop/request/AlipayUserCertifyOpenCertifyRequest.php';
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
实例化的时候要加斜杠:
~~~php
$aop = new \AopClient();
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
接下来第一步就是验签了,坑也随之而来。
首先确保公钥私钥是对的(开发者私钥就是应用私钥,支付宝公钥是支付宝给的,这个自行解决,卡在这里就可以自行放弃了,生活很美好,为什么要写代码来折磨自己)
~~~html
1./*'请填写开发者私钥去头去尾去回车,一行字符串'*/
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
~~~html
2./*'请填写支付宝公钥,一行字符串'*/
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
第二步就是支付宝本身的坑,直接上代码了。
此处数次跟官方技术沟通,被告知,沙箱环境没有用,无法辅助测试,无语更无奈(沙箱环境: [支付宝开放平台-接口调试](https://openhome.alipay.com/platform/demoManage.htm#/alipay.user.certify.open.initialize "支付宝开放平台-接口调试"))
### 第一步:alipay.user.certify.open.initialize(身份认证初始化服务):
[alipay.user.certify.open.initialize(身份认证初始化服务) | API](https://opendocs.alipay.com/apis/api_2/alipay.user.certify.open.initialize "alipay.user.certify.open.initialize(身份认证初始化服务) | API")
## 请求示例(此处注意有坑:这个帖子一直提醒我,但是被我一直忽略 [https://www.uzhan5.com/post/6504](https://www.uzhan5.com/post/6504 "https://www.uzhan5.com/post/6504"))
~~~php
官方代码:
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = 'your app_id';
$aop->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串';
$aop->alipayrsaPublicKey='请填写支付宝公钥,一行字符串';
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='GBK';
$aop->format='json';
//$request = new AlipayUserCertifyOpenInitializeRequest (); //TODO 官方这个地方写错了,应该用下面的代码才能实现
$request = new AlipayUserCertifyOpenQueryRequest();
$request->setBizContent("{" .
"\"outer_order_no\":\"ZGYD201809132323000001234\"," .
"\"biz_code\":\"FACE\"," .
"\"identity_param\":\"{\\\"identity_type\\\":\\\"CERT_INFO\\\",\\\"cert_type\\\":\\\"IDENTITY_CARD\\\",\\\"cert_name\\\":\\\"收委\\\",\\\"cert_no\\\":\\\"260104197909275964\\\"}\"," .
"\"merchant_config\":\"{\\\"return_url\\\":\\\"xxx\\\"}\"," .
"\"face_contrast_picture\":\"xydasf==\"" .
" }");
$result = $aop->execute ( $request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if(!empty($resultCode)&&$resultCode == 10000){
echo "成功";
} else {
echo "失败";
}
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
代码中的坑:
~~~php
$request->setBizContent("{" .
"\"outer_order_no\":\"ZGYD201809132323000001234\"," .
"\"biz_code\":\"FACE\"," .
"\"identity_param\":\"{\\\"identity_type\\\":\\\"CERT_INFO\\\",\\\"cert_type\\\":\\\"IDENTITY_CARD\\\",\\\"cert_name\\\":\\\"收委\\\",\\\"cert_no\\\":\\\"260104197909275964\\\"}\"," .
"\"merchant_config\":\"{\\\"return_url\\\":\\\"xxx\\\"}\"," .
"\"face_contrast_picture\":\"xydasf==\"" .
" }");
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
修改后的代码:
~~~php
$newsigndata=array();
$newsigndata['outer_order_no']=md5(time());
$newsigndata['biz_code']="FACE";
$newsigndata['identity_param']['identity_type']="CERT_INFO";
$newsigndata['identity_param']['cert_type']="IDENTITY_CARD";
$newsigndata['identity_param']['cert_name']="刘洋";
$newsigndata['identity_param']['cert_no']="220623199002030051";
$newsigndata['merchant_config']['return_url']="http://kmxlm-api.maoln.com/api/Reflect/reflectList";
$newsigndata['face_contrast_picture']="xydasf==";
$tosign=json_encode($newsigndata);
$request->setBizContent($tosign);
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
~~~bash
{"alipay_user_certify_open_initialize_response":{"code":"40004","msg":"Business Failed","sub_code":"unknown-sub-code","sub_msg":"未知的错误码UNKNOWN_ERROR"},"sign":"GNjUpr/i4iCd0mPE5LgdIuNXBY0D6QJfAx241kGrWz3kvtzCeu7+izZs51iN1aGSgrdg2pQcQbsk7taGG7WxUquHryiV4RIZhZ3CQXNdpZeOWibVd8DAINviKLYlVyxZrHnV2rpxCFPHdRtlSiFffkqlqiEMwTgPT7a8pC65ESo="}
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
### 上面是之前第一步一直没绕过的报错,终究是解决了。
到此验签通过(通过之前一直报错40004未知错误,就是验签失败,也是上面的官方代码所致,在此顺便贴一下前面没走通之前的一个报错),进入第二个环节:
### 第二步:alipay.user.certify.open.certify(身份认证开始认证)
[alipay.user.certify.open.certify(身份认证开始认证) | API](https://opendocs.alipay.com/apis/api_2/alipay.user.certify.open.certify "alipay.user.certify.open.certify(身份认证开始认证) | API")
不要相信沙箱环境,没意义。直接进入开干,坑也来了。
跟第一步里面坑一样,json传需要重新拼接
~~~php
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = 'your app_id';
$aop->rsaPrivateKey = '请填写开发者私钥去头去尾去回车,一行字符串';
$aop->alipayrsaPublicKey='请填写支付宝公钥,一行字符串';
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset='GBK';
$aop->format='json';
$request = new AlipayUserCertifyOpenCertifyRequest ();
$request->setBizContent("{" .
"\"certify_id\":\"OC201809253000000393900404029253\"" .
" }");
$result = $aop->pageExecute ( $request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if(!empty($resultCode)&&$resultCode == 10000){
echo "成功";
} else {
echo "失败";
}
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
跳坑操作:
~~~php
$data=array();
$data['certify_id']=$certify_id;
$tosign=json_encode($data);
$request->setBizContent($tosign);
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
虽然距离成功不远,但是支付宝会让你感到绝望,你执行后的结果是这样的,如下所示:
![](https://img-blog.csdnimg.cn/20200514092734942.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MDUwMzYw,size_16,color_FFFFFF,t_70)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")编辑
### **惊不惊喜意不意外**
支付宝让你永远不要放弃,其实在这个页面之前你还会看到一个字符串被打印出来,1200多个字符。抱着怀疑的态度去打印接收参数:
![](https://img-blog.csdnimg.cn/20200514092926579.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MDUwMzYw,size_16,color_FFFFFF,t_70)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")编辑
结果如下所示:
~~~html
<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=UTF-8' method='POST'><input type='hidden' name='biz_content' value='{"certify_id":"f496ac867cdfc08f25b5c095ca29a739"}'/><input type='hidden' name='app_id' value='2021001160633987'/><input type='hidden' name='version' value='1.0'/><input type='hidden' name='format' value='json'/><input type='hidden' name='sign_type' value='RSA2'/><input type='hidden' name='method' value='alipay.user.certify.open.certify'/><input type='hidden' name='timestamp' value='2020-05-13 23:45:14'/><input type='hidden' name='alipay_sdk' value='alipay-sdk-php-20200415'/><input type='hidden' name='charset' value='UTF-8'/><input type='hidden' name='sign' value='OTnUvXOrqwqkAwfjH0xbKXzlTOLeGWjPPXsDRe9m5msitI/67Poyc2JXaRYwahNFqiqs3MqI2MyUggdQdNBsnc6sxPzqYQxEwSGmHzZI9aCJnpAC5RNC6bkdxe/iyI72dYyuO5XN7zVIEiWn++cQzOIGKuUSQsMqXNRVa/8O/EIcZdvKNB2d9EDSyInr5Xc/HGoGB79bMl2cLVwUaOMU/OsqcJ1kh1wAcSVYmDb+Gr6XFGpOItW3oPpb70B57UU7xTA8Yco2BzdlTOfoOy4KZMsvF0vZc7QO7gkJHJF02CHSfYFtwZA7o6qbWAOalnjxjMiSYnBinnihcxgQnDxB4Q=='/><input type='submit' value='ok' style='display:none;''></form><script>document.forms['alipaysubmit'].submit();</script>
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
转码后的结果:
~~~html
<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=UTF-8' method='POST'>
<input type='hidden' name='biz_content' value='{"certify_id":"f496ac867cdfc08f25b5c095ca29a739"}'/>
<input type='hidden' name='app_id' value='2021001160633987'/>
<input type='hidden' name='version' value='1.0'/>
<input type='hidden' name='format' value='json'/>
<input type='hidden' name='sign_type' value='RSA2'/>
<input type='hidden' name='method' value='alipay.user.certify.open.certify'/>
<input type='hidden' name='timestamp' value='2020-05-13 23:45:14'/>
<input type='hidden' name='alipay_sdk' value='alipay-sdk-php-20200415'/>
<input type='hidden' name='charset' value='UTF-8'/>
<input type='hidden' name='sign' value='OTnUvXOrqwqkAwfjH0xbKXzlTOLeGWjPPXsDRe9m5msitI/67Poyc2JXaRYwahNFqiqs3MqI2MyUggdQdNBsnc6sxPzqYQxEwSGmHzZI9aCJnpAC5RNC6bkdxe/iyI72dYyuO5XN7zVIEiWn++cQzOIGKuUSQsMqXNRVa/8O/EIcZdvKNB2d9EDSyInr5Xc/HGoGB79bMl2cLVwUaOMU/OsqcJ1kh1wAcSVYmDb+Gr6XFGpOItW3oPpb70B57UU7xTA8Yco2BzdlTOfoOy4KZMsvF0vZc7QO7gkJHJF02CHSfYFtwZA7o6qbWAOalnjxjMiSYnBinnihcxgQnDxB4Q=='/>
<input type='submit' value='ok' style='display:none;'>
</form>
<script>document.forms['alipaysubmit'].submit();</script>
~~~
![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")
看清了没,亮瞎了双眼,不按正常套路出牌,可能官方给的示例正常返回值都是java的操作,php直接就是这个了,后面我们在别的地方解析出了人家扫码的那个码的链接如下:
[出错了-404](https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-java-4.5.0.ALL&app_id=2018040102484116&biz_content=%7B%22certify_id%22%3A%223f3523824cc8ed0c9c569d0e5973fa19%22%7D&charset=GBK&format=json&method=alipay.user.certify.open.certify&sign=BYqQIh7r3IdT6%2B5cz7Dc39xc7DG%2BOPLUi0gO5ejYpSx1YX11j0zZcAdUiF0py6qP7loJj8yK3UqdBo0ZvxxdsWJrIYdXPz6pYqUd8iHa74aTwTqY2jmRd3sflKY48c%2FlHhbDpEDZuDIz1dygf%2FiaqfF8j%2BtSTw48uBSRbbh0tjPOkGQhmSpgCBa3ZSCywEhle%2ByuWbEA9linuEAyST3bLEeL%2BaxkPQNdZYk%2Bfvbd5cWK%2Byp3GHW8zfDCbeaMcFynTXnESPo27uSRBGGmhwe7sPzmuLBxjDQzVYeqbrkihf%2FD36fQxanIHEtJlR%2FLsyRyymwBa37CiYTQsN9cyQ1i0g%3D%3D&sign_type=RSA2×tamp=2020-05-13+22%3A58%3A39&version=1.0 "出错了-404")
以为自己可以了,其实是越走越远。
我把上面的表单的参数一个一个填写到链接里面,再生成二维码,试了20次,所有的结局都是悲壮的。
在下班之前最后脑洞一开,**突然想到把这个html直接放到页面里面,将页面链接生成一个二维码,再用支付宝扫码,最后通了**。11:55分,下班回家。
梦里依然是这些代码,感觉棒棒的。
最后附上控制器跟页面以及报错日志的文件在附件。
点击链接加入群聊【ThinkPHP56小功能】:[正在跳转](https://jq.qq.com/?_wv=1027&k=Z0c641WP "正在跳转")
- 支付宝身份验证接口踩坑实录-PHP(基于ThinkPHP5)(第二版更新中)
- 抖音小程序开发之授权登录+支付宝支付+微信支付(ThinkPHP5-第三版修订中)
- TP5小知识点锦集(长期更新)
- PHP 二维码生成+识别
- 高德地图点聚合点击事件以及内容渲染
- ThinkPhP5使用phpexcle 导出数据(复制粘贴就可使用)
- Fastadmin微信小程序授权登录+获取手机号插件
- PHP -AES-128-CBC位加密解密
- PHP-Rsa分段加密解密(ThinkPHP5)
- PHP大转盘抽奖代码片段
- Fastadmin 项目上线关闭调试模式注意事项(记一次require-table.js修改事件)
- ThinkPHP5条件查询FIND_IN_SET正反使用
- ThinkPhP5整合微信小程序订阅消息
- think-queue处理延时任务事件
- ThinkPHP5 生成二维码
- Python3定时监控指定文件内容变换-(增加多行,遍历每行进行逻辑分析处理)
- Python3开发声光报警器监控触发报警
- ThinkPHP5下载文件流到本地
- 百度鹰眼抽轨迹集合稀算法&缩放比例调整显示静态图(ThinkPHP5)
- PHP 导出Excle
- Fastadmin 自定义Tab选项卡(B表的条件查询A表的数据,在A表里面加B表的参数作为选项卡)
- Fastadmin 修改url组件跳转为复制功能
- 微信H5分享好友跟朋友圈-基于Easywechat
- Python3抓取监控日志文件关键词跟内容变化修正版
- ThinkPHP5上传图片压缩处理-(解决IOS拍照上传旋转90度问题)最近更新2021年12月9日11:35:07
- 二维数组根据‘key’分组为新的三维数组
- ThinkPHP5 成功部署Workerman 运行示例
- Fastadmin框架TOKEN的使用
- ThinkPHP5 -微信小程序订阅消息开发-插件(插件基于fastadmin开发)
- ThinkPHP5-文本转义emoji表情
- ThinkPHP5 自定义命令行处理 监听Redis key失效触发回调事件,实现延迟任务 实现自动取消订单,自动完成订单
- Fastadmin插件Shopro商城里面短信插件修改为腾讯云短信插件步骤
- Fastadmin框架自定义搜索操作流程
- ThinkPHP5 处理 微信小程序内容安全审核
- Fastadmin自定义快捷搜索之模糊搜索关联他表
- php根据年月获取指定月份天数及日期数组的方法
- PHP构造函数使用校验token
- 基于ThinkPHP5&Redis腾讯云短信验证码注册登录基础业务逻辑代码整合
- ThinkPHP 解决跨域问题
- 支付宝沙箱环境测试支付(好久没做都忘了,写个博客比较省事)
- ThinkPHP5生成抖音小程序带参数二维码
- ThinkPHP5导入Excle-简单丝滑
- PHP生成带参数的小程序二维码
- ThinkPHP5成功调通IOS苹果支付
- swoole写聊天室,简单粗暴
- 微信小程序内容安全鉴别的时候,不成功因为没有转码
- Fastadmin 后台Excle文件上传(更新新增功能)
- Lnmp 配置thinkphp5 Nginx基础设置,包含http+https配置
- 通过经纬度获取数据库信息自动计算地址距离远近
- 二维数组根据某个字段排序
- PHP二维数组去重,最简单的方法
- TP5微信redis列队群发模板消息Sendmsg
- PHP检测是否关注公众号,亲测可用
- 小程序推广分享带参数二维码生成
- 基于ThinkPHP5微信H5授权登录获取用户信息(改进版)
- php过滤微信昵称中的表情
- Socket.io