CheckAlipaySignService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.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. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  47. string content = new AlipayFunction().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. PxcModels.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. string SignUrlList = function.CheckNull(merchantadd.SeoKeyword);
  62. if (string.IsNullOrEmpty(SignUrlList))
  63. {
  64. SignUrlList = "Alipay:" + confirm_url + ";";
  65. }
  66. else
  67. {
  68. SignUrlList = "Alipay:" + confirm_url+ ";" + SignUrlList;
  69. }
  70. merchantadd.SeoKeyword = SignUrlList;
  71. merchantadd.SeoTitle = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString();
  72. db.SaveChanges();
  73. }
  74. return "wait";
  75. }
  76. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_CONFIRM_SUCCESS")
  77. {
  78. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId && m.QueryCount < 2);
  79. if (merchantadd != null)
  80. {
  81. merchantadd.QueryCount = 2;
  82. db.SaveChanges();
  83. }
  84. }
  85. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_APPLY_ORDER_CANCELED")
  86. {
  87. PxcModels.MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  88. if (merchantadd != null)
  89. {
  90. string Season = function.CheckNull(merchantadd.SeoDescription);
  91. if (string.IsNullOrEmpty(Season))
  92. {
  93. Season = "Alipay:" + dic["alipay_open_agent_order_query_response"]["reject_reason"].ToString() + ";";
  94. }
  95. else
  96. {
  97. Season = Regex.Replace(Season, "Alipay:.*?;", "");
  98. Season += "Alipay:" + dic["alipay_open_agent_order_query_response"]["reject_reason"].ToString() + ";";
  99. }
  100. merchantadd.SeoDescription = Season;
  101. merchantadd.QueryCount = -1;
  102. PxcModels.MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  103. if (merchant != null)
  104. {
  105. merchant.Status = -1;
  106. }
  107. db.SaveChanges();
  108. }
  109. }
  110. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_AUDITING")
  111. {
  112. return "wait";
  113. }
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public:merchant:signerr");
  119. }
  120. return "";
  121. }
  122. }
  123. }