Tai.Wallet.Recharge.EntityFrameworkCore 0.2.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tai.Wallet.Recharge.EntityFrameworkCore --version 0.2.2
                    
NuGet\Install-Package Tai.Wallet.Recharge.EntityFrameworkCore -Version 0.2.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tai.Wallet.Recharge.EntityFrameworkCore" Version="0.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tai.Wallet.Recharge.EntityFrameworkCore" Version="0.2.2" />
                    
Directory.Packages.props
<PackageReference Include="Tai.Wallet.Recharge.EntityFrameworkCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tai.Wallet.Recharge.EntityFrameworkCore --version 0.2.2
                    
#r "nuget: Tai.Wallet.Recharge.EntityFrameworkCore, 0.2.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Tai.Wallet.Recharge.EntityFrameworkCore@0.2.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tai.Wallet.Recharge.EntityFrameworkCore&version=0.2.2
                    
Install as a Cake Addin
#tool nuget:?package=Tai.Wallet.Recharge.EntityFrameworkCore&version=0.2.2
                    
Install as a Cake Tool

Tai.Wallet.Recharge

可选的钱包充值模块。它组合 Tai.Wallet 和 Tai.Payment:服务端为当前用户生成支付请求,支付成功并确认属于这笔充值后, 给该用户的钱包记一笔 Recharge 本金流水并增加余额。充值可以按规则自动赠送(如"充 100 送 20"),也可以通过扩展点叠加优惠券, 赠送部分另记一笔赠金流水。

Tai.Wallet 核心不引用 Tai.Payment,也不知道本模块存在。只做内部记账或提现的宿主不需要安装本模块和支付模块。 本模块只依赖支付的 Domain 层与网关抽象,不引用微信、支付宝或 Stripe Provider;网关密钥、回调地址和启用的网关仍由宿主按 Tai.Payment 的方式配置,本模块不保存任何凭据。

不包含充值退款、余额付款或前端页面。优惠券的具体实现不在本模块,由 Tai.Wallet.Voucher.Recharge 或宿主提供。

自动发布使用模块版本标签和 NuGet Trusted Publishing,配置与操作见发布指南。

构建与验证

需要 .NET 10 SDK;ABP 使用 10.7.0,Microsoft 配套包与 dotnet-ef 使用 10.0.12。在本目录执行:

dotnet tool restore
bash scripts/verify.sh
dotnet run --project samples/Tai.Wallet.Recharge.Sample -- --migrate
dotnet run --project samples/Tai.Wallet.Recharge.Sample --urls http://localhost:5300

verify.sh 执行还原、Release 构建、测试、迁移一致性检查、临时 SQLite 迁移,并把 src 下的项目逐个打包到 artifacts/packages。SQLite 示例迁移不能代替生产数据库验证。

模块结构

包 内容
Tai.Wallet.Recharge.Domain.Shared 充值状态、赠送类型、常量、错误码与中英文本地化
Tai.Wallet.Recharge.Domain WalletRecharge、WalletRechargeOffer 聚合,WalletRechargeManager,IWalletRechargeCouponProvider 扩展点,支付完成事件处理器与补账后台作业
Tai.Wallet.Recharge.Application.Contracts / Application IWalletRechargeAppService、IWalletRechargeOfferAppService、DTO 和权限
Tai.Wallet.Recharge.EntityFrameworkCore IWalletRechargeDbContext、模型映射与默认仓储
Tai.Wallet.Recharge.HttpApi api/wallet/recharges、api/wallet/recharge-offers 控制器

依赖:Tai.Wallet.Domain 0.2.0、Tai.Payment.Domain 0.1.1 和 Volo.Abp.BackgroundJobs.Abstractions。

从 0.1.x 升级

0.2.0 需要 Tai.Wallet 0.2.0(本金与赠金拆分),宿主要同时升级钱包包并生成迁移:钱包的新列,以及本模块的 TaiWalletRechargeOffers 表和 TaiWalletRecharges 的赠送、优惠券列。已有充值的赠送字段为 0,没有配置赠送规则时行为与 0.1.x 一致。 WalletRechargeManager 的构造函数增加了规则仓储和优惠券实现,继承它的宿主需要同步调整。

0.2.1 把 RemoteService 名从 TaiWalletRecharge 改为 WalletRecharge(WalletRechargeRemoteServiceConsts.RemoteServiceName), 路由不变。用 ABP 生成前端或客户端代理的宿主需要重新生成,RemoteServices 配置键同步改名。没有模型变化。

0.2.2 为 IWalletRechargeDbContext.WalletRechargeOffers 提供默认实现,修复旧宿主升级后因未声明该属性而出现的 CS0535 编译错误。已显式实现该属性的宿主可以保持原样。本次没有模型变化;从 0.1.x 升级仍需完成上述数据库迁移。

