fix: 更新平台证书下载及证书读取

This commit is contained in:
邹景立 2025-03-05 15:12:40 +08:00
parent 989fd68111
commit 1f0b17fd1c
2 changed files with 12 additions and 18 deletions

View File

@ -42,13 +42,14 @@ class Cert extends BasicWePay
public function download() public function download()
{ {
try { try {
$aes = new DecryptAes($this->config['mch_v3_key']);
$result = $this->doRequest('GET', '/v3/certificates');
$certs = []; $certs = [];
$result = $this->doRequest('GET', '/v3/certificates');
$decrypt = new DecryptAes($this->config['mch_v3_key']);
foreach ($result['data'] as $vo) { foreach ($result['data'] as $vo) {
$certs[$vo['serial_no']] = [ $certs[$vo['serial_no']] = [
'expire' => strtotime($vo['expire_time']), 'expire' => strtotime($vo['expire_time']),
'content' => $aes->decryptToString( 'serial' => $vo['serial_no'],
'content' => $decrypt->decryptToString(
$vo['encrypt_certificate']['associated_data'], $vo['encrypt_certificate']['associated_data'],
$vo['encrypt_certificate']['nonce'], $vo['encrypt_certificate']['nonce'],
$vo['encrypt_certificate']['ciphertext'] $vo['encrypt_certificate']['ciphertext']

View File

@ -367,35 +367,28 @@ abstract class BasicWePay
protected function _getCert($serial = '') protected function _getCert($serial = '')
{ {
$certs = $this->tmpFile("{$this->config['mch_id']}_certs"); $certs = $this->tmpFile("{$this->config['mch_id']}_certs");
if (empty($certs) || empty($certs[$serial]['content'])) { if (empty($certs) || empty($certs[$serial]['serial']) || empty($certs[$serial]['content'])) {
Cert::instance($this->config)->download(); Cert::instance($this->config)->download();
$certs = $this->tmpFile("{$this->config['mch_id']}_certs"); $certs = $this->tmpFile("{$this->config['mch_id']}_certs");
} }
foreach ($certs as $cert) { foreach ($certs as $cert) {
if ($certs[$serial]['expire'] > time()) { if (!empty($cert['serial']) && !empty($cert['content']) && $cert['expire'] > time()) {
$this->config['cert_package'][$cert['serial']] = $cert['content']; $this->config['cert_package'][$cert['serial']] = $cert['content'];
if (empty($this->config['mp_cert_serial'])) { if (empty($this->config['mp_cert_serial']) && (empty($serial) || $serial === $cert['serial'])) {
$this->config['mp_cert_serial'] = $cert['serial']; $this->config['mp_cert_serial'] = $cert['serial'];
$this->config['mp_cert_content'] = $cert['content']; $this->config['mp_cert_content'] = $cert['content'];
} }
} }
} }
// 未设置序号时,直接返回默认证书内容 if (isset($this->config['cert_package'][$serial])) {
if (empty($serial) && !empty($this->config['mp_cert_content'])) { return $this->config['cert_package'][$serial];
} elseif (!empty($this->config['mp_cert_content'])) {
return $this->config['mp_cert_content']; return $this->config['mp_cert_content'];
} } elseif ($cert = $this->withCertPayment()) {
// 遍历证书数组,找到匹配的证书
if ($cert = $this->withCertPayment()) {
return $cert; return $cert;
} } else {
// 检查指定序号的证书是否存在
if (!isset($this->config['cert_package'][$serial])) {
throw new InvalidResponseException("读取平台证书失败!"); throw new InvalidResponseException("读取平台证书失败!");
} }
return $this->config['cert_package'][$serial];
} }
/** /**