当前位置:首页 > 程序开发 > 支付宝身份验证API开发记录

支付宝身份验证API开发记录

程序开发 / 星之宇 / 2021-10-1 8:00 / 浏览:8513 / 评论:0

因为有项目要用到支付宝人脸认证,所以就有了这次开发记录。

支付宝SDK:https://opendocs.alipay.com/open/54/103419

支付宝身份认证官方文档:https://opendocs.alipay.com/open/20181012100420932508/api


一、授权申请

1、支付宝身份认证能力开通:https://app.alipay.com/abilityprod/detail?abilityCode=AM010501000000015744 (需要企业认证才可以开通,个人认证无法开通)

2、申请一个应用和密钥,绑定支付宝身份认证能力(教程请去支付宝支持帮助)


二、SDK上传并加载

require_once 'aop/AopClient.php';
require_once 'aop/request/AlipayUserCertifyOpenInitializeRequest.php'; //身份认证初始化服务
require_once 'aop/request/AlipayUserCertifyOpenCertifyRequest.php'; //身份认证开始认证
require_once 'aop/request/AlipayUserCertifyOpenQueryRequest.php'; //身份认证记录查询


三、身份认证初始化服务

接口:alipay.user.certify.open.initialize

        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = 'app id 应用id';
        $aop->rsaPrivateKey = '开发者私钥';
        $aop->alipayrsaPublicKey = '支付宝公钥';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $params = array();
        $params = [
            'outer_order_no' => md5(time().rand()), //商户请求的唯一标识,最大32位
            'biz_code' => 'FACE',
            'identity_param' => [
                'identity_type' => 'CERT_INFO',
                'cert_type' => 'IDENTITY_CARD',
                'cert_name' => '身份证姓名',
                'cert_no' => '身份证号码',
            ],
            'merchant_config' => [
                'return_url' => 'https://www.myxzy.com', //回跳的目标地址
            ]
        ];
        $request = new AlipayUserCertifyOpenInitializeRequest();
        $request->setBizContent(json_encode($params)); //$params转化为json
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            return $result->$responseNode->certify_id; //本次申请操作的唯一标识
        }
本接口主要的坑:参数出现二维数组,并不是一维数组。验签通过就会返回certify_id


四、身份认证开始认证

接口:alipay.user.certify.open.certify

        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = 'app id 应用id';
        $aop->rsaPrivateKey = '开发者私钥';
        $aop->alipayrsaPublicKey = '支付宝公钥';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $params = array();
        $params['certify_id'] = 'certify_id'; //上一步获取的操作的唯一标识
        $request = new AlipayUserCertifyOpenCertifyRequest();
        $request->setBizContent(json_encode($params));
        $result = $aop->pageExecute($request,'GET');
        return htmlspecialchars($result);
直接把返回的结果生成二维码,使用支付宝扫描即可进行实名认证。


其他方式一直返回“永远不要放弃”的报错

499-1.png

本接口最大的坑:官网给的文档很有迷惑性,没讲清楚如何生成二维码,以为像支付宝支付一样生成一个链接转化成二维码即可,但是完全不是一回事。估计大部分不成功的就卡在这里。

其实我这里还碰到一个坑,就是&转义成了&导致生成的二维码扫码失败。


五、身份认证记录查询

接口:alipay.user.certify.open.query

        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = 'app id 应用id';
        $aop->rsaPrivateKey = '开发者私钥';
        $aop->alipayrsaPublicKey = '支付宝公钥';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $params = array();
        $params['certify_id'] =  'certify_id'; //上一步获取的操作的唯一标识
        $request = new AlipayUserCertifyOpenQueryRequest();
        $request->setBizContent(json_encode($params));
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            if($result->$responseNode->passed=='T'){
                return true;
            }
        }
第四步用户认证后,这步查询才会成功。

目前有 0 条评论

    • 昵称
    • 邮箱
    • 网址