1
0
Roc 6 жил өмнө
parent
commit
81955835d5

+ 2 - 19
samples/WebApplicationSample/Controllers/AlipayController.cs

@@ -1,4 +1,3 @@
-using System.Collections.Generic;
 using System.Text;
 using System.Threading.Tasks;
 using Essensoft.AspNetCore.Payment.Alipay;
@@ -405,15 +404,7 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
-                var parameters = new Dictionary<string, string>();
-                {
-                    foreach (var iter in Request.Query)
-                    {
-                        parameters.Add(iter.Key, iter.Value);
-                    }
-                }
-
-                var notify = await _notifyClient.ExecuteAsync<AlipayTradePagePayReturn>(parameters, _optionsAccessor.Value);
+                var notify = await _notifyClient.ExecuteAsync<AlipayTradePagePayReturn>(Request, _optionsAccessor.Value);
                 ViewData["response"] = "支付成功";
                 return View();
             }
@@ -433,15 +424,7 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
-                var parameters = new Dictionary<string, string>();
-                {
-                    foreach (var iter in Request.Query)
-                    {
-                        parameters.Add(iter.Key, iter.Value);
-                    }
-                }
-
-                var notify = await _notifyClient.ExecuteAsync<AlipayTradeWapPayReturn>(parameters, _optionsAccessor.Value);
+                var notify = await _notifyClient.ExecuteAsync<AlipayTradeWapPayReturn>(Request, _optionsAccessor.Value);
                 ViewData["response"] = "支付成功";
                 return View();
             }

+ 4 - 29
samples/WebApplicationSample/Controllers/NotifyController.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Collections.Generic;
 using System.Threading.Tasks;
 using Essensoft.AspNetCore.Payment.Alipay;
 using Essensoft.AspNetCore.Payment.Alipay.Notify;
@@ -42,13 +41,7 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
-                var parameters = new Dictionary<string, string>();
-                foreach (var iter in Request.Form)
-                {
-                    parameters.Add(iter.Key, iter.Value);
-                }
-
-                var notify = await _client.ExecuteAsync<AlipayTradePrecreateNotify>(parameters, _optionsAccessor.Value);
+                var notify = await _client.ExecuteAsync<AlipayTradePrecreateNotify>(Request, _optionsAccessor.Value);
                 if ("TRADE_SUCCESS" == notify.TradeStatus)
                 {
                     Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
@@ -73,13 +66,7 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
-                var parameters = new Dictionary<string, string>();
-                foreach (var iter in Request.Form)
-                {
-                    parameters.Add(iter.Key, iter.Value);
-                }
-
-                var notify = await _client.ExecuteAsync<AlipayTradeAppPayNotify>(parameters, _optionsAccessor.Value);
+                var notify = await _client.ExecuteAsync<AlipayTradeAppPayNotify>(Request, _optionsAccessor.Value);
                 if ("TRADE_SUCCESS" == notify.TradeStatus)
                 {
                     Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
@@ -104,13 +91,7 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
-                var parameters = new Dictionary<string, string>();
-                foreach (var iter in Request.Form)
-                {
-                    parameters.Add(iter.Key, iter.Value);
-                }
-
-                var notify = await _client.ExecuteAsync<AlipayTradePagePayNotify>(parameters, _optionsAccessor.Value);
+                var notify = await _client.ExecuteAsync<AlipayTradePagePayNotify>(Request, _optionsAccessor.Value);
                 if ("TRADE_SUCCESS" == notify.TradeStatus)
                 {
                     Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
@@ -135,13 +116,7 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
-                var parameters = new Dictionary<string, string>();
-                foreach (var iter in Request.Form)
-                {
-                    parameters.Add(iter.Key, iter.Value);
-                }
-
-                var notify = await _client.ExecuteAsync<AlipayTradeWapPayNotify>(parameters, _optionsAccessor.Value);
+                var notify = await _client.ExecuteAsync<AlipayTradeWapPayNotify>(Request, _optionsAccessor.Value);
                 if ("TRADE_SUCCESS" == notify.TradeStatus)
                 {
                     Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);

+ 23 - 1
src/Essensoft.AspNetCore.Payment.Alipay/AlipayNotifyClient.cs

@@ -4,6 +4,7 @@ using System.Text;
 using System.Threading.Tasks;
 using Essensoft.AspNetCore.Payment.Alipay.Parser;
 using Essensoft.AspNetCore.Payment.Alipay.Utility;
+using Microsoft.AspNetCore.Http;
 
 namespace Essensoft.AspNetCore.Payment.Alipay
 {
@@ -20,7 +21,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay
 
         #region IAlipayNotifyClient Members
 
-        public Task<T> ExecuteAsync<T>(IDictionary<string, string> parameters, AlipayOptions options) where T : AlipayNotify
+        public Task<T> ExecuteAsync<T>(HttpRequest request, AlipayOptions options) where T : AlipayNotify
         {
             if (options == null)
             {
@@ -37,6 +38,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay
                 throw new ArgumentNullException(nameof(options.AppPrivateKey));
             }
 
+            var parameters = GetParameters(request);
             var parser = new AlipayDictionaryParser<T>();
             var rsp = parser.Parse(parameters);
             CheckNotifySign(parameters, options);
@@ -47,6 +49,26 @@ namespace Essensoft.AspNetCore.Payment.Alipay
 
         #region Common Method
 
+        private Dictionary<string, string> GetParameters(HttpRequest request)
+        {
+            var parameters = new Dictionary<string, string>();
+            if (request.Method == "POST")
+            {
+                foreach (var iter in request.Form)
+                {
+                    parameters.Add(iter.Key, iter.Value);
+                }
+            }
+            else
+            {
+                foreach (var iter in request.Query)
+                {
+                    parameters.Add(iter.Key, iter.Value);
+                }
+            }
+            return parameters;
+        }
+
         private void CheckNotifySign(IDictionary<string, string> dictionary, AlipayOptions options)
         {
             if (dictionary == null || dictionary.Count == 0)

+ 4 - 4
src/Essensoft.AspNetCore.Payment.Alipay/IAlipayNotifyClient.cs

@@ -1,5 +1,5 @@
-using System.Collections.Generic;
-using System.Threading.Tasks;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Http;
 
 namespace Essensoft.AspNetCore.Payment.Alipay
 {
@@ -12,9 +12,9 @@ namespace Essensoft.AspNetCore.Payment.Alipay
         /// 执行 Alipay 通知请求解析。
         /// </summary>
         /// <typeparam name="T">领域对象</typeparam>
-        /// <param name="parameters">通知参数</param>
+        /// <param name="request">控制器的请求</param>
         /// <param name="options">配置选项</param>
         /// <returns>领域对象</returns>
-        Task<T> ExecuteAsync<T>(IDictionary<string, string> parameters, AlipayOptions options) where T : AlipayNotify;
+        Task<T> ExecuteAsync<T>(HttpRequest request, AlipayOptions options) where T : AlipayNotify;
     }
 }