Sparkdo.Weixin.Payment
1.0.3
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Sparkdo.Weixin.Payment --version 1.0.3
NuGet\Install-Package Sparkdo.Weixin.Payment -Version 1.0.3
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="Sparkdo.Weixin.Payment" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sparkdo.Weixin.Payment" Version="1.0.3" />
<PackageReference Include="Sparkdo.Weixin.Payment" />
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 Sparkdo.Weixin.Payment --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Sparkdo.Weixin.Payment, 1.0.3"
#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 Sparkdo.Weixin.Payment@1.0.3
#: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=Sparkdo.Weixin.Payment&version=1.0.3
#tool nuget:?package=Sparkdo.Weixin.Payment&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
微信支付配置说明
本文档详细说明了微信支付模块的配置选项和使用方法。
快速开始
基本配置
var options = new WeixinPaymentOptions
{
AppId = "wx1234567890abcdef",
MchId = "1234567890",
APIv3Key = "your-api-v3-key-32-characters-long",
// 使用证书文件配置
Certificate = CertificateConfiguration.FromFile(
"apiclient_cert.p12",
"your-certificate-password"
),
// 使用平台证书验签(推荐)
SignatureVerify = "PlatformCertificate"
};
配置项详细说明
1. AppId(应用号)
- 说明:微信应用的唯一标识符
- 格式:以"wx"开头的18位字符串
- 示例:
wx1234567890abcdef - 获取方式:在微信开放平台或微信公众平台申请
2. MchId(商户号)
- 说明:微信支付商户的唯一标识符
- 格式:10位数字字符串
- 示例:
1234567890 - 获取方式:在微信支付商户平台申请
3. APIv3Key(API密钥)
- 说明:用于微信支付APIv3接口的加密和解密
- 格式:32个字符的字符串
- 示例:
your-api-v3-key-32-characters-long - 安全提示:请妥善保管,不要硬编码在代码中
证书配置
方式一:使用证书文件
Certificate = CertificateConfiguration.FromFile(
"apiclient_cert.p12",
"your-certificate-password"
);
方式二:使用Base64证书内容
Certificate = CertificateConfiguration.FromBase64String(
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC..."
);
方式三:使用字符串隐式转换
// 文件路径方式(用|分隔文件路径和密码)
Certificate = "apiclient_cert.p12|your-certificate-password";
// Base64内容方式
Certificate = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC...";
签名验证配置
方式一:平台证书验签(推荐)
SignatureVerify = "PlatformCertificate";
// 或者使用显式构造
SignatureVerify = new SignatureVerifyConfiguration
{
Method = SignatureVerifyMethod.PlatformCertificate
};
方式二:公钥文件验签
// 使用静态工厂方法
SignatureVerify = SignatureVerifyConfiguration.FromPublicKeyFile("certs/wechat_public_key.pem");
// 使用字符串格式
SignatureVerify = "PublicKey|certs/wechat_public_key.pem";
方式三:Base64公钥字符串验签
// 使用静态工厂方法
SignatureVerify = SignatureVerifyConfiguration.FromPublicKeyContent(
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA..."
);
// 使用字符串格式
SignatureVerify = "PublicKey|MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...";
方式四:不验签(仅测试环境)
SignatureVerify = "None";
⚠️ 警告:生产环境不建议使用 None 选项。
依赖注入配置
Program.cs 配置示例
// 在Program.cs中配置
builder.Services.Configure<WeixinPaymentOptions>(options =>
{
options.AppId = builder.Configuration["Weixin:Payment:AppId"];
options.MchId = builder.Configuration["Weixin:Payment:MchId"];
options.APIv3Key = builder.Configuration["Weixin:Payment:APIv3Key"];
options.Certificate = builder.Configuration["Weixin:Payment:Certificate"];
options.SignatureVerify = builder.Configuration["Weixin:Payment:SignatureVerify"];
});
appsettings.json 配置示例
{
"Weixin": {
"Payment": {
"AppId": "wx1234567890abcdef",
"MchId": "1234567890",
"APIv3Key": "your-api-v3-key-32-characters-long",
"Certificate": "apiclient_cert.p12|your-certificate-password",
"SignatureVerify": "PlatformCertificate"
}
}
}
不同验签方式的配置文件示例
{
"Weixin": {
"Payment": {
"AppId": "wx1234567890abcdef",
"MchId": "1234567890",
"APIv3Key": "your-api-v3-key-32-characters-long",
"Certificate": "apiclient_cert.p12|your-certificate-password",
// 平台证书验签(推荐)
"SignatureVerify": "PlatformCertificate"
// 或使用公钥文件验签
// "SignatureVerify": "PublicKey|certs/wechat_public_key.pem"
// 或使用Base64公钥字符串验签
// "SignatureVerify": "PublicKey|MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA..."
// 或不验签(仅测试环境)
// "SignatureVerify": "None"
}
}
}
环境变量配置
为了提高安全性,敏感信息建议使用环境变量:
# 设置环境变量
export Weixin__Payment__AppId="wx1234567890abcdef"
export Weixin__Payment__MchId="1234567890"
export Weixin__Payment__APIv3Key="your-api-v3-key-32-characters-long"
export Weixin__Payment__Certificate="apiclient_cert.p12|your-certificate-password"
export Weixin__Payment__SignatureVerify="PlatformCertificate"
配置验证
所有必需的配置项都使用了数据验证注解:
AppId:必须提供MchId:必须提供APIv3Key:必须提供
如果缺少必需的配置,系统会在运行时抛出验证异常。
最佳实践
安全性:
- 不要将敏感信息硬编码在代码中
- 使用环境变量或安全的配置管理系统
- 生产环境必须使用签名验证
证书管理:
- 定期更新商户证书
- 妥善保管证书文件和密码
- 考虑使用Key Vault等安全存储
验签配置:
- 生产环境推荐使用
PlatformCertificate - 如果使用公钥验签,确保公钥是最新的
- 测试环境可以暂时使用
None,但要记得在生产环境中开启
- 生产环境推荐使用
配置管理:
- 使用依赖注入进行配置管理
- 区分不同环境的配置
- 定期审查和更新配置
故障排除
常见错误
证书加载失败:
- 检查证书文件路径是否正确
- 确认证书密码是否正确
- 验证证书文件是否损坏
签名验证失败:
- 确认公钥文件格式正确
- 检查公钥内容是否是最新的
- 验证配置格式是否正确
配置验证失败:
- 检查必需字段是否都已配置
- 确认配置值格式是否正确
- 查看详细的验证错误信息
调试技巧
- 启用详细日志以查看配置加载过程
- 使用配置验证工具检查配置的正确性
- 在测试环境中先验证配置的有效性
更多信息
| Product | Versions 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.
-
net10.0
- JetBrains.Annotations (>= 2025.2.4)
- Microsoft.Extensions.Configuration (>= 10.0.8)
- Microsoft.Extensions.Configuration.CommandLine (>= 10.0.8)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 10.0.8)
- Microsoft.Extensions.Configuration.FileExtensions (>= 10.0.8)
- Microsoft.Extensions.Configuration.Json (>= 10.0.8)
- Microsoft.Extensions.Configuration.UserSecrets (>= 10.0.8)
- Microsoft.Extensions.DependencyInjection (>= 10.0.8)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.8)
- Microsoft.Extensions.DependencyModel (>= 10.0.8)
- Microsoft.Extensions.Diagnostics (>= 10.0.8)
- Microsoft.Extensions.FileProviders.Physical (>= 10.0.8)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.8)
- Microsoft.Extensions.Http (>= 10.0.8)
- Microsoft.Extensions.Localization (>= 10.0.8)
- Microsoft.Extensions.Logging (>= 10.0.8)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.8)
- Microsoft.Extensions.ObjectPool (>= 10.0.8)
- Microsoft.Extensions.Options (>= 10.0.8)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.8)
- Microsoft.Extensions.Options.DataAnnotations (>= 10.0.8)
- Microsoft.Extensions.Primitives (>= 10.0.8)
- Nito.AsyncEx.Context (>= 5.1.2)
- Portable.BouncyCastle (>= 1.9.0)
- Sparkdo.Mediation.Http (>= 1.0.3)
- System.Linq.Dynamic.Core (>= 1.7.2)
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 |
|---|---|---|
| 3.0.0-preview.1 | 60 | 10/30/2026 |
| 1.0.4-preview.3 | 88 | 6/8/2026 |
| 1.0.4-preview.2 | 81 | 6/7/2026 |
| 1.0.4-preview.1 | 88 | 6/7/2026 |
| 1.0.3 | 169 | 6/6/2026 |
| 1.0.3-preview.1 | 88 | 5/17/2026 |
| 1.0.2 | 135 | 4/27/2026 |
| 1.0.2-preview.5 | 99 | 4/15/2026 |
| 1.0.2-preview.4 | 105 | 2/8/2026 |
| 1.0.2-preview.3 | 106 | 2/1/2026 |
| 1.0.2-preview.2 | 105 | 1/31/2026 |
| 1.0.2-preview.1 | 215 | 12/4/2025 |
| 1.0.1 | 253 | 11/27/2025 |
| 1.0.0 | 242 | 11/25/2025 |
| 1.0.0-preview.5 | 177 | 10/24/2025 |
| 1.0.0-preview.4 | 149 | 10/3/2025 |
| 1.0.0-preview.2 | 206 | 10/2/2025 |
| 1.0.0-preview.1 | 193 | 10/2/2025 |