This commit is contained in:
zpc 2026-01-27 13:45:39 +08:00
parent 876ed8b43d
commit 77232bc303
5 changed files with 40 additions and 1 deletions

View File

@ -62,6 +62,11 @@ try
builder.Configuration.GetSection("AmapSettings").Bind(amapSettings);
builder.Services.AddSingleton(amapSettings);
// 配置应用程序设置(测试环境等)
var appSettings = new AppSettings();
builder.Configuration.GetSection("AppSettings").Bind(appSettings);
builder.Services.AddSingleton(appSettings);
// 配置微信支付设置
builder.Services.Configure<WechatPaySettings>(builder.Configuration.GetSection("WechatPaySettings"));

View File

@ -3,6 +3,9 @@
"DefaultConnection": "Server=192.168.195.15;uid=sa;pwd=Dbt@com@123;Database=honey_box;MultipleActiveResultSets=true;pooling=true;min pool size=5;max pool size=32767;connect timeout=20;Encrypt=True;TrustServerCertificate=True;",
"Redis": "192.168.195.15:6379,abortConnect=false,connectTimeout=5000"
},
"AppSettings": {
"IsTestEnvironment": true
},
"WechatPaySettings": {
"DefaultMerchant": {
"Name": "默认商户",

View File

@ -5,6 +5,7 @@ using System.Xml;
using HoneyBox.Core.Interfaces;
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;
@ -24,6 +25,7 @@ public class WechatPayService : IWechatPayService
private readonly IWechatService _wechatService;
private readonly IRedisService _redisService;
private readonly WechatPaySettings _settings;
private readonly AppSettings _appSettings;
private readonly Lazy<IWechatPayV3Service>? _v3ServiceLazy;
/// <summary>
@ -46,6 +48,11 @@ public class WechatPayService : IWechatPayService
/// </summary>
private static readonly TimeSpan FAILED_SHIPPING_EXPIRY = TimeSpan.FromDays(3);
/// <summary>
/// 测试用户支付金额(分)
/// </summary>
private const int TEST_USER_PAY_AMOUNT = 1; // 0.01元 = 1分
public WechatPayService(
HoneyBoxDbContext dbContext,
HttpClient httpClient,
@ -54,6 +61,7 @@ public class WechatPayService : IWechatPayService
IWechatService wechatService,
IRedisService redisService,
IOptions<WechatPaySettings> settings,
AppSettings appSettings,
Lazy<IWechatPayV3Service>? v3ServiceLazy = null)
{
_dbContext = dbContext;
@ -63,6 +71,7 @@ public class WechatPayService : IWechatPayService
_wechatService = wechatService;
_redisService = redisService;
_settings = settings.Value;
_appSettings = appSettings;
_v3ServiceLazy = v3ServiceLazy;
}
@ -132,6 +141,14 @@ public class WechatPayService : IWechatPayService
var body = TruncateBody(request.Body, 30);
var totalFee = (int)Math.Round(request.Amount * 100); // 转换为分
// 测试环境下IsTest=2 的用户支付金额改为 0.01 元
if (_appSettings.IsTestEnvironment && user.IsTest == 2)
{
_logger.LogInformation("测试用户支付金额调整: UserId={UserId}, 原金额={OriginalAmount}分, 调整为={TestAmount}分",
request.UserId, totalFee, TEST_USER_PAY_AMOUNT);
totalFee = TEST_USER_PAY_AMOUNT;
}
var unifiedOrderParams = new Dictionary<string, string>
{
{ "appid", appId },

View File

@ -264,9 +264,10 @@ public class ServiceModule : Module
var wechatService = c.Resolve<IWechatService>();
var redisService = c.Resolve<IRedisService>();
var settings = c.Resolve<Microsoft.Extensions.Options.IOptions<WechatPaySettings>>();
var appSettings = c.Resolve<AppSettings>();
// Autofac 原生支持 Lazy<T>,直接解析即可
var v3ServiceLazy = c.Resolve<Lazy<IWechatPayV3Service>>();
return new WechatPayService(dbContext, httpClientFactory.CreateClient(), logger, configService, wechatService, redisService, settings, v3ServiceLazy);
return new WechatPayService(dbContext, httpClientFactory.CreateClient(), logger, configService, wechatService, redisService, settings, appSettings, v3ServiceLazy);
}).As<IWechatPayService>().InstancePerLifetimeScope();
// 注册支付服务

View File

@ -0,0 +1,13 @@
namespace HoneyBox.Model.Models.Auth;
/// <summary>
/// 应用程序配置
/// </summary>
public class AppSettings
{
/// <summary>
/// 是否测试环境
/// 测试环境下IsTest=2 的用户支付金额会改为 0.01 元
/// </summary>
public bool IsTestEnvironment { get; set; } = false;
}