fix: 发货时使用正确的商户配置

- WechatService 添加 IWechatPayConfigService 依赖
- GetMerchantConfigByOrderNoAsync 优先使用 WechatPayConfigService 获取商户配置
- 修复发货时使用错误商户导致 access_token 获取失败的问题
This commit is contained in:
zpc 2026-02-10 15:41:54 +08:00
parent 27613ab5b2
commit d9dc8e4a6e
2 changed files with 19 additions and 4 deletions

View File

@ -20,6 +20,7 @@ public class WechatService : IWechatService
private readonly WechatPaySettings _wechatPaySettings; private readonly WechatPaySettings _wechatPaySettings;
private readonly IRedisService _redisService; private readonly IRedisService _redisService;
private readonly HoneyBoxDbContext _dbContext; private readonly HoneyBoxDbContext _dbContext;
private readonly IWechatPayConfigService? _wechatPayConfigService;
// 微信API端点 // 微信API端点
private const string WechatCodeToSessionUrl = "https://api.weixin.qq.com/sns/jscode2session"; private const string WechatCodeToSessionUrl = "https://api.weixin.qq.com/sns/jscode2session";
@ -35,13 +36,15 @@ public class WechatService : IWechatService
ILogger<WechatService> logger, ILogger<WechatService> logger,
IOptions<WechatPaySettings> wechatPaySettings, IOptions<WechatPaySettings> wechatPaySettings,
IRedisService redisService, IRedisService redisService,
HoneyBoxDbContext dbContext) HoneyBoxDbContext dbContext,
IWechatPayConfigService? wechatPayConfigService = null)
{ {
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); _logger = logger ?? throw new ArgumentNullException(nameof(logger));
_wechatPaySettings = wechatPaySettings?.Value ?? throw new ArgumentNullException(nameof(wechatPaySettings)); _wechatPaySettings = wechatPaySettings?.Value ?? throw new ArgumentNullException(nameof(wechatPaySettings));
_redisService = redisService ?? throw new ArgumentNullException(nameof(redisService)); _redisService = redisService ?? throw new ArgumentNullException(nameof(redisService));
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
_wechatPayConfigService = wechatPayConfigService;
} }
/// <summary> /// <summary>
@ -1110,8 +1113,19 @@ public class WechatService : IWechatService
/// </summary> /// </summary>
private async Task<WechatPayMerchantConfig?> GetMerchantConfigByOrderNoAsync(string orderNo) private async Task<WechatPayMerchantConfig?> GetMerchantConfigByOrderNoAsync(string orderNo)
{ {
// 直接使用现有的 GetMerchantConfigAsync 方法获取默认商户配置 // 优先使用 WechatPayConfigService支持多商户配置
// 该方法已经从数据库读取配置并支持缓存 if (_wechatPayConfigService != null)
{
var config = _wechatPayConfigService.GetMerchantByOrderNo(orderNo);
if (!string.IsNullOrEmpty(config.MchId))
{
_logger.LogInformation("[发货] 使用 WechatPayConfigService 获取商户配置: MchId={MchId}, AppId={AppId}",
config.MchId, config.AppId);
return config;
}
}
// 回退到旧的配置读取方式
return await GetMerchantConfigAsync(); return await GetMerchantConfigAsync();
} }

View File

@ -29,7 +29,8 @@ public class ServiceModule : Module
var wechatPaySettings = c.Resolve<Microsoft.Extensions.Options.IOptions<WechatPaySettings>>(); var wechatPaySettings = c.Resolve<Microsoft.Extensions.Options.IOptions<WechatPaySettings>>();
var redisService = c.Resolve<IRedisService>(); var redisService = c.Resolve<IRedisService>();
var dbContext = c.Resolve<HoneyBoxDbContext>(); var dbContext = c.Resolve<HoneyBoxDbContext>();
return new WechatService(httpClientFactory.CreateClient(), logger, wechatPaySettings, redisService, dbContext); var configService = c.ResolveOptional<IWechatPayConfigService>();
return new WechatService(httpClientFactory.CreateClient(), logger, wechatPaySettings, redisService, dbContext, configService);
}).As<IWechatService>().InstancePerLifetimeScope(); }).As<IWechatService>().InstancePerLifetimeScope();
// 注册 IP 地理位置服务 // 注册 IP 地理位置服务