AlipayPayBackService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. var machines = db.PosMachines.Select(m => new { m.DeviceKind, m.BrandId, m.Detail, m.BindMerchantId }).Where(m => m.DeviceKind == "1" && m.BindMerchantId == order.MerchantId);
  76. foreach (var machine in machines)
  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. // decimal hdfee = 0.0038M * order.PayMoney * (1 - set.GetPercent - 0.01M);
  134. // hdfee = decimal.Parse(hdfee.ToString("f2"));
  135. // decimal servicefee = 0.01M * order.PayMoney - hdfee;
  136. // servicefee = decimal.Parse(servicefee.ToString("f2"));
  137. List<RoyaltyParameters> Parameters = new List<RoyaltyParameters>();
  138. Parameters.Add(new RoyaltyParameters()
  139. {
  140. Account = AlipayFunction.Instance.Pid,
  141. Amount = fee.ToString("f2"),
  142. });
  143. string ProfitShareResult = AlipayFunction.Instance.Settle(OrderNo, TradeNo, Parameters, merchantAdd.AlipayAuthToken);
  144. order.DivideLog = "请求分账日志:" + ProfitShareResult;
  145. order.DivideFlag = 1;
  146. db.SaveChanges();
  147. //开始监听分账状态
  148. Dictionary<string, object> req = new Dictionary<string, object>();
  149. req.Add("TradeNo", TradeNo); //支付宝订单号
  150. req.Add("OrderNo", OrderNo); //商户订单号
  151. req.Add("AuthToken", merchantAdd.AlipayAuthToken);
  152. RedisDbconn.Instance.AddList("AlipayShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
  153. }
  154. else
  155. {
  156. RedisDbconn.Instance.AddList("ConsumerOrders:Divi:1:List", order.Id.ToString());
  157. // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:1:" + order.MerchantId, order);
  158. }
  159. }
  160. }
  161. }
  162. db.SaveChanges();
  163. db.Dispose();
  164. }
  165. catch (Exception ex)
  166. {
  167. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付宝分账队列异常");
  168. }
  169. Thread.Sleep(2000);
  170. }
  171. }
  172. //触发分账接口
  173. public void StartDivi()
  174. {
  175. Thread th = new Thread(ListenDivi);
  176. th.IsBackground = true;
  177. th.Start();
  178. }
  179. public void ListenDivi()
  180. {
  181. bool op = true;
  182. while (op)
  183. {
  184. string content = RedisDbconn.Instance.RPop<string>("AddAlipayDiviQueue");
  185. if (!string.IsNullOrEmpty(content))
  186. {
  187. try
  188. {
  189. int id = int.Parse(function.CheckInt(content));
  190. WebCMSEntities db = new WebCMSEntities();
  191. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == id);
  192. if (order != null)
  193. {
  194. MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
  195. MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
  196. if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
  197. {
  198. //发起分账
  199. decimal fee = order.PayMoney * 0.01M;
  200. if (fee >= 0.01M)
  201. {
  202. string TradeNo = order.SeoTitle;
  203. string OrderNo = order.OrderNo;
  204. List<RoyaltyParameters> Parameters = new List<RoyaltyParameters>();
  205. Parameters.Add(new RoyaltyParameters()
  206. {
  207. Account = AlipayFunction.Instance.Pid,
  208. Amount = fee.ToString("f2"),
  209. });
  210. string ProfitShareResult = AlipayFunction.Instance.Settle(OrderNo, TradeNo, Parameters, merchantAdd.AlipayAuthToken);
  211. order.DivideLog = "请求分账日志:" + ProfitShareResult;
  212. order.DivideFlag = 1;
  213. db.SaveChanges();
  214. //开始监听分账状态
  215. Dictionary<string, object> req = new Dictionary<string, object>();
  216. req.Add("TradeNo", TradeNo); //支付宝订单号
  217. req.Add("OrderNo", OrderNo); //商户订单号
  218. req.Add("AuthToken", merchantAdd.AlipayAuthToken);
  219. RedisDbconn.Instance.AddList("AlipayShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
  220. }
  221. }
  222. }
  223. db.Dispose();
  224. }
  225. catch (Exception ex)
  226. {
  227. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "补微信分账异常");
  228. }
  229. }
  230. else
  231. {
  232. Thread.Sleep(2000);
  233. }
  234. }
  235. }
  236. }
  237. }