فهرست منبع

[示例] 更新

Roc 7 سال پیش
والد
کامیت
0e9b9c0c1f

+ 5 - 5
samples/WebApplicationSample/Controllers/AlipayController.cs

@@ -1,19 +1,19 @@
-using Essensoft.AspNetCore.Payment.Alipay;
+using System.Threading.Tasks;
+using Essensoft.AspNetCore.Payment.Alipay;
 using Essensoft.AspNetCore.Payment.Alipay.Domain;
 using Essensoft.AspNetCore.Payment.Alipay.Notify;
 using Essensoft.AspNetCore.Payment.Alipay.Request;
 using Microsoft.AspNetCore.Mvc;
 using WebApplicationSample.Models;
-using System.Threading.Tasks;
 
 namespace WebApplicationSample.Controllers
 {
     public class AlipayController : Controller
     {
-        private readonly AlipayClient _client = null;
-        private readonly AlipayNotifyClient _notifyClient = null;
+        private readonly IAlipayClient _client = null;
+        private readonly IAlipayNotifyClient _notifyClient = null;
 
-        public AlipayController(AlipayClient client, AlipayNotifyClient notifyClient)
+        public AlipayController(IAlipayClient client, IAlipayNotifyClient notifyClient)
         {
             _client = client;
             _notifyClient = notifyClient;

+ 3 - 3
samples/WebApplicationSample/Controllers/JDPayController.cs

@@ -9,10 +9,10 @@ namespace WebApplicationSample.Controllers
 {
     public class JDPayController : Controller
     {
-        private readonly JDPayClient _client = null;
-        private readonly JDPayNotifyClient _notifyClient = null;
+        private readonly IJDPayClient _client = null;
+        private readonly IJDPayNotifyClient _notifyClient = null;
 
-        public JDPayController(JDPayClient client, JDPayNotifyClient notifyClient)
+        public JDPayController(IJDPayClient client, IJDPayNotifyClient notifyClient)
         {
             _client = client;
             _notifyClient = notifyClient;

+ 3 - 3
samples/WebApplicationSample/Controllers/LianLianPayController.cs

@@ -9,10 +9,10 @@ namespace WebApplicationSample.Controllers
 {
     public class LianLianPayController : Controller
     {
-        private readonly LianLianPayClient _client = null;
-        private readonly LianLianPayNotifyClient _notifyClient = null;
+        private readonly ILianLianPayClient _client = null;
+        private readonly ILianLianPayNotifyClient _notifyClient = null;
 
-        public LianLianPayController(LianLianPayClient client, LianLianPayNotifyClient notifyClient)
+        public LianLianPayController(ILianLianPayClient client, ILianLianPayNotifyClient notifyClient)
         {
             _client = client;
             _notifyClient = notifyClient;

+ 12 - 12
samples/WebApplicationSample/Controllers/NotifyController.cs

@@ -21,8 +21,8 @@ namespace WebApplicationSample.Controllers
     [Route("notify/alipay")]
     public class AlipayNotifyController : Controller
     {
-        private readonly AlipayNotifyClient _client = null;
-        public AlipayNotifyController(AlipayNotifyClient client)
+        private readonly IAlipayNotifyClient _client = null;
+        public AlipayNotifyController(IAlipayNotifyClient client)
         {
             _client = client;
         }
@@ -135,8 +135,8 @@ namespace WebApplicationSample.Controllers
     [Route("notify/wechatpay")]
     public class WeChatPayNotifyController : Controller
     {
-        private readonly WeChatPayNotifyClient _client = null;
-        public WeChatPayNotifyController(WeChatPayNotifyClient client)
+        private readonly IWeChatPayNotifyClient _client = null;
+        public WeChatPayNotifyController(IWeChatPayNotifyClient client)
         {
             _client = client;
         }
@@ -204,8 +204,8 @@ namespace WebApplicationSample.Controllers
     [Route("notify/qpay")]
     public class QPayNotifyController : Controller
     {
-        private readonly QPayNotifyClient _client = null;
-        public QPayNotifyController(QPayNotifyClient client)
+        private readonly IQPayNotifyClient _client = null;
+        public QPayNotifyController(IQPayNotifyClient client)
         {
             _client = client;
         }
@@ -266,8 +266,8 @@ namespace WebApplicationSample.Controllers
     [Route("notify/jdpay")]
     public class JDPayNotifyController : Controller
     {
-        private readonly JDPayNotifyClient _client = null;
-        public JDPayNotifyController(JDPayNotifyClient client)
+        private readonly IJDPayNotifyClient _client = null;
+        public JDPayNotifyController(IJDPayNotifyClient client)
         {
             _client = client;
         }
@@ -312,8 +312,8 @@ namespace WebApplicationSample.Controllers
     [Route("notify/lianlianpay")]
     public class LianLianPayNotifyController : Controller
     {
-        private readonly LianLianPayNotifyClient _client = null;
-        public LianLianPayNotifyController(LianLianPayNotifyClient client)
+        private readonly ILianLianPayNotifyClient _client = null;
+        public LianLianPayNotifyController(ILianLianPayNotifyClient client)
         {
             _client = client;
         }
@@ -358,8 +358,8 @@ namespace WebApplicationSample.Controllers
     [Route("notify/unionpay")]
     public class UnionPayNotifyController : Controller
     {
-        private readonly UnionPayNotifyClient _client = null;
-        public UnionPayNotifyController(UnionPayNotifyClient client)
+        private readonly IUnionPayNotifyClient _client = null;
+        public UnionPayNotifyController(IUnionPayNotifyClient client)
         {
             _client = client;
         }

+ 6 - 6
samples/WebApplicationSample/Controllers/QPayController.cs

@@ -1,15 +1,15 @@
-using Essensoft.AspNetCore.Payment.QPay;
+using System.Threading.Tasks;
+using Essensoft.AspNetCore.Payment.QPay;
 using Essensoft.AspNetCore.Payment.QPay.Request;
 using Microsoft.AspNetCore.Mvc;
 using WebApplicationSample.Models;
-using System.Threading.Tasks;
 
 namespace WebApplicationSample.Controllers
 {
     public class QPayController : Controller
     {
-        private readonly QPayClient _client = null;
-        public QPayController(QPayClient client)
+        private readonly IQPayClient _client = null;
+        public QPayController(IQPayClient client)
         {
             _client = client;
         }
@@ -150,7 +150,7 @@ namespace WebApplicationSample.Controllers
             {
                 OutTradeNo = viewModel.OutTradeNo,
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, "qpayCertificateName");
             ViewData["response"] = response.Body;
             return View();
         }
@@ -192,7 +192,7 @@ namespace WebApplicationSample.Controllers
                 OpUserId = viewModel.OpUserId,
                 OpUserPasswd = viewModel.OpUserPasswd,
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, "qpayCertificateName");
             ViewData["response"] = response.Body;
             return View();
         }

+ 3 - 3
samples/WebApplicationSample/Controllers/UnionPayController.cs

@@ -9,10 +9,10 @@ namespace WebApplicationSample.Controllers
 {
     public class UnionPayController : Controller
     {
-        private readonly UnionPayClient _client = null;
-        private readonly UnionPayNotifyClient _notifyClient = null;
+        private readonly IUnionPayClient _client = null;
+        private readonly IUnionPayNotifyClient _notifyClient = null;
 
-        public UnionPayController(UnionPayClient client, UnionPayNotifyClient notifyClient)
+        public UnionPayController(IUnionPayClient client, IUnionPayNotifyClient notifyClient)
         {
             _client = client;
             _notifyClient = notifyClient;

+ 13 - 14
samples/WebApplicationSample/Controllers/WeChatPayController.cs

@@ -1,17 +1,16 @@
-using Essensoft.AspNetCore.Payment.WeChatPay;
+using System.Threading.Tasks;
+using Essensoft.AspNetCore.Payment.WeChatPay;
 using Essensoft.AspNetCore.Payment.WeChatPay.Request;
 using Microsoft.AspNetCore.Mvc;
 using Newtonsoft.Json;
 using WebApplicationSample.Models;
-using System.Threading.Tasks;
 
 namespace WebApplicationSample.Controllers
 {
     public class WeChatPayController : Controller
     {
-        private readonly WeChatPayClient _client = null;
-
-        public WeChatPayController(WeChatPayClient client)
+        private readonly IWeChatPayClient _client = null;
+        public WeChatPayController(IWeChatPayClient client)
         {
             _client = client;
         }
@@ -159,7 +158,7 @@ namespace WebApplicationSample.Controllers
                 TradeType = viewModel.TradeType,
             };
             var response = await _client.ExecuteAsync(request);
-            
+
             // mweb_url为拉起微信支付收银台的中间页面,可通过访问该url来拉起微信客户端,完成支付,mweb_url的有效期为5分钟。
             return Redirect(response.MwebUrl);
         }
@@ -234,7 +233,7 @@ namespace WebApplicationSample.Controllers
                 TransactionId = viewModel.TransactionId,
                 OutTradeNo = viewModel.OutTradeNo,
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, "wechatpayCertificateName");
             ViewData["response"] = response.Body;
             return View();
         }
@@ -276,7 +275,7 @@ namespace WebApplicationSample.Controllers
                 RefundDesc = viewModel.RefundDesc,
                 NotifyUrl = viewModel.NotifyUrl,
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, "wechatpayCertificateName");
             ViewData["response"] = response.Body;
             return View();
         }
@@ -337,7 +336,7 @@ namespace WebApplicationSample.Controllers
                 AccountType = viewModel.AccountType,
                 TarType = viewModel.TarType,
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, "wechatpayCertificateName");
             ViewData["response"] = response.Body;
             return View();
         }
@@ -361,7 +360,7 @@ namespace WebApplicationSample.Controllers
                 Desc = viewModel.Desc,
                 SpbillCreateIp = viewModel.SpbillCreateIp
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, "wechatpayCertificateName");
             ViewData["response"] = response.Body;
             return View();
         }
@@ -379,7 +378,7 @@ namespace WebApplicationSample.Controllers
             {
                 PartnerTradeNo = viewModel.PartnerTradeNo,
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, "wechatpayCertificateName");
             ViewData["response"] = response.Body;
             return View();
         }
@@ -402,7 +401,7 @@ namespace WebApplicationSample.Controllers
                 Amount = viewModel.Amount,
                 Desc = viewModel.Desc,
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, "wechatpayCertificateName");
             ViewData["response"] = response.Body;
             return View();
         }
@@ -420,7 +419,7 @@ namespace WebApplicationSample.Controllers
             {
                 PartnerTradeNo = viewModel.PartnerTradeNo,
             };
-            var response = await _client.ExecuteAsync(request);
+            var response = await _client.ExecuteAsync(request, "wechatpayCertificateName");
             ViewData["response"] = response.Body;
             return View();
         }
@@ -432,7 +431,7 @@ namespace WebApplicationSample.Controllers
             if (Request.Method == "POST")
             {
                 var request = new WeChatPayGetPublicKeyRequest();
-                var response = await _client.ExecuteAsync(request);
+                var response = await _client.ExecuteAsync(request, "wechatpayCertificateName");
                 ViewData["response"] = response.Body;
                 return View();
             }

+ 25 - 20
samples/WebApplicationSample/Startup.cs

@@ -1,6 +1,4 @@
-using System;
-using System.Security.Cryptography.X509Certificates;
-using System.Text.Encodings.Web;
+using System.Text.Encodings.Web;
 using System.Text.Unicode;
 using Essensoft.AspNetCore.Payment.Alipay;
 using Essensoft.AspNetCore.Payment.JDPay;
@@ -37,30 +35,35 @@ namespace WebApplicationSample
                 options.MinimumSameSitePolicy = SameSiteMode.None;
             });
 
-            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
-
+            // 引入HttpClient
+            services.AddHttpClient();
+
+            //引入HttpClient API证书的使用(仅QPay / WeChatPay的部分API使用到)。
+            //services.AddHttpClient("qpayCertificateName").ConfigurePrimaryHttpMessageHandler(() =>
+            //{
+            //    var certificate = new X509Certificate2("", "", X509KeyStorageFlags.MachineKeySet);
+            //    var handler = new HttpClientHandler();
+            //    handler.ClientCertificates.Add(certificate);
+            //    return handler;
+            //});
+
+            //services.AddHttpClient("wechatpayCertificateName").ConfigurePrimaryHttpMessageHandler(() =>
+            //{
+            //    var certificate = new X509Certificate2("", "", X509KeyStorageFlags.MachineKeySet);
+            //    var handler = new HttpClientHandler();
+            //    handler.ClientCertificates.Add(certificate);
+            //    return handler;
+            //});
+
+            // 引入Payment 依赖注入
             services.AddAlipay();
-            services.AddAlipayHttpClient();
-
             services.AddJDPay();
-            services.AddJDPayHttpClient();
-
             services.AddQPay();
-            services.AddQPayHttpClient();
-            //services.AddQPayCertificateHttpClient(new X509Certificate2(Convert.FromBase64String(Configuration["QPay:Certificate"]), Configuration["QPay:MchId"], X509KeyStorageFlags.MachineKeySet));
-            //services.AddQPayCertificateHttpClient("Default", new X509Certificate2(Convert.FromBase64String(Configuration["QPay:Certificate"]), Configuration["QPay:MchId"], X509KeyStorageFlags.MachineKeySet));
-
             services.AddUnionPay();
-            services.AddUnionPayHttpClient();
-
             services.AddWeChatPay();
-            services.AddWeChatPayHttpClient();
-            //services.AddWeChatPayCertificateHttpClient(new X509Certificate2(Convert.FromBase64String(Configuration["WeChatPay:Certificate"]), Configuration["WeChatPay:MchId"], X509KeyStorageFlags.MachineKeySet));
-            //services.AddWeChatPayCertificateHttpClient("Default", new X509Certificate2(Convert.FromBase64String(Configuration["WeChatPay:Certificate"]), Configuration["WeChatPay:MchId"], X509KeyStorageFlags.MachineKeySet));
-
             services.AddLianLianPay();
-            services.AddLianLianPayHttpClient();
 
+            // 在 appsettings.json 中 配置选项
             services.Configure<AlipayOptions>(Configuration.GetSection("Alipay"));
             services.Configure<JDPayOptions>(Configuration.GetSection("JDPay"));
             services.Configure<QPayOptions>(Configuration.GetSection("QPay"));
@@ -72,6 +75,8 @@ namespace WebApplicationSample
             {
                 opt.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
             });
+
+            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.