Browse Source

补全分润、分账等逻辑,添加查询分账结果接口,静态变量定义全局引用

DuGuYang 1 year ago
parent
commit
a36e678b01

+ 17 - 0
Config/Haoda.cs

@@ -0,0 +1,17 @@
+using System.Collections.Generic;
+using Library;
+
+namespace AppConfig
+{
+    /// <summary>
+    /// 好哒固定参数配置
+    /// </summary>
+    public class Haoda
+    {
+        //分账接收方用户编码
+        public static string AcctNo = "230817000000000278";
+
+        //公众号appid或者小程序appid
+        public static string SubAppId = "wx6e074b8e07b6577e";
+    }
+}

+ 5 - 5
Util/HaoDa/CheckWeChatSignService.cs

@@ -81,7 +81,7 @@ namespace MySystem
                         {
                             merchantadd.OpenAct = 1;//是否开通分账(1 是 0 否)
                             //分账接收方签约
-                            JsonData Infos = JsonMapper.ToObject(HaoDaHelper.Instance.DivideAccountsSign("230817000000000278", merchantadd.OutMchtNo, merchantadd.StoreNo));
+                            JsonData Infos = JsonMapper.ToObject(HaoDaHelper.Instance.DivideAccountsSign(AppConfig.Haoda.AcctNo, merchantadd.OutMchtNo, merchantadd.StoreNo));
                             //失败
                             if (Infos["resultCode"].ToString() == "0")
                             {
@@ -92,17 +92,17 @@ namespace MySystem
                             {
                                 if (!string.IsNullOrEmpty(merchantadd.ToAcctNo))
                                 {
-                                    if (!merchantadd.ToAcctNo.Contains(",230817000000000278,"))
+                                    if (!merchantadd.ToAcctNo.Contains("," + AppConfig.Haoda.AcctNo + ","))
                                     {
-                                        merchantadd.ToAcctNo += "," + "230817000000000278" + ",";
+                                        merchantadd.ToAcctNo += "," + AppConfig.Haoda.AcctNo + ",";
                                     }
                                 }
                                 else
                                 {
-                                    merchantadd.ToAcctNo += "," + "230817000000000278" + ",";
+                                    merchantadd.ToAcctNo += "," + AppConfig.Haoda.AcctNo + ",";
                                 }
                                 //绑定微信appid接口
-                                JsonData bindInfo = JsonMapper.ToObject(HaoDaHelper.Instance.BindWeChatAppId(merchantadd.OutMchtNo, merchantadd.StoreNo, "wx6e074b8e07b6577e"));
+                                JsonData bindInfo = JsonMapper.ToObject(HaoDaHelper.Instance.BindWeChatAppId(merchantadd.OutMchtNo, merchantadd.StoreNo, AppConfig.Haoda.SubAppId));
                                 //失败
                                 if (bindInfo["resultCode"].ToString() == "0")
                                 {

+ 25 - 0
Util/HaoDa/HaoDaHelper.cs

@@ -378,6 +378,31 @@ namespace MySystem
             return result;
         }
 
+        /// <summary>
+        /// 订单分账结果查询
+        /// </summary>
+        /// <param name="applyNo">分账批次号</param>
+        /// <param name="mchtNo">慧掌柜商户号</param>
+        /// <returns></returns>
+        public string OrderDivideAccountsQuery(string applyNo, string mchtNo)
+        {
+            function.WriteLog(DateTime.Now.ToString(), "好哒订单分账结果查询");
+            Dictionary<string, object> reqdic = new Dictionary<string, object>();
+            reqdic.Add("applyNo", applyNo); // 账户号	
+            reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
+            string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
+            function.WriteLog(req, "好哒订单分账结果查询");
+            Dictionary<string, string> headdic = GetHeader(req);
+            string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
+            function.WriteLog("请求头\n" + head, "好哒订单分账结果查询");
+            function.WriteLog("请求参数\n" + req, "好哒订单分账结果查询");
+            function.WriteLog("请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/apply/query", "好哒订单分账结果查询");
+            string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/apply/query", req, headdic);
+            function.WriteLog("返回\n" + result + "\n\n", "好哒订单分账结果查询");
+
+            return result;
+        }
+
         /// <summary>
         /// 聚合支付退款接口
         /// </summary>

+ 2 - 26
Util/HaoDa/ProfitHelper.cs

@@ -107,7 +107,6 @@ namespace MySystem
                                     long CurDiviPersons = DiviPersons;
                                     long QueueCount = RedisDbconn.Instance.Count("ConsumerOrders:Divi:" + PayMode + ":" + MerchantId);
                                     long CurQueueCount = QueueCount;
-                                    // int CheckNum = 0; //判断活动人数是否大于当前队列订单数来确认while循环的数量,如果是就少循环一次,判处自己的订单名额
                                     if (CurDiviPersons >= CurQueueCount)
                                     {
                                         CurDiviPersons = CurQueueCount;
@@ -118,15 +117,7 @@ namespace MySystem
                                     }
                                     decimal OtherMoney = 0; // 退款金额>最大退款金额-当前退款金额,次金额应归还给商户
 
-                                    // List<ConsumerOrders> orders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrders:Divi:" + PayMode + ":" + MerchantId);
-                                    // foreach(ConsumerOrders sub in orders)
-                                    // {
-                                    //     function.WriteLog(sub.Id.ToString(), "返现队列");
-                                    // }
-                                    // function.WriteLog("\n\n", "返现队列");
-
                                     // 减去自己付的订单
-                                    // CurDiviPersons = CurDiviPersons - 1;
                                     int OutCount = 0; // 出局人数
                                     while (CurDiviPersons > 0)
                                     {
@@ -170,14 +161,8 @@ namespace MySystem
                                                     }).Entity;
                                                     db.SaveChanges();
                                                     MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == suborder.MerchantId) ?? new MerchantAddInfo();
-                                                    // if(order.PayMode == 1)
-                                                    // {
-                                                    //     AlipayFunction.Instance.Refund(suborder.SeoTitle, GetMoney.ToString("f2"), "门店分红", merchantAdd.AlipayAuthToken);
-                                                    // }
-                                                    // else
-                                                    // {
-                                                    //     Dictionary<string, string> dic = WeChatFunction.Instance.Refund(merchantAdd.SubMchid, GetMoney, suborder.PayMoney, suborder.OrderNo, DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), "门店分红");
-                                                    // }
+                                                    
+                                                    //聚合支付退款
                                                     var info = HaoDaHelper.Instance.AggregatedPayRefund(merchantAdd.SubMchid, merchantAdd.AlipayPid, "HDTK" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), merchantAdd.AlipayAuthToken, GetMoney.ToString());
                                                     if (suborder.CurDivi < suborder.MaxDivi)
                                                     {
@@ -202,15 +187,6 @@ namespace MySystem
                                             CurDiviPersons = 0;
                                         }
                                     }
