소스 검색

调整支付宝支付接口

lcl 1 년 전
부모
커밋
c7af7dcfd4
3개의 변경된 파일78개의 추가작업 그리고 17개의 파일을 삭제
  1. 25 10
      AppStart/Alipay/AlipayFunction.cs
  2. 52 6
      Areas/Api/Controllers/AlipayController.cs
  3. 1 1
      Areas/Api/Controllers/v1/MainServer/ConsumerOrdersController.cs

+ 25 - 10
AppStart/Alipay/AlipayFunction.cs

@@ -10,6 +10,7 @@ using Aop.Api.Response;
 using Aop.Api.Util;
 using System.Collections;
 using MySystem.MainModels;
+using Newtonsoft.Json;
 
 namespace MySystem
 {
@@ -19,7 +20,7 @@ namespace MySystem
         private string AppId = "2021003116616751";
         private string PrivateKey = "MIIEpAIBAAKCAQEA1KjMzZJIRG6QQ3+c/euVrc+8RR0xMw6A+EONSKhJq4Bg+rtxQPCfdF2bpZ4NMaSqfalSiLoxrZSYBYOZhpDP67i9LXGUoFCWdRL7ROTqJvuU6KyEeMplQ6Q3uwWbuKbh9COt0vhqn/pjlUsomTwq+DRQv10awynkZikqceXdP0EH4iGZG0hyerEP5yLHpHiggf4f7TOsovHX+fGLPmWFO1zUTHHbz7HxU4BQKn3yuje4yMhoyv5h/wZzeVwvj+Qti8Hlh351rF0f5BHwN1YMGIrWuPU5bF8OMHS/UXPxh7Bs795ZZyplYztTz26QfKfLWVVsLOng4pvgPS+OgzZydwIDAQABAoIBAEu6mMQT9Zw46zK+PP/HZxPwSBEsphqSbab7F1LqauGh4+cl3NzNU1szAnxl3jjxdK0vIO8DrzErGz/Lb16WLxS4QtXt/olgaVPjoIsWIbQkHxEEbmA9YvZFgnsdMij9dLVmBz57rCywE8pUa262GG8u4hpjY2Zggq9GtZC91J3zOeETBG828gU+FLHGcDpfz7pD1jC0noDfDRxmIImOMZvChIvvY+tm6MpySXN8lw1vxhLc0aZbKSQkuVyCyB4fIsdJJvh2dRPiY+BA0jXNK3mWwnnHzac/9JXqWFlwk6iPIIjXPUldz7Zq3EpCI+9p+UqLtIu6BAUbk1biFTt8x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hqn/pjlUsomTwq+DRQv10awynkZikqceXdP0EH4iGZG0hyerEP5yLHpHiggf4f7TOsovHX+fGLPmWFO1zUTHHbz7HxU4BQKn3yuje4yMhoyv5h/wZzeVwvj+Qti8Hlh351rF0f5BHwN1YMGIrWuPU5bF8OMHS/UXPxh7Bs795ZZyplYztTz26QfKfLWVVsLOng4pvgPS+OgzZydwIDAQAB";
-        private string AlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxycehrmnTl2IaIAECP+0HWc1eWGM/T6x022K7K6fdf+xoyC97TBJaG4G3Bcon4gsJX+Sad5IA9OJIQ4eN1joM35hIlwcFZ6MmCuVXx/SMAKThIjK0jsRc6aTxVQG+GVCgS7dXMKb4Sm+vK2wEM6xtDeqcfYMB0BqGZRvxbIV59a2ooPrha4FkV6nNC9DQefzC0picHXV64oYUjMM2ed4ASTQVds7HP9BZfalz//APZcWdBV08f+xv7n79Twz/gNtVAadfuTAlcjsS6aK+G/OrZtr/diuemeRPRUKlVhcr5xOQDhqBgtdIw8M9Wp6I4ULXRlthKVBWToH9j6n9vDIiwIDAQAB";
+        public string AlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxycehrmnTl2IaIAECP+0HWc1eWGM/T6x022K7K6fdf+xoyC97TBJaG4G3Bcon4gsJX+Sad5IA9OJIQ4eN1joM35hIlwcFZ6MmCuVXx/SMAKThIjK0jsRc6aTxVQG+GVCgS7dXMKb4Sm+vK2wEM6xtDeqcfYMB0BqGZRvxbIV59a2ooPrha4FkV6nNC9DQefzC0picHXV64oYUjMM2ed4ASTQVds7HP9BZfalz//APZcWdBV08f+xv7n79Twz/gNtVAadfuTAlcjsS6aK+G/OrZtr/diuemeRPRUKlVhcr5xOQDhqBgtdIw8M9Wp6I4ULXRlthKVBWToH9j6n9vDIiwIDAQAB";
         public AlipayFunction(HttpContext context)
         {
             _context = context;
@@ -194,22 +195,36 @@ namespace MySystem
             return result;
         }
 
-        public string CreateTrade(string OrderNo, string Subject, string Amount, string BuyerId, string Notify_Url, string Token)
+        public string CreateTrade(string OrderNo, string Subject, decimal Amount, string BuyerId, string MerPid, string Notify_Url)
         {
             IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AppId, PrivateKey, "json", "1.0", "RSA2", AlipayPublicKey, "utf-8", false);
             AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
             request.SetNotifyUrl(Notify_Url);
-            request.BizContent = "{" +
-            "\"out_trade_no\":\"" + OrderNo + "\"," +
-            "\"total_amount\":" + Amount + "," +
-            "\"subject\":\"" + Subject + "\"," +
-            "\"buyer_id\":\"" + BuyerId + "\"," +
-            "\"timeout_express\":\"10m\"" +
-            "}";
+            Dictionary<string, object> bizContent = new Dictionary<string, object>();
+            bizContent.Add("out_trade_no", OrderNo);
+            bizContent.Add("total_amount", Amount);
+            bizContent.Add("subject", Subject);
+            // bizContent.Add("seller_id", MerPid);
+            bizContent.Add("buyer_id", BuyerId);
+            bizContent.Add("timeout_express", "10m");
+
+            // 结算信息,按需传入
+            // Dictionary<string, object> settleInfo = new Dictionary<string, object>();
+            // List<object> settleDetailInfos = new List<object>();
+            // Dictionary<string, object> settleDetail = new Dictionary<string, object>();
+            // settleDetail.Add("trans_in_type", "userId");
+            // settleDetail.Add("trans_in", MerPid);
+            // settleDetail.Add("amount", Amount);
+            // settleDetailInfos.Add(settleDetail);
+            // settleInfo.Add("settle_detail_infos", settleDetailInfos);
+            // bizContent.Add("settle_info", settleInfo);
+
+            string Contentjson = JsonConvert.SerializeObject(bizContent);
+            request.BizContent = Contentjson;
             string result = "";
             try
             {
-                AlipayTradeCreateResponse response = client.Execute(request, null, Token);
+                AlipayTradeCreateResponse response = client.Execute(request, null, "202306BB117e046cfc29434c8f4acfd2a43aeX03");
                 result = response.Body;
                 function.WriteLog(DateTime.Now.ToString() + "\n" + result, "支付宝统一收单交易创建");
             }

+ 52 - 6
Areas/Api/Controllers/AlipayController.cs

@@ -11,6 +11,7 @@ using MySystem.MainModels;
 using LitJson;
 using Library;
 using System.IO;
+using Aop.Api.Util;
 
 namespace MySystem.Areas.Api.Controllers.v1
 {
@@ -26,16 +27,61 @@ namespace MySystem.Areas.Api.Controllers.v1
 
 
         #region 支付宝服务平台支付回调
-        public Dictionary<string, string> Notice()
+        public string Notice()
         {
             StreamReader sr = new StreamReader(Request.Body);
             string requestMes = sr.ReadToEnd();
             function.WriteLog(DateTime.Now.ToString() + "\r\n" + requestMes + "\r\n\r\n", "支付宝服务平台支付回调");
-            RedisDbconn.Instance.AddList("PayCallBack", requestMes);
-            Dictionary<string, string> dic = new Dictionary<string, string>();
-            dic.Add("code", "SUCCESS");
-            dic.Add("message", "");
-            return dic;
+            return PayCallBack(new AlipayFunction(_accessor.HttpContext).AlipayPublicKey, requestMes);
+        }
+
+        public string PayCallBack(string publickey, string requestMes, bool IsRedis = false)
+        {
+            // Library.function.WriteLog(requestMes, "支付宝回调日志");
+            var result = "fail";
+            var sPara = GetRequestPost(requestMes);
+            if (sPara.Count > 0) //判断是否有带返回参数
+            {
+                // Library.function.WriteLog("有参数", "支付宝回调日志");
+                bool verifyResult = false;
+                var aliNotify = new Com.Alipay.Notify();
+                string signtype = sPara["sign_type"];
+                var total_fee = 0m;
+                string strPrice = sPara["total_amount"];//金额
+                decimal.TryParse(strPrice, out total_fee);
+                verifyResult = AlipaySignature.RSACheckV1(sPara, publickey, "UTF-8", signtype, false);
+                // Library.function.WriteLog(verifyResult.ToString(), "支付宝回调日志");
+                if (verifyResult) //验证成功
+                {
+                    // Library.function.WriteLog("验证成功\r\n\r\n", "支付宝回调日志");
+                    //商户订单号
+                    string out_trade_no = sPara["out_trade_no"];
+                    //支付宝交易号
+                    string trade_no = sPara["trade_no"];
+                    //交易状态 TRADE_SUCCESS
+                    string trade_status = sPara["trade_status"];
+
+                    //交易成功
+                    if (trade_status == "TRADE_FINISHED" || trade_status == "TRADE_SUCCESS")
+                    {
+                        RedisDbconn.Instance.AddList("PayCallBack", "{\"out_trade_no\":\"" + out_trade_no + "\",\"transaction_id\":\"" + trade_no + "\",\"total_fee\":\"" + total_fee + "\",\"pay_mode\":\"1\",\"openid\":\"\",\"attach\":\"\"}");
+                        result = "success";
+                    }
+                }
+            }
+            return result;
+        }
+        
+        private SortedDictionary<string, string> GetRequestPost(string canshu)
+        {
+            var sArray = new SortedDictionary<string, string>();
+            string[] canshulist = canshu.Split('&');
+            foreach (var substring in canshulist)
+            {
+                string[] datalist = substring.Split('=');
+                sArray.Add(datalist[0], HttpUtility.UrlDecode(datalist[1]));
+            }
+            return sArray;
         }
         #endregion
         

+ 1 - 1
Areas/Api/Controllers/v1/MainServer/ConsumerOrdersController.cs

@@ -300,7 +300,7 @@ namespace MySystem.Areas.Api.Controllers.v1
             maindb.SaveChanges();
             if (PayMode == 1)
             { 
-                string backString = new AlipayFunction(_accessor.HttpContext).CreateTrade(OrderNo, merchant.Name, PayMoney.ToString("f2"), openid, SourceHost + "api/alipay/notice", "202306BB725a6fd38fa947409e857c63b9233X03");
+                string backString = new AlipayFunction(_accessor.HttpContext).CreateTrade(OrderNo, merchant.Name, PayMoney, openid, merchantAdd.SeoTitle, SourceHost + "api/alipay/notice");
                 JsonData obj = JsonMapper.ToObject(backString);
                 if (obj["alipay_trade_create_response"]["code"].ToString() == "10000")
                 {