1
0
Эх сурвалжийг харах

添加支付宝网页支付回跳示例,

Roc 8 жил өмнө
parent
commit
016a7eac3d

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

@@ -3,19 +3,23 @@ using Essensoft.AspNetCore.Alipay.Domain;
 using Essensoft.AspNetCore.Alipay.Request;
 using Microsoft.AspNetCore.Mvc;
 using System.Threading.Tasks;
+using Essensoft.AspNetCore.Alipay.Notify;
 
 namespace WebApplicationSample.Controllers
 {
     public class AlipayController : Controller
     {
         public readonly AlipayClient _client = null;
-        public AlipayController(AlipayClient client)
+        private readonly AlipayNotifyClient _notifyClient = null;
+
+        public AlipayController(AlipayClient client, AlipayNotifyClient notifyClient)
         {
             _client = client;
+            _notifyClient = notifyClient;
         }
 
         [HttpPost]
-        public async Task<IActionResult> PagePay(string out_trade_no, string subject, string total_amount, string body, string product_code, string notify_url)
+        public async Task<IActionResult> PagePay(string out_trade_no, string subject, string total_amount, string body, string product_code, string notify_url, string return_url)
         {
             var model = new AlipayTradePagePayModel()
             {
@@ -28,6 +32,7 @@ namespace WebApplicationSample.Controllers
             var req = new AlipayTradePagePayRequest();
             req.SetBizModel(model);
             req.SetNotifyUrl(notify_url);
+            req.SetReturnUrl(return_url);
 
             var response = await _client.PageExecuteAsync(req, null, "GET");
             return Redirect(response.Body);
@@ -166,5 +171,20 @@ namespace WebApplicationSample.Controllers
             var response = await _client.ExecuteAsync(req);
             return Ok(response.Body);
         }
+
+
+        [HttpGet]
+        public IActionResult Return()
+        {
+            try
+            {
+                var notify = _notifyClient.Execute<AlipayTradePagePayReturnResponse>(Request);
+                return Content("success", "text/plain");
+            }
+            catch
+            {
+                return Content("error", "text/plain");
+            }
+        }
     }
 }

+ 0 - 20
samples/WebApplicationSample/Properties/PublishProfiles/FolderProfile.pubxml

@@ -1,20 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-此文件由 Web 项目的发布/打包过程使用。可以通过编辑此 MSBuild 文件
-自定义此过程的行为。为了解与此相关的更多内容,请访问 https://go.microsoft.com/fwlink/?LinkID=208121。 
--->
-<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <PropertyGroup>
-    <WebPublishMethod>FileSystem</WebPublishMethod>
-    <PublishProvider>FileSystem</PublishProvider>
-    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
-    <LastUsedPlatform>Any CPU</LastUsedPlatform>
-    <SiteUrlToLaunchAfterPublish />
-    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
-    <ExcludeApp_Data>False</ExcludeApp_Data>
-    <ProjectGuid>4e02282b-63b1-4ae9-8a41-28b5346b54a1</ProjectGuid>
-    <publishUrl>C:\Users\roc\Desktop\WebApplicationSample</publishUrl>
-    <DeleteExistingFiles>True</DeleteExistingFiles>
-    <TargetFramework>netcoreapp2.0</TargetFramework>
-  </PropertyGroup>
-</Project>

+ 2 - 2
samples/WebApplicationSample/Properties/launchSettings.json

@@ -21,7 +21,7 @@
       "environmentVariables": {
         "ASPNETCORE_ENVIRONMENT": "Development"
       },
-      "applicationUrl": "http://localhost:61476/"
+      "applicationUrl": "http://localhost:5000/"
     }
   }
-}
+}

+ 11 - 4
samples/WebApplicationSample/Views/Home/Index.cshtml

@@ -30,10 +30,13 @@
             </p>
             <h3>*注册绑定配置(参数配置3):</h3>
             <p>
-                services.Configure<AlipayOptions>(Configuration.GetSection("Alipay"));<br />
-                services.Configure<WeChatPayOptions>(Configuration.GetSection("WeChatPay"));<br />
-                services.Configure<QPayOptions>(Configuration.GetSection("QPay"));<br />
-                services.Configure<JdPayOptions>(Configuration.GetSection("JdPay"));
+                services.Configure<AlipayOptions>
+                    (Configuration.GetSection("Alipay"));<br />
+                    services.Configure<WeChatPayOptions>
+                        (Configuration.GetSection("WeChatPay"));<br />
+                        services.Configure<QPayOptions>
+                            (Configuration.GetSection("QPay"));<br />
+                            services.Configure<JdPayOptions>(Configuration.GetSection("JdPay"));
             </p>
             <h3>*参数配置方式(3选1,推荐使用3):</h3>
             <p>1.使用代码配置如:services.AddAlipay(Options => { Options.AppId = ""; });</p>
@@ -87,6 +90,10 @@
                             <label>notify_url:</label>
                             <input type="text" name="notify_url" value="http://www.baidu.com/notify/alipay/pagepay">
                         </p>
+                        <p>
+                            <label>return_url:</label>
+                            <input type="text" name="return_url" value="http://www.baidu.com/alipay/return">
+                        </p>
                         <p>
                             <label>&nbsp;</label>
                             <input type="submit" class="button" value="提交">

