Shipeng.Payment 4.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Shipeng.Payment --version 4.1.0
                    
NuGet\Install-Package Shipeng.Payment -Version 4.1.0
                    
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="Shipeng.Payment" Version="4.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shipeng.Payment" Version="4.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Shipeng.Payment" />
                    
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 Shipeng.Payment --version 4.1.0
                    
#r "nuget: Shipeng.Payment, 4.1.0"
                    
#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 Shipeng.Payment@4.1.0
                    
#: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=Shipeng.Payment&version=4.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Shipeng.Payment&version=4.1.0
                    
Install as a Cake Tool

Shipeng.Payment

Shipeng.Payment 是支付能力集成包,当前重点封装微信支付 V3 的基础能力:下单、查单、关单、退款、退款查询、回调验签、回调解密、JSAPI/小程序调起支付参数构建,以及元和分之间的金额转换。

这个包只处理支付基础设施,不处理业务订单、库存、权益、优惠券、分账、发票、会员积分等业务逻辑。业务系统应先创建本地支付单或退款单,再调用本包请求微信支付;收到微信回调后,本包只负责验签和解密,业务状态变更由业务模块自己完成。

安装

dotnet add package Shipeng.Payment --version 4.1.0

适用场景

  • 小程序、公众号、后台管理系统、商城、服务平台需要接入微信支付 V3。
  • 业务金额以 decimal 元保存,但微信支付接口要求传 int 分。
  • 需要统一封装微信支付签名、证书、APIv3 回调解密和验签。
  • 希望支付能力从核心工具包中独立出来,只有需要支付的项目才引用支付包。

主要能力

  • IWeChatPayService.CreatePayAsync:创建微信支付订单,支持 JSAPI/小程序支付和 Native 扫码支付。
  • IWeChatPayService.QueryByOutTradeNoAsync:根据商户订单号查询支付状态。
  • IWeChatPayService.QueryByTransactionIdAsync:根据微信支付订单号查询支付状态。
  • IWeChatPayService.CloseAsync:关闭未支付订单。
  • IWeChatPayService.RefundAsync:申请退款。
  • IWeChatPayService.QueryRefundByOutRefundNoAsync:根据商户退款单号查询退款状态。
  • IWeChatPayService.ParsePayNotify:解析支付成功回调,只负责验签、解密、反序列化。
  • IWeChatPayService.ParseRefundNotify:解析退款回调,只负责验签、解密、反序列化。
  • IWeChatPayService.VerifyNotifySignature:验证微信支付/退款回调签名。
  • IWeChatPayService.DecryptNotifyResource:使用 APIv3 密钥解密回调 resource。
  • IWeChatPayService.BuildNotifySuccess:构建微信回调成功响应。
  • IWeChatPayService.BuildNotifyFail:构建微信回调失败响应。
  • WeChatPayAmountHelper.ConvertYuanToFen:将业务金额“元”转换为微信支付需要的“分”。
  • WeChatPayAmountHelper.ConvertFenToYuan:将微信支付返回的“分”转换为业务展示常用的“元”。

金额转换

微信支付金额单位是分,业务系统通常使用元。不要在业务代码里到处写 (int)(amount * 100),这样会带来四舍五入规则不一致和精度边界问题。

var totalFee = WeChatPayAmountHelper.ConvertYuanToFen(12.345m); // 1235
var refundFee = WeChatPayAmountHelper.ConvertYuanToFen(3.10m);  // 310
var yuan = WeChatPayAmountHelper.ConvertFenToYuan(1235);        // 12.35

WeChatPayAmountHelper.EnsurePositiveFen(totalFee, nameof(totalFee));

ConvertYuanToFen 的规则:

  • 参数单位是元,类型是 decimal。
  • 金额不能小于 0。
  • 使用 MidpointRounding.AwayFromZero 四舍五入到分。
  • 转换后的分值不能超过 int.MaxValue。
  • 支付和退款请求仍需要大于 0,业务可用 EnsurePositiveFen 做显式校验。

创建支付订单

