Skip to content

Commit

Permalink
[WeChatPay] 修正 微信支付公钥 验签
Browse files Browse the repository at this point in the history
  • Loading branch information
roc916 committed Feb 24, 2025
1 parent 4b451cf commit 04a7792
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/Essensoft.Paylinks.WeChatPay.Client/WeChatPayClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,29 @@ public class WeChatPayClient(IHttpClientFactory httpClientFactory, IWeChatPayPla
public async Task<T> ExecuteAsync<T>(IWeChatPayRequest<T> request, WeChatPayClientOptions options, CancellationToken cancellationToken = default) where T : WeChatPayResponse
{
string? certSerialNo = null;
string? certPublicKey = null;

if (request is IWeChatPaySecretRequest<T> secretRequest)
if (!string.IsNullOrEmpty(options.WeChatPayPublicKeyId) && !string.IsNullOrEmpty(options.WeChatPayPublicKey))
{
// 加密敏感信息
string certPublicKey;

if (!string.IsNullOrEmpty(options.WeChatPayPublicKeyId) && !string.IsNullOrEmpty(options.WeChatPayPublicKey))
certSerialNo = options.WeChatPayPublicKeyId;
certPublicKey = options.WeChatPayPublicKey;
}
else
{
var certificateManager = certificateManagerFactory.Create(options.MchId);
var certificate = certificateManager.GetAvailableCertificates().OrderByDescending(c => c.EffectiveTime).FirstOrDefault() ?? throw new WeChatPayException("验签失败: 微信平台证书管理器中未找到有效平台证书");
if (string.IsNullOrEmpty(certificate.PublicKey))
{
certSerialNo = options.WeChatPayPublicKeyId;
certPublicKey = options.WeChatPayPublicKey;
throw new WeChatPayException("验签失败: 平台证书公钥为空");
}
else
{
var certificateManager = certificateManagerFactory.Create(options.MchId);
var certificate = certificateManager.GetAvailableCertificates().OrderByDescending(c => c.EffectiveTime).FirstOrDefault() ?? throw new WeChatPayException("验签失败: 微信平台证书管理器中未找到有效平台证书");
if (string.IsNullOrEmpty(certificate.PublicKey))
{
throw new WeChatPayException("验签失败: 平台证书公钥为空");
}

certSerialNo = certificate.SerialNo;
certPublicKey = certificate.PublicKey;
}
certSerialNo = certificate.SerialNo;
certPublicKey = certificate.PublicKey;
}

if (request is IWeChatPaySecretRequest<T> secretRequest)
{
// 加密敏感信息
secretRequest.EncryptSecretRequest(certPublicKey);
}

Expand All @@ -54,8 +53,6 @@ public async Task<T> ExecuteAsync<T>(IWeChatPayRequest<T> request, WeChatPayClie
if (request.GetNeedVerify())
{
// 验签
string certPublicKey;

if (headers.Serial.StartsWith(WeChatPayConstants.PublicKeyIdPrefix)) // 微信支付公钥
{
if (!string.IsNullOrEmpty(options.WeChatPayPublicKeyId) && !string.IsNullOrEmpty(options.WeChatPayPublicKey))
Expand Down

0 comments on commit 04a7792

Please sign in to comment.