CheckAlipaySignService.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. string confirm_url = "/static/alipay_confirm_url/";
  59. string fileName = function.MD5_16(Guid.NewGuid().ToString());
  60. function.CreateQRCode2(sign_url, fileName, "/" + confirm_url);
  61. merchantadd.AlipaySignUrl = confirm_url;
  62. merchantadd.AlipayPid = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString();
  63. db.SaveChanges();
  64. }
  65. return "wait";
  66. }
  67. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_CONFIRM_SUCCESS")
  68. {
  69. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.QueryCount < 2);
  70. if (merchantadd != null)
  71. {
  72. merchantadd.QueryCount = 2;
  73. db.SaveChanges();
  74. }
  75. }
  76. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_APPLY_ORDER_CANCELED")
  77. {
  78. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  79. if (merchantadd != null)
  80. {
  81. string Season = function.CheckNull(merchantadd.SeoDescription);
  82. if (string.IsNullOrEmpty(Season))
  83. {
  84. Season = "Alipay:" + dic["alipay_open_agent_order_query_response"]["reject_reason"].ToString() + ";";
  85. }
  86. else
  87. {
  88. Season = Regex.Replace(Season, "Alipay:.*?;", "");
  89. Season += "Alipay:" + dic["alipay_open_agent_order_query_response"]["reject_reason"].ToString() + ";";
  90. }
  91. merchantadd.SeoDescription = Season;
  92. merchantadd.QueryCount = -1;
  93. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  94. if (merchant != null)
  95. {
  96. merchant.Status = -1;
  97. }
  98. db.SaveChanges();
  99. }
  100. }
  101. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_AUDITING")
  102. {
  103. return "wait";
  104. }
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public:merchant:signerr");
  110. }
  111. return "";
  112. }
  113. }
  114. }