一个php文件实现支付宝身份认证
1、生成二维码
<?php
header('Content-type:text/html; Charset=utf-8');
/*** 请填写以下配置信息 ***/
$appid = 'xxxxx'; //https://open.alipay.com 账户中心->密钥管理->开放平台密钥,填写开通了“支付宝身份验证”应用的APPID
$outTradeNo = strtoupper(md5(uniqid())); //商户请求的唯一标识,不能重复
$certName = '张三'; //真实姓名。填写需要验证的真实姓名
$certNo = 'xxxxx'; //身份证号码。填写需要验证的证件号码
$returnUrl = 'http://www.xxx.com'; //需要回跳的目标地址,一般指定为商户业务页面
//商户私钥,填写对应签名算法类型的私钥,如何生成密钥参考:https://docs.open.alipay.com/291/105971和https://docs.open.alipay.com/200/105310
$rsaPrivateKey='xxxxx';
/*** 配置结束 ***/
$aliPay = new AlipayService();
$aliPay->setAppid($appid);
$aliPay->setOutTradeNo($outTradeNo);
$aliPay->setCertName($certName);
$aliPay->setCertNo($certNo);
$aliPay->setReturnUrl($returnUrl);
$aliPay->setRsaPrivateKey($rsaPrivateKey);
if(isAlipay()):
$result = $aliPay->initialize();
$response = $result['alipay_user_certify_open_initialize_response'];
if($response && $response['code']=='10000'){
$url = $aliPay->getCertifyUrl($response['certify_id']);
header("Location:{
$url}");
exit();
}else{
echo $response['msg'].' : '.$response['sub_msg'];
}
else:
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<title>支付宝实名认证</title>
<link href="https://cdn.dedemao.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.dedemao.com/jquery/2.1.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h1 align="center">支付宝身份验证(实名认证)</h1>
<h3>请使用支付宝APP扫码进行实名认证:</h3>
<img src="https://sapi.k780.com/?app=qr.get&level=H&size=6&data=<?php echo getCurrentUrl()?>" />
<br>
</div>
</body>
</html>
<?php
endif;
class AlipayService
{
protected $appId;
protected $charset;
//私钥值
protected $rsaPrivateKey;
protected $outTradeNo;
protected $returnUrl;
protected $certName;
protected $certNo;
public function __construct()
{
$this->charset = 'utf-8';
}
public function setAppid($appid)
{
$this->appId = $appid;
}
public function setOutTradeNo($outTradeNo)
{
$this->outTradeNo = $outTradeNo;
}
public function setCertName($certName)
{
$this->certName = $certName;
}
public function setCertNo($certNo)</