支付宝小程序的身份认证,需要添加支付宝(支付宝身份验证)能力,签约成功后可以开始了
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