lcl hai 1 ano
pai
achega
080e84ec24

+ 31 - 30
AppStart/Helper/Profit/ProfitHelper.cs

@@ -193,7 +193,7 @@ namespace MySystem
                             decimal GetPercent = set.GetPercent / 100; //商家实收比例
                             decimal MinPayMoney = set.MinPayMoney; //订单参与门槛
                             int IsAll = set.IsAll; //是否收全额
-                            decimal TotalActual = 0;
+                            // decimal TotalActual = 0;
                             if (IsAll == 0 && PayMoney >= MinPayMoney && GetPercent < 1)
                             {
                                 decimal DiviMoney = PayMoney * (1 - GetPercent - cusumerFeePercent - profitPercent);
@@ -209,6 +209,10 @@ namespace MySystem
                                     {
                                         CurDiviPersons = CurQueueCount;
                                     }
+                                    if(order.QueryCount < CurDiviPersons)
+                                    {
+                                        CurDiviPersons = order.QueryCount;
+                                    }
                                     if (DiviPersons < QueueCount)
                                     {
                                         QueueCount = DiviPersons;
@@ -245,31 +249,28 @@ namespace MySystem
                                                     {
                                                         OtherMoney += DiffMoney;
                                                     }
-                                                    bool check = db.ConsumerProfit.Any(m => m.ConsumerId == suborder.ConsumerId && m.OrderId == OrderId);
-                                                    if(!check)
+                                                    ConsumerProfit cprofit = db.ConsumerProfit.Add(new ConsumerProfit()
                                                     {
-                                                        ConsumerProfit cprofit = db.ConsumerProfit.Add(new ConsumerProfit()
-                                                        {
-                                                            CreateDate = DateTime.Now,
-                                                            ConsumerId = suborder.ConsumerId,
-                                                            MerchantId = suborder.MerchantId,
-                                                            OrderId = OrderId,
-                                                            GetMoney = GetMoney,
-                                                            QueryCount = PayMode,
-                                                            SeoTitle = order.OrderNo,
-                                                            SeoKeyword = order.PayMoney.ToString(),
-                                                        }).Entity;
-                                                        db.SaveChanges();
-                                                        MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == suborder.MerchantId) ?? new MerchantAddInfo();
-                                                        Dictionary<string, string> dic = WeChatFunction.Instance.Refund(merchantAdd.SubMchid, GetMoney, suborder.PayMoney, suborder.OrderNo, DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8));
-                                                        if (suborder.CurDivi < suborder.MaxDivi)
-                                                        {
-                                                            RedisDbconn.Instance.AddList("ConsumerOrders:Divi:" + PayMode + ":" + MerchantId, suborder);
-                                                        }
-                                                        else
-                                                        {
-                                                            OutCount += 1;
-                                                        }
+                                                        CreateDate = DateTime.Now,
+                                                        ConsumerId = suborder.ConsumerId,
+                                                        MerchantId = suborder.MerchantId,
+                                                        OrderId = suborder.Id,
+                                                        GetMoney = GetMoney,
+                                                        QueryCount = PayMode,
+                                                        Sort = OrderId,
+                                                        SeoTitle = suborder.OrderNo,
+                                                        SeoKeyword = suborder.PayMoney.ToString(),
+                                                    }).Entity;
+                                                    db.SaveChanges();
+                                                    // MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == suborder.MerchantId) ?? new MerchantAddInfo();
+                                                    // Dictionary<string, string> dic = WeChatFunction.Instance.Refund(merchantAdd.SubMchid, GetMoney, suborder.PayMoney, suborder.OrderNo, DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8));                                             
+                                                    if (suborder.CurDivi < suborder.MaxDivi)
+                                                    {
+                                                        RedisDbconn.Instance.AddList("ConsumerOrders:Divi:" + PayMode + ":" + MerchantId, suborder);
+                                                    }
+                                                    else
+                                                    {
+                                                        OutCount += 1;
                                                     }
                                                 }
                                             }
@@ -286,11 +287,11 @@ 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 + ";", "消费者返现日志");
+                                    // 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 + ";", "消费者返现日志");
 
 
                                     //代理商分润

+ 15 - 12
AppStart/Helper/ProfitShareService.cs