-                                    // long DoCount = DiviPersons > (QueueCount + OutCount) ? DiviPersons - QueueCount : 0;
-                                    // decimal MerchantGetMoney = PayMoney * GetPercent;
-                                    // decimal DoMoney = DoCount * DiviMoney;
-                                    // TotalActual = PayMoney * GetPercent + DoCount * DiviMoney + OtherMoney;
-                                    // function.WriteLog("返现金额:" + DiviMoney + ";返现人数:" + DiviPersons + ";返现商户:" + TotalActual + ";返现溢出:" + OtherMoney + ";商户应得金额:" + MerchantGetMoney + ";返现差额:" + DoMoney + ";", "消费者返现日志");
-
-
-                                    //代理商分润
-                                    // AgentProfit(db, order);
                                 }
                             }
                         }

+ 68 - 73
Util/HaoDa/ProfitShareService.cs

@@ -1,77 +1,72 @@
-// using System;
-// using System.Collections.Generic;
-// using System.Linq;
-// using MySystem.Models.Main;
-// using Library;
-// using LitJson;
-// using System.Text.RegularExpressions;
-// using System.Threading;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using MySystem.Models.Main;
+using Library;
+using LitJson;
+using System.Text.RegularExpressions;
+using System.Threading;
 
-// namespace MySystem
-// {
-//     public class ProfitShareService
-//     {
-//         public readonly static ProfitShareService Instance = new ProfitShareService();
-//         private ProfitShareService()
-//         { }
+namespace MySystem
+{
+    public class ProfitShareService
+    {
+        public readonly static ProfitShareService Instance = new ProfitShareService();
+        private ProfitShareService()
+        { }
 
-//         public void Start()
-//         {
-//             Thread th = new Thread(StartListen);
-//             th.IsBackground = true;
-//             th.Start();
-//         }
+        public void Start()
+        {
+            Thread th = new Thread(StartListen);
+            th.IsBackground = true;
+            th.Start();
+        }
 
-//         public void StartListen()
-//         {
-//             while (true)
-//             {
-//                 string content = RedisDbconn.Instance.RPop<string>("ProfitShareQueue");
-//                 if (!string.IsNullOrEmpty(content))
-//                 {
-//                     StartDo(content);
-//                 }
-//                 else
-//                 {
-//                     Thread.Sleep(2000);
-//                 }
-//             }
-//         }
+        public void StartListen()
+        {
+            while (true)
+            {
+                string content = RedisDbconn.Instance.RPop<string>("ProfitShareQueue");
+                if (!string.IsNullOrEmpty(content))
+                {
+                    StartDo(content);
+                }
+                else
+                {
+                    Thread.Sleep(2000);
+                }
+            }
+        }
 
-//         public void StartDo(string content)
-//         {
-//             try
-//             {
-//                 JsonData jsonObj = JsonMapper.ToObject(content);
-//                 string SubMchid = jsonObj["SubMchid"].ToString(); //子商户号
-//                 string TradeNo = jsonObj["TradeNo"].ToString(); //微信订单号
-//                 string OrderNo = jsonObj["OrderNo"].ToString(); //商户订单号
-//                 string result = WeChatFunction.Instance.QueryProfitShare(SubMchid, TradeNo, OrderNo);
-//                 JsonData resultObj = JsonMapper.ToObject(result);
-//                 if(result.Contains("\"state\":"))
-//                 {
-//                     string state = resultObj["state"].ToString();
-//                     if(state == "FINISHED")
-//                     {
-//                         WebCMSEntities db = new WebCMSEntities();
-//                         ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
-//                         if (order != null)
-//                         {
-//                             RedisDbconn.Instance.AddList("ConsumerOrders:Divi:" + order.PayMode + ":List", order.Id.ToString());
-//                             // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + order.PayMode + ":" + order.MerchantId, order);
-//                         }
-//                         db.Dispose();
-//                     }
-//                     else
-//                     {
-//                         RedisDbconn.Instance.AddList("ProfitShareQueue", content);
-//                     }
-//                 }
-//             }
-//             catch (Exception ex)
-//             {
-//                 function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "分账队列异常");
-//             }
-//         }
-//     }
-// }
+        public void StartDo(string content)
+        {
+            try
+            {
+                JsonData jsonObj = JsonMapper.ToObject(content);
+                string ApplyNo = jsonObj["ApplyNo"].ToString(); //微信订单号
+                string MchtNo = jsonObj["MchtNo"].ToString(); //商户订单号
+                string OrderNo = jsonObj["OrderNo"].ToString(); //商户订单号
+                JsonData result = JsonMapper.ToObject(HaoDaHelper.Instance.OrderDivideAccountsQuery(ApplyNo, MchtNo));
+                string state = result["data"]["status"].ToString();
+                if (result["data"]["status"].ToString() == "0")
+                {
+                    WebCMSEntities db = new WebCMSEntities();
+                    ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
+                    if (order != null)
+                    {
+                        RedisDbconn.Instance.AddList("ConsumerOrders:Divi:" + order.PayMode + ":List", order.Id.ToString());
+                    }
+                    db.Dispose();
+                }
+                else
+                {
+                    RedisDbconn.Instance.AddList("ProfitShareQueue", content);
+                }
+            }
+            catch (Exception ex)
+            {
+                function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "分账队列异常");
+            }
+        }
+    }
+}

