Преглед изворни кода

WeChatPayV3Response 新增 StatusCode

Roc пре 5 година
родитељ
комит
5cb2d1b45d

+ 1 - 1
samples/WebApplicationSample/Controllers/WeChatPayController.cs

@@ -669,7 +669,7 @@ namespace WebApplicationSample.Controllers
 
             var response = await _client.ExecuteAsync(request, _optionsAccessor.Value);
 
-            if (!string.IsNullOrEmpty(response.PrepayId))
+            if (response.StatusCode == 200)
             {
                 var req = new WeChatPayAppSdkRequest
                 {

+ 1 - 1
src/Essensoft.AspNetCore.Payment.WeChatPay/Parser/IWeChatPayV3ResponseJsonParser.cs

@@ -2,6 +2,6 @@
 {
     public interface IWeChatPayV3ResponseJsonParser<T> where T : WeChatPayV3Response
     {
-        T Parse(string body);
+        T Parse(string body, int statusCode);
     }
 }

+ 2 - 1
src/Essensoft.AspNetCore.Payment.WeChatPay/Parser/WeChatPayV3ResponseJsonParser.cs

@@ -8,7 +8,7 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay.Parser
     {
         private static readonly JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions { IgnoreNullValues = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
 
-        public T Parse(string body)
+        public T Parse(string body, int statusCode)
         {
             T result = null;
 
@@ -27,6 +27,7 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay.Parser
             }
 
             result.Body = body;
+            result.StatusCode = statusCode;
             return result;
         }
     }

+ 6 - 4
src/Essensoft.AspNetCore.Payment.WeChatPay/Utility/HttpClientExtensions.cs

@@ -39,7 +39,7 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay.Utility
             }
         }
 
-        public static async Task<(string serial, string timestamp, string nonce, string signature, string body)> GetAsync<T>(this HttpClient client, IWeChatPayV3GetRequest<T> request, WeChatPayOptions options) where T : WeChatPayV3Response
+        public static async Task<(string serial, string timestamp, string nonce, string signature, string body, int statusCode)> GetAsync<T>(this HttpClient client, IWeChatPayV3GetRequest<T> request, WeChatPayOptions options) where T : WeChatPayV3Response
         {
             var url = request.GetRequestUrl();
             var authorization = BuildAuthorizationString(url, "GET", null, options);
@@ -57,12 +57,13 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay.Utility
                 var nonce = resp.Headers.GetValues(WeChatPayConsts.Wechatpay_Nonce).First();
                 var signature = resp.Headers.GetValues(WeChatPayConsts.Wechatpay_Signature).First();
                 var body = await respContent.ReadAsStringAsync();
+                var statusCode = (int)resp.StatusCode;
 
-                return (serial, timestamp, nonce, signature, body);
+                return (serial, timestamp, nonce, signature, body, statusCode);
             }
         }
 
-        public static async Task<(string serial, string timestamp, string nonce, string signature, string body)> PostAsync<T>(this HttpClient client, IWeChatPayV3PostRequest<T> request, WeChatPayOptions options) where T : WeChatPayV3Response
+        public static async Task<(string serial, string timestamp, string nonce, string signature, string body, int statusCode)> PostAsync<T>(this HttpClient client, IWeChatPayV3PostRequest<T> request, WeChatPayOptions options) where T : WeChatPayV3Response
         {
             var url = request.GetRequestUrl();
             var content = SerializeBizModel(request);
@@ -81,8 +82,9 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay.Utility
                 var nonce = resp.Headers.GetValues(WeChatPayConsts.Wechatpay_Nonce).First();
                 var signature = resp.Headers.GetValues(WeChatPayConsts.Wechatpay_Signature).First();
                 var body = await respContent.ReadAsStringAsync();
+                var statusCode = (int)resp.StatusCode;
 
-                return (serial, timestamp, nonce, signature, body);
+                return (serial, timestamp, nonce, signature, body, statusCode);
             }
         }
 

+ 4 - 4
src/Essensoft.AspNetCore.Payment.WeChatPay/WeChatPayClient.cs

@@ -227,9 +227,9 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay
             }
 
             var client = _httpClientFactory.CreateClient(nameof(WeChatPayClient));
-            var (serial, timestamp, nonce, signature, body) = await client.GetAsync(request, options);
+            var (serial, timestamp, nonce, signature, body, statusCode) = await client.GetAsync(request, options);
             var parser = new WeChatPayV3ResponseJsonParser<T>();
-            var response = parser.Parse(body);
+            var response = parser.Parse(body, statusCode);
 
             // 为下载微信支付平台证书证书响应时,
             if (response is WeChatPayCertificatesResponse resp)
@@ -282,9 +282,9 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay
             }
 
             var client = _httpClientFactory.CreateClient(nameof(WeChatPayClient));
-            var (serial, timestamp, nonce, signature, body) = await client.PostAsync(request, options);
+            var (serial, timestamp, nonce, signature, body, statusCode) = await client.PostAsync(request, options);
             var parser = new WeChatPayV3ResponseJsonParser<T>();
-            var response = parser.Parse(body);
+            var response = parser.Parse(body, statusCode);
 
             await CheckV3ResponseSignAsync(options, serial, timestamp, nonce, signature, body);
 

+ 6 - 0
src/Essensoft.AspNetCore.Payment.WeChatPay/WeChatPayV3Response.cs

@@ -9,5 +9,11 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay
         /// </summary>
         [JsonIgnore]
         public string Body { get; set; }
+
+        /// <summary>
+        /// HTTP状态码
+        /// </summary>
+        [JsonIgnore]
+        public int StatusCode { get; set; }
     }
 }