@@ -48,21 +48,24 @@ namespace MySystem
                 string OrderNo = jsonObj["OrderNo"].ToString(); //商户订单号
                 string result = WeChatFunction.Instance.QueryProfitShare(SubMchid, TradeNo, OrderNo);
                 JsonData resultObj = JsonMapper.ToObject(result);
-                string state = resultObj["state"].ToString();
-                if(state == "FINISHED")
+                if(result.Contains("\"state\":"))
                 {
-                    WebCMSEntities db = new WebCMSEntities();
-                    ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 1);
-                    if (order != null)
+                    string state = resultObj["state"].ToString();
+                    if(state == "FINISHED")
                     {
-                        RedisDbconn.Instance.AddList("ConsumerOrders:Divi:" + order.PayMode + ":List", order.Id.ToString());
-                        RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + order.PayMode + ":" + order.MerchantId, order);
+                        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);
                     }
-                    db.Dispose();
-                }
-                else
-                {
-                    RedisDbconn.Instance.AddList("ProfitShareQueue", content);
                 }
             }
             catch (Exception ex)

+ 74 - 22
AppStart/Helper/WeChatPayBackService.cs

@@ -77,7 +77,72 @@ namespace MySystem
                             order.SeoTitle = TradeNo;
                             MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
                             order.UserId = merchant.UserId;
-                            db.SaveChanges();
+                            ConsumerOrders check = db.ConsumerOrders.FirstOrDefault(m => m.Id < order.Id && m.Status > 0 && m.PayMode == order.PayMode && m.MerchantId == order.MerchantId);
+                            if(check != null)
+                            {
+                                order.QueryCount = check.QueryCount + 1;
+                            }
+                            else
+                            {
+                                order.QueryCount = 1;
+                            }
+                            db.SaveChanges();                            
+                        }
+                    }
+                    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()
+        {
+            Thread th = new Thread(StartProfitShareListen);
+            th.IsBackground = true;
+            th.Start();
+        }
+
+        public void StartProfitShareListen()
+        {
+            bool op = true;
+            while (op)
+            {
+                try
+                {
+                    WebCMSEntities db = new WebCMSEntities();
+                    DateTime checkDate = DateTime.Now.AddMinutes(-1);
+                    var list = db.ConsumerOrders.Select(m => new { m.Id, m.CreateDate, m.Status, m.IsAct }).Where(m => m.Status == 1 && m.IsAct == 1 && m.CreateDate < checkDate).OrderBy(m => m.Id).Take(10).ToList();
+                    foreach(var sub in list)
+                    {
+                        ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
+                        if(order != null)
+                        {
+                            order.Status = 2;
+                            MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
                             MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
                             if(order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
                             {
@@ -93,6 +158,8 @@ namespace MySystem
                                 decimal fee = order.PayMoney; //单位:分
                                 if(fee >= 1)
                                 {
+                                    string TradeNo = order.SeoTitle;
+                                    string OrderNo = order.OrderNo;
                                     List<ReceiverList> Receivers = new List<ReceiverList>();
                                     Receivers.Add(new ReceiverList()
                                     {
@@ -118,30 +185,15 @@ namespace MySystem
                             }
                         }
                     }
+                    db.SaveChanges();
                     db.Dispose();
                 }
+                catch (Exception ex)
+                {
+                    function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信支付回调异常");
+                }
+                Thread.Sleep(2000);
             }
         }
-
-        private string ALGORITHM = "AES/GCM/NoPadding";
-        private int TAG_LENGTH_BIT = 128;
-        private int NONCE_LENGTH_BYTE = 12;
-
-        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);
-        }
     }
 }

+ 21 - 46
Controllers/HomeController.cs

