合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
​ 源码:[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&timestamp=2020-05-13+22%3A58%3A39&version=1.0 "出错了-404") 以为自己可以了,其实是越走越远。 我把上面的表单的参数一个一个填写到链接里面,再生成二维码,试了20次,所有的结局都是悲壮的。 在下班之前最后脑洞一开,**突然想到把这个html直接放到页面里面,将页面链接生成一个二维码,再用支付宝扫码,最后通了**。11:55分,下班回家。 梦里依然是这些代码,感觉棒棒的。 最后附上控制器跟页面以及报错日志的文件在附件。 点击链接加入群聊【ThinkPHP56小功能】:[正在跳转](https://jq.qq.com/?_wv=1027&k=Z0c641WP "正在跳转")  ​