读取虚拟支付配置优化

This commit is contained in:
Dev
2026-10-27 16:51:41 +08:00
parent f1d03be571
commit 20f9113d94
2 changed files with 22 additions and 8 deletions
+2 -1
View File
@@ -371,7 +371,8 @@ class Order extends Api
*/ */
private function assertVirtualPaySignature() private function assertVirtualPaySignature()
{ {
$token = (string)config('common.virtual_pay.msg_token'); $virtualPay = config('common.virtual_pay');
$token = is_array($virtualPay) ? (string)($virtualPay['msg_token'] ?? '') : '';
if ($token === '') { if ($token === '') {
throw new \Exception('未配置虚拟支付消息令牌'); throw new \Exception('未配置虚拟支付消息令牌');
} }
@@ -43,7 +43,7 @@ class VirtualPayService
*/ */
public static function createClientPay($orderNo, $amountYuan, $openid) public static function createClientPay($orderNo, $amountYuan, $openid)
{ {
$offerId = (string)config('common.virtual_pay.offer_id'); $offerId = (string)(self::payConfig()['offer_id'] ?? '');
if ($offerId === '') { if ($offerId === '') {
self::logMissingOfferId(); self::logMissingOfferId();
throw new \Exception('未配置虚拟支付 OfferID'); throw new \Exception('未配置虚拟支付 OfferID');
@@ -159,9 +159,11 @@ class VirtualPayService
*/ */
public static function decryptMessage($encrypt, $msgSignature, $nonce, $timestamp) public static function decryptMessage($encrypt, $msgSignature, $nonce, $timestamp)
{ {
$token = (string)config('common.virtual_pay.msg_token'); $payConfig = self::payConfig();
$aesKey = (string)config('common.virtual_pay.aes_key'); $wechat = config('common.wechat');
$appId = (string)config('common.wechat.app_id'); $token = (string)($payConfig['msg_token'] ?? '');
$aesKey = (string)($payConfig['aes_key'] ?? '');
$appId = is_array($wechat) ? (string)($wechat['app_id'] ?? '') : '';
if ($token === '' || $aesKey === '' || $appId === '') { if ($token === '' || $aesKey === '' || $appId === '') {
throw new \Exception('未配置虚拟支付消息加解密参数'); throw new \Exception('未配置虚拟支付消息加解密参数');
} }
@@ -378,6 +380,17 @@ class VirtualPayService
return (string)$sessionKey; return (string)$sessionKey;
} }
/**
* ThinkPHP 5 的 config() 只按第一个点拆成两级,三级键会读成空
* @return array
*/
private static function payConfig()
{
$config = config('common.virtual_pay');
return is_array($config) ? $config : [];
}
/** /**
* OfferID 为空时记下 .env 有没有被读到,不记录密钥内容 * OfferID 为空时记下 .env 有没有被读到,不记录密钥内容
*/ */
@@ -431,7 +444,7 @@ class VirtualPayService
if ($fen < 1) { if ($fen < 1) {
throw new \Exception('支付金额不能小于0.01元'); throw new \Exception('支付金额不能小于0.01元');
} }
$coinPerYuan = (int)config('common.virtual_pay.coin_per_yuan'); $coinPerYuan = (int)(self::payConfig()['coin_per_yuan'] ?? 0);
if ($coinPerYuan < 1) { if ($coinPerYuan < 1) {
$coinPerYuan = 1; $coinPerYuan = 1;
} }
@@ -456,7 +469,7 @@ class VirtualPayService
*/ */
private static function env() private static function env()
{ {
return (int)config('common.virtual_pay.env') === 1 ? 1 : 0; return (int)(self::payConfig()['env'] ?? 0) === 1 ? 1 : 0;
} }
/** /**
@@ -465,7 +478,7 @@ class VirtualPayService
*/ */
private static function appKey() private static function appKey()
{ {
$config = config('common.virtual_pay'); $config = self::payConfig();
$key = self::env() === 1 ? (string)($config['sandbox_app_key'] ?? '') : (string)($config['app_key'] ?? ''); $key = self::env() === 1 ? (string)($config['sandbox_app_key'] ?? '') : (string)($config['app_key'] ?? '');
if ($key === '') { if ($key === '') {
throw new \Exception('未配置虚拟支付 AppKey'); throw new \Exception('未配置虚拟支付 AppKey');