Browse Source

LianLianPay 实现(#26)

Roc 6 years ago
parent
commit
d729d25c73

+ 10 - 7
samples/WebApplicationSample/Controllers/LianLianPayController.cs

@@ -3,6 +3,7 @@ using Essensoft.AspNetCore.Payment.LianLianPay;
 using Essensoft.AspNetCore.Payment.LianLianPay.Notify;
 using Essensoft.AspNetCore.Payment.LianLianPay.Request;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Options;
 using WebApplicationSample.Models;
 
 namespace WebApplicationSample.Controllers
@@ -11,11 +12,13 @@ namespace WebApplicationSample.Controllers
     {
         private readonly ILianLianPayClient _client;
         private readonly ILianLianPayNotifyClient _notifyClient;
+        private readonly IOptions<LianLianPayOptions> _optionsAccessor;
 
-        public LianLianPayController(ILianLianPayClient client, ILianLianPayNotifyClient notifyClient)
+        public LianLianPayController(ILianLianPayClient client, ILianLianPayNotifyClient notifyClient, IOptions<LianLianPayOptions> optionsAccessor)
         {
             _client = client;
             _notifyClient = notifyClient;
+            _optionsAccessor = optionsAccessor;
         }
 
         /// <summary>
@@ -58,7 +61,7 @@ namespace WebApplicationSample.Controllers
                 FlagPayProduct = "0",
                 FlagChnl = "2"
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, _optionsAccessor.Value);
             return Redirect(response.GatewayUrl);
         }
 
@@ -93,7 +96,7 @@ namespace WebApplicationSample.Controllers
                 FlagPayProduct = "0",
                 FlagChnl = "3"
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, _optionsAccessor.Value);
             return Redirect(response.GatewayUrl);
         }
 
@@ -121,7 +124,7 @@ namespace WebApplicationSample.Controllers
                 DtOrder = viewModel.DtOrder,
                 OidPayBill = viewModel.OidPayBill
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, _optionsAccessor.Value);
             ViewData["response"] = response.Body;
             return View();
         }
@@ -154,7 +157,7 @@ namespace WebApplicationSample.Controllers
                 OidPaybill = viewModel.OidPayBill,
                 NotifyUrl = viewModel.NotifyUrl
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, _optionsAccessor.Value);
             ViewData["response"] = response.Body;
             return View();
         }
@@ -183,7 +186,7 @@ namespace WebApplicationSample.Controllers
                 DtRefund = viewModel.DtRefund,
                 OidRefundNo = viewModel.OidRefundNo
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, _optionsAccessor.Value);
             ViewData["response"] = response.Body;
             return View();
         }
@@ -197,7 +200,7 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
-                var notify = await _notifyClient.ExecuteAsync<LianLianPayReceiveMoneyReturnResponse>(Request);
+                var notify = await _notifyClient.ExecuteAsync<LianLianPayReceiveMoneyReturnResponse>(Request, _optionsAccessor.Value);
                 ViewData["response"] = "支付成功";
                 return View();
             }

+ 5 - 3
samples/WebApplicationSample/Controllers/NotifyController.cs

@@ -367,10 +367,12 @@ namespace WebApplicationSample.Controllers
     public class LianLianPayNotifyController : Controller
     {
         private readonly ILianLianPayNotifyClient _client;
+        private readonly IOptions<LianLianPayOptions> _optionsAccessor;
 
-        public LianLianPayNotifyController(ILianLianPayNotifyClient client)
+        public LianLianPayNotifyController(ILianLianPayNotifyClient client, IOptions<LianLianPayOptions> optionsAccessor)
         {
             _client = client;
+            _optionsAccessor = optionsAccessor;
         }
 
         [Route("receivemoney")]
@@ -379,7 +381,7 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
-                var notify = await _client.ExecuteAsync<LianLianPayReceiveMoneyNotify>(Request);
+                var notify = await _client.ExecuteAsync<LianLianPayReceiveMoneyNotify>(Request, _optionsAccessor.Value);
                 Console.WriteLine("NoOrder: " + notify.NoOrder);
                 return LianLianPayNotifyResult.Success;
             }
