WeChatPayBackService.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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("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("OrderNo:" + OrderNo, "微信支付回调监控");
  63. LogHelper.Instance.WriteLog("HdOrderNo:" + HdOrderNo, "微信支付回调监控");
  64. LogHelper.Instance.WriteLog("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("找到订单", "微信支付回调监控");
  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("赋值", "微信支付回调监控");
  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. }
  85. else if(addinfo.QueryCount == 1 && order.PayMode == 1)
  86. {
  87. addinfo.QueryCount = 2;
  88. merchant.QueryCount = 2;
  89. addinfo.AlipayRemark = "";
  90. }
  91. order.UserId = merchant.UserId;
  92. LogHelper.Instance.WriteLog("创客:" + merchant.UserId, "微信支付回调监控");
  93. ConsumerOrders check = db.ConsumerOrders.FirstOrDefault(m => m.Id < order.Id && m.Status > 0 && m.PayMode == order.PayMode && m.MerchantId == order.MerchantId);
  94. if (check != null)
  95. {
  96. order.QueryCount = check.QueryCount + 1;
  97. }
  98. else
  99. {
  100. order.QueryCount = 1;
  101. }
  102. db.SaveChanges();
  103. LogHelper.Instance.WriteLog("检测商户订单数:" + order.QueryCount, "微信支付回调监控");
  104. Models.Main1.WebCMSEntities db1 = new Models.Main1.WebCMSEntities();
  105. 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);
  106. foreach(var machine in machines)
  107. {
  108. if(!string.IsNullOrEmpty(addinfo.AliMerchantId) && machine.BrandId == 1)
  109. {
  110. AliIotFunction.Instance.IotTradeVoice(machine.SeoDescription, addinfo.AliMerchantId, order.PayMoney.ToString("f2"), "OTHER");
  111. }
  112. else if(machine.BrandId == 2)
  113. {
  114. TianYuVoiceHelper.Instance.doSomething(DateTime.Now.ToString("yyyyMMddHHmmssfff"), machine.Detail, order.PayMoney.ToString("f2"));
  115. }
  116. else if(machine.BrandId == 3)
  117. {
  118. MqLinksHelper.Instance.doSomething(machine.Detail, order.PayMoney.ToString("f2"));
  119. }
  120. }
  121. db1.Dispose();
  122. LogHelper.Instance.WriteLog("执行完毕", "微信支付回调监控");
  123. }
  124. db.Dispose();
  125. LogHelper.Instance.WriteLog("结束", "微信支付回调监控");
  126. }
  127. }
  128. //分账队列
  129. public void StartProfitShare()
  130. {
  131. Thread th = new Thread(StartProfitShareListen);
  132. th.IsBackground = true;
  133. th.Start();
  134. }
  135. public void StartProfitShareListen()
  136. {
  137. bool op = true;
  138. while (op)
  139. {
  140. try
  141. {
  142. WebCMSEntities db = new WebCMSEntities();
  143. DateTime checkDate = DateTime.Now.AddMinutes(-2);
  144. 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();
  145. foreach (var sub in list)
  146. {
  147. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
  148. if (order != null)
  149. {
  150. int Status = 2;
  151. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  152. MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
  153. if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
  154. {
  155. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  156. //发起分账
  157. decimal fee = order.PayMoney;
  158. if (fee >= 1)
  159. {
  160. string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
  161. string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
  162. string orderNo = order.SeoTitle; //交易订单号(好哒平台订单号,G开头)
  163. string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
  164. string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
  165. string acctType = "0"; //账户类型(0:商户收款账户(大B),1:分账接收方账户(小B))
  166. decimal settleAmount = (decimal)order.SettleAmount / 100M;
  167. decimal divideAmt = (1 - 0.0038M) * order.PayMoney;
  168. divideAmt = decimal.Parse(divideAmt.ToString("f2"));
  169. decimal hdfee = 0.0038M * order.PayMoney * (1 - set.GetPercent / 100 - 0.01M);
  170. hdfee = decimal.Parse(hdfee.ToString("f2"));
  171. decimal servicefee = 0.01M * order.PayMoney - hdfee;
  172. servicefee = decimal.Parse(servicefee.ToString("f2"));
  173. decimal amount = order.PayMoney*(1-0.0038M) - servicefee;
  174. amount = decimal.Parse(amount.ToString("f2"));
  175. decimal otherAmt = settleAmount - servicefee - amount;
  176. if(otherAmt != 0)
  177. {
  178. amount += otherAmt;
  179. }
  180. amount = amount * 100; //金额(分)
  181. servicefee = servicefee * 100;
  182. string seviceAmount = servicefee.ToString("f0"); //服务费
  183. string amountAmount = amount.ToString("f0");
  184. string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, acctType, amountAmount, seviceAmount));
  185. order.DivideLog = "请求分账日志:" + result;
  186. if(result.Contains("\"resultCode\":\"0\"") && (result.Contains("分账金额不足") || result.Contains("不存在")))
  187. {
  188. Status = 1;
  189. }
  190. //开始监听分账状态
  191. // Dictionary<string, object> req = new Dictionary<string, object>();
  192. // req.Add("ApplyNo", applyNo);
  193. // req.Add("OrderNo", order.OrderNo);
  194. // req.Add("MchtNo", mchtNo);
  195. // RedisDbconn.Instance.AddList("ProfitShareHdQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
  196. }
  197. else
  198. {
  199. RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
  200. }
  201. }
  202. order.Status = Status;
  203. if(Status == 2)
  204. {
  205. order.DivideFlag = 1;
  206. order.DivideDate = DateTime.Now;
  207. }
  208. }
  209. }
  210. db.SaveChanges();
  211. db.Dispose();
  212. }
  213. catch (Exception ex)
  214. {
  215. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "好哒分账队列异常");
  216. }
  217. Thread.Sleep(2000);
  218. }
  219. }
  220. //触发分账接口
  221. public void StartDivi()
  222. {
  223. Thread th = new Thread(ListenDivi);
  224. th.IsBackground = true;
  225. th.Start();
  226. }
  227. public void ListenDivi()
  228. {
  229. bool op = true;
  230. while (op)
  231. {
  232. string content = RedisDbconn.Instance.RPop<string>("AddHdDiviQueue");
  233. if (!string.IsNullOrEmpty(content))
  234. {
  235. try
  236. {
  237. int id = int.Parse(function.CheckInt(content));
  238. WebCMSEntities db = new WebCMSEntities();
  239. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == id);
  240. if (order != null)
  241. {
  242. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  243. MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
  244. if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
  245. {
  246. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  247. //发起分账
  248. decimal fee = order.PayMoney;
  249. string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
  250. string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
  251. string orderNo = order.SeoTitle; //交易订单号(好哒平台订单号,G开头)
  252. string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
  253. string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
  254. string acctType = "0"; //账户类型(0:商户收款账户(大B),1:分账接收方账户(小B))
  255. decimal settleAmount = (decimal)order.SettleAmount / 100M;
  256. decimal divideAmt = (1 - 0.0038M) * order.SettleAmount;
  257. divideAmt = decimal.Parse(divideAmt.ToString("f2"));
  258. decimal hdfee = 0.0038M * order.SettleAmount * (1 - set.GetPercent / 100 - 0.01M);
  259. hdfee = decimal.Parse(hdfee.ToString("f2"));
  260. decimal servicefee = 0.01M * order.SettleAmount - hdfee;
  261. servicefee = decimal.Parse(servicefee.ToString("f2"));
  262. decimal amount = order.SettleAmount*(1-0.0038M) - servicefee;
  263. amount = decimal.Parse(amount.ToString("f2"));
  264. decimal otherAmt = divideAmt - servicefee - amount;
  265. if(otherAmt != 0)
  266. {
  267. amount += otherAmt;
  268. }
  269. amount = amount * 100; //金额(分)
  270. servicefee = servicefee * 100;
  271. string seviceAmount = servicefee.ToString("f0"); //服务费
  272. string amountAmount = amount.ToString("f0");
  273. string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, acctType, amountAmount, seviceAmount));
  274. order.DivideLog = "请求分账日志:" + result;
  275. db.SaveChanges();
  276. }
  277. }
  278. db.Dispose();
  279. }
  280. catch (Exception ex)
  281. {
  282. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "补分账异常");
  283. }
  284. }
  285. else
  286. {
  287. Thread.Sleep(2000);
  288. }
  289. }
  290. }
  291. //监听支付结算订单
  292. public void StartSettleOrder()
  293. {
  294. Thread th = new Thread(SettleOrderListen);
  295. th.IsBackground = true;
  296. th.Start();
  297. }
  298. public void SettleOrderListen()
  299. {
  300. bool op = true;
  301. while (op)
  302. {
  303. string content = RedisDbconn.Instance.RPop<string>("PartOrderQueue");
  304. if (!string.IsNullOrEmpty(content))
  305. {
  306. try
  307. {
  308. //{"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}
  309. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content, "监听支付结算订单");
  310. if(content.Contains("\"settleAmount\"") && content.Contains("\"txSubCode\":31,"))
  311. {
  312. LogHelper.Instance.WriteLog("符合条件", "监听支付结算订单");
  313. JsonData jsonObj = JsonMapper.ToObject(content);
  314. string orderNo = jsonObj["orderNo"].ToString();
  315. int settleAmount = int.Parse(function.CheckInt(jsonObj["settleAmount"].ToString()));
  316. LogHelper.Instance.WriteLog("orderNo:" + orderNo, "监听支付结算订单");
  317. LogHelper.Instance.WriteLog("settleAmount:" + settleAmount, "监听支付结算订单");
  318. WebCMSEntities db = new WebCMSEntities();
  319. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == orderNo);
  320. if(order != null)
  321. {
  322. order.SettleAmount = settleAmount;
  323. db.SaveChanges();
  324. LogHelper.Instance.WriteLog("finish", "监听支付结算订单");
  325. }
  326. else
  327. {
  328. Thread.Sleep(2000);
  329. RedisDbconn.Instance.AddList("PartOrderQueue", content);
  330. LogHelper.Instance.WriteLog("again", "监听支付结算订单");
  331. }
  332. db.Dispose();
  333. }
  334. LogHelper.Instance.WriteLog("\n\n", "监听支付结算订单");
  335. }
  336. catch (Exception ex)
  337. {
  338. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "监听支付结算订单异常");
  339. }
  340. }
  341. else
  342. {
  343. Thread.Sleep(2000);
  344. }
  345. }
  346. }
  347. }
  348. }