AlipayPayBackService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.PxcModels;
  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. function.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(merchant.Sort == 0)
  108. {
  109. string content = AlipayFunction.Instance.BindSettleRelation(DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), merchantAdd.SeoTitle);
  110. merchantAdd.Sort = 1;
  111. db.SaveChanges();
  112. }
  113. //发起分账
  114. decimal fee = order.PayMoney * 0.99M;
  115. if(fee >= 1)
  116. {
  117. string TradeNo = order.SeoTitle;
  118. string OrderNo = order.OrderNo;
  119. List<RoyaltyParameters> Parameters = new List<RoyaltyParameters>();
  120. Parameters.Add(new RoyaltyParameters()
  121. {
  122. Account = merchantAdd.SeoTitle,
  123. Amount = fee.ToString("f2"),
  124. });
  125. AlipayFunction.Instance.Settle(OrderNo, TradeNo, Parameters);
  126. //开始监听分账状态
  127. Dictionary<string, object> req = new Dictionary<string, object>();
  128. req.Add("TradeNo", TradeNo); //支付宝订单号
  129. req.Add("OrderNo", OrderNo); //商户订单号
  130. RedisDbconn.Instance.AddList("AlipayShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
  131. }
  132. else
  133. {
  134. RedisDbconn.Instance.AddList("ConsumerOrders:Divi:1:List", order.Id.ToString());
  135. // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:1:" + order.MerchantId, order);
  136. }
  137. }
  138. }
  139. }
  140. db.SaveChanges();
  141. db.Dispose();
  142. }
  143. catch (Exception ex)
  144. {
  145. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付宝分账队列异常");
  146. }
  147. Thread.Sleep(2000);
  148. }
  149. }
  150. }
  151. }