@@ -395,7 +397,7 @@ namespace WebApplicationSample.Controllers
         {
             try
             {
-                var notify = await _client.ExecuteAsync<LianLianPayRefundNotify>(Request);
+                var notify = await _client.ExecuteAsync<LianLianPayRefundNotify>(Request, _optionsAccessor.Value);
                 Console.WriteLine("NoRefund: " + notify.NoRefund);
                 return LianLianPayNotifyResult.Success;
             }

+ 2 - 10
src/Essensoft.AspNetCore.Payment.LianLianPay/ILianLianPayClient.cs

@@ -12,16 +12,8 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay
         /// </summary>
         /// <typeparam name="T">领域对象</typeparam>
         /// <param name="request">具体的LianLianPay API请求</param>
+        /// <param name="options">配置选项</param>
         /// <returns>领域对象</returns>
-        Task<T> ExecuteAsync<T>(ILianLianPayRequest<T> request) where T : LianLianPayResponse;
-
-        /// <summary>
-        /// 执行LianLianPay API请求。
-        /// </summary>
-        /// <typeparam name="T">领域对象</typeparam>
-        /// <param name="request">具体的LianLianPay API请求</param>
-        /// <param name="optionsName">配置选项名称</param>
-        /// <returns>领域对象</returns>
-        Task<T> ExecuteAsync<T>(ILianLianPayRequest<T> request, string optionsName) where T : LianLianPayResponse;
+        Task<T> ExecuteAsync<T>(ILianLianPayRequest<T> request, LianLianPayOptions options) where T : LianLianPayResponse;
     }
 }

+ 2 - 10
src/Essensoft.AspNetCore.Payment.LianLianPay/ILianLianPayNotifyClient.cs

@@ -13,16 +13,8 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay
         /// </summary>
         /// <typeparam name="T">领域对象</typeparam>
         /// <param name="request">控制器的请求</param>
+        /// <param name="options">配置选项</param>
         /// <returns>领域对象</returns>
-        Task<T> ExecuteAsync<T>(HttpRequest request) where T : LianLianPayNotify;
-
-        /// <summary>
-        /// 执行LianLianPay通知请求解析。
-        /// </summary>
-        /// <typeparam name="T">领域对象</typeparam>
-        /// <param name="request">控制器的请求</param>
-        /// <param name="optionsName">配置选项名称</param>
-        /// <returns>领域对象</returns>
-        Task<T> ExecuteAsync<T>(HttpRequest request, string optionsName) where T : LianLianPayNotify;
+        Task<T> ExecuteAsync<T>(HttpRequest request, LianLianPayOptions options) where T : LianLianPayNotify;
     }
 }

+ 6 - 23
src/Essensoft.AspNetCore.Payment.LianLianPay/LianLianPayClient.cs

@@ -7,12 +7,11 @@ using Essensoft.AspNetCore.Payment.LianLianPay.Request;
 using Essensoft.AspNetCore.Payment.LianLianPay.Utility;
 using Essensoft.AspNetCore.Payment.Security;
 using Microsoft.Extensions.Logging;
-using Microsoft.Extensions.Options;
 using Newtonsoft.Json;
 
 namespace Essensoft.AspNetCore.Payment.LianLianPay
 {
-    /// <summary>
+       /// <summary>
     /// LianLianPay 客户端。
     /// </summary>
     public class LianLianPayClient : ILianLianPayClient
@@ -23,34 +22,22 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay
         private const string TIME_STAMP = "time_stamp";
         private const string SIGN = "sign";
 
-        private readonly ILogger _logger;
-        private readonly IHttpClientFactory _clientFactory;
-        private readonly IOptionsSnapshot<LianLianPayOptions> _optionsSnapshotAccessor;
+        private readonly IHttpClientFactory _httpClientFactory;
 
         #region LianLianPayClient Constructors
 
         public LianLianPayClient(
-            ILogger<LianLianPayClient> logger,
-            IHttpClientFactory clientFactory,
-            IOptionsSnapshot<LianLianPayOptions> optionsAccessor)
+            IHttpClientFactory httpClientFactory)
         {
-            _logger = logger;
-            _clientFactory = clientFactory;
-            _optionsSnapshotAccessor = optionsAccessor;
+            _httpClientFactory = httpClientFactory;
         }
 
         #endregion
 
         #region ILianLianPayClient Members
 