@@ -50,52 +50,27 @@ namespace MySystem.Controllers
 
         public string test()
         {
-            string txt = "";
-            //{"id":"dc4512b7-83cd-5fa5-91e9-a80fc8562e96","create_time":"2023-06-01T13:42:42+08:00","resource_type":"encrypt-resource","event_type":"TRANSACTION.SUCCESS","summary":"支付成功","resource":{"original_type":"transaction","algorithm":"AEAD_AES_256_GCM","ciphertext":"n/mRI+vnpUsIoNPM9dCsQvcbqOkhQkpoeWAPsdenYw/FvyDmg9Cc4N8yRa6nFxVYpQR7pbjxYLQcZk8bf2Us4mo6ZCUN4B/JH0NT4ja1eSyy7no6n2mJyufpf2oig9KbqabTjiFk84zIyxdg3wtttEohdYZr1w8rPSF0+7MR5LAdx73g+v5f/gbu0r3AzwErbO7fF9vOoJqNgqcc3VgpAhoH9xwed+PNCUmgGH+YjO0lvdurztJL0iXT9P6kiy+36Tcsp6wT2SdVXCzxS65wNhPI2M0S2A0WDPpBw5cswR/glLFwo9XqT5gavU19oS6w3mLNUlB1OsEMt3tqcTDCS4zk5I9uT45uMAAvF2EiJBsnGxSUuRD1qL4jZ8hdxV2MRquy8Nbxr1wj926ye98BXdR9ZaAH9G6UMk4YyTjiVMbvhOqciqfuFlwxNkXtpV8b3QAWOhsIw0E210Tss2JDNW38ITaCwfBwSyRfYjYFhoIEbpEVpUZBn2NqxxeK1iv/h2zp3BapS+Zy0iMDba9GDjJVqEHDc6in3rSB3e5/VFjdVm3dWVH3+Gry/pU9ATTJNWaJsr/z3sWzzOoa3u0SsihPIdhJ4QiDlsYAz0JGVvbcqMXyaEb9tRR2oYWy4kOOsYirnSmhuRbeow==","associated_data":"transaction","nonce":"QxHnxeB6xtrU"}}
-            // string content = "3IOiuqVNgoBm+slkn6EEgZyTaaEfq8CAHlSZUZuBfgxvhGz1+MWGqQ0wb0H3Jo5s9GveNP6zlrJAcxT7jGjYKMawN3WH+0zwQXZziYX3K3fXzamdputjuGz4MU2UBnDnIihgeaGfExH85UJQVJGoqJ38QiFo8MBaFRMz9FtspT3+tcBUryRCqdHvF57sNnKvEYJeacDKI8vEQV0YiUvytRsjMKbvLLwx+m6WsaIP4y4XPBuwmiJ5wYNFkVzUWUxzlctkjGsUXQGJrAch30pjBIr39gxMeWnph5fS6Jszivplki7jhSJjOgckRVmqOYvdmf8zcIeW3i02axg1YoxjPvs4JP+NZ3cQAZMI4FQLlV2w3pJGFKY7ZBIP73F6sHLMn6FReOvzWmWaQ+WvbGw9PpRpckkV83Eu9D6TpKRcw/M9cQA8Vh+24vuA6aFhMl2kUVTIdziOQrhH5GBvURKGm+YCtokOnsU21V01UCgEWA2NDL1D0GZZFwmMy4IiR1QY7IAwoqavt7h8l/8X5qHvhucqkdaRmYlLMM2S/xOob2HIiTA3l8Nj8BYXcbhr52fv14qY4qFSgtOc6NrverxANIIdv+YhCXU49cY25L1MjCQINUbvJ78jaFaeuv8nNcqjmfADyTC8c+jI4J7W1Z4B0isnT18XYcu6rYQhvjoyoW41ATVFDZtV3Xh/y7n14foLT5qXfkG21SABCviVL79N+36koa8rQB5yJQ==";
-            // string txt = WeChatFunction.Instance.AesGcmDecrypt("transaction", "ACgp5LfxJ2Pl", content);
-            // Dictionary<string, string> result = WeChatFunction.Instance.Refund("1645266943", 0.01M, 0.1M, "2023060214014405449699901", DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8));
-            // txt = Newtonsoft.Json.JsonConvert.SerializeObject(result);
-            // txt = Newtonsoft.Json.JsonConvert.SerializeObject(WeChatFunction.Instance.QueryMerchant("KXS2023060213554647660112"));
-            // txt = WeChatFunction.Instance.QueryProfitShare("1646405429","4200001821202306083551994076","2023060816062365164446474");
-            // List<ReceiverList> Receivers = new List<ReceiverList>();
-            // Receivers.Add(new ReceiverList()
-            // {
-            //     type = "MERCHANT_ID",  //分账接收方类型
-            //     account = WeChatFunction.Instance.MchId,  //分账接收方账号
-            //     amount = 3,  //分账金额
-            //     description = "服务费",  //分账描述
-            // });
-            // txt += WeChatFunction.Instance.ProfitShare("1646405429","4200001822202306085756641548","2023060816074331055600540", Receivers) + "\n\n";
-            // txt += WeChatFunction.Instance.ProfitShare("1646405429","4200001844202306083343778985","2023060817035052325490272", Receivers) + "\n\n";
-            // txt += WeChatFunction.Instance.ProfitShare("1646405429","4200001831202306081658093673","2023060817040647494610620", Receivers) + "\n\n";
-            // txt += WeChatFunction.Instance.ProfitShare("1646405429","4200001817202306080966122385","2023060817041984735120293", Receivers) + "\n\n";
-            // txt = WeChatFunction.Instance.ProfitShare("1646405429","4200001821202306083551994076","2023060816062365164446474",Receivers);
-
-
-            // Dictionary<string, object> req = new Dictionary<string, object>();
-            // req.Add("SubMchid", "1646405429"); //子商户号
-            // req.Add("TradeNo", "4200001822202306085756641548"); //微信订单号
-            // req.Add("OrderNo", "2023060816074331055600540"); //商户订单号
-            // RedisDbconn.Instance.AddList("ProfitShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
-
-            // req = new Dictionary<string, object>();
-            // req.Add("SubMchid", "1646405429"); //子商户号
-            // req.Add("TradeNo", "4200001844202306083343778985"); //微信订单号
-            // req.Add("OrderNo", "2023060817035052325490272"); //商户订单号
-            // RedisDbconn.Instance.AddList("ProfitShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
-
-            // req = new Dictionary<string, object>();
-            // req.Add("SubMchid", "1646405429"); //子商户号
-            // req.Add("TradeNo", "4200001831202306081658093673"); //微信订单号
-            // req.Add("OrderNo", "2023060817040647494610620"); //商户订单号
-            // RedisDbconn.Instance.AddList("ProfitShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
-
-            // req = new Dictionary<string, object>();
-            // req.Add("SubMchid", "1646405429"); //子商户号
-            // req.Add("TradeNo", "4200001817202306080966122385"); //微信订单号
-            // req.Add("OrderNo", "2023060817041984735120293"); //商户订单号
-            // RedisDbconn.Instance.AddList("ProfitShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
+            string txt = "ok";
+            // txt = WeChatFunction.Instance.QueryProfitShare("1646405429","4200001807202306098649105270","2023060910241258564572510");
+            // ProfitHelper.Instance.ReturnStat(Id, 2);
+            WebCMSEntities db = new WebCMSEntities();
+            List<int> oids = new List<int>();
+            oids.Add(15);
+            oids.Add(16);
+            oids.Add(17);
+            oids.Add(18);
+            oids.Add(19);
+            oids.Add(20);
+            foreach(int oid in oids)
+            {
+                ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == oid);
+                if(order != null)
+                {
+                    RedisDbconn.Instance.AddList("ConsumerOrders:Divi:2:List", order.Id.ToString());
+                    RedisDbconn.Instance.AddList("ConsumerOrders:Divi:2:" + order.MerchantId, order);
+                }
+            }
+            db.Dispose();
             return txt;
         }
     

+ 7 - 6
Startup.cs

@@ -171,13 +171,14 @@ namespace MySystem
             });
 
             //必须打开的
-            MerchantConfirmService.Instance.Start(); //特约商户进件队列
-            CheckWeChatSignService.Instance.Start(); //特约商户签约队列
-            WeChatPayBackService.Instance.Start(); //微信支付回调队列
-            ProfitHelper.Instance.StartActive(); //发放达标奖励队列
-            ProfitHelper.Instance.StartListenTrade(); //支付宝返现队列
+            // MerchantConfirmService.Instance.Start(); //特约商户进件队列
+            // CheckWeChatSignService.Instance.Start(); //特约商户签约队列
+            // WeChatPayBackService.Instance.Start(); //微信支付回调队列
+            // WeChatPayBackService.Instance.StartProfitShare(); //监听已支付订单,超过1分钟的订单执行分账
+            // ProfitHelper.Instance.StartActive(); //发放达标奖励队列
+            // ProfitHelper.Instance.StartListenTrade(); //支付宝返现队列
             ProfitHelper.Instance.StartListenWxTrade(); //微信返现队列
-            ProfitShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现
+            // ProfitShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现
             //必须打开的
         }
     }