Browse Source

[WebApplicationSample] 应用网关 - 激活开发者模式

Roc 5 years ago
parent
commit
29bc3ffc72
1 changed files with 94 additions and 0 deletions
  1. 94 0
      samples/WebApplicationSample/Controllers/NotifyController.cs

+ 94 - 0
samples/WebApplicationSample/Controllers/NotifyController.cs

@@ -1,7 +1,11 @@
 using System;
+using System.Collections.Generic;
+using System.Text;
 using System.Threading.Tasks;
+using System.Xml;
 using Essensoft.AspNetCore.Payment.Alipay;
 using Essensoft.AspNetCore.Payment.Alipay.Notify;
+using Essensoft.AspNetCore.Payment.Alipay.Utility;
 using Essensoft.AspNetCore.Payment.WeChatPay;
 using Essensoft.AspNetCore.Payment.WeChatPay.Notify;
 using Microsoft.AspNetCore.Mvc;
@@ -23,6 +27,51 @@ namespace WebApplicationSample.Controllers
             _optionsAccessor = optionsAccessor;
         }
 
+        /// <summary>
+        /// 应用网关
+        /// </summary>
+        /// <returns></returns>
+        [Route("gateway")]
+        [HttpPost]
+        public async Task<IActionResult> Gateway()
+        {
+            try
+            {
+                // 获取参数
+                var Dic = new Dictionary<string, string>();
+                if (Request.Method == "POST")
+                {
+                    foreach (var iter in Request.Form)
+                    {
+                        Dic.Add(iter.Key, iter.Value);
+                    }
+                }
+
+                // 激活开发者模式
+                if ("alipay.service.check".Equals(Dic["service"]))
+                {
+                    var options = _optionsAccessor.Value;
+
+                    var signStr = Dic["sign"];
+                    Dic.Remove("sign");
+
+                    var signContent = AlipaySignature.GetSignContent(Dic);
+
+                    // 加签方式为公钥证书时,从公钥证书获取的RSA公钥 options.AlipayPublicCertKey
+                    var isSuccess = AlipaySignature.RSACheckContent(signContent, signStr, options.AlipayPublicCertKey, options.Charset, options.SignType);
+                    var response = MakeVerifyGWResponse(isSuccess, options.AlipayPublicCertKey, options.AppPrivateKey, options.Charset, options.SignType);
+
+                    return Content(response, "text/xml");
+                }
+
+                return NoContent();
+            }
+            catch
+            {
+                return NoContent();
+            }
+        }
+
         /// <summary>
         /// 扫码支付异步通知
         /// </summary>
@@ -118,6 +167,51 @@ namespace WebApplicationSample.Controllers
                 return NoContent();
             }
         }
+
+
+        private string MakeVerifyGWResponse(bool isSuccess, string certPublicKey, string appPrivateKey, string charset, string signType)
+        {
+            var xmlDoc = new XmlDocument(); //创建实例
+            var xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "GBK", null);
+            xmlDoc.AppendChild(xmldecl);
+
+            var xmlElem = xmlDoc.CreateElement("alipay"); //新建元素
+            xmlDoc.AppendChild(xmlElem); //添加元素
+
+            var alipay = xmlDoc.SelectSingleNode("alipay");
+
+            var response = xmlDoc.CreateElement("response");
+            var success = xmlDoc.CreateElement("success");
+            if (isSuccess)
+            {
+                success.InnerText = "true";//设置文本节点 
+                response.AppendChild(success);//添加到<Node>节点中 
+            }
+            else
+            {
+                success.InnerText = "false";//设置文本节点 
+                response.AppendChild(success);//添加到<Node>节点中 
+                var err = xmlDoc.CreateElement("error_code");
+                err.InnerText = "VERIFY_FAILED";
+                response.AppendChild(err);
+            }
+
+            var biz_content = xmlDoc.CreateElement("biz_content");
+            biz_content.InnerText = certPublicKey;
+            response.AppendChild(biz_content);
+
+            alipay.AppendChild(response);
+
+            var sign = xmlDoc.CreateElement("sign");
+            sign.InnerText = AlipaySignature.RSASignContent(response.InnerXml, appPrivateKey, charset, signType);
+            alipay.AppendChild(sign);
+
+            var sign_type = xmlDoc.CreateElement("sign_type");
+            sign_type.InnerText = signType;
+            alipay.AppendChild(sign_type);
+
+            return xmlDoc.InnerXml;
+        }
     }
 
     #endregion