This commit is contained in:
gpu 2026-01-24 14:30:45 +08:00
parent 9867b87594
commit 6f92062305
2 changed files with 86 additions and 12 deletions

View File

@ -4,15 +4,15 @@
"Redis": "192.168.195.15:6379,abortConnect=false,connectTimeout=5000"
},
"WechatSettings": {
"AppId": "wx595ec949c6efd72b",
"AppSecret": "1677345a20450146cf4610a41b49794d"
"AppId": "",
"AppSecret": ""
},
"WechatPaySettings": {
"DefaultMerchant": {
"Name": "默认商户",
"MchId": "1680394019",
"AppId": "wx595ec949c6efd72b",
"Key": "bad162066cbc9c34bb457e6997b7255b",
"MchId": "",
"AppId": "",
"Key": "",
"OrderPrefix": "MYH",
"Weight": 1,
"NotifyUrl": ""

View File

@ -4,6 +4,7 @@ using HoneyBox.Model.Data;
using HoneyBox.Model.Entities;
using HoneyBox.Model.Models.Auth;
using HoneyBox.Model.Models.Payment;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@ -423,8 +424,8 @@ public class WechatService : IWechatService
};
}
// 获取商户配置
var merchantConfig = GetMerchantConfig();
// 获取商户配置(优先从数据库读取)
var merchantConfig = await GetMerchantConfigAsync();
if (merchantConfig == null)
{
_logger.LogError("[创建支付订单] 未找到商户配置");
@ -455,7 +456,7 @@ public class WechatService : IWechatService
// 构建统一下单参数
var unifiedOrderParams = new SortedDictionary<string, string>
{
{ "appid", _wechatSettings.AppId },
{ "appid", merchantConfig.AppId },
{ "mch_id", merchantConfig.MchId },
{ "nonce_str", nonceStr },
{ "body", title },
@ -528,7 +529,7 @@ public class WechatService : IWechatService
var payParams = new SortedDictionary<string, string>
{
{ "appId", _wechatSettings.AppId },
{ "appId", merchantConfig.AppId },
{ "timeStamp", timeStamp },
{ "nonceStr", payNonceStr },
{ "package", $"prepay_id={prepayId}" },
@ -545,7 +546,7 @@ public class WechatService : IWechatService
OrderNo = orderNo,
Res = new NativePayParams
{
AppId = _wechatSettings.AppId,
AppId = merchantConfig.AppId,
TimeStamp = timeStamp,
NonceStr = payNonceStr,
Package = $"prepay_id={prepayId}",
@ -590,11 +591,84 @@ public class WechatService : IWechatService
}
/// <summary>
/// 获取商户配置
/// 获取商户配置(优先从数据库读取)
/// </summary>
private async Task<WechatPayMerchantConfig?> GetMerchantConfigAsync()
{
try
{
// 1. 尝试从数据库读取 weixinpay 配置
var weixinpayConfig = await _dbContext.Configs
.Where(c => c.ConfigKey == "weixinpay")
.Select(c => c.ConfigValue)
.FirstOrDefaultAsync();
if (!string.IsNullOrEmpty(weixinpayConfig))
{
var config = JsonSerializer.Deserialize<JsonElement>(weixinpayConfig);
var mchId = config.TryGetProperty("mch_id", out var mchIdProp) ? mchIdProp.GetString() : null;
var appId = config.TryGetProperty("appid", out var appIdProp) ? appIdProp.GetString() : null;
var key = config.TryGetProperty("keys", out var keysProp) ? keysProp.GetString() : null;
if (!string.IsNullOrEmpty(mchId) && !string.IsNullOrEmpty(key))
{
// 如果 weixinpay 中没有 appid尝试从 wechat_setting 获取
if (string.IsNullOrEmpty(appId))
{
var wechatSettingConfig = await _dbContext.Configs
.Where(c => c.ConfigKey == "wechat_setting")
.Select(c => c.ConfigValue)
.FirstOrDefaultAsync();
if (!string.IsNullOrEmpty(wechatSettingConfig))
{
var wechatSetting = JsonSerializer.Deserialize<JsonElement>(wechatSettingConfig);
appId = wechatSetting.TryGetProperty("appid", out var wechatAppIdProp) ? wechatAppIdProp.GetString() : null;
}
}
// 如果还是没有 appid使用 appsettings.json 中的配置
if (string.IsNullOrEmpty(appId))
{
appId = _wechatSettings.AppId;
}
_logger.LogInformation("[微信支付] 从数据库读取配置: MchId={MchId}, AppId={AppId}", mchId, appId);
return new WechatPayMerchantConfig
{
Name = "数据库配置",
MchId = mchId,
AppId = appId ?? string.Empty,
Key = key,
OrderPrefix = "MYH",
Weight = 1
};
}
}
_logger.LogWarning("[微信支付] 数据库中未找到有效的微信支付配置,使用 appsettings.json 配置");
}
catch (Exception ex)
{
_logger.LogWarning(ex, "[微信支付] 从数据库读取配置失败,使用 appsettings.json 配置");
}
// 2. 回退到 appsettings.json 配置
if (!string.IsNullOrEmpty(_wechatPaySettings.DefaultMerchant?.MchId))
{
return _wechatPaySettings.DefaultMerchant;
}
return _wechatPaySettings.Merchants.FirstOrDefault();
}
/// <summary>
/// 获取商户配置(同步版本,用于兼容)
/// </summary>
private WechatPayMerchantConfig? GetMerchantConfig()
{
// 优先使用默认商户配置
// 优先使用 appsettings.json 中的默认商户配置
if (!string.IsNullOrEmpty(_wechatPaySettings.DefaultMerchant?.MchId))
{
return _wechatPaySettings.DefaultMerchant;