From 61924829367a889d629c3295cdcc5efb87a5f536 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Sun, 14 Apr 2024 02:30:55 +0800 Subject: [PATCH] WeChat pay API request failure handling --- common.props | 2 +- .../DefaultWeChatPayApiRequester.cs | 27 ++++++++++------- .../Exceptions/CallWeChatPayApiException.cs | 29 ------------------- .../WeChatPayCommonErrorResponse.cs | 10 +++++-- 4 files changed, 25 insertions(+), 43 deletions(-) delete mode 100644 src/Pay/EasyAbp.Abp.WeChat.Pay/Exceptions/CallWeChatPayApiException.cs diff --git a/common.props b/common.props index 04865ec..40287f8 100644 --- a/common.props +++ b/common.props @@ -1,7 +1,7 @@ latest - 3.1.0-preview.1 + 3.1.0-preview.2 $(NoWarn);CS1591 true EasyAbp Team diff --git a/src/Pay/EasyAbp.Abp.WeChat.Pay/ApiRequests/DefaultWeChatPayApiRequester.cs b/src/Pay/EasyAbp.Abp.WeChat.Pay/ApiRequests/DefaultWeChatPayApiRequester.cs index 4f9fb26..34a39b9 100644 --- a/src/Pay/EasyAbp.Abp.WeChat.Pay/ApiRequests/DefaultWeChatPayApiRequester.cs +++ b/src/Pay/EasyAbp.Abp.WeChat.Pay/ApiRequests/DefaultWeChatPayApiRequester.cs @@ -4,9 +4,9 @@ using System.Text; using System.Threading.Tasks; using EasyAbp.Abp.WeChat.Common.Extensions; -using EasyAbp.Abp.WeChat.Pay.Exceptions; using EasyAbp.Abp.WeChat.Pay.Options; using EasyAbp.Abp.WeChat.Pay.Security; +using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Volo.Abp.DependencyInjection; @@ -24,22 +24,26 @@ public class DefaultWeChatPayApiRequester : IWeChatPayApiRequester, ITransientDe }; private readonly IHttpClientFactory _httpClientFactory; - private readonly IWeChatPayAuthorizationGenerator _authorizationGenerator; private readonly IAbpWeChatPayOptionsProvider _optionsProvider; + private readonly IWeChatPayAuthorizationGenerator _authorizationGenerator; + private readonly ILogger _logger; - public DefaultWeChatPayApiRequester(IHttpClientFactory httpClientFactory, + public DefaultWeChatPayApiRequester( + IHttpClientFactory httpClientFactory, IAbpWeChatPayOptionsProvider optionsProvider, - IWeChatPayAuthorizationGenerator authorizationGenerator) + IWeChatPayAuthorizationGenerator authorizationGenerator, + ILogger logger) { _httpClientFactory = httpClientFactory; _optionsProvider = optionsProvider; _authorizationGenerator = authorizationGenerator; + _logger = logger; } public async Task RequestAsync(HttpMethod method, string url, string body, string mchId = null) { var response = await RequestRawAsync(method, url, body, mchId); - await ValidateResponseAsync(response); + await LogFailureResponseAsync(response); return await response.Content.ReadAsStringAsync(); } @@ -52,7 +56,8 @@ public async Task RequestAsync(HttpMethod method, string u return JsonConvert.DeserializeObject(responseString); } - public async Task RequestRawAsync(HttpMethod method, string url, string body = null, string mchId = null) + public async Task RequestRawAsync(HttpMethod method, string url, string body = null, + string mchId = null) { var request = CreateRequest(method, url, body); @@ -115,18 +120,18 @@ private string HandleRequestObject(HttpMethod method, object body) return WeChatReflectionHelper.ConvertToQueryString(body); } - protected virtual Task ValidateResponseAsync(HttpResponseMessage responseMessage) + protected virtual async Task LogFailureResponseAsync(HttpResponseMessage responseMessage) { switch (responseMessage.StatusCode) { case HttpStatusCode.OK: - return Task.CompletedTask; case HttpStatusCode.Accepted: - return Task.CompletedTask; case HttpStatusCode.NoContent: - return Task.CompletedTask; + return; default: - throw new CallWeChatPayApiException("微信支付 API 调用失败,状态码为非 200。"); + _logger.LogError("微信支付接口调用失败,HTTP状态码:{StatusCode},返回内容:{Content}", + responseMessage.StatusCode, await responseMessage.Content.ReadAsStringAsync()); + break; } } } diff --git a/src/Pay/EasyAbp.Abp.WeChat.Pay/Exceptions/CallWeChatPayApiException.cs b/src/Pay/EasyAbp.Abp.WeChat.Pay/Exceptions/CallWeChatPayApiException.cs deleted file mode 100644 index 8a9a597..0000000 --- a/src/Pay/EasyAbp.Abp.WeChat.Pay/Exceptions/CallWeChatPayApiException.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Runtime.Serialization; -using Volo.Abp; -using Volo.Abp.ExceptionHandling; - -namespace EasyAbp.Abp.WeChat.Pay.Exceptions -{ - [Serializable] - public class CallWeChatPayApiException : AbpException, IHasErrorCode, IHasErrorDetails - { - public string Code { get; set; } - - public string Details { get; set; } - - public CallWeChatPayApiException() - { - } - - public CallWeChatPayApiException(SerializationInfo serializationInfo, - StreamingContext context) : base(serializationInfo, - context) - { - } - - public CallWeChatPayApiException(string message) : base(message) - { - } - } -} \ No newline at end of file diff --git a/src/Pay/EasyAbp.Abp.WeChat.Pay/Services/ParametersModel/WeChatPayCommonErrorResponse.cs b/src/Pay/EasyAbp.Abp.WeChat.Pay/Services/ParametersModel/WeChatPayCommonErrorResponse.cs index 75e0ebf..a363673 100644 --- a/src/Pay/EasyAbp.Abp.WeChat.Pay/Services/ParametersModel/WeChatPayCommonErrorResponse.cs +++ b/src/Pay/EasyAbp.Abp.WeChat.Pay/Services/ParametersModel/WeChatPayCommonErrorResponse.cs @@ -6,17 +6,23 @@ namespace EasyAbp.Abp.WeChat.Pay.Services.ParametersModel; public abstract class WeChatPayCommonErrorResponse { /// - /// 错误代码,具体定义可以参考 命名空间下的常量定义。 + /// 详细错误码,具体定义可以参考 命名空间下的常量定义。 /// [JsonProperty("code")] public string Code { get; set; } /// - /// 具体的错误信息。 + /// 错误描述,使用易理解的文字表示错误的原因。 /// [JsonProperty("message")] public string Message { get; set; } + /// + /// 具体的错误信息。 + /// + [JsonProperty("detail")] + public string Detail { get; set; } + public class InnerDetail { ///