宿主接入

宿主模块按需依赖:

[DependsOn(
    typeof(TaiWalletRechargeApplicationModule),
    typeof(TaiWalletRechargeHttpApiModule),
    typeof(TaiWalletRechargeEntityFrameworkCoreModule),
    typeof(TaiWalletEntityFrameworkCoreModule),
    typeof(PaymentHttpApiModule),                    // 网关回调入口
    typeof(PaymentEntityFrameworkCoreModule),
    typeof(PaymentWeChatModule),                     // 按需选择 Provider
    typeof(AbpBackgroundJobsEntityFrameworkCoreModule) // 或宿主已有的后台作业实现
)]

宿主 DbContext 实现 IWalletDbContext、IPaymentDbContext、IWalletRechargeDbContext(以及持久化后台作业时的 IBackgroundJobsDbContext)。充值相关属性可按示例显式声明:

public DbSet<WalletRecharge> WalletRecharges => Set<WalletRecharge>();
public DbSet<WalletRechargeOffer> WalletRechargeOffers => Set<WalletRechargeOffer>();

从 0.2.2 起,第二个属性也可使用接口默认实现,通过 IWalletRechargeDbContext 访问;若要直接通过宿主 DbContext 访问该属性,仍需显式声明。无论是否声明属性,都必须在 OnModelCreating 调用 ConfigureWalletRecharge() 注册实体映射。 仅调用模型配置不能满足 0.2.0、0.2.1 的接口要求,使用这两个版本的宿主需补齐第二个属性。

在 OnModelCreating 调用 ConfigureWallet()、ConfigurePayment()、ConfigureWalletRecharge()、ConfigureBackgroundJobs(), 再用 ReplaceDbContext 接入:

context.Services.AddAbpDbContext<HostDbContext>(o =>
{
    o.ReplaceDbContext<IWalletDbContext>();
    o.ReplaceDbContext<IPaymentDbContext>();
    o.ReplaceDbContext<IWalletRechargeDbContext>();
    o.ReplaceDbContext<IBackgroundJobsDbContext>();
});

默认表为 TaiWalletRecharges、TaiWalletRechargeOffers,通过 WalletRechargeDbProperties 修改前缀和 Schema。充值记录与钱包流水在同一事务中提交, 因此连接字符串名沿用钱包的 TaiWallet,两者必须位于同一个数据库。示例提供 SQLite 迁移,生产宿主针对自己的 DbContext 生成迁移。

配置 WalletRechargeOptions(示例绑定 WalletRecharge 节):

选项 默认值 说明
Currency CNY 充值币种,必须是钱包的记账币种,且为两位小数的 Tai.Payment 币种
MinAmount / MaxAmount 1 / 100000000 单笔充值金额范围,单位为最小货币单位
ProductName Wallet recharge 服务端生成的支付商品名称

宿主还需要:

  • 使用 UTC 时钟(AbpClockOptions.Kind = DateTimeKind.Utc),与 Tai.Payment 一致。
  • 安装 ABP 后台作业实现。补账作业在支付完成事件中入队;建议使用与支付同库的持久化存储,使作业记录与支付完成一起提交。 未安装实现时只记录警告,需由用户调用刷新或宿主调用 WalletRechargeManager.SettleAsync 补账。
  • 若分布式事件总线使用真实消息代理,配置 ABP Outbox,避免支付已提交但事件丢失。
  • 多实例部署时配置共享的 ABP 分布式锁。

HTTP 接口与权限

RemoteService 名为 WalletRecharge,路由前缀为 api/wallet。

请求 权限 说明
POST /api/wallet/recharges/quote Wallet.Recharges.Create 请求体 { amount, couponCode? },返回将获得的赠送,无副作用
POST /api/wallet/recharges Wallet.Recharges.Create 为自己创建充值,请求体为 requestId、amount 和可选的 couponCode
GET /api/wallet/recharges/{id} Wallet.Recharges 查询自己的充值状态
POST /api/wallet/recharges/{id}/checkout Wallet.Recharges.Create 用 { "gateway": "WeChat" } 发起支付
POST /api/wallet/recharges/{id}/refresh Wallet.Recharges 向网关查单,已支付则入账
GET /api/wallet/recharge-offers/available Wallet.Recharges.Create 当前用户可用的赠送规则
GET /api/wallet/recharge-offers、GET /api/wallet/recharge-offers/{id} Wallet.Recharges.Offers 管理端查看规则,可按 isEnabled 过滤
POST /api/wallet/recharge-offers、PUT /api/wallet/recharge-offers/{id} Wallet.Recharges.Offers.Manage 创建、修改规则
POST /api/wallet/recharge-offers/{id}/enable、/disable Wallet.Recharges.Offers.Manage 启用、停用;规则不能删除

