Просмотр исходного кода

添加微信支付回调队列

lcl 1 год назад
Родитель
Сommit
58a33fd0d4
2 измененных файлов с 40 добавлено и 51 удалено
  1. 36 48
      AppStart/Helper/WeChatPayBackService.cs
  2. 4 3
      Startup.cs

+ 36 - 48
AppStart/Helper/WeChatPayBackService.cs

@@ -8,6 +8,7 @@ using Org.BouncyCastle.Crypto.Modes;
 using Org.BouncyCastle.Crypto.Engines;
 using Org.BouncyCastle.Crypto.Parameters;
 using System.Text;
+using System.Threading;
 
 namespace MySystem
 {
@@ -17,31 +18,11 @@ namespace MySystem
         private WeChatPayBackService()
         { }
 
-        public void Start(JobMqMsg jobInfo)
+        public void Start()
         {
-            string content = "";
-            try
-            {
-                dosomething();
-                // string Msg = "success";
-                // jobInfo.Status = Msg == "success" ? 1 : 0;
-                // jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
-                // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
-            }
-            catch (Exception ex)
-            {
-                if (!string.IsNullOrEmpty(content))
-                {
-                    Dictionary<string, string> data = new Dictionary<string, string>();
-                    data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
-                    data.Add("ErrMsg", ex.ToString());
-                    function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
-                }
-                else
-                {
-                    function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
-                }
-            }
+            Thread th = new Thread(dosomething);
+            th.IsBackground = true;
+            th.Start();
         }
 
         public void dosomething()
@@ -52,38 +33,45 @@ namespace MySystem
                 string content = RedisDbconn.Instance.RPop<string>("WeChatPayBack");
                 if (!string.IsNullOrEmpty(content))
                 {
-                    JsonData jsonObj = JsonMapper.ToObject(content);
-                    if (jsonObj.Count > 0)
+                    try
                     {
-                        if (jsonObj["event_type"].ToString() == "TRANSACTION.SUCCESS")
+                        JsonData jsonObj = JsonMapper.ToObject(content);
+                        if (jsonObj.Count > 0)
                         {
-                            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 = orderObj["out_trade_no"].ToString();
-                            WebCMSEntities db = new WebCMSEntities();
-                            ConsumerOrderForNo forNo = db.ConsumerOrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
-                            if (forNo != null)
+                            if (jsonObj["event_type"].ToString() == "TRANSACTION.SUCCESS")
                             {
-                                ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == forNo.OrderIds && m.Status == 0);
-                                if (order != null)
+                                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 = orderObj["out_trade_no"].ToString();
+                                WebCMSEntities db = new WebCMSEntities();
+                                ConsumerOrderForNo forNo = db.ConsumerOrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
+                                if (forNo != null)
                                 {
-                                    order.Status = 1;
-                                    order.UpdateDate = DateTime.Now;
-                                    order.PayMoney = order.PayMoney;
-                                    order.MaxDivi = order.MaxDivi;
-                                    db.SaveChanges();
-                                    RedisDbconn.Instance.AddList("ConsumerOrdersStat", order.Id);
-                                    RedisDbconn.Instance.AddList("ConsumerOrders:Divi:List", order.Id.ToString());
-                                    RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + order.MerchantId, order);
-                                    // ConsumerOrdersStatService.Instance.Stat(order);
+                                    ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == forNo.OrderIds && m.Status == 0);
+                                    if (order != null)
+                                    {
+                                        order.Status = 1;
+                                        order.UpdateDate = DateTime.Now;
+                                        order.PayMoney = order.PayMoney;
+                                        order.MaxDivi = order.MaxDivi;
+                                        db.SaveChanges();
+                                        RedisDbconn.Instance.AddList("ConsumerOrdersStat", order.Id);
+                                        RedisDbconn.Instance.AddList("ConsumerOrders:Divi:List", order.Id.ToString());
+                                        RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + order.MerchantId, order);
+                                        // ConsumerOrdersStatService.Instance.Stat(order);
+                                    }
                                 }
+                                db.Dispose();
                             }
-                            db.Dispose();
                         }
                     }
+                    catch (Exception ex)
+                    {
+                        function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
+                    }
                 }
                 else
                 {

+ 4 - 3
Startup.cs

@@ -171,9 +171,10 @@ namespace MySystem
             });
 
             //必须打开的
-            // MerchantConfirmService.Instance.Start();
-            // CheckWeChatSignService.Instance.Start();
-            // ProfitHelper.Instance.StartListenTrade();
+            MerchantConfirmService.Instance.Start();
+            CheckWeChatSignService.Instance.Start();
+            WeChatPayBackService.Instance.Start();
+            ProfitHelper.Instance.StartListenTrade();
             //必须打开的
         }
     }