CheckAlipaySignService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 CheckAlipaySignService
  12. {
  13. public readonly static CheckAlipaySignService Instance = new CheckAlipaySignService();
  14. private CheckAlipaySignService()
  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>("AlipaySignQueue");
  27. if (!string.IsNullOrEmpty(content))
  28. {
  29. string result = StartDo(Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantSign>(content));
  30. if(result == "wait")
  31. {
  32. Thread.Sleep(10000);
  33. RedisDbconn.Instance.AddList("AlipaySignQueue", content);
  34. }
  35. }
  36. else
  37. {
  38. Thread.Sleep(2000);
  39. }
  40. }
  41. }
  42. public string StartDo(MerchantSign sign)
  43. {
  44. try
  45. {
  46. WebCMSEntities db = new WebCMSEntities();
  47. string content = AlipayFunction.Instance.QuerySignStatus(sign.BusinessCode);
  48. JsonData dic = JsonMapper.ToObject(content);
  49. if (dic["alipay_open_agent_order_query_response"]["code"].ToString() == "10000")
  50. {
  51. if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_CONFIRM")
  52. {
  53. string sign_url = dic["alipay_open_agent_order_query_response"]["confirm_url"].ToString();
  54. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.QueryCount == 0);
  55. if (merchantadd != null)
  56. {
  57. merchantadd.QueryCount = 1;
  58. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  59. if (merchant != null)
  60. {
  61. merchant.QueryCount = 1;
  62. }
  63. string confirm_url = "/static/alipay_confirm_url/";
  64. string fileName = function.MD5_16(Guid.NewGuid().ToString());
  65. function.CreateQRCode2(sign_url, fileName, "/" + confirm_url);
  66. merchantadd.AlipaySignUrl = confirm_url;
  67. merchantadd.AlipayPid = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString();
  68. db.SaveChanges();
  69. }
  70. return "wait";
  71. }
  72. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_CONFIRM_SUCCESS")
  73. {
  74. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.QueryCount < 2);
  75. if (merchantadd != null)
  76. {
  77. merchantadd.QueryCount = 2;
  78. merchantadd.AlipayPid = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString();
  79. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  80. if (merchant != null)
  81. {
  82. merchant.QueryCount = 2;
  83. }
  84. db.SaveChanges();
  85. }
  86. }
  87. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_APPLY_ORDER_CANCELED")
  88. {
  89. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  90. if (merchantadd != null)
  91. {
  92. string Season = function.CheckNull(merchantadd.SeoDescription);
  93. if (string.IsNullOrEmpty(Season))
  94. {
  95. Season = "Alipay:" + dic["alipay_open_agent_order_query_response"]["reject_reason"].ToString() + ";";
  96. }
  97. else
  98. {
  99. Season = Regex.Replace(Season, "Alipay:.*?;", "");
  100. Season += "Alipay:" + dic["alipay_open_agent_order_query_response"]["reject_reason"].ToString() + ";";
  101. }
  102. merchantadd.SeoDescription = Season;
  103. merchantadd.QueryCount = -1;
  104. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  105. if (merchant != null)
  106. {
  107. merchant.QueryCount = -1;
  108. }
  109. db.SaveChanges();
  110. }
  111. }
  112. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_AUDITING")
  113. {
  114. return "wait";
  115. }
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public:merchant:signerr");
  121. }
  122. return "";
  123. }
  124. }
  125. }