Sfoglia il codice sorgente

[WeChatPay] 修正 微信支付公钥 验签

Roc 7 mesi fa
parent
commit
04a7792680
1 ha cambiato i file con 17 aggiunte e 20 eliminazioni
  1. 17 20
      src/Essensoft.Paylinks.WeChatPay.Client/WeChatPayClient.cs

+ 17 - 20
src/Essensoft.Paylinks.WeChatPay.Client/WeChatPayClient.cs

@@ -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
     public async Task<T> ExecuteAsync<T>(IWeChatPayRequest<T> request, WeChatPayClientOptions options, CancellationToken cancellationToken = default) where T : WeChatPayResponse
     {
     {
         string? certSerialNo = null;
         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);
             secretRequest.EncryptSecretRequest(certPublicKey);
         }
         }
 
 
@@ -54,8 +53,6 @@ public class WeChatPayClient(IHttpClientFactory httpClientFactory, IWeChatPayPla
             if (request.GetNeedVerify())
             if (request.GetNeedVerify())
             {
             {
                 // 验签
                 // 验签
-                string certPublicKey;
-
                 if (headers.Serial.StartsWith(WeChatPayConstants.PublicKeyIdPrefix)) // 微信支付公钥
                 if (headers.Serial.StartsWith(WeChatPayConstants.PublicKeyIdPrefix)) // 微信支付公钥
                 {
                 {
                     if (!string.IsNullOrEmpty(options.WeChatPayPublicKeyId) && !string.IsNullOrEmpty(options.WeChatPayPublicKey))
                     if (!string.IsNullOrEmpty(options.WeChatPayPublicKeyId) && !string.IsNullOrEmpty(options.WeChatPayPublicKey))