AlipayPayBackService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.Models;
  5. using Library;
  6. using LitJson;
  7. using System.Threading;
  8. namespace MySystem
  9. {
  10. public class AlipayPayBackService
  11. {
  12. public readonly static AlipayPayBackService Instance = new AlipayPayBackService();
  13. private AlipayPayBackService()
  14. { }
  15. public void Start()
  16. {
  17. Thread th = new Thread(Listen);
  18. th.IsBackground = true;
  19. th.Start();
  20. }
  21. public void Listen()
  22. {
  23. bool op = true;
  24. while (op)
  25. {
  26. string content = RedisDbconn.Instance.RPop<string>("PayCallBack");
  27. if (!string.IsNullOrEmpty(content))
  28. {
  29. try
  30. {
  31. dosomething(content);
  32. }
  33. catch (Exception ex)
  34. {
  35. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付宝回调异常");
  36. }
  37. }
  38. else
  39. {
  40. Thread.Sleep(2000);
  41. }
  42. }
  43. }
  44. private void dosomething(string content)
  45. {
  46. //{"out_trade_no":"2023061514385419860385846","transaction_id":"2023061522001451251430691917","total_fee":"0.01","pay_mode":"1","openid":"","attach":""}
  47. JsonData orderObj = JsonMapper.ToObject(content);
  48. string OrderNo = orderObj["out_trade_no"].ToString();
  49. string TradeNo = orderObj["transaction_id"].ToString();
  50. WebCMSEntities db = new WebCMSEntities();
  51. ConsumerOrderForNo forNo = db.ConsumerOrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
  52. if (forNo != null)
  53. {
  54. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == forNo.OrderIds && m.Status == 0);
  55. if (order != null)
  56. {
  57. order.Status = 1;
  58. order.UpdateDate = DateTime.Now;
  59. order.PayMoney = order.PayMoney;
  60. order.MaxDivi = order.MaxDivi;
  61. order.SeoTitle = TradeNo;
  62. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  63. order.UserId = merchant.UserId;
  64. ConsumerOrders check = db.ConsumerOrders.FirstOrDefault(m => m.Id < order.Id && m.Status > 0 && m.PayMode == order.PayMode && m.MerchantId == order.MerchantId);
  65. if(check != null)
  66. {
  67. order.QueryCount = check.QueryCount + 1;
  68. }
  69. else
  70. {
  71. order.QueryCount = 1;
  72. }
  73. db.SaveChanges();
  74. //语音播报
  75. PosMachines machine = db.PosMachines.FirstOrDefault(m => m.PosSn == order.SnNo);
  76. if(machine != null)
  77. {
  78. if(machine.BrandId == 2)
  79. {
  80. TianYuVoiceHelper.Instance.doSomething(DateTime.Now.ToString("yyyyMMddHHmmssfff"), machine.Detail, order.PayMoney.ToString("f2"));
  81. }
  82. else if(machine.BrandId == 3)
  83. {
  84. MqLinksHelper.Instance.doSomething(machine.Detail, order.PayMoney.ToString("f2"));
  85. }
  86. }
  87. }
  88. }
  89. db.Dispose();
  90. }
  91. //分账队列
  92. public void StartProfitShare()
  93. {
  94. Thread th = new Thread(StartProfitShareListen);
  95. th.IsBackground = true;
  96. th.Start();
  97. }
  98. public void StartProfitShareListen()
  99. {
  100. bool op = true;
  101. while (op)
  102. {
  103. try
  104. {
  105. WebCMSEntities db = new WebCMSEntities();
  106. DateTime checkDate = DateTime.Now.AddMinutes(-1);
  107. var list = db.ConsumerOrders.Select(m => new { m.Id, m.CreateDate, m.Status, m.IsAct, m.PayMode }).Where(m => m.Status == 1 && m.PayMode == 1 && m.IsAct == 1 && m.CreateDate < checkDate).OrderBy(m => m.Id).Take(10).ToList();
  108. foreach(var sub in list)
  109. {
  110. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
  111. if(order != null)
  112. {
  113. order.Status = 2;
  114. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  115. MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
  116. if(order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
  117. {
  118. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  119. //添加分账接收方
  120. if(merchantAdd.Sort == 0)
  121. {
  122. string content = AlipayFunction.Instance.BindSettleRelation(DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), AlipayFunction.Instance.Pid, merchantAdd.AlipayAuthToken);
  123. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content, "支付宝添加分账方");
  124. merchantAdd.Sort = 1;
  125. db.SaveChanges();
  126. }
  127. //发起分账
  128. decimal fee = order.PayMoney * 0.01M;
  129. if(fee >= 0.01M)
  130. {
  131. string TradeNo = order.SeoTitle;
  132. string OrderNo = order.OrderNo;
  133. List<RoyaltyParameters> Parameters = new List<RoyaltyParameters>();
  134. Parameters.Add(new RoyaltyParameters()
  135. {
  136. Account = AlipayFunction.Instance.Pid,
  137. Amount = fee.ToString("f2"),
  138. });
  139. AlipayFunction.Instance.Settle(OrderNo, TradeNo, Parameters, merchantAdd.AlipayAuthToken);
  140. //开始监听分账状态
  141. Dictionary<string, object> req = new Dictionary<string, object>();
  142. req.Add("TradeNo", TradeNo); //支付宝订单号
  143. req.Add("OrderNo", OrderNo); //商户订单号
  144. req.Add("AuthToken", merchantAdd.AlipayAuthToken);
  145. RedisDbconn.Instance.AddList("AlipayShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
  146. }
  147. else
  148. {
  149. RedisDbconn.Instance.AddList("ConsumerOrders:Divi:1:List", order.Id.ToString());
  150. // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:1:" + order.MerchantId, order);
  151. }
  152. }
  153. }
  154. }
  155. db.SaveChanges();
  156. db.Dispose();
  157. }
  158. catch (Exception ex)
  159. {
  160. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付宝分账队列异常");
  161. }
  162. Thread.Sleep(2000);
  163. }
  164. }
  165. }
  166. }