WeChatPayBackService.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using MySystem.Models.Main;
  7. using Org.BouncyCastle.Crypto.Modes;
  8. using Org.BouncyCastle.Crypto.Engines;
  9. using Org.BouncyCastle.Crypto.Parameters;
  10. using System.Text;
  11. using System.Threading;
  12. namespace MySystem
  13. {
  14. public class WeChatPayBackService
  15. {
  16. public readonly static WeChatPayBackService Instance = new WeChatPayBackService();
  17. private WeChatPayBackService()
  18. { }
  19. public void Start()
  20. {
  21. Thread th = new Thread(Listen);
  22. th.IsBackground = true;
  23. th.Start();
  24. }
  25. public void Listen()
  26. {
  27. bool op = true;
  28. while (op)
  29. {
  30. string content = RedisDbconn.Instance.RPop<string>("WeChatPayBackHd");
  31. if (!string.IsNullOrEmpty(content))
  32. {
  33. try
  34. {
  35. dosomething(content);
  36. }
  37. catch (Exception ex)
  38. {
  39. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信支付回调异常");
  40. }
  41. }
  42. else
  43. {
  44. Thread.Sleep(2000);
  45. }
  46. }
  47. }
  48. public void dosomething(string content)
  49. {
  50. LogHelper.Instance.WriteLog("\n\n" + DateTime.Now.ToString() + "\n" + content, "微信支付回调监控");
  51. JsonData jsonObj = JsonMapper.ToObject(content);
  52. if (jsonObj.Count > 0)
  53. {
  54. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "json通过", "微信支付回调监控");
  55. string OrderNo = jsonObj["outOrderNo"].ToString();
  56. string HdOrderNo = jsonObj["orderNo"].ToString();
  57. string TradeNo = "";
  58. if (content.Contains("\"payChannelOrderNo\""))
  59. {
  60. TradeNo = jsonObj["payChannelOrderNo"].ToString();
  61. }
  62. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "OrderNo:" + OrderNo, "微信支付回调监控");
  63. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "HdOrderNo:" + HdOrderNo, "微信支付回调监控");
  64. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "TradeNo:" + TradeNo, "微信支付回调监控");
  65. WebCMSEntities db = new WebCMSEntities();
  66. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 0);
  67. if (order != null)
  68. {
  69. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "找到订单", "微信支付回调监控");
  70. order.Status = 1;
  71. order.UpdateDate = DateTime.Now;
  72. order.PayMoney = order.PayMoney;
  73. order.MaxDivi = order.MaxDivi;
  74. order.SeoTitle = HdOrderNo;
  75. order.SeoKeyword = TradeNo;
  76. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "赋值", "微信支付回调监控");
  77. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  78. MerchantAddInfo addinfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  79. if (addinfo.Status == 1 && order.PayMode == 2)
  80. {
  81. addinfo.Status = 2;
  82. merchant.Status = 2;
  83. addinfo.WeChatRemark = "";
  84. if (merchant.Latitude == 0)
  85. {
  86. //签约成功调用腾讯地图接口获取地址
  87. RedisDbconn.Instance.AddList("GetTencentAddressInfoQueueHd", "{\"MerChantId\":\"" + merchant.Id + "\",\"Address\":\"" + merchant.Areas + merchant.Address + "\"}");
  88. }
  89. }
  90. else if (addinfo.QueryCount == 1 && order.PayMode == 1)
  91. {
  92. addinfo.QueryCount = 2;
  93. merchant.QueryCount = 2;
  94. addinfo.AlipayRemark = "";
  95. if (merchant.Latitude == 0)
  96. {
  97. //签约成功调用腾讯地图接口获取地址
  98. RedisDbconn.Instance.AddList("GetTencentAddressInfoQueueHd", "{\"MerChantId\":\"" + merchant.Id + "\",\"Address\":\"" + merchant.Areas + merchant.Address + "\"}");
  99. }
  100. }
  101. order.UserId = merchant.UserId;
  102. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "创客:" + merchant.UserId, "微信支付回调监控");
  103. ConsumerOrders check = db.ConsumerOrders.FirstOrDefault(m => m.Id < order.Id && m.Status > 0 && m.PayMode == order.PayMode && m.MerchantId == order.MerchantId);
  104. if (check != null)
  105. {
  106. order.QueryCount = check.QueryCount + 1;
  107. }
  108. else
  109. {
  110. order.QueryCount = 1;
  111. }
  112. if(addinfo.BrandId > 0)
  113. {
  114. decimal fee = decimal.Parse(function.CheckNum(addinfo.FeeRate)) * 100;
  115. order.Sort = (int)fee;
  116. }
  117. db.SaveChanges();
  118. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "检测商户订单数:" + order.QueryCount, "微信支付回调监控");
  119. Models.Main1.WebCMSEntities db1 = new Models.Main1.WebCMSEntities();
  120. var machines = db1.PosMachines.Select(m => new { m.SeoDescription, m.DeviceKind, m.BrandId, m.Detail, m.BindMerchantId }).Where(m => m.DeviceKind == "2" && m.BindMerchantId == order.MerchantId);
  121. foreach (var machine in machines)
  122. {
  123. if (!string.IsNullOrEmpty(addinfo.AliMerchantId) && machine.BrandId == 1)
  124. {
  125. AliIotFunction.Instance.IotTradeVoice(machine.SeoDescription, addinfo.AliMerchantId, order.PayMoney.ToString("f2"), "OTHER");
  126. }
  127. else if (machine.BrandId == 2)
  128. {
  129. TianYuVoiceHelper.Instance.doSomething(DateTime.Now.ToString("yyyyMMddHHmmssfff"), machine.Detail, order.PayMoney.ToString("f2"));
  130. }
  131. else if (machine.BrandId == 3)
  132. {
  133. MqLinksHelper.Instance.doSomething(machine.Detail, order.PayMoney.ToString("f2"));
  134. }
  135. }
  136. db1.Dispose();
  137. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "执行完毕", "微信支付回调监控");
  138. //发送订单到客小爽MQ
  139. OrderMessageHelper.SendOrderMsg(order);
  140. //给商户单独推送订单消息
  141. string orderData = "{\"order_no\":\"" + order.OrderNo + "\",\"order_status\":\"" + order.Status + "\",\"order_time\":\"" + order.UpdateDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + "\"}";
  142. RedisDbconn.Instance.AddList("OrderPushQueue", orderData);
  143. //推送交易给java
  144. PosPushDataNewHelper.Trade(order, addinfo);
  145. }
  146. else
  147. {
  148. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "没找到订单", "微信支付回调监控");
  149. ConsumerOrders checkorder = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
  150. if (checkorder != null)
  151. {
  152. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "找到订单状态为2", "微信支付回调监控");
  153. }
  154. else
  155. {
  156. RedisDbconn.Instance.AddList("WeChatPayBackHdDead", content);
  157. }
  158. }
  159. db.Dispose();
  160. LogHelper.Instance.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "结束", "微信支付回调监控");
  161. }
  162. }
  163. //分账队列
  164. public void StartProfitShare()
  165. {
  166. Thread th = new Thread(StartProfitShareListen);
  167. th.IsBackground = true;
  168. th.Start();
  169. }
  170. public void StartProfitShareListen()
  171. {
  172. bool op = true;
  173. while (op)
  174. {
  175. try
  176. {
  177. WebCMSEntities db = new WebCMSEntities();
  178. DateTime checkDate = DateTime.Now.AddMinutes(-2);
  179. var list = db.ConsumerOrders.Select(m => new { m.Id, m.SettleAmount, m.Status, m.IsAct, m.PayMode }).Where(m => m.Status == 1 && m.IsAct == 1 && m.SettleAmount > 0).OrderBy(m => m.Id).Take(10).ToList();
  180. foreach (var sub in list)
  181. {
  182. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
  183. if (order != null)
  184. {
  185. int Status = 2;
  186. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  187. MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
  188. if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
  189. {
  190. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  191. //发起分账
  192. decimal fee = order.PayMoney;
  193. if (fee >= 1)
  194. {
  195. string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
  196. string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
  197. string orderNo = order.SeoTitle; //交易订单号(好哒平台订单号,G开头)
  198. string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
  199. string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
  200. string acctType = "0"; //账户类型(0:商户收款账户(大B),1:分账接收方账户(小B))
  201. decimal settleAmount = (decimal)order.SettleAmount / 100M;
  202. decimal divideAmt = (1 - 0.0038M) * order.PayMoney;
  203. divideAmt = decimal.Parse(divideAmt.ToString("f2"));
  204. decimal hdfee = 0.0038M * order.PayMoney * (1 - set.GetPercent / 100 - 0.01M);
  205. hdfee = decimal.Parse(hdfee.ToString("f2"));
  206. decimal servicefee = 0.01M * order.PayMoney - hdfee;
  207. servicefee = decimal.Parse(servicefee.ToString("f2"));
  208. decimal amount = order.PayMoney * (1 - 0.0038M) - servicefee;
  209. amount = decimal.Parse(amount.ToString("f2"));
  210. decimal otherAmt = settleAmount - servicefee - amount;
  211. if (otherAmt != 0)
  212. {
  213. amount += otherAmt;
  214. }
  215. amount = amount * 100; //金额(分)
  216. servicefee = servicefee * 100;
  217. string seviceAmount = servicefee.ToString("f0"); //服务费
  218. string amountAmount = amount.ToString("f0");
  219. string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, acctType, amountAmount, seviceAmount), merchantAdd.BrandId);
  220. order.DivideLog = "请求分账日志:" + result;
  221. order.TradePayNo = applyNo;
  222. if (result.Contains("\"resultCode\":\"0\"") && (result.Contains("分账金额不足") || result.Contains("不存在")))
  223. {
  224. Status = 1;
  225. }
  226. //开始监听分账状态
  227. // Dictionary<string, object> req = new Dictionary<string, object>();
  228. // req.Add("ApplyNo", applyNo);
  229. // req.Add("OrderNo", order.OrderNo);
  230. // req.Add("MchtNo", mchtNo);
  231. // RedisDbconn.Instance.AddList("ProfitShareHdQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
  232. }
  233. else
  234. {
  235. RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
  236. }
  237. }
  238. order.Status = Status;
  239. if (Status == 2)
  240. {
  241. order.DivideFlag = 1;
  242. order.DivideDate = DateTime.Now;
  243. }
  244. }
  245. }
  246. db.SaveChanges();
  247. db.Dispose();
  248. }
  249. catch (Exception ex)
  250. {
  251. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "好哒分账队列异常");
  252. }
  253. Thread.Sleep(2000);
  254. }
  255. }
  256. //触发分账接口
  257. public void StartDivi()
  258. {
  259. Thread th = new Thread(ListenDivi);
  260. th.IsBackground = true;
  261. th.Start();
  262. }
  263. public void ListenDivi()
  264. {
  265. bool op = true;
  266. while (op)
  267. {
  268. string content = RedisDbconn.Instance.RPop<string>("AddHdDiviQueue");
  269. if (!string.IsNullOrEmpty(content))
  270. {
  271. try
  272. {
  273. int id = int.Parse(function.CheckInt(content));
  274. WebCMSEntities db = new WebCMSEntities();
  275. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == id);
  276. if (order != null)
  277. {
  278. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  279. MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
  280. if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
  281. {
  282. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  283. //发起分账
  284. decimal fee = order.PayMoney;
  285. if (fee >= 1)
  286. {
  287. string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
  288. string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
  289. string orderNo = order.SeoTitle; //交易订单号(好哒平台订单号,G开头)
  290. string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
  291. string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
  292. string acctType = "0"; //账户类型(0:商户收款账户(大B),1:分账接收方账户(小B))
  293. decimal settleAmount = (decimal)order.SettleAmount / 100M;
  294. decimal divideAmt = (1 - 0.0038M) * order.PayMoney;
  295. divideAmt = decimal.Parse(divideAmt.ToString("f2"));
  296. decimal hdfee = 0.0038M * order.PayMoney * (1 - set.GetPercent / 100 - 0.01M);
  297. hdfee = decimal.Parse(hdfee.ToString("f2"));
  298. decimal servicefee = 0.01M * order.PayMoney - hdfee;
  299. servicefee = decimal.Parse(servicefee.ToString("f2"));
  300. decimal amount = order.PayMoney * (1 - 0.0038M) - servicefee;
  301. amount = decimal.Parse(amount.ToString("f2"));
  302. decimal otherAmt = settleAmount - servicefee - amount;
  303. if (otherAmt != 0)
  304. {
  305. amount += otherAmt;
  306. }
  307. amount = amount * 100; //金额(分)
  308. servicefee = servicefee * 100;
  309. string seviceAmount = servicefee.ToString("f0"); //服务费
  310. string amountAmount = amount.ToString("f0");
  311. string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, acctType, amountAmount, seviceAmount));
  312. order.DivideLog = "请求分账日志:" + result;
  313. //开始监听分账状态
  314. // Dictionary<string, object> req = new Dictionary<string, object>();
  315. // req.Add("ApplyNo", applyNo);
  316. // req.Add("OrderNo", order.OrderNo);
  317. // req.Add("MchtNo", mchtNo);
  318. // RedisDbconn.Instance.AddList("ProfitShareHdQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
  319. }
  320. }
  321. }
  322. db.Dispose();
  323. }
  324. catch (Exception ex)
  325. {
  326. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "补分账异常");
  327. }
  328. }
  329. else
  330. {
  331. Thread.Sleep(2000);
  332. }
  333. }
  334. }
  335. //监听支付结算订单
  336. public void StartSettleOrder()
  337. {
  338. Thread th = new Thread(SettleOrderListen);
  339. th.IsBackground = true;
  340. th.Start();
  341. }
  342. public void SettleOrderListen()
  343. {
  344. bool op = true;
  345. while (op)
  346. {
  347. string content = RedisDbconn.Instance.RPop<string>("PartOrderQueue");
  348. if (!string.IsNullOrEmpty(content))
  349. {
  350. try
  351. {
  352. //{"id":137403,"mchtNo":"1494637","tradeMchtNo":"030500107115593","channelMchtNo":"030500107115593","orderNo":"G2231123C02179895853","channelOrderNo":"50231123B00032642694","txSubCode":31,"paymentType":2,"status":0,"partStatus":0,"partAmount":0,"payAmount":1000,"payServiceFee":4,"settleAmount":996,"totalPerkAmount":0,"payTime":"2023-11-23 23:36:05","algoDate":"2023-11-23 00:00:00","bizDate":"2023-11-23 00:00:00","storeNo":"21627867","deviceSn":" ","cupsNo":"ylzf","cardType":0,"notPartAmount":996,"realAmount":996}
  353. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content, "监听支付结算订单");
  354. if (content.Contains("\"settleAmount\"") && content.Contains("\"txSubCode\":31,"))
  355. {
  356. LogHelper.Instance.WriteLog("符合条件", "监听支付结算订单");
  357. JsonData jsonObj = JsonMapper.ToObject(content);
  358. string orderNo = jsonObj["orderNo"].ToString();
  359. int settleAmount = int.Parse(function.CheckInt(jsonObj["settleAmount"].ToString()));
  360. LogHelper.Instance.WriteLog("orderNo:" + orderNo, "监听支付结算订单");
  361. LogHelper.Instance.WriteLog("settleAmount:" + settleAmount, "监听支付结算订单");
  362. WebCMSEntities db = new WebCMSEntities();
  363. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == orderNo);
  364. if (order != null)
  365. {
  366. order.SettleAmount = settleAmount;
  367. db.SaveChanges();
  368. LogHelper.Instance.WriteLog("finish", "监听支付结算订单");
  369. }
  370. else
  371. {
  372. Thread.Sleep(2000);
  373. RedisDbconn.Instance.AddList("PartOrderQueue", content);
  374. LogHelper.Instance.WriteLog("again", "监听支付结算订单");
  375. }
  376. db.Dispose();
  377. }
  378. LogHelper.Instance.WriteLog("\n\n", "监听支付结算订单");
  379. }
  380. catch (Exception ex)
  381. {
  382. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "监听支付结算订单异常");
  383. }
  384. }
  385. else
  386. {
  387. Thread.Sleep(2000);
  388. }
  389. }
  390. }
  391. }
  392. }