WeChatPayBackService.cs 20 KB

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