var request = new CreateWeChatPayRequestModel
{
    outTradeNo = localPayOrder.PayNo,
    description = "农产品商城订单",
    totalFee = WeChatPayAmountHelper.ConvertYuanToFen(localPayOrder.PayAmount),
    openId = userOpenId,
    tradeType = WeChatPayTradeTypeEnum.JsApi
};

CreateWeChatPayDto result = await weChatPayService.CreatePayAsync(request);

// JSAPI/小程序支付:把 result.jsApiPay 返回给前端调起支付。
// Native 扫码支付:把 result.codeUrl 生成二维码给用户扫码。

参数说明:

  • outTradeNo:商户系统内部支付单号,同一个商户号下必须唯一。建议使用本地支付单号,不要直接使用业务订单号。
  • description:商品或订单标题,会展示在微信支付侧。
  • totalFee:支付金额,单位分,必须大于 0。
  • openId:JSAPI/小程序支付必传;Native 扫码支付不需要。
  • tradeType:支付类型,JsApi 用于小程序/公众号,Native 用于扫码支付。

申请退款

var refundRequest = new CreateWeChatRefundRequestModel
{
    outTradeNo = localPayOrder.PayNo,
    outRefundNo = localRefundOrder.RefundNo,
    reason = "用户取消订单",
    refundFee = WeChatPayAmountHelper.ConvertYuanToFen(localRefundOrder.RefundAmount),
    totalFee = WeChatPayAmountHelper.ConvertYuanToFen(localPayOrder.PayAmount),
    notifyUrl = "https://example.com/api/payment/wechat/refund-notify"
};

string refundJson = await weChatPayService.RefundAsync(refundRequest);

退款参数说明:

  • outTradeNo:创建支付订单时传给微信的商户订单号。
  • transactionId:微信支付订单号;如果同时传 transactionId 和 outTradeNo,微信优先使用 transactionId。
  • outRefundNo:商户退款单号,必须唯一。
  • refundFee:本次退款金额,单位分,必须大于 0。
  • totalFee:原支付订单总金额,单位分,不是剩余可退金额。
  • notifyUrl:退款结果回调地址,可为空;为空时建议主动查退款状态。

回调处理边界

本包的回调方法只做三件事:

  • 验证微信回调签名。
  • 使用 APIv3 密钥解密 resource。
  • 反序列化为支付或退款结果 DTO。

业务模块还必须自己完成:

  • 根据 out_trade_no 或 transaction_id 查询本地支付单。
  • 校验支付单金额、状态、商户号、应用号是否匹配。
  • 使用事务或幂等锁更新本地订单、支付单、库存、权益等业务状态。
  • 处理重复回调,微信回调可能多次到达。
  • 回调处理成功后返回 BuildNotifySuccess();处理失败时返回 BuildNotifyFail(message) 让微信重试。

配置项

WeChatPayOptions 需要至少配置:

  • appId:小程序或公众号 AppId。
  • mchId:微信支付商户号。
  • serialNo:商户 API 证书序列号。
  • privateKeyPath:商户 API 私钥文件路径,支持绝对路径或相对路径。
  • apiV3Key:APIv3 密钥,必须是 32 字节。
  • notifyUrl:支付结果回调地址。
  • platformCertificatePath:微信支付平台证书路径;配置后可验证回调签名。

依赖边界

该包只引用当前模块需要的 NuGet 依赖:

  • Microsoft.Extensions.Options

发布检查

发布前建议执行:

dotnet restore
dotnet build -c Release

确认生成的 Shipeng.Payment.4.1.0.nupkg 中包含 README,且业务项目只在需要支付能力时引用本包。

API Reference / API 索引

完整公开类型、方法、属性签名请查看 API_REFERENCE.md。该文件从源码 public 声明生成,用于补充 README 中的场景说明和示例。

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
4.1.1 180 6/30/2026
4.1.0 153 6/30/2026

4.1.0: Split the original monolithic foundation library into focused Shipeng.* NuGet packages. Consumers can reference authentication, MongoDB, SqlSugar, EF Core, database index, third-party integration, logging, mapping, payment, and hybrid paging capabilities on demand.