+ 1 - 1
Util/HaoDa/TestOpenDivideAccountsService.cs

@@ -91,7 +91,7 @@ namespace MySystem
         {
             WebCMSEntities db = new WebCMSEntities();
             MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
-            string result = HaoDaHelper.Instance.BindWeChatAppId(AddInfo.OutMchtNo, AddInfo.StoreNo,"wx6e074b8e07b6577e");
+            string result = HaoDaHelper.Instance.BindWeChatAppId(AddInfo.OutMchtNo, AddInfo.StoreNo,AppConfig.Haoda.SubAppId);
             db.Dispose();
         }
     }

+ 10 - 46
Util/HaoDa/WeChatPayBackService.cs

@@ -54,13 +54,6 @@ namespace MySystem
             JsonData jsonObj = JsonMapper.ToObject(content);
             if (jsonObj.Count > 0)
             {
-                // if (jsonObj["event_type"].ToString() == "TRANSACTION.SUCCESS")
-                // {
-                // JsonData resource = jsonObj["resource"];
-                //开始解密
-                // string WxPayResourceDecryptModel = AesGcmDecrypt(resource["associated_data"].ToString(), resource["nonce"].ToString(), resource["ciphertext"].ToString());
-                // {\"sp_mchid\":\"1611167423\",\"sub_mchid\":\"1622024882\",\"sp_appid\":\"wxe2c051b3e46c0f6f\",\"out_trade_no\":\"2022022621562926396898863\",\"transaction_id\":\"4200001412202202267619496496\",\"trade_type\":\"JSAPI\",\"trade_state\":\"SUCCESS\",\"trade_state_desc\":\"支付成功\",\"bank_type\":\"OTHERS\",\"attach\":\"\",\"success_time\":\"2022-02-26T21:56:42+08:00\",\"payer\":{\"sp_openid\":\"omawy5W6jb0pgPfuKUVs6K3bEhzk\",\"sub_openid\":\"\"},\"amount\":{\"total\":1,\"payer_total\":1,\"currency\":\"CNY\",\"payer_currency\":\"CNY\"}}
-                // JsonData orderObj = JsonMapper.ToObject(WxPayResourceDecryptModel);
                 string OrderNo = jsonObj["outOrderNo"].ToString();
                 string TradeNo = jsonObj["orderNo"].ToString();
                 WebCMSEntities db = new WebCMSEntities();
@@ -90,27 +83,9 @@ namespace MySystem
                     }
                 }
                 db.Dispose();
