AlipayShareService.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.Text.RegularExpressions;
  8. using System.Threading;
  9. namespace MySystem
  10. {
  11. public class AlipayShareService
  12. {
  13. public readonly static AlipayShareService Instance = new AlipayShareService();
  14. private AlipayShareService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(StartListen);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void StartListen()
  23. {
  24. while (true)
  25. {
  26. string content = RedisDbconn.Instance.RPop<string>("AlipayShareQueue");
  27. if (!string.IsNullOrEmpty(content))
  28. {
  29. StartDo(content);
  30. }
  31. else
  32. {
  33. Thread.Sleep(2000);
  34. }
  35. }
  36. }
  37. public void StartDo(string content)
  38. {
  39. try
  40. {
  41. JsonData jsonObj = JsonMapper.ToObject(content);
  42. string TradeNo = jsonObj["TradeNo"].ToString(); //支付宝订单号
  43. string OrderNo = jsonObj["OrderNo"].ToString(); //商户订单号
  44. string AuthToken = jsonObj["AuthToken"].ToString();
  45. string result = AlipayFunction.Instance.QuerySettle(OrderNo, TradeNo, AuthToken);
  46. JsonData resultObj = JsonMapper.ToObject(result);
  47. if(result.Contains("\"alipay_trade_order_settle_query_response\""))
  48. {
  49. string code = resultObj["alipay_trade_order_settle_query_response"]["code"].ToString();
  50. if(code == "10000")
  51. {
  52. WebCMSEntities db = new WebCMSEntities();
  53. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
  54. if (order != null)
  55. {
  56. order.DivideLog = "分账审核结果:" + result;
  57. db.SaveChanges();
  58. RedisDbconn.Instance.AddList("ConsumerOrders:Divi:" + order.PayMode + ":List", order.Id.ToString());
  59. // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + order.PayMode + ":" + order.MerchantId, order);
  60. }
  61. db.Dispose();
  62. }
  63. else
  64. {
  65. RedisDbconn.Instance.AddList("AlipayShareQueue", content);
  66. }
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付宝查询分账异常");
  72. }
  73. }
  74. }
  75. }