From 39e59495886354885a7ada36f43dac902665e324 Mon Sep 17 00:00:00 2001 From: JeffreySu Date: Sun, 29 Jan 2023 15:55:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20TenPayApiResultCode.Tr?= =?UTF-8?q?yGetCode()=20=E6=96=B9=E6=B3=95=E7=9A=84=20204=20=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Apis/Entities/TenPayApiResultCodeTests.cs | 11 +++++++++++ .../Apis/Entities/TenPayApiResultCode.cs | 3 +++ .../HttpHandlers/TenPayApiRequest.cs | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3.Test/Apis/Entities/TenPayApiResultCodeTests.cs b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3.Test/Apis/Entities/TenPayApiResultCodeTests.cs index ab53d6911e..d9db64f270 100644 --- a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3.Test/Apis/Entities/TenPayApiResultCodeTests.cs +++ b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3.Test/Apis/Entities/TenPayApiResultCodeTests.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Senparc.CO2NET.Extensions; using Senparc.Weixin.TenPayV3.Apis.Entities; using System; using System.Collections.Generic; @@ -21,5 +22,15 @@ public void TryGetCodeTest() Assert.AreEqual("RESOURCE_ALREADY_EXISTS", result.ErrorCode); Assert.AreEqual("创建请求重入,但本次请求与上次请求信息不一致", result.Solution); } + + [TestMethod] + public void TryGetCode_204Test() + { + var result = TenPayApiResultCode.TryGetCode(System.Net.HttpStatusCode.NoContent, null); + Assert.IsNotNull(result); + Console.WriteLine(result.ToJson(true)); + Assert.AreEqual("204", result.StateCode); + Assert.IsTrue(result.Success); + } } } diff --git a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/TenPayApiResultCode.cs b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/TenPayApiResultCode.cs index be5f6035c4..b49fc5a412 100644 --- a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/TenPayApiResultCode.cs +++ b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/TenPayApiResultCode.cs @@ -119,9 +119,12 @@ public record class TenPayApiResultCode /// 尝试获取 HTTP 返回代码,并附带备注信息 /// /// + /// /// public static TenPayApiResultCode TryGetCode(HttpStatusCode httpStatusCode, string responseContent) { + responseContent ??= "{}"; + TenPayApiResultCode resultCode = null; if (CodesCollection.TryGetValue(((int)httpStatusCode).ToString(), out var result)) diff --git a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs index 4d6150a128..433fea304c 100644 --- a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs +++ b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs @@ -173,6 +173,10 @@ public async Task GetHttpResponseMessageAsync(string url, o /// /// /// 如果为 GET 请求,此参数可为 null + /// + /// + /// + /// /// public async Task RequestAsync(string url, object data, int timeOut = Config.TIME_OUT, ApiRequestMethod requestMethod = ApiRequestMethod.POST, bool checkSign = true, Func createDefaultInstance = null) where T : ReturnJsonBase/*, new()*/ From 96539867e70c0e3e33bbaa602b5f0beed5cd52b7 Mon Sep 17 00:00:00 2001 From: JeffreySu Date: Sun, 29 Jan 2023 16:24:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?v0.6.8.12=20=E4=BF=AE=E5=A4=8D=E9=AA=8C?= =?UTF-8?q?=E7=AD=BE=E6=97=B6=20204=EF=BC=88NoContent=EF=BC=89=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8B=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Apis/Entities/ReturnJsonBase.cs | 3 +- .../Apis/Entities/TenPayApiResultCode.cs | 8 ++- .../HttpHandlers/TenPayApiRequest.cs | 52 +++++++++++-------- .../Senparc.Weixin.TenPayV3.net7.csproj | 37 ++++++------- 4 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/ReturnJsonBase.cs b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/ReturnJsonBase.cs index fd41ce5ca4..0d62951f31 100644 --- a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/ReturnJsonBase.cs +++ b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/ReturnJsonBase.cs @@ -50,7 +50,8 @@ public class ReturnJsonBase /// /// 回复签名是否正确 在有错误的情况下,或不要求验证签名时 为null /// 通常情况下,必须为 true 才表明签名通过 + /// 注意:在 204(NoContent) 的 下,此属性始终为 true /// - public bool? VerifySignSuccess { get; set; } = null; + public bool? VerifySignSuccess { get; set; } = null; } } diff --git a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/TenPayApiResultCode.cs b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/TenPayApiResultCode.cs index b49fc5a412..9fc8b7b083 100644 --- a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/TenPayApiResultCode.cs +++ b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/Entities/TenPayApiResultCode.cs @@ -123,7 +123,7 @@ public record class TenPayApiResultCode /// public static TenPayApiResultCode TryGetCode(HttpStatusCode httpStatusCode, string responseContent) { - responseContent ??= "{}"; + //responseContent ??= "{}"; TenPayApiResultCode resultCode = null; @@ -187,7 +187,13 @@ public static TenPayApiResultCode TryGetCode(HttpStatusCode httpStatusCode, stri return resultCode; } + /// + /// 是否为成功消息 + /// public bool Success { get; protected set; } = false; + /// + /// HttpStatusCode + /// public string StateCode { get; protected set; } public string ErrorCode { get; protected set; } public string ErrorMessage { get; internal set; } diff --git a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs index 433fea304c..29cfbb24a5 100644 --- a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs +++ b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs @@ -44,6 +44,7 @@ and limitations under the License. using Senparc.Weixin.TenPayV3.Helpers; using System; using System.Linq; +using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Security.Cryptography; @@ -195,40 +196,47 @@ public async Task RequestAsync(string url, object data, int timeOut = Conf #endif //检查响应代码 - TenPayApiResultCode resutlCode = TenPayApiResultCode.TryGetCode(responseMessage.StatusCode, content); + TenPayApiResultCode resultCode = TenPayApiResultCode.TryGetCode(responseMessage.StatusCode, content); - if (resutlCode.Success) + if (resultCode.Success) { - //TODO:待测试 - //验证微信签名 - //result.Signed = VerifyTenpaySign(responseMessage.Headers, content); - var wechatpayTimestamp = responseMessage.Headers.GetValues("Wechatpay-Timestamp").First(); - var wechatpayNonce = responseMessage.Headers.GetValues("Wechatpay-Nonce").First(); - var wechatpaySignatureBase64 = responseMessage.Headers.GetValues("Wechatpay-Signature").First();//后续需要base64解码 - var wechatpaySerial = responseMessage.Headers.GetValues("Wechatpay-Serial").First(); + if (resultCode.StateCode == ((int)HttpStatusCode.NoContent).ToString()) + { + result.VerifySignSuccess = true; + } + else + { + //TODO:待测试 + //验证微信签名 + //result.Signed = VerifyTenpaySign(responseMessage.Headers, content); + var wechatpayTimestamp = responseMessage.Headers.GetValues("Wechatpay-Timestamp").First(); + var wechatpayNonce = responseMessage.Headers.GetValues("Wechatpay-Nonce").First(); + var wechatpaySignatureBase64 = responseMessage.Headers.GetValues("Wechatpay-Signature").First();//后续需要base64解码 + var wechatpaySerial = responseMessage.Headers.GetValues("Wechatpay-Serial").First(); - result = content.GetObject(); + result = content.GetObject(); - if (checkSign) - { - try - { - var pubKey = await TenPayV3InfoCollection.GetAPIv3PublicKeyAsync(this._tenpayV3Setting, wechatpaySerial); - result.VerifySignSuccess = TenPaySignHelper.VerifyTenpaySign(wechatpayTimestamp, wechatpayNonce, wechatpaySignatureBase64, content, pubKey); - } - catch (Exception ex) + if (checkSign) { - throw new TenpayApiRequestException("RequestAsync 签名验证失败:" + ex.Message, ex); + try + { + var pubKey = await TenPayV3InfoCollection.GetAPIv3PublicKeyAsync(this._tenpayV3Setting, wechatpaySerial); + result.VerifySignSuccess = TenPaySignHelper.VerifyTenpaySign(wechatpayTimestamp, wechatpayNonce, wechatpaySignatureBase64, content, pubKey); + } + catch (Exception ex) + { + throw new TenpayApiRequestException("RequestAsync 签名验证失败:" + ex.Message, ex); + } } } } else { result = createDefaultInstance?.Invoke() ?? GetInstance(true); - resutlCode.Additional = content; + resultCode.Additional = content; } - //T result = resutlCode.Success ? (await responseMessage.Content.ReadAsStringAsync()).GetObject() : new T(); - result.ResultCode = resutlCode; + //T result = resultCode.Success ? (await responseMessage.Content.ReadAsStringAsync()).GetObject() : new T(); + result.ResultCode = resultCode; return result; } diff --git a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Senparc.Weixin.TenPayV3.net7.csproj b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Senparc.Weixin.TenPayV3.net7.csproj index 729d82e7e7..3931906182 100644 --- a/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Senparc.Weixin.TenPayV3.net7.csproj +++ b/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Senparc.Weixin.TenPayV3.net7.csproj @@ -1,7 +1,7 @@ netstandard2.1 - 0.6.8.11 + 0.6.8.12 Senparc.Weixin.TenPayV3 Senparc.Weixin.TenPayV3 10.0 @@ -23,23 +23,24 @@ https://github.com/JeffreySu/WeiXinMPSDK icon.jpg - v0.1.0 创世 - v0.3.500.2 重构加密方法 - v0.3.500.3 修正合单支付的 URL 路径错误 - v0.3.500.4 完成商家券接口、委托营销接口、消费卡接口、支付有礼接口 - v0.5.1 修复PayV3营销工具商户券API - v0.5.6 使用 Senparc.Weixin.Config.TenPayV3Host 提供可配置的 API 域名 - v0.5.7 升级微信支付请求的方法,支持多种加密方式 - v0.6.1 修复 CloseOrderAsync() 参数问题 - v0.6.2.2 修复 TenPayHttpClient 赋值问题 - v0.6.3 添加“发起商家转账API” - v0.6.5 重构 BasePayApis.GetPayApiUrl() 方法 - v0.6.8.2 MarketingApis.ModifyBusifavorStockInformationAsync 方法单独提取参数 stock_id - v0.6.8.3 MarketingApis.ModifyBusifavorStockBudgetAsync 方法单独提取参数 stock_id - v0.6.8.4 修改 week_day 类型为 int[] - v0.6.8.7 优化 TenPayApiResultCode 获取逻辑,修复 TryGetCode() 方法中当匹配不到预设错误信息时,返回 null 的问题 - v0.6.8.8 修复 RefundQueryAsync() URL 问题 - + v0.1.0 创世 + v0.3.500.2 重构加密方法 + v0.3.500.3 修正合单支付的 URL 路径错误 + v0.3.500.4 完成商家券接口、委托营销接口、消费卡接口、支付有礼接口 + v0.5.1 修复PayV3营销工具商户券API + v0.5.6 使用 Senparc.Weixin.Config.TenPayV3Host 提供可配置的 API 域名 + v0.5.7 升级微信支付请求的方法,支持多种加密方式 + v0.6.1 修复 CloseOrderAsync() 参数问题 + v0.6.2.2 修复 TenPayHttpClient 赋值问题 + v0.6.3 添加“发起商家转账API” + v0.6.5 重构 BasePayApis.GetPayApiUrl() 方法 + v0.6.8.2 MarketingApis.ModifyBusifavorStockInformationAsync 方法单独提取参数 stock_id + v0.6.8.3 MarketingApis.ModifyBusifavorStockBudgetAsync 方法单独提取参数 stock_id + v0.6.8.4 修改 week_day 类型为 int[] + v0.6.8.7 优化 TenPayApiResultCode 获取逻辑,修复 TryGetCode() 方法中当匹配不到预设错误信息时,返回 null 的问题 + v0.6.8.8 修复 RefundQueryAsync() URL 问题 + v0.6.8.12 修复验签时 204(NoContent)情况下的异常 + https://github.com/JeffreySu/WeiXinMPSDK