支付凭证优化

This commit is contained in:
Dev
2026-10-27 18:52:26 +08:00
parent 25804d500e
commit d6e67ef8ed
2 changed files with 47 additions and 5 deletions
@@ -384,6 +384,33 @@ class VirtualPayService
if ($body === false) {
throw new \Exception('虚拟支付参数编码失败');
}
$service = new EasyWeChatService();
$result = self::requestXpay($service, $uri, $body, $sessionKey);
$errcode = (int)($result['errcode'] ?? 0);
// 40001 凭证不是最新,42001 凭证过期。刷新后只重试一次
if ($errcode === 40001 || $errcode === 42001) {
Log::info('虚拟支付 access_token 失效,刷新后重试 ' . $uri);
$service->refreshMiniAccessToken(true);
$result = self::requestXpay($service, $uri, $body, $sessionKey);
}
Log::info('虚拟支付请求 ' . $uri . ' ' . json_encode([
'payload' => $payload,
'res' => $result,
], JSON_UNESCAPED_UNICODE));
return is_array($result) ? $result : [];
}
/**
* @param EasyWeChatService $service
* @param string $uri
* @param string $body
* @param string $sessionKey
* @return array
* @throws \Exception
*/
private static function requestXpay(EasyWeChatService $service, $uri, $body, $sessionKey)
{
// access_token 由小程序 HTTP 客户端自动附在 query 上,不参与 pay_sig
$query = [
'pay_sig' => hash_hmac('sha256', $uri . '&' . $body, self::appKey()),
@@ -392,7 +419,6 @@ class VirtualPayService
$query['signature'] = hash_hmac('sha256', $body, $sessionKey);
}
try {
$service = new EasyWeChatService();
$response = $service->app->getClient()->post($uri, [
'query' => $query,
'headers' => ['Content-Type' => 'application/json'],
@@ -403,10 +429,6 @@ class VirtualPayService
Log::error('虚拟支付请求异常 ' . $uri . ' ' . $e->getMessage());
throw new \Exception('虚拟支付请求失败');
}
Log::info('虚拟支付请求 ' . $uri . ' ' . json_encode([
'payload' => $payload,
'res' => $result,
], JSON_UNESCAPED_UNICODE));
return is_array($result) ? $result : [];
}