-        public async Task<T> ExecuteAsync<T>(ILianLianPayRequest<T> request) where T : LianLianPayResponse
+        public async Task<T> ExecuteAsync<T>(ILianLianPayRequest<T> request, LianLianPayOptions options) where T : LianLianPayResponse
         {
-            return await ExecuteAsync(request, null);
-        }
-
-        public async Task<T> ExecuteAsync<T>(ILianLianPayRequest<T> request, string optionsName) where T : LianLianPayResponse
-        {
-            var options = _optionsSnapshotAccessor.Get(optionsName);
             var txtParams = new LianLianPayDictionary(request.GetParameters())
             {
                 { OID_PARTNER, options.OidPartner },
@@ -79,13 +66,9 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay
                 content = Serialize(txtParams);
             }
 
-            _logger.Log(options.LogLevel, "Request:{content}", content);
-
-            using (var client = _clientFactory.CreateClient())
+            using (var client = _httpClientFactory.CreateClient(nameof(LianLianPay)))
             {
                 var body = await client.DoPostAsync(request.GetRequestUrl(), content);
-                _logger.Log(options.LogLevel, "Response:{body}", body);
-
                 var parser = new LianLianPayJsonParser<T>();
                 var rsp = parser.Parse(body);
 

+ 2 - 14
src/Essensoft.AspNetCore.Payment.LianLianPay/LianLianPayNotifyClient.cs

@@ -15,17 +15,10 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay
     /// </summary>
     public class LianLianPayNotifyClient : ILianLianPayNotifyClient
     {
-        private readonly ILogger _logger;
-        private readonly IOptionsSnapshot<LianLianPayOptions> _optionsSnapshotAccessor;
-
         #region LianLianPayNotifyClient Constructors
 
-        public LianLianPayNotifyClient(
-            ILogger<LianLianPayClient> logger,
-            IOptionsSnapshot<LianLianPayOptions> optionsAccessor)
+        public LianLianPayNotifyClient()
         {
-            _logger = logger;
-            _optionsSnapshotAccessor = optionsAccessor;
         }
 
         #endregion
@@ -37,15 +30,11 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay
             return await ExecuteAsync<T>(request, null);
         }
 
-        public async Task<T> ExecuteAsync<T>(HttpRequest request, string optionsName) where T : LianLianPayNotify
+        public async Task<T> ExecuteAsync<T>(HttpRequest request, LianLianPayOptions options) where T : LianLianPayNotify
         {
-            var options = _optionsSnapshotAccessor.Get(optionsName);
             if (request.HasFormContentType)
             {
                 var parameters = await GetParametersAsync(request);
-                var query = LianLianPayUtility.BuildQuery(parameters);
-                _logger.Log(options.LogLevel, "Request:{query}", query);
-
                 var parser = new LianLianPayDictionaryParser<T>();
                 var rsp = parser.Parse(parameters);
                 CheckNotifySign(parameters, options);
@@ -55,7 +44,6 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay
             if (request.HasTextJsonContentType())
             {
                 var body = await new StreamReader(request.Body).ReadToEndAsync();
-                _logger.Log(options.LogLevel, "Request:{body}", body);
 
                 var parser = new LianLianPayJsonParser<T>();
                 var rsp = parser.Parse(body);

+ 0 - 5
src/Essensoft.AspNetCore.Payment.LianLianPay/LianLianPayOptions.cs

@@ -62,10 +62,5 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay
         /// 签名方式
         /// </summary>
         public string SignType { get; } = "RSA";
-
-        /// <summary>
-        /// 日志等级
-        /// </summary>
-        public LogLevel LogLevel { get; set; } = LogLevel.Information;
     }
 }

+ 5 - 2
src/Essensoft.AspNetCore.Payment.LianLianPay/ServiceCollectionExtensions.cs

@@ -15,8 +15,11 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay
             this IServiceCollection services,
             Action<LianLianPayOptions> setupAction)
         {
-            services.AddScoped<ILianLianPayClient, LianLianPayClient>();
-            services.AddScoped<ILianLianPayNotifyClient, LianLianPayNotifyClient>();
+            services.AddHttpClient(nameof(LianLianPayClient));
+
+            services.AddSingleton<ILianLianPayClient, LianLianPayClient>();
+            services.AddSingleton<ILianLianPayNotifyClient, LianLianPayNotifyClient>();
+
             if (setupAction != null)
             {
                 services.Configure(setupAction);