From 5aff97be526475c4ed00c3b6519cf50ece42cd76 Mon Sep 17 00:00:00 2001 From: Anyon Date: Sat, 7 Mar 2026 09:46:36 +0800 Subject: [PATCH] fix: Add PHP version guard for OpenSSL key cleanup --- WePayV3/Contracts/BasicWePay.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/WePayV3/Contracts/BasicWePay.php b/WePayV3/Contracts/BasicWePay.php index 8f628f3..b880f1f 100644 --- a/WePayV3/Contracts/BasicWePay.php +++ b/WePayV3/Contracts/BasicWePay.php @@ -338,13 +338,26 @@ abstract class BasicWePay } $signature = ''; if (!openssl_sign($data, $signature, $pkeyid, 'sha256WithRSAEncryption')) { - openssl_free_key($pkeyid); + $this->freeKey($pkeyid); throw new InvalidArgumentException("Failed to sign data with private key"); } - openssl_free_key($pkeyid); + $this->freeKey($pkeyid); return base64_encode($signature); } + /** + * 兼容释放 OpenSSL 密钥资源 + * @param mixed $pkey + * @return void + */ + protected function freeKey($pkey) + { + // PHP 8+ 中 openssl_free_key 已废弃,交由运行时回收即可。 + if (version_compare(PHP_VERSION, '8.0.0', '<') && function_exists('openssl_free_key')) { + openssl_free_key($pkey); + } + } + /** * 通过CURL模拟网络请求 * @param string $method 请求方法 @@ -519,4 +532,4 @@ abstract class BasicWePay throw new InvalidDecryptException('Rsa Encrypt Error.'); } } -} \ No newline at end of file +}