用户与租户取自认证上下文,客户端提交的其他字段会被忽略。他人的充值一律返回 404,管理员权限也不会读写他人的充值。 amount 为最小货币单位的整数,例如 1000 表示 10.00 CNY。不要把 Tai.Payment 的支付建单接口开放给付款人; 本模块在服务端生成支付请求,商品代码固定为 tai-wallet-recharge,幂等键为 tai-wallet-recharge:{充值ID}, 数量 1,单价等于钱包金额除以 100。

状态:WaitingForPayment、PaymentFailed、Paid(已支付、待入账)、Credited。 充值 DTO 的 amount 始终是实付金额(本金),bonusAmount = offerBonus + couponBonus 为另记的赠金。

Wallet.Recharges.Offers 是独立的权限树,不包含查看他人充值的权限。

充值赠送与优惠券

所有优惠都是"实付记本金,另外赠送记赠金",不修改支付金额。"实付 90 到账 100"应配置为"满 90 送 10"。 赠金不可提现,扣款和提现作废规则见 Tai.Wallet README。

赠送规则 WalletRechargeOffer:

字段 规则
MinAmount 实付门槛,≥ 1,最小货币单位
BonusType / BonusValue Fixed 时为固定金额;Percentage 时为基点(1~10000),赠送 floor(amount × value / 10000)
MaxBonus 仅 Percentage 可设,赠送封顶
StartTime / EndTime 可空,UTC,左闭右开
PerUserLimit 每人可用次数,空表示不限
  • 创建充值时,从启用、在有效期内、达到门槛且本人未用完的规则中选赠送最多的一条;相同时选门槛高的,再相同时选创建早的。
  • 规则在创建充值时锁定,之后修改或停用规则不影响已创建的充值。每人次数统计所有引用该规则的充值,不论是否支付, 因为用户可以继续支付那笔充值。创建充值按用户加分布式锁,并发创建不会超过每人上限。

优惠券通过 IWalletRechargeCouponProvider 接入:

public interface IWalletRechargeCouponProvider
{
    Task<WalletRechargeCouponQuote> QuoteAsync(WalletRechargeCouponContext context);     // 无副作用,无效时抛业务异常
    Task<string> ReserveAsync(WalletRechargeCouponContext context, Guid rechargeId);    // 在创建充值的事务中占用
    Task ConfirmAsync(string reference, Guid rechargeId);                              // 在入账事务中确认,必须幂等
}

默认实现 NullWalletRechargeCouponProvider 对任何券码返回 Tai.Wallet.Recharge:CouponNotSupported。 安装 Tai.Wallet.Voucher.Recharge 或在宿主中用 [Dependency(ReplaceServices = true)] 替换即可启用。

  • 规则与券默认互斥,取赠送较大的一项,相同时用券;券报价 StackableWithOffer = true 时两者叠加。 未被采用的券不会占用,报价接口用 coupon.applied = false 和 reason 说明原因。
  • 券在创建充值时与这笔充值绑定,本模块不释放占用:Tai.Payment 的 Failed 不是终态,释放后再支付成功会超发。
  • 券码只以规范化后的 SHA-256 保存在 CouponKey 中用于幂等比对,不保存明文,也不写入审计日志。

幂等、事务与补账

  • 同一用户重复提交同一 requestId 返回原充值,不重新计算优惠;金额或券码不同返回 Tai.Wallet:RequestConflict。 requestId 按租户和用户隔离。
  • 本金流水 ID 等于充值 ID,赠金流水 ID 为创建充值时生成的 BonusTransactionId,沿用 BalanceManager 的幂等, 重复回调、刷新、作业重试最多各产生一笔。本金、赠金流水、券确认和入账标记在同一事务中提交。
  • 入账前校验支付请求 ID、租户、用户、币种、精确金额和唯一的充值商品;任一不一致返回 Tai.Wallet.Recharge:PaymentMismatch,不入账。支付未完成不入账。
  • 支付完成与钱包入账是两次独立提交。支付完成事件只登记补账作业;入账失败时作业按 ABP 策略重试, 用户刷新或宿主调用 SettleAsync 也会补上同一笔入账,不需要重新付款。
  • 钱包被禁用时拒绝创建和发起支付;若付款后钱包才被禁用,入账失败,重新启用后补账。
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 121 11/19/2026
1.1.0 189 11/1/2026
1.0.0 137 10/17/2026
0.2.2 112 10/17/2026
0.2.1 118 10/11/2026
0.2.0 110 10/11/2026
0.1.0 114 10/10/2026