Galosys.Foundation.Payment
成熟度: 🟢 稳定 — 统一支付抽象 + 插件路由,含完整测试覆盖
统一支付抽象模块,通过 IPaymentDispatcher + IPaymentChannel 插件路由实现多支付通道统一管理。代码只需依赖 IPaymentDispatcher,无需关心底层支付通道实现。
功能特性
- 插件路由 —
PluginRegistry<IPaymentChannel, PaymentRequest> 自动按 Channel 字符串分发
- 统一 API —
IPaymentDispatcher 提供创建/查询/退款/回调/关单 5 个操作
- 可扩展 — 实现
IPaymentChannel 接口即可接入新支付通道
- 内置 Mock —
MockPaymentChannel 用于开发和测试环境
- 完整 DTO — 6 种支付场景(PagePay/FaceToFace/JsApi/H5/Native/App)、5 种订单状态
安装
<PackageReference Include="Galosys.Foundation.Payment" Version="x.x.x" />
快速开始
1. 注册服务
// 方式一:模块自动发现(推荐)
await Host.CreateDefaultBuilder()
.UseModularization()
.Locate()
.RunAsync();
// 方式二:手动注册
services.AddPayment();
services.AddTransient<IPaymentChannel, AlipayChannel>();
services.AddTransient<IPaymentChannel, WeChatPayChannel>();
2. 创建支付
using Galosys.Foundation.Payment;
public class OrderService
{
private readonly IPaymentDispatcher _payment;
public OrderService(IPaymentDispatcher payment) => _payment = payment;
// PC 网页支付
public async Task<PaymentCreateResult> CreatePagePayAsync()
{
return await _payment.PayAsync(new PaymentCreateRequest
{
Channel = "alipay", // 路由到 AlipayChannel
OutTradeNo = "ORD001",
Amount = 99.9m,
Subject = "测试订单",
PaymentType = PaymentType.PagePay,
NotifyUrl = "https://example.com/notify",
ReturnUrl = "https://example.com/return"
});
}
// 微信小程序支付
public async Task<PaymentCreateResult> CreateJsApiAsync()
{
return await _payment.PayAsync(new PaymentCreateRequest
{
Channel = "wechat",
OutTradeNo = "ORD002",
Amount = 199.0m,
Subject = "小程序订单",
PaymentType = PaymentType.JsApi,
OpenId = "用户的OpenId"
});
}
}
3. 查询/退款/回调/关单
// 查询订单状态
var result = await _payment.QueryAsync("alipay", "ORD001");
// 退款
var refund = await _payment.RefundAsync(new PaymentRefundRequest
{
Channel = "alipay",
OutTradeNo = "ORD001",
OutRefundNo = "REF001",
RefundAmount = 50.0m,
TotalAmount = 99.9m,
Reason = "部分退款"
});
// 处理支付回调
var notify = await _payment.HandleNotifyAsync("alipay", new PaymentNotifyRequest
{
RawBody = rawBody,
Headers = headers
});
// 关闭订单
await _payment.CloseAsync("alipay", "ORD001");
4. 扩展支付通道
using Galosys.Foundation.Payment;
public class AlipayChannel : IPaymentChannel
{
public string ChannelName => "alipay";
public bool Supports(PaymentRequest request)
=> string.Equals(request.Channel, ChannelName, StringComparison.OrdinalIgnoreCase);
public Task<PaymentCreateResult> CreateAsync(PaymentCreateRequest request)
{
// 调用支付宝 SDK
return Task.FromResult(new PaymentCreateResult("ORD001", payUrl: "https://..."));
}
public Task<PaymentQueryResult> QueryAsync(string outTradeNo) => throw new NotImplementedException();
public Task<PaymentRefundResult> RefundAsync(PaymentRefundRequest request) => throw new NotImplementedException();
public Task<PaymentNotifyResult> HandleNotifyAsync(PaymentNotifyRequest request) => throw new NotImplementedException();
public Task CloseAsync(string outTradeNo) => throw new NotImplementedException();
}
// 注册
services.AddTransient<IPaymentChannel, AlipayChannel>();
5. 开发/测试环境使用 Mock
services.AddPayment();
// MockPaymentChannel 已内置,无需额外注册,Channel 传 "mock" 即可
var result = await _payment.PayAsync(new PaymentCreateRequest
{
Channel = "mock",
OutTradeNo = "TEST001",
Amount = 1.0m,
Subject = "测试"
});
支付场景(PaymentType)
| 值 |
说明 |
PagePay |
PC 网页支付 |
FaceToFace |
当面付(扫码) |
JsApi |
微信公众号/小程序支付 |
H5 |
H5 手机浏览器支付 |
Native |
Native 扫码支付 |
App |
APP 支付 |
订单状态(PaymentStatus)
| 值 |
说明 |
Pending |
待支付 |
Paid |
已支付 |
Closed |
已关闭 |
Refunded |
已全额退款 |
RefundPartial |
部分退款 |
核心类
| 类名 |
说明 |
IPaymentDispatcher |
统一支付调度器接口(消费者使用) |
IPaymentChannel |
支付通道接口(通道实现者使用) |
PaymentDispatcher |
调度器实现,通过 PluginRegistry 自动路由 |
MockPaymentChannel |
内置 Mock 通道,用于开发/测试 |
PaymentCreateRequest |
创建订单请求 |
PaymentRefundRequest |
退款请求 |
PaymentNotifyRequest |
回调通知请求 |
PaymentCreateResult |
创建结果(PayUrl/QrCode/PrepayId) |
PaymentQueryResult |
查询结果(Status/Amount/PaidAt) |
PaymentNotifyResult |
回调处理结果 |
PaymentRefundResult |
退款结果 |
PaymentType |
支付场景枚举 |
PaymentStatus |
订单状态枚举 |
依赖
Galosys.Foundation.Core(PluginRegistry<TPlugin, TRequest>、IModule)