fix(pay): 修复 create_order 返回 HTML 导致前端 JSON 解析失败
根因:PHP 8.0+ 中 openssl_free_key() 已弃用,调用会触发弃用警告。 服务端若 html_errors=On,该警告以 <br> 等 HTML 形式输出,污染 JSON 响应, 前端 res.json() 报 'Unexpected token <'。 修复: - buildSign / verifyNotifySign 中仅在 PHP < 8.0 时调用 openssl_free_key - 文件顶部设置 display_errors=0 / html_errors=0 / error_reporting=E_ALL, 确保 API 错误只进日志,不污染响应
This commit is contained in:
+13
-2
@@ -11,6 +11,11 @@
|
||||
* 本地 git-ignored 的「密钥文件_git拉黑.txt」读取;两者均不会进入版本库,也不会被部署脚本上传。
|
||||
*/
|
||||
|
||||
// 防止 PHP 错误/弃用警告以 HTML 形式污染 JSON / notify 纯文本响应
|
||||
@ini_set('display_errors', '0');
|
||||
@ini_set('html_errors', '0');
|
||||
error_reporting(E_ALL);
|
||||
|
||||
// 防止任何前置输出污染 JSON / notify 纯文本响应
|
||||
ob_start();
|
||||
require_once __DIR__ . '/../core.php';
|
||||
@@ -130,7 +135,10 @@ function buildSign($params, $privateKeyPem) {
|
||||
throw new Exception('商户私钥加载失败: ' . openssl_error_string());
|
||||
}
|
||||
openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA256);
|
||||
openssl_free_key($key);
|
||||
// PHP 8.0+ 已弃用 openssl_free_key,资源会自动释放;显式调用会触发弃用警告,污染 JSON 响应
|
||||
if (PHP_VERSION_ID < 80000) {
|
||||
@openssl_free_key($key);
|
||||
}
|
||||
return base64_encode($signature);
|
||||
}
|
||||
|
||||
@@ -156,7 +164,10 @@ function verifyNotifySign($params, $publicKeyPem) {
|
||||
$key = openssl_pkey_get_public($publicKeyPem);
|
||||
if ($key === false) return false;
|
||||
$ok = openssl_verify($data, base64_decode($sign), $key, OPENSSL_ALGO_SHA256);
|
||||
openssl_free_key($key);
|
||||
// PHP 8.0+ 已弃用 openssl_free_key,资源会自动释放
|
||||
if (PHP_VERSION_ID < 80000) {
|
||||
@openssl_free_key($key);
|
||||
}
|
||||
return $ok === 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user