微信官方验证签名文档https://wechatpay-api.gitbook.io/wechatpay-api-v3/qian-ming-zhi-nan-1/qian-ming-yan-zheng
public
function get_cert()
{
$url = 'https://api.mch.weixin.qq.com/v3/certificates';
$url_parts = parse_url($url);
$http_method = 'GET';
$timestamp = time();
$nonce = $this->set_nonce();
$body = '';
$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
$message = $http_method . "\n" .
$canonical_url . "\n" .
$timestamp . "\n" .
$nonce . "\n" .
$body . "\n";
$sign = $this->wechat_sign($message);
$rs = $this->wechat_req($nonce, $timestamp, $sign, $url, $http_method, '');
return $rs;
}
在这里插入代码片
//回调首先是要接收头部信息,回调方法里最好保存回调信息用来调试 $response_body回调主体接收 : $response_body= file_get_contents(‘php://input’); $header的接收要注意,如果服务器不是Apche不要用getallheaders
//百度Nginx getallheaders 就可以找到替代方法
我用的CI框架$header = $this->input->request_headers();
TP应该是$this->request->header();具体看官方文档
在这里插入代码片
public
function verify_wechatnotify($header, $response_body)
{
$plat = $this->get_cert();
$serial_no = $plat['data'][0]['serial_no'];
if ($header['Wechatpay-Serial'] != $serial_no) {
return $data=['code'=>'0','msg'=> '平台证书序列号不一致,签名验证不通过'];
}
$associatedData = $plat['data'][0]['encrypt_certificate']['associated_data'];
$nonceStr = $plat['data'][0]['encrypt_certificate']['nonce'];
$ciphertext = $plat['data'][0]['encrypt_certificate']['ciphertext'];
$plat_cert_decrtpt = $this->decryptToString($associatedData, $nonceStr, $ciphertext);
$pub_key = openssl_pkey_get_public($plat_cert_decrtpt);
$Signature = $header['Wechatpay-Signature'];
$message = $header['Wechatpay-Timestamp'] . "\n" . $header['Wechatpay-Nonce'] . "\n" . $response_body . "\n";
$sign = base64_decode($Signature);
$ok = openssl_verify($message, $sign, $pub_key, OPENSSL_ALGO_SHA256);
if ($ok == 1) {
return 1;
} else {
return false;
}
}
注意:商户证书跟平台证书是不一样的,我一开始一直拿商户证书来验签,浪费了时间。后面的获取平台证书序列了之后,一直不知道怎么生成平台公钥。其实解密之后就是公钥了
|