mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-07-15 14:04:32 +08:00
feat(tenpayv3): 验签时不再抛出异常
This commit is contained in:
parent
9e6bd2e824
commit
23b6b94335
@ -1,24 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Exceptions
|
||||
{
|
||||
public class WechatTenpayResponseVerificationException : WechatTenpayException
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
internal WechatTenpayResponseVerificationException()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
internal WechatTenpayResponseVerificationException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
internal WechatTenpayResponseVerificationException(string message, Exception innerException)
|
||||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -35,39 +35,23 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
if (callbackSignature == null) throw new ArgumentNullException(nameof(callbackSignature));
|
||||
if (callbackSerialNumber == null) throw new ArgumentNullException(nameof(callbackSerialNumber));
|
||||
|
||||
if (client.CertificateManager == null)
|
||||
if (client.CertificateManager != null)
|
||||
{
|
||||
throw new Exceptions.WechatTenpayResponseVerificationException($"You must set an instance of `{nameof(Settings.CertificateManager)}` at first.");
|
||||
}
|
||||
else
|
||||
{
|
||||
string? certificate = client.CertificateManager.GetCertificate(callbackSerialNumber);
|
||||
if (certificate == null)
|
||||
throw new Exceptions.WechatTenpayResponseVerificationException("Cannot get certificate by the serial number, may not be stored.");
|
||||
|
||||
string? publicKey = null;
|
||||
try
|
||||
{
|
||||
publicKey = Utilities.RSAUtility.ExportPublicKey(certificate);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exceptions.WechatTenpayResponseVerificationException("Cannot get public key of the certificate, may not be a valid certificate data.", ex);
|
||||
}
|
||||
string certificate = client.CertificateManager.GetCertificate(callbackSerialNumber)!;
|
||||
string publicKey = Utilities.RSAUtility.ExportPublicKey(certificate);
|
||||
|
||||
try
|
||||
{
|
||||
return Utilities.RSAUtility.VerifyWithSHA256(
|
||||
publicKey: publicKey,
|
||||
plainText: GetPlainTextForSignature(timestamp: callbackTimestamp, nonce: callbackNonce, body: callbackBody),
|
||||
signature: callbackSignature
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exceptions.WechatTenpayResponseVerificationException("Verify event signature failed. Please see the `InnerException` for more details.", ex);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static string GetPlainTextForSignature(string timestamp, string nonce, string body)
|
||||
|
@ -23,42 +23,23 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
|
||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||
if (response == null) throw new ArgumentNullException(nameof(response));
|
||||
|
||||
if (client.CertificateManager == null)
|
||||
if (client.CertificateManager != null)
|
||||
{
|
||||
throw new Exceptions.WechatTenpayResponseVerificationException($"You must set an instance of `{nameof(Settings.CertificateManager)}` at first.");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (response.WechatpayCertSerialNumber == null)
|
||||
throw new Exceptions.WechatTenpayResponseVerificationException("Cannot read the serial number in headers of this response.");
|
||||
|
||||
string? certificate = client.CertificateManager.GetCertificate(response.WechatpayCertSerialNumber);
|
||||
if (certificate == null)
|
||||
throw new Exceptions.WechatTenpayResponseVerificationException("Cannot get certificate by the serial number, may not be stored.");
|
||||
|
||||
string? publicKey = null;
|
||||
try
|
||||
{
|
||||
publicKey = Utilities.RSAUtility.ExportPublicKey(certificate);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exceptions.WechatTenpayResponseVerificationException("Cannot get public key of the certificate, may not be a valid certificate data.", ex);
|
||||
}
|
||||
string certificate = client.CertificateManager.GetCertificate(response.WechatpayCertSerialNumber)!;
|
||||
string publicKey = Utilities.RSAUtility.ExportPublicKey(certificate);
|
||||
|
||||
try
|
||||
{
|
||||
return Utilities.RSAUtility.VerifyWithSHA256(
|
||||
publicKey: publicKey,
|
||||
plainText: GetPlainTextForSignature(response),
|
||||
signature: response.WechatpaySignature
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exceptions.WechatTenpayResponseVerificationException("Verify response signature failed.", ex);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static string GetPlainTextForSignature(WechatTenpayResponse response)
|
||||
|
Loading…
Reference in New Issue
Block a user