Skip to content

Commit

Permalink
Merge pull request #112 from EasyAbp/failure-handling
Browse files Browse the repository at this point in the history
WeChat pay API request failure handling
  • Loading branch information
gdlcf88 committed Apr 13, 2024
2 parents 1514f42 + 6192482 commit 4c5ceb1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 43 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>3.1.0-preview.1</Version>
<Version>3.1.0-preview.2</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<DefaultWeChatPayApiRequester> _logger;

public DefaultWeChatPayApiRequester(IHttpClientFactory httpClientFactory,
public DefaultWeChatPayApiRequester(
IHttpClientFactory httpClientFactory,
IAbpWeChatPayOptionsProvider optionsProvider,
IWeChatPayAuthorizationGenerator authorizationGenerator)
IWeChatPayAuthorizationGenerator authorizationGenerator,
ILogger<DefaultWeChatPayApiRequester> logger)
{
_httpClientFactory = httpClientFactory;
_optionsProvider = optionsProvider;
_authorizationGenerator = authorizationGenerator;
_logger = logger;
}

public async Task<string> 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();
}
Expand All @@ -52,7 +56,8 @@ public async Task<string> RequestAsync(HttpMethod method, string url, string bod
return JsonConvert.DeserializeObject<TResponse>(responseString);
}

public async Task<HttpResponseMessage> RequestRawAsync(HttpMethod method, string url, string body = null, string mchId = null)
public async Task<HttpResponseMessage> RequestRawAsync(HttpMethod method, string url, string body = null,
string mchId = null)
{
var request = CreateRequest(method, url, body);

Expand Down Expand Up @@ -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;
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ namespace EasyAbp.Abp.WeChat.Pay.Services.ParametersModel;
public abstract class WeChatPayCommonErrorResponse
{
/// <summary>
/// 错误代码,具体定义可以参考 <see cref="BasicPaymentErrorCode"/> 命名空间下的常量定义。
/// 详细错误码,具体定义可以参考 <see cref="BasicPaymentErrorCode"/> 命名空间下的常量定义。
/// </summary>
[JsonProperty("code")]
public string Code { get; set; }

/// <summary>
/// 具体的错误信息
/// 错误描述,使用易理解的文字表示错误的原因
/// </summary>
[JsonProperty("message")]
public string Message { get; set; }

/// <summary>
/// 具体的错误信息。
/// </summary>
[JsonProperty("detail")]
public string Detail { get; set; }

public class InnerDetail
{
/// <summary>
Expand Down

0 comments on commit 4c5ceb1

Please sign in to comment.