支付宝身份认证(刷脸)小程序PHP

支付宝小程序的身份认证,需要添加支付宝(支付宝身份验证)能力,签约成功后可以开始了

TP代码

public function aliUserCode(): array
{
    // 引入支付宝SDK
    vendor('.alipayapp.aop.AopClient');
    vendor('.alipayapp.aop.request.AlipayUserCertifyOpenInitializeRequest');
    vendor('.alipayapp.aop.request.AlipayUserCertifyOpenCertifyRequest');
    $aop = new \AopClient();
    $data = Payment::read(12, 1, true); // 数据库保存的支付宝配置信息,没有的可以忽略
    $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
    $aop->appId = $data['pay_config']['app_id']; // 支付宝APPID
    $aop->rsaPrivateKey = $data['pay_config']['private_key']; // 应用私钥
    $aop->alipayrsaPublicKey = $data['pay_config']['ali_public_key']; // 支付宝公钥
    $aop->apiVersion = '1.0';
    $aop->signType = 'RSA2';
    $aop->postCharset='utf-8';
    $aop->format='json';
    // 身份认证初始化服务
    $request = new \AlipayUserCertifyOpenInitializeRequest  ();
    $order_no = time().rand(1111,9999);
    $newsigndata=array();
    $newsigndata['outer_order_no']= $order_no;
    $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']= '4418219999999999999'; // 证件号码
    $newsigndata['merchant_config']['return_url']= url('api/payment/fundAuthNotify');
    $newsigndata['face_contrast_picture']="xydasf==";
    $tosign=\json_encode($newsigndata);
    $request->setBizContent($tosign);
    $result = $aop->execute ($request);
    $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
    $resultCode = $result->$responseNode->code;
    if(empty($resultCode) || $resultCode != 10000){
            return '失败';
    }
    // (身份认证开始认证)
    $certify_id = $result->$responseNode->certify_id;
    $request = new \AlipayUserCertifyOpenCertifyRequest();
    $tousersigndata = ['certify_id'=>$certify_id];
    $tousersign=\json_encode($tousersigndata);
    $request->setBizContent($tousersign);
    $result = $aop->pageExecute ( $request, 'get');  // 加get 返回url链接 ,不加返回form表单
    return $result; // 如果是小程序的话就直接返回url链接,客户端的话就返回表单
}

认证场景码(biz_code)这个参数有多种选择

identity_param:需要验证的身份信息参数,格式为 json

         cert_type:证件类型,必填,当前支持  (IDENTITY_CARD:身份证)(HOME_VISIT_PERMIT_HK_MC:港澳居民来往内地通行证)(HOME_VISIT_PERMIT_TAIWAN:台湾居民来往内地通行证)(RESIDENCE_PERMIT_HK_MC:港澳居民居住证)(RESIDENCE_PERMIT_TAIWAN:台湾居民居住证)

返回结果:如下(链接有删减,不能直接使用,你用你的链接就可以了)

https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-php-20200415&app_id=2021002123684200&biz_content=%7B%22certify_id%22%3A%2221ad2ac1bbe81d2b1b68571daef4ce53%22%7D&charset=utf-8&format=json&method=alipay.user.certify.open.certify&sign=KtXWxI%..................................................................3A09%3A07&version=1.0

 

