mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2026-02-12 02:36:21 +08:00
refactor: clean code
This commit is contained in:
@@ -19,13 +19,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
/// <param name="callbackSignature">微信回调通知中的 Wechatpay-Signature 字段。</param>
|
||||
/// <param name="callbackSerialNumber">微信回调通知中的 Wechatpay-Serial 字段。</param>
|
||||
/// <returns></returns>
|
||||
public static bool VerifyEventSignature(
|
||||
this WechatTenpayClient client,
|
||||
string callbackTimestamp,
|
||||
string callbackNonce,
|
||||
string callbackBody,
|
||||
string callbackSignature,
|
||||
string callbackSerialNumber)
|
||||
public static bool VerifyEventSignature(this WechatTenpayClient client, string callbackTimestamp, string callbackNonce, string callbackBody, string callbackSignature, string callbackSerialNumber)
|
||||
{
|
||||
return VerifyEventSignature(client, callbackTimestamp, callbackNonce, callbackBody, callbackSignature, callbackSerialNumber, out _);
|
||||
}
|
||||
@@ -44,14 +38,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
/// <param name="error"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public static bool VerifyEventSignature(
|
||||
this WechatTenpayClient client,
|
||||
string callbackTimestamp,
|
||||
string callbackNonce,
|
||||
string callbackBody,
|
||||
string callbackSignature,
|
||||
string callbackSerialNumber,
|
||||
out Exception? error)
|
||||
public static bool VerifyEventSignature(this WechatTenpayClient client, string callbackTimestamp, string callbackNonce, string callbackBody, string callbackSignature, string callbackSerialNumber, out Exception? error)
|
||||
{
|
||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||
if (callbackTimestamp == null) throw new ArgumentNullException(nameof(callbackTimestamp));
|
||||
|
||||
@@ -40,11 +40,55 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||
if (response == null) throw new ArgumentNullException(nameof(response));
|
||||
|
||||
return VerifyResponseSignature(client, response.WechatpayTimestamp, response.WechatpayNonce, Encoding.UTF8.GetString(response.RawBytes), response.WechatpaySignature, response.WechatpayCertificateSerialNumber, out error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>验证响应签名。</para>
|
||||
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_1.shtml </para>
|
||||
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/wechatpay/wechatpay4_1.shtml </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 bool VerifyResponseSignature(this WechatTenpayClient client, string responseTimestamp, string responseNonce, string responseBody, string responseSignature, string responseSerialNumber)
|
||||
{
|
||||
return VerifyResponseSignature(client, responseTimestamp, responseNonce, responseBody, responseSignature, responseSerialNumber, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>验证响应签名。</para>
|
||||
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_1.shtml </para>
|
||||
/// <para>REF: https://pay.weixin.qq.com/wiki/doc/apiv3_partner/wechatpay/wechatpay4_1.shtml </para>
|
||||
/// </summary>
|
||||
/// <typeparam name="TResponse"></typeparam>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="responseTimestamp"></param>
|
||||
/// <param name="responseNonce">。</param>
|
||||
/// <param name="responseBody"></param>
|
||||
/// <param name="responseSignature"></param>
|
||||
/// <param name="responseSerialNumber"></param>
|
||||
/// <param name="error"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public static bool VerifyResponseSignature(this WechatTenpayClient client, string responseTimestamp, string responseNonce, string responseBody, string responseSignature, string responseSerialNumber, out Exception? error)
|
||||
{
|
||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||
if (responseTimestamp == null) throw new ArgumentNullException(nameof(responseTimestamp));
|
||||
if (responseNonce == null) throw new ArgumentNullException(nameof(responseNonce));
|
||||
if (responseBody == null) throw new ArgumentNullException(nameof(responseBody));
|
||||
if (responseSignature == null) throw new ArgumentNullException(nameof(responseSignature));
|
||||
if (responseSerialNumber == null) throw new ArgumentNullException(nameof(responseSerialNumber));
|
||||
|
||||
if (client.PlatformCertificateManager != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cert = client.PlatformCertificateManager.GetEntry(response.WechatpayCertificateSerialNumber)!;
|
||||
var cert = client.PlatformCertificateManager.GetEntry(responseSerialNumber)!;
|
||||
if (!cert.HasValue)
|
||||
{
|
||||
error = new Exceptions.WechatTenpayResponseVerificationException("Verify signature of response failed, because there is no platform certificate matched the serial number.");
|
||||
@@ -54,8 +98,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
error = null;
|
||||
return Utilities.RSAUtility.VerifyWithSHA256ByCertificate(
|
||||
certificate: cert.Value.Certificate,
|
||||
plainText: GetPlainTextForSignature(response),
|
||||
signature: response.WechatpaySignature
|
||||
plainText: GetPlainTextForSignature(timestamp: responseTimestamp, nonce: responseNonce, body: responseBody),
|
||||
signature: responseSignature
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -69,11 +113,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
return false;
|
||||
}
|
||||
|
||||
private static string GetPlainTextForSignature(WechatTenpayResponse response)
|
||||
private static string GetPlainTextForSignature(string timestamp, string nonce, string body)
|
||||
{
|
||||
string timestamp = response.WechatpayTimestamp;
|
||||
string nonce = response.WechatpayNonce;
|
||||
string body = Encoding.UTF8.GetString(response.RawBytes);
|
||||
return $"{timestamp}\n{nonce}\n{body}\n";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user