DotNetCore.SKIT.FlurlHttpCl.../src/SKIT.FlurlHttpClient.Wechat.TenpayV3/Extensions/WechatTenpayClientResponseVerificationExtensions.cs

196 lines
9.3 KiB
C#
Raw Normal View History

using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
{
using SKIT.FlurlHttpClient.Primitives;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Constants;
public static class WechatTenpayClientResponseVerificationExtensions
{
/// <summary>
/// <para>验证响应签名。</para>
2024-02-05 16:27:37 +08:00
/// <para>
/// REF: <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/merchant/development/interface-rules/signature-verification.html ]]> <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/partner/development/interface-rules/signature-verification.html ]]>
/// </para>
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="client"></param>
/// <param name="response"></param>
/// <returns></returns>
public static ErroredResult VerifyResponseSignature<TResponse>(this WechatTenpayClient client, TResponse response)
where TResponse : WechatTenpayResponse
{
2024-01-29 23:12:37 +08:00
if (client is null) throw new ArgumentNullException(nameof(client));
if (response is null) throw new ArgumentNullException(nameof(response));
return VerifyResponseSignature(
client,
responseTimestamp: response.WechatpayTimestamp,
responseNonce: response.WechatpayNonce,
2024-01-29 23:12:37 +08:00
responseBody: Encoding.UTF8.GetString(response.GetRawBytes()),
responseSignature: response.WechatpaySignature,
responseSignatureType: response.WechatpaySignatureType,
responseSerialNumber: response.WechatpayCertificateSerialNumber
);
}
/// <summary>
/// <para>验证响应签名。</para>
2024-02-05 16:27:37 +08:00
/// <para>
/// REF: <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/merchant/development/interface-rules/signature-verification.html ]]> <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/partner/development/interface-rules/signature-verification.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="responseTimestamp"></param>
/// <param name="responseNonce">。</param>
/// <param name="responseBody"></param>
/// <param name="responseSignature"></param>
/// <param name="responseSerialNumber"></param>
/// <returns></returns>
public static ErroredResult VerifyResponseSignature(this WechatTenpayClient client, string responseTimestamp, string responseNonce, string responseBody, string responseSignature, string responseSerialNumber)
{
return VerifyResponseSignature(
client,
responseTimestamp: responseTimestamp,
responseNonce: responseNonce,
responseBody: responseBody,
responseSignature: responseSignature,
responseSignatureType: SignSchemes.WECHATPAY2_RSA_2048_WITH_SHA256,
responseSerialNumber
);
}
/// <summary>
/// <para>验证响应签名。</para>
2024-02-05 16:27:37 +08:00
/// <para>
/// REF: <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/merchant/development/interface-rules/signature-verification.html ]]> <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/partner/development/interface-rules/signature-verification.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="responseTimestamp"></param>
/// <param name="responseNonce">。</param>
/// <param name="responseBody"></param>
/// <param name="responseSignature"></param>
/// <param name="responseSignatureType"></param>
/// <param name="responseSerialNumber"></param>
/// <returns></returns>
public static ErroredResult VerifyResponseSignature(this WechatTenpayClient client, string responseTimestamp, string responseNonce, string responseBody, string responseSignature, string responseSignatureType, string responseSerialNumber)
2022-03-11 10:38:15 +08:00
{
2024-01-29 23:12:37 +08:00
if (client is null) throw new ArgumentNullException(nameof(client));
2022-03-11 10:38:15 +08:00
return WechatTenpayClientSigningExtensions.VerifySignature(
client,
strTimestamp: responseTimestamp,
strNonce: responseNonce,
strContent: responseBody,
strSignature: responseSignature,
2024-01-29 23:12:37 +08:00
strSignScheme: responseSignatureType,
strSerialNumber: responseSerialNumber
);
}
/// <summary>
/// <para>异步验证响应签名。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/merchant/development/interface-rules/signature-verification.html ]]> <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/partner/development/interface-rules/signature-verification.html ]]>
/// </para>
/// </summary>
/// <typeparam name="TResponse"></typeparam>
/// <param name="client"></param>
/// <param name="response"></param>
2024-02-07 18:02:46 +08:00
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<ErroredResult> VerifyResponseSignatureAsync<TResponse>(this WechatTenpayClient client, TResponse response, CancellationToken cancellationToken = default)
where TResponse : WechatTenpayResponse
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (response is null) throw new ArgumentNullException(nameof(response));
return VerifyResponseSignatureAsync(
client,
responseTimestamp: response.WechatpayTimestamp,
responseNonce: response.WechatpayNonce,
responseBody: Encoding.UTF8.GetString(response.GetRawBytes()),
responseSignature: response.WechatpaySignature,
responseSignatureType: response.WechatpaySignatureType,
responseSerialNumber: response.WechatpayCertificateSerialNumber,
cancellationToken: cancellationToken
);
}
/// <summary>
/// <para>异步验证响应签名。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/merchant/development/interface-rules/signature-verification.html ]]> <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/partner/development/interface-rules/signature-verification.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="responseTimestamp"></param>
/// <param name="responseNonce">。</param>
/// <param name="responseBody"></param>
/// <param name="responseSignature"></param>
/// <param name="responseSerialNumber"></param>
2024-02-07 18:02:46 +08:00
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<ErroredResult> VerifyResponseSignatureAsync(this WechatTenpayClient client, string responseTimestamp, string responseNonce, string responseBody, string responseSignature, string responseSerialNumber, CancellationToken cancellationToken = default)
{
return VerifyResponseSignatureAsync(
client,
responseTimestamp: responseTimestamp,
responseNonce: responseNonce,
responseBody: responseBody,
responseSignature: responseSignature,
responseSignatureType: SignSchemes.WECHATPAY2_RSA_2048_WITH_SHA256,
responseSerialNumber: responseSerialNumber,
cancellationToken
);
}
/// <summary>
/// <para>异步验证响应签名。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/merchant/development/interface-rules/signature-verification.html ]]> <br/>
/// <![CDATA[ https://pay.weixin.qq.com/docs/partner/development/interface-rules/signature-verification.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="responseTimestamp"></param>
/// <param name="responseNonce">。</param>
/// <param name="responseBody"></param>
/// <param name="responseSignature"></param>
/// <param name="responseSignatureType"></param>
/// <param name="responseSerialNumber"></param>
2024-02-07 18:02:46 +08:00
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task<ErroredResult> VerifyResponseSignatureAsync(this WechatTenpayClient client, string responseTimestamp, string responseNonce, string responseBody, string responseSignature, string responseSignatureType, string responseSerialNumber, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
return WechatTenpayClientSigningExtensions.VerifySignatureAsync(
client,
strTimestamp: responseTimestamp,
strNonce: responseNonce,
strContent: responseBody,
strSignature: responseSignature,
strSignScheme: responseSignatureType,
strSerialNumber: responseSerialNumber,
cancellationToken
);
}
}
}