-                // }
             }
         }
 
-        // public string AesGcmDecrypt(string associatedData, string nonce, string ciphertext)
-        // {
-        //     GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine());
-        //     AeadParameters aeadParameters = new AeadParameters(
-        //         new KeyParameter(Encoding.UTF8.GetBytes(AppConfig.WeChatParam.AesGemKey)),
-        //         128,
-        //         Encoding.UTF8.GetBytes(nonce),
-        //         Encoding.UTF8.GetBytes(associatedData));
-        //     gcmBlockCipher.Init(false, aeadParameters);
-
-        //     byte[] data = Convert.FromBase64String(ciphertext);
-        //     byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(data.Length)];
-        //     int length = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0);
-        //     gcmBlockCipher.DoFinal(plaintext, length);
-        //     return Encoding.UTF8.GetString(plaintext);
-        // }
-
         //分账队列
         public void StartProfitShare()
         {
@@ -140,39 +115,28 @@ namespace MySystem
                             if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
                             {
                                 MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
-                                //添加分账接收方
-                                // if(merchant.Version == 0)
-                                // {
-                                //     AddOpenDivideAccounts addOpenDivideAccounts = 
-                                //     HaoDaHelper.Instance.AddOpenDivideAccounts();
-                                //     merchant.Version = 1;
-                                //     db.SaveChanges();
-                                // }
                                 //发起分账
                                 decimal fee = order.PayMoney; //单位:分
                                 if (fee >= 1)
                                 {
-                                    string TradeNo = order.SeoTitle;
-                                    string OrderNo = order.OrderNo;
-                                    string applyNo = "FZ202307050006";
-                                    string mchtNo = "";
-                                    string orderNo = "";
-                                    string acctNo = "";
-                                    string acctType = "1";
-                                    string amount = "";
+                                    string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
+                                    string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
+                                    string orderNo = order.OrderNo; //交易订单号(好哒平台订单号,G开头)
+                                    string acctNo = AppConfig.Haoda.AcctNo; //账户号,账户类型为商户收款账户时填收款商户号, 账户类型为分账接收方时填分账接收方账户号
+                                    string acctType = "1"; //账户类型(0:商户收款账户(大B),1:分账接收方账户(小B))
+                                    string amount = (fee * (1M - 0.0038M)).ToString(); //金额(分)
                                     HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, acctType, amount));
 
                                     //开始监听分账状态
                                     Dictionary<string, object> req = new Dictionary<string, object>();
-                                    req.Add("SubMchid", merchantAdd.SubMchid); //子商户号
-                                    req.Add("TradeNo", TradeNo); //微信订单号
-                                    req.Add("OrderNo", OrderNo); //商户订单号
+                                    req.Add("ApplyNo", applyNo);
+                                    req.Add("OrderNo", orderNo);
+                                    req.Add("MchtNo", mchtNo);
                                     RedisDbconn.Instance.AddList("ProfitShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
                                 }
                                 else
                                 {
                                     RedisDbconn.Instance.AddList("ConsumerOrders:Divi:2:List", order.Id.ToString());
-                                    // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:2:" + order.MerchantId, order);
                                 }
                             }
                         }
@@ -182,7 +146,7 @@ namespace MySystem
                 }
                 catch (Exception ex)
                 {
-                    function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信分账队列异常");
+                    function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "好哒分账队列异常");
                 }
                 Thread.Sleep(2000);
             }