WeChatPayBackService.cs 17 KB

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