AlipayPayBackService.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. }
  76. db.Dispose();
  77. }
  78. //分账队列
  79. public void StartProfitShare()
  80. {
  81. Thread th = new Thread(StartProfitShareListen);
  82. th.IsBackground = true;
  83. th.Start();
  84. }
  85. public void StartProfitShareListen()
  86. {
  87. bool op = true;
  88. while (op)
  89. {
  90. try
  91. {
  92. WebCMSEntities db = new WebCMSEntities();
  93. DateTime checkDate = DateTime.Now.AddMinutes(-1);
  94. 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();
  95. foreach(var sub in list)
  96. {
  97. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
  98. if(order != null)
  99. {
  100. order.Status = 2;
  101. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  102. MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
  103. if(order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
  104. {
  105. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  106. //添加分账接收方
  107. if(merchantAdd.Sort == 0)
  108. {
  109. string content = AlipayFunction.Instance.BindSettleRelation(DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), AlipayFunction.Instance.Pid, merchantAdd.AlipayAuthToken);
  110. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content, "支付宝添加分账方");
  111. merchantAdd.Sort = 1;
  112. db.SaveChanges();
  113. }
  114. //发起分账
  115. decimal fee = order.PayMoney * 0.01M;
  116. if(fee >= 0.01M)
  117. {
  118. string TradeNo = order.SeoTitle;
  119. string OrderNo = order.OrderNo;
  120. List<RoyaltyParameters> Parameters = new List<RoyaltyParameters>();
  121. Parameters.Add(new RoyaltyParameters()
  122. {
  123. Account = AlipayFunction.Instance.Pid,
  124. Amount = fee.ToString("f2"),
  125. });
  126. AlipayFunction.Instance.Settle(OrderNo, TradeNo, Parameters, merchantAdd.AlipayAuthToken);
  127. //开始监听分账状态
  128. Dictionary<string, object> req = new Dictionary<string, object>();
  129. req.Add("TradeNo", TradeNo); //支付宝订单号
  130. req.Add("OrderNo", OrderNo); //商户订单号
  131. req.Add("AuthToken", merchantAdd.AlipayAuthToken);
  132. RedisDbconn.Instance.AddList("AlipayShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
  133. }
  134. else
  135. {
  136. RedisDbconn.Instance.AddList("ConsumerOrders:Divi:1:List", order.Id.ToString());
  137. // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:1:" + order.MerchantId, order);
  138. }
  139. }
  140. }
  141. }
  142. db.SaveChanges();
  143. db.Dispose();
  144. }
  145. catch (Exception ex)
  146. {
  147. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付宝分账队列异常");
  148. }
  149. Thread.Sleep(2000);
  150. }
  151. }
  152. }
  153. }