From 04a77926800f13e12de4abc300ec05762eb297e7 Mon Sep 17 00:00:00 2001 From: Roc Date: Mon, 24 Feb 2025 19:53:28 +0800 Subject: [PATCH] =?UTF-8?q?[WeChatPay]=20=E4=BF=AE=E6=AD=A3=20=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=94=AF=E4=BB=98=E5=85=AC=E9=92=A5=20=E9=AA=8C?= =?UTF-8?q?=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WeChatPayClient.cs | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/Essensoft.Paylinks.WeChatPay.Client/WeChatPayClient.cs b/src/Essensoft.Paylinks.WeChatPay.Client/WeChatPayClient.cs index 31ddc5668..8434a036c 100644 --- a/src/Essensoft.Paylinks.WeChatPay.Client/WeChatPayClient.cs +++ b/src/Essensoft.Paylinks.WeChatPay.Client/WeChatPayClient.cs @@ -14,30 +14,29 @@ public class WeChatPayClient(IHttpClientFactory httpClientFactory, IWeChatPayPla public async Task ExecuteAsync(IWeChatPayRequest request, WeChatPayClientOptions options, CancellationToken cancellationToken = default) where T : WeChatPayResponse { string? certSerialNo = null; + string? certPublicKey = null; - if (request is IWeChatPaySecretRequest 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 secretRequest) + { + // 加密敏感信息 secretRequest.EncryptSecretRequest(certPublicKey); } @@ -54,8 +53,6 @@ public async Task ExecuteAsync(IWeChatPayRequest request, WeChatPayClie if (request.GetNeedVerify()) { // 验签 - string certPublicKey; - if (headers.Serial.StartsWith(WeChatPayConstants.PublicKeyIdPrefix)) // 微信支付公钥 { if (!string.IsNullOrEmpty(options.WeChatPayPublicKeyId) && !string.IsNullOrEmpty(options.WeChatPayPublicKey))