+ 1 - 1
src/Essensoft.AspNetCore.Alipay/Notify/AlipayTradePagePayReturnResponse.cs

@@ -4,7 +4,7 @@ namespace Essensoft.AspNetCore.Alipay.Notify
 {
     /// <summary>
     /// 电脑网站支付页面回跳参数
-    /// 更新时间:2017-09-05
+    /// 更新时间:2017-11-02
     /// </summary>
     public class AlipayTradePagePayReturnResponse : AlipayObject
     {

+ 5 - 7
src/Essensoft.AspNetCore.QPay/QPayCertificateClient.cs

@@ -19,21 +19,19 @@ namespace Essensoft.AspNetCore.QPay
 
         public QPayOptions Options { get; set; }
 
-        protected internal HttpClientHandler ClientHandler { get; set; }
-
         protected internal HttpClientEx Client { get; set; }
 
         public QPayCertificateClient(IOptions<QPayOptions> optionsAccessor)
         {
             Options = optionsAccessor?.Value ?? new QPayOptions();
-            ClientHandler = new HttpClientHandler();
 
+            var clientHandler = new HttpClientHandler();
             if (File.Exists(Options.Certificate)) // 是文件则以文件名的形式创建,否则以Base64String方式
-                ClientHandler.ClientCertificates.Add(new X509Certificate2(Options.Certificate, Options.MchId));
+                clientHandler.ClientCertificates.Add(new X509Certificate2(Options.Certificate, Options.MchId));
             else
-                ClientHandler.ClientCertificates.Add(new X509Certificate2(Convert.FromBase64String(Options.Certificate), Options.MchId));
+                clientHandler.ClientCertificates.Add(new X509Certificate2(Convert.FromBase64String(Options.Certificate), Options.MchId));
 
-            Client = new HttpClientEx(ClientHandler);
+            Client = new HttpClientEx(clientHandler);
         }
 
         public QPayCertificateClient(string appId, string appSecret, string mchId, string key, string certificate)
@@ -73,7 +71,7 @@ namespace Essensoft.AspNetCore.QPay
                 throw new Exception("sign check fail: Body is Empty!");
             }
 
-            var sign = response.Sign;
+            var sign = response?.Sign;
             if (!response.IsError && !string.IsNullOrEmpty(sign))
             {
                 var cal_sign = Md5.GetMD5WithKey(response.Parameters, Options.Key);

+ 6 - 12
src/Essensoft.AspNetCore.WeChatPay/WeChatPayCertificateClient.cs

@@ -25,21 +25,19 @@ namespace Essensoft.AspNetCore.WeChatPay
 
         public WeChatPayOptions Options { get; set; }
 
-        protected internal HttpClientHandler ClientHandler { get; set; }
-
         protected internal HttpClientEx Client { get; set; }
 
         public WeChatPayCertificateClient(IOptions<WeChatPayOptions> optionsAccessor)
         {
             Options = optionsAccessor?.Value ?? new WeChatPayOptions();
-            ClientHandler = new HttpClientHandler();
 
+            var clientHandler = new HttpClientHandler();
             if (File.Exists(Options.Certificate)) // 是文件则以文件名的形式创建,否则以Base64String方式
-                ClientHandler.ClientCertificates.Add(new X509Certificate2(Options.Certificate, Options.MchId));
+                clientHandler.ClientCertificates.Add(new X509Certificate2(Options.Certificate, Options.MchId));
             else
-                ClientHandler.ClientCertificates.Add(new X509Certificate2(Convert.FromBase64String(Options.Certificate), Options.MchId));
+                clientHandler.ClientCertificates.Add(new X509Certificate2(Convert.FromBase64String(Options.Certificate), Options.MchId));
 
-            Client = new HttpClientEx(ClientHandler);
+            Client = new HttpClientEx(clientHandler);
         }
 
         public WeChatPayCertificateClient(string appId, string appSecret, string mchId, string key, string certificate)
@@ -57,15 +55,11 @@ namespace Essensoft.AspNetCore.WeChatPay
                 sortedTxtParams.Add(MCHAPPID, Options.AppId);
                 sortedTxtParams.Add(MCHID, Options.MchId);
             }
-            else if (request is WeChatPayGetPublicKeyRequest)
+            else if (request is WeChatPayGetPublicKeyRequest || request is WeChatPayPayBankRequest || request is WeChatPayQueryBankRequest)
             {
                 sortedTxtParams.Add(MCH_ID, Options.MchId);
                 sortedTxtParams.Add(SIGN_TYPE, "MD5");
             }
-            else if (request is WeChatPayPayBankRequest || request is WeChatPayQueryBankRequest)
-            {
-                sortedTxtParams.Add(MCH_ID, Options.MchId);
-            }
             else if (request is WeChatPayGetTransferInfoRequest)
             {
                 sortedTxtParams.Add(APPID, Options.AppId);
@@ -104,7 +98,7 @@ namespace Essensoft.AspNetCore.WeChatPay
                 throw new Exception("sign check fail: Body is Empty!");
             }
 
-            var sign = response.Sign;
+            var sign = response?.Sign;
             if (!response.IsError && !string.IsNullOrEmpty(sign))
             {
                 var cal_sign = Md5.GetMD5WithKey(response.Parameters, Options.Key);