Skip to content

Commit

Permalink
Merge pull request #2775 from JeffreySu/Developer
Browse files Browse the repository at this point in the history
Developer
  • Loading branch information
JeffreySu authored Jan 29, 2023
2 parents 7cb7d97 + 9653986 commit 31c7a7d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public class ReturnJsonBase
/// <summary>
/// 回复签名是否正确 在有错误的情况下,或不要求验证签名时 为null
/// <para>通常情况下,必须为 true 才表明签名通过</para>
/// <para>注意:在 204(NoContent) 的 <see cref="System.Net.HttpStatusCode"/> 下,此属性始终为 true</para>
/// </summary>
public bool? VerifySignSuccess { get; set; } = null;
public bool? VerifySignSuccess { get; set; } = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ public record class TenPayApiResultCode
/// 尝试获取 HTTP 返回代码,并附带备注信息
/// </summary>
/// <param name="httpStatusCode"></param>
/// <param name="responseContent"></param>
/// <returns></returns>
public static TenPayApiResultCode TryGetCode(HttpStatusCode httpStatusCode, string responseContent)
{
//responseContent ??= "{}";

TenPayApiResultCode resultCode = null;

if (CodesCollection.TryGetValue(((int)httpStatusCode).ToString(), out var result))
Expand Down Expand Up @@ -184,7 +187,13 @@ public static TenPayApiResultCode TryGetCode(HttpStatusCode httpStatusCode, stri
return resultCode;
}

/// <summary>
/// 是否为成功消息
/// </summary>
public bool Success { get; protected set; } = false;
/// <summary>
/// HttpStatusCode
/// </summary>
public string StateCode { get; protected set; }
public string ErrorCode { get; protected set; }
public string ErrorMessage { get; internal set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -173,6 +174,10 @@ public async Task<HttpResponseMessage> GetHttpResponseMessageAsync(string url, o
/// <typeparam name="T"></typeparam>
/// <param name="url"></param>
/// <param name="data">如果为 GET 请求,此参数可为 null</param>
/// <param name="timeOut"></param>
/// <param name="requestMethod"></param>
/// <param name="checkSign"></param>
/// <param name="createDefaultInstance"></param>
/// <returns></returns>
public async Task<T> RequestAsync<T>(string url, object data, int timeOut = Config.TIME_OUT, ApiRequestMethod requestMethod = ApiRequestMethod.POST, bool checkSign = true, Func<T> createDefaultInstance = null)
where T : ReturnJsonBase/*, new()*/
Expand All @@ -191,40 +196,47 @@ public async Task<T> RequestAsync<T>(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<T>();
result = content.GetObject<T>();

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<T>(true);
resutlCode.Additional = content;
resultCode.Additional = content;
}
//T result = resutlCode.Success ? (await responseMessage.Content.ReadAsStringAsync()).GetObject<T>() : new T();
result.ResultCode = resutlCode;
//T result = resultCode.Success ? (await responseMessage.Content.ReadAsStringAsync()).GetObject<T>() : new T();
result.ResultCode = resultCode;

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.6.8.11</Version>
<Version>0.6.8.12</Version>
<AssemblyName>Senparc.Weixin.TenPayV3</AssemblyName>
<RootNamespace>Senparc.Weixin.TenPayV3</RootNamespace>
<LangVersion>10.0</LangVersion>
Expand All @@ -23,23 +23,24 @@
<PackageProjectUrl>https://github.com/JeffreySu/WeiXinMPSDK</PackageProjectUrl>
<PackageIcon>icon.jpg</PackageIcon>
<PackageReleaseNotes>
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 问题
</PackageReleaseNotes>
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)情况下的异常
</PackageReleaseNotes>
<RepositoryUrl>https://github.com/JeffreySu/WeiXinMPSDK</RepositoryUrl>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
Expand Down

0 comments on commit 31c7a7d

Please sign in to comment.