/** *SDK调用说明 */ public function index() { //调用测试 import('AlipaySDK.Alipay'); $pay = new \Alipay(); /** * $trade_type 交易类型 app wap web */ $data = $pay->alipay('215360251411626', '0.01', '测试', '测试', 'web', 'http://www.test.com/callback'); //wap 结果string(880) "https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-php-20180705&app_id=2018072660781410&biz;_content={"body":"测试","subject":"测试","out_trade_no":"215360251411626","timeout_express":"90m","total_amount":"0.01","product_code":"QUICK_WAP_PAY"}&charset=UTF-8&format=json&method=alipay.trade.wap.pay&notify_url=http://www.test.com/callback&sign=RKPyAKAPv28GKfhWIdsCCOualcg2DMCbhpny6yw/tTvJ3Och+yIfS1au7zbGBnm5sFoX2enfgfZekTqBCyzK0/Irw01zDSAOJM4mPYxs96OUkOglWS5Zuq64JwQBAzesFvzKBGgsH2ecL8EUCclDbBIIrAT/DWr6KenyvQvXOxVQNwn540NoS754oBU0w9vLdrGj0Jljvn8D6cbDgnHiE8qLOgpaXv1ROLwtol0yj2H7cM/G7A9RM5Y8jV1aVNYWND8M4XAK5Wr8zwoa9RocbqDpYfw66NXSvrsuFpHf7MpWtSybH5TnxOP5kwzr7pJRDi0XRAyD9u1geei9thUsqA==&sign_type=RSA2&timestamp=2018-09-04+11:50:49&version=1.0" //app 结果直接返回客户端无需处理 alipay_sdk=alipay-sdk-php-20180705&app_id=2018072660781410&biz;_content={"body":"测试","subject":"测试","out_trade_no":"215360251411626","timeout_express":"90m","total_amount":"0.01","product_code":"QUICK_MSECURITY_PAY"}&charset=UTF-8&format=json&method=alipay.trade.app.pay¬ify_url=http://www.test.com/callback&sign_type=RSA2×tamp=2018-09-04+11:54:53&version=1.0&sign=Ab6a0lDxaNcWLL7XcETM85DbBrJI8Sj6czPGKBNK17MZ85FBeStKL+nA/Z4tMORC7H39ooVCDz9ILpbRUfU0eaE7Oe2MOz+Jsj7oUEqwvbvt7eebyPgy2PmPlfAwcZypKla0nUJwHOF5F0cL+mvMGk/K7f2PYMtWL/dIfPOQGC1hBoKFiS+ZF7jIJ98R0AaITAjvQ1drAmJqXpMzUCa1XsTCuQXMXYKu8DNGb/hooJu0xUH/qUU+4fKD0TmBYANQHIrKIBNFl6J2Gw504Mlcfh929ce+YdzF8DXQjacsBSp7f08UJ/FfRvhVXqAfX8lAFETGjc7FXGgU6simNgv1UQ== //web 结果 https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-php-20180705&app_id=2018072660781410&biz;_content={"body":"测试","subject":"测试","out_trade_no":"215360251411626","timeout_express":"90m","total_amount":"0.01","product_code":"FAST_INSTANT_TRADE_PAY"}&charset=UTF-8&format=json&method=alipay.trade.page.pay¬ify_url=http://www.test.com/callback&sign=l9+A5ZByhwLjQwzRes9+ymKkFVu7uMND4j6cIWmXIXHjrBnGaHkcr+hVkwocaU3d/hB2hdPnxDruhyYFgchnsIA/XWumosG9uEnKyD/T52+VZ8FxLh5wpwH0ucvKzbMjgIWk9dPVjUAU5Zz7QjqKsJ+AsItFpttd0tEVZD4je2PFp5mizeCWT0id0H+y44IGexN705rNe6TGpfJDG0RSDsbD/ZoCzd8FRzC9cv5uwf00J1E9Yc0bUH/l37WFDBTv/LZnqdfeWpaiu8ro7CJ3R+z/xGQTHToo2zLtEc4Cwop096X8WqwSdGbyI9FCemcFzigRB+6yzD9nS4PsWEG3Ug==&sign_type=RSA2×tamp=2018-09-04+11:56:06&version=1.0 echo $data; exit; } /** * 回调地址 */ public function callback() { import('AlipaySDK.Alipay'); $pay = new \Alipay(); $data = $_POST; $verify_result = $pay->check($data); if ($verify_result) { // 验证成功 $out_trade_no = $data['out_trade_no']; // 支付宝交易号 $trade_no = $data['trade_no']; // 交易状态 $trade_status = $data['trade_status']; if ($trade_status == 'TRADE_FINISHED') { //成功之后的操作 } else if ($trade_status == 'TRADE_SUCCESS') { //成功之后的操作 } echo "success"; // 请不要修改或删除 } else { // 验证失败 echo "fail"; } } 声明:是文档根据Alipay文档二次整合,完全开源。如果觉得还可以请点击收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值