Files
DotNetCore.SKIT.FlurlHttpCl…/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/WechatTenpayClient.cs

177 lines
7.6 KiB
C#
Raw Normal View History

using System;
2021-05-10 15:30:00 +08:00
using System.Net.Http;
2024-01-29 23:12:37 +08:00
using System.Reflection;
2021-05-10 15:30:00 +08:00
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
{
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Constants;
2021-05-10 15:30:00 +08:00
/// <summary>
/// 一个微信支付 API HTTP 客户端。
/// </summary>
public class WechatTenpayClient : CommonClientBase, ICommonClient
2021-05-10 15:30:00 +08:00
{
/// <summary>
/// 获取当前客户端使用的微信支付商户凭证。
2021-05-10 15:30:00 +08:00
/// </summary>
public Settings.Credentials Credentials { get; }
2021-05-10 15:30:00 +08:00
/// <summary>
/// 获取当前客户端是否开启微信支付平台 API 认证方案回退开关。
/// </summary>
public bool PlatformAuthFallbackSwitch { get; }
/// <summary>
/// 获取当前客户端使用的微信支付平台 API 认证方案。
/// </summary>
public Settings.PlatformAuthScheme PlatformAuthScheme { get; }
2021-05-10 15:30:00 +08:00
/// <summary>
/// 获取当前客户端使用的微信支付平台证书管理器。
2021-05-10 15:30:00 +08:00
/// </summary>
public Settings.ICertificateManager PlatformCertificateManager { get; }
/// <summary>
/// 获取当前客户端使用的微信支付平台公钥管理器。
/// </summary>
public Settings.IPublicKeyManager PlatformPublicKeyManager { get; }
/// <summary>
2021-11-25 21:15:54 +08:00
/// 获取是否自动加密请求中的敏感信息字段。
/// </summary>
protected internal bool AutoEncryptRequestSensitiveProperty { get; }
/// <summary>
2021-11-25 21:15:54 +08:00
/// 获取是否自动解密请求中的敏感信息字段。
/// </summary>
protected internal bool AutoDecryptResponseSensitiveProperty { get; }
2021-05-10 15:30:00 +08:00
/// <summary>
/// 用指定的配置项初始化 <see cref="WechatTenpayClient"/> 类的新实例。
/// </summary>
/// <param name="options">配置项。</param>
public WechatTenpayClient(WechatTenpayClientOptions options)
2024-01-29 23:12:37 +08:00
: this(options, null)
2021-05-10 15:30:00 +08:00
{
2024-01-29 23:12:37 +08:00
}
/// <summary>
///
/// </summary>
/// <param name="options"></param>
/// <param name="httpClient"></param>
/// <param name="disposeClient"></param>
internal protected WechatTenpayClient(WechatTenpayClientOptions options, HttpClient? httpClient, bool disposeClient = true)
: base(httpClient, disposeClient)
{
if (options is null) throw new ArgumentNullException(nameof(options));
2021-05-10 15:30:00 +08:00
Credentials = new Settings.Credentials(options);
PlatformAuthFallbackSwitch = options.PlatformAuthFallbackSwitch;
PlatformAuthScheme = options.PlatformAuthScheme;
PlatformCertificateManager = options.PlatformCertificateManager;
PlatformPublicKeyManager = options.PlatformPublicKeyManager;
AutoEncryptRequestSensitiveProperty = options.AutoEncryptRequestSensitiveProperty;
AutoDecryptResponseSensitiveProperty = options.AutoDecryptResponseSensitiveProperty;
2021-05-10 15:30:00 +08:00
FlurlClient.BaseUrl = options.Endpoint ?? WechatTenpayEndpoints.DEFAULT;
2024-01-29 23:12:37 +08:00
FlurlClient.WithHeader(HttpHeaders.Accept, "application/json");
FlurlClient.WithHeader(HttpHeaders.AcceptLanguage, options.AcceptLanguage);
FlurlClient.WithHeader(HttpHeaders.UserAgent, $"OS/{Environment.OSVersion.Platform} SKIT.FlurlHttpClient.Wechat.Tenpay/{AssemblyProps.VERSION}");
2024-01-29 23:12:37 +08:00
FlurlClient.WithTimeout(options.Timeout <= 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromMilliseconds(options.Timeout));
Interceptors.Add(new Interceptors.WechatTenpayRequestSigningInterceptor(
2022-05-09 19:28:47 +08:00
scheme: options.SignScheme,
2021-05-10 15:30:00 +08:00
mchId: options.MerchantId,
mchCertSn: options.MerchantCertificateSerialNumber,
mchCertPk: options.MerchantCertificatePrivateKey
));
2021-05-10 15:30:00 +08:00
}
2021-07-11 00:55:41 +08:00
/// <summary>
/// 使用当前客户端生成一个新的 <see cref="IFlurlRequest"/> 对象。
/// </summary>
/// <param name="request"></param>
2024-01-29 23:12:37 +08:00
/// <param name="httpMethod"></param>
2021-07-11 00:55:41 +08:00
/// <param name="urlSegments"></param>
/// <returns></returns>
2024-01-29 23:12:37 +08:00
public IFlurlRequest CreateFlurlRequest(WechatTenpayRequest request, HttpMethod httpMethod, params object[] urlSegments)
2021-07-11 00:55:41 +08:00
{
2024-01-29 23:12:37 +08:00
IFlurlRequest flurlRequest = base.CreateFlurlRequest(request, httpMethod, urlSegments);
2021-07-11 00:55:41 +08:00
if (PlatformAuthFallbackSwitch)
{
this._EnsureRequestWechatpaySerialNumberIsSet(request);
}
if (AutoEncryptRequestSensitiveProperty)
{
this.EncryptRequestSensitiveProperty(request);
}
if (request.WechatpaySerialNumber is not null)
2021-07-11 00:55:41 +08:00
{
flurlRequest.WithHeader("Wechatpay-Serial", request.WechatpaySerialNumber);
2021-07-11 00:55:41 +08:00
}
return flurlRequest;
}
2021-05-10 15:30:00 +08:00
/// <summary>
/// 异步发起请求。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="flurlRequest"></param>
2021-07-11 00:55:41 +08:00
/// <param name="httpContent"></param>
2021-05-10 15:30:00 +08:00
/// <param name="cancellationToken"></param>
/// <returns></returns>
2024-01-29 23:12:37 +08:00
public async Task<T> SendFlurlRequestAsync<T>(IFlurlRequest flurlRequest, HttpContent? httpContent = null, CancellationToken cancellationToken = default)
2021-05-10 15:30:00 +08:00
where T : WechatTenpayResponse, new()
{
2024-01-29 23:12:37 +08:00
if (flurlRequest is null) throw new ArgumentNullException(nameof(flurlRequest));
2021-05-10 15:30:00 +08:00
2024-02-05 21:24:16 +08:00
using IFlurlResponse flurlResponse = await base.SendFlurlRequestAsync(flurlRequest, httpContent, cancellationToken).ConfigureAwait(false);
return await WrapFlurlResponseAsJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
2021-05-10 15:30:00 +08:00
}
/// <summary>
/// 异步发起请求。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="flurlRequest"></param>
2021-05-10 15:30:00 +08:00
/// <param name="data"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
2024-01-29 23:12:37 +08:00
public async Task<T> SendFlurlRequestAsJsonAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
2021-05-10 15:30:00 +08:00
where T : WechatTenpayResponse, new()
{
2024-01-29 23:12:37 +08:00
if (flurlRequest is null) throw new ArgumentNullException(nameof(flurlRequest));
bool isSimpleRequest = data is null ||
flurlRequest.Verb == HttpMethod.Get ||
flurlRequest.Verb == HttpMethod.Head ||
flurlRequest.Verb == HttpMethod.Options;
using IFlurlResponse flurlResponse = isSimpleRequest ?
2024-02-05 21:24:16 +08:00
await base.SendFlurlRequestAsync(flurlRequest, null, cancellationToken).ConfigureAwait(false) :
await base.SendFlurlRequestAsJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
return await WrapFlurlResponseAsJsonAsync<T>(flurlResponse, cancellationToken).ConfigureAwait(false);
2021-05-10 15:30:00 +08:00
}
2024-01-29 23:12:37 +08:00
private new async Task<TResponse> WrapFlurlResponseAsJsonAsync<TResponse>(IFlurlResponse flurlResponse, CancellationToken cancellationToken = default)
2022-01-04 19:27:53 +08:00
where TResponse : WechatTenpayResponse, new()
2021-05-10 15:30:00 +08:00
{
2024-02-05 21:24:16 +08:00
TResponse result = await base.WrapFlurlResponseAsJsonAsync<TResponse>(flurlResponse, cancellationToken).ConfigureAwait(false);
2021-05-10 15:30:00 +08:00
if (AutoDecryptResponseSensitiveProperty && result.IsSuccessful())
{
this.DecryptResponseSensitiveProperty(result);
}
2021-05-10 15:30:00 +08:00
return result;
}
}
}