2021-05-10 15:30:00 +08:00
|
|
|
|
# SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
基于 `Flurl.Http` 的[微信商户平台](https://pay.weixin.qq.com/) HTTP API SDK。
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
2024-03-09 22:28:48 +08:00
|
|
|
|
本模块仅支持微信支付 v3 版 API,如需接入微信支付 v2 版 API,请移步 [`SKIT.FlurlHttpClient.Wechant.TenpayV2`](../WechatTenpayV2/README.md) 模块。
|
2022-01-25 14:42:58 +08:00
|
|
|
|
|
2021-05-10 15:30:00 +08:00
|
|
|
|
---
|
|
|
|
|
|
2021-05-12 01:06:19 +08:00
|
|
|
|
## 功能
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
|
|
|
|
- 基于微信支付 v3 版 API 封装。
|
2023-08-11 21:31:37 +08:00
|
|
|
|
- 支持商户(直连模式)、合作伙伴(服务商、渠道商、机构、银行模式)两种角色模式。
|
2023-03-02 19:36:47 +08:00
|
|
|
|
- 请求时自动生成签名(支持国际 RSA 算法或国密 SM 算法),无需开发者手动干预。
|
|
|
|
|
- 提供了微信支付所需的 AES、RSA、SM2/SM3/SM4、SHA-256 等算法工具类。
|
|
|
|
|
- 提供了调起支付签名、解析响应敏感数据、解析回调通知事件敏感数据等扩展方法。
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
## 快速入门
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
2024-02-07 11:22:05 +08:00
|
|
|
|
> [!IMPORTANT]
|
|
|
|
|
> 此目录下的文档适用于 v3.x 版本的模块。如果你正在使用 2.x 版本,请移步至 GitHub/Gitee 的已归档分支。
|
|
|
|
|
|
2021-05-10 15:30:00 +08:00
|
|
|
|
### 安装:
|
|
|
|
|
|
2021-05-12 18:20:52 +08:00
|
|
|
|
提示:如果你使用 Visual Studio NuGet 管理器图形化界面,请在搜索结果中勾选“**包括预发行版**”。
|
|
|
|
|
|
2021-05-10 15:30:00 +08:00
|
|
|
|
```shell
|
|
|
|
|
# 通过 NuGet 安装
|
|
|
|
|
> Install-Package SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|
|
|
|
|
|
|
|
|
# 通过 dotnet-tools 安装
|
|
|
|
|
> dotnet add package SKIT.FlurlHttpClient.Wechat.TenpayV3
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 初始化:
|
|
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
2021-07-20 15:35:04 +08:00
|
|
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
|
|
|
|
var options = new WechatTenpayClientOptions()
|
|
|
|
|
{
|
|
|
|
|
MerchantId = "微信商户号",
|
|
|
|
|
MerchantV3Secret = "微信商户 v3 API 密钥",
|
2022-01-21 14:51:30 +08:00
|
|
|
|
MerchantCertificateSerialNumber = "微信商户证书序列号",
|
2024-02-20 12:49:36 +08:00
|
|
|
|
MerchantCertificatePrivateKey = System.IO.File.ReadAllText("/微信商户证书私钥文件路径/apiclient_key.pem"),
|
2025-02-25 18:03:37 +08:00
|
|
|
|
|
|
|
|
|
// 基于平台证书的认证方式还需设置以下参数:
|
|
|
|
|
PlatformAuthScheme = Settings.PlatformAuthScheme.Certificate,
|
|
|
|
|
PlatformCertificateManager = new InMemoryCertificateManager()
|
|
|
|
|
|
|
|
|
|
// 基于平台公钥的认证方式还需设置以下参数:
|
|
|
|
|
PlatformAuthScheme = Settings.PlatformAuthScheme.PublicKey,
|
|
|
|
|
PlatformPublicKeyManager = new InMemoryPublicKeyManager()
|
2021-05-10 15:30:00 +08:00
|
|
|
|
};
|
2024-02-07 11:22:05 +08:00
|
|
|
|
var client = WechatTenpayClientBuilder.Create(options).Build();
|
2021-05-10 15:30:00 +08:00
|
|
|
|
```
|
|
|
|
|
|
2025-02-25 18:03:37 +08:00
|
|
|
|
🔥 平台证书管理器、平台公钥管理器的具体用法请参阅下文的基础用法与加密、验签有关的章节。
|
2024-11-15 18:55:17 +08:00
|
|
|
|
|
2021-05-10 15:30:00 +08:00
|
|
|
|
### 请求 & 响应:
|
|
|
|
|
|
|
|
|
|
```csharp
|
|
|
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
|
|
|
|
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
|
|
|
|
|
2021-07-14 10:25:11 +08:00
|
|
|
|
/* 以 JSAPI 统一下单接口为例 */
|
2021-05-10 15:30:00 +08:00
|
|
|
|
var request = new CreatePayTransactionJsapiRequest()
|
|
|
|
|
{
|
|
|
|
|
OutTradeNumber = "商户订单号",
|
|
|
|
|
AppId = "微信 AppId",
|
|
|
|
|
Description = "订单描述",
|
|
|
|
|
ExpireTime = DateTimeOffset.Now.AddMinutes(15),
|
|
|
|
|
NotifyUrl = "https://example.com",
|
2023-03-09 21:45:57 +08:00
|
|
|
|
Amount = new CreatePayTransactionJsapiRequest.Types.Amount()
|
2021-05-10 15:30:00 +08:00
|
|
|
|
{
|
|
|
|
|
Total = 1
|
|
|
|
|
},
|
2023-03-09 21:45:57 +08:00
|
|
|
|
Payer = new CreatePayTransactionJsapiRequest.Types.Payer()
|
2021-05-10 15:30:00 +08:00
|
|
|
|
{
|
2021-05-28 19:27:50 +08:00
|
|
|
|
OpenId = "微信 OpenId"
|
2021-05-10 15:30:00 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
2021-06-07 00:32:04 +08:00
|
|
|
|
var response = await client.ExecuteCreatePayTransactionJsapiAsync(request);
|
2021-07-11 00:57:32 +08:00
|
|
|
|
if (response.IsSuccessful())
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("PrepayId:" + response.PrepayId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("错误代码:" + response.ErrorCode);
|
|
|
|
|
Console.WriteLine("错误描述:" + response.ErrorMessage);
|
|
|
|
|
}
|
2021-05-10 15:30:00 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
## 基础用法
|
|
|
|
|
|
2024-02-07 11:22:05 +08:00
|
|
|
|
- ⭐ [如何快速找到需要调用的 API 模型类名 / 方法名(附完整 API 对照表)?](./Basic_ModelDefinition.md)
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
- [如何查看商户证书序列号?](./Basic_CertificateSerialNumber.md)
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
- [如何加密请求中的敏感数据?](./Basic_RequestSensitiveDataEncryption.md)
|
2022-03-03 17:19:29 +08:00
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
- [如何解密响应中的敏感数据?](./Basic_ResponseSensitiveDataDecryption.md)
|
2021-11-25 21:15:54 +08:00
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
- [如何验证响应签名?](./Basic_ResponseSignatureVerification.md)
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
- [如何解密回调通知事件中的敏感数据?](./Basic_EventResourceDecryption.md)
|
2021-07-20 15:35:04 +08:00
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
- [如何验证回调通知事件签名?](./Basic_EventSignatureVerification.md)
|
2021-05-10 15:30:00 +08:00
|
|
|
|
|
2024-02-07 11:22:05 +08:00
|
|
|
|
- ⭐ [如何生成客户端(JSAPI、App、小程序等)所需的参数及二次签名?](./Basic_Parameters.md)
|
2021-11-29 12:14:42 +08:00
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
- [如何自定义额外的 API 接口?](./Basic_Extensions.md)
|
2021-07-27 20:26:41 +08:00
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
- [如何接入国密算法?](./Basic_SMAlgorithm.md)
|
2022-11-14 13:25:07 +08:00
|
|
|
|
|
2024-05-17 16:39:51 +08:00
|
|
|
|
- [如何接入境外支付 API?](./Basic_GlobalAPI.md)
|
|
|
|
|
|
2023-03-02 19:36:47 +08:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 高级技巧
|
2021-07-31 23:08:11 +08:00
|
|
|
|
|
2024-03-09 22:28:48 +08:00
|
|
|
|
- [配置 JSON 序列化器](./Advanced_JsonSerializer.md)
|
2024-02-21 21:59:41 +08:00
|
|
|
|
|
2024-03-09 22:28:48 +08:00
|
|
|
|
- [配置 HttpClient](./Advanced_HttpClient.md)
|
2022-12-15 11:59:29 +08:00
|
|
|
|
|
2024-03-09 22:28:48 +08:00
|
|
|
|
- [使用拦截器](./Advanced_Interceptor.md)
|
2022-12-15 11:59:29 +08:00
|
|
|
|
|
2024-03-09 22:28:48 +08:00
|
|
|
|
- [避免内存泄漏](./Advanced_Dispose.md)
|
|
|
|
|
|
|
|
|
|
- [与 `IHttpClientFactory` 集成](./Advanced_HttpClientFactory.md)
|
2022-12-15 11:59:29 +08:00
|
|
|
|
|
2021-07-31 23:08:11 +08:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 示例项目
|
|
|
|
|
|
|
|
|
|
为方便开发者快速掌握本库的使用方法,这里提供一个示例项目以供参考。
|
|
|
|
|
|
|
|
|
|
请阅读[《示例项目说明》](./Sample.md)。
|
2024-02-07 18:02:46 +08:00
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## 迁移指南
|
|
|
|
|
|
|
|
|
|
- [v3.x 迁移指南](./Migration_V3.md)
|