CheckAlipaySignService.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // MERCHANT_INFO_HOLD=暂存,提交事务出现业务校验异常时,会暂存申请单信息,可以调用业务接口修正参数,并重新提交
  52. // MERCHANT_AUDITING=审核中,申请信息正在人工审核中
  53. // MERCHANT_CONFIRM=待商户确认,申请信息审核通过,等待联系人确认签约或授权
  54. // MERCHANT_CONFIRM_SUCCESS=商户确认成功,商户同意签约或授权
  55. // MERCHANT_CONFIRM_TIME_OUT=商户超时未确认,如果商户受到确认信息15天内未确认,则需要重新提交申请信息
  56. // MERCHANT_APPLY_ORDER_CANCELED=审核失败或商户拒绝,申请信息审核被驳回,或者商户选择拒绝签约或授权
  57. if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_CONFIRM")
  58. {
  59. string sign_url = dic["alipay_open_agent_order_query_response"]["confirm_url"].ToString();
  60. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  61. if (merchantadd != null)
  62. {
  63. merchantadd.QueryCount = 1;
  64. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  65. if (merchant != null)
  66. {
  67. merchant.QueryCount = 1;
  68. }
  69. string confirm_url = dic["alipay_open_agent_order_query_response"]["confirm_url"].ToString();
  70. string fileName = function.MD5_16(Guid.NewGuid().ToString());
  71. function.CreateQRCode2(sign_url, fileName, "/" + confirm_url);
  72. merchantadd.AlipaySignUrl = confirm_url;
  73. merchantadd.AlipayPid = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString();
  74. db.SaveChanges();
  75. }
  76. return "wait";
  77. }
  78. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_CONFIRM_SUCCESS")
  79. {
  80. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  81. if (merchantadd != null)
  82. {
  83. merchantadd.AlipayPid = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString();
  84. if(dic["alipay_open_agent_order_query_response"]["restrict_infos"].Count > 0)
  85. {
  86. merchantadd.QueryCount = -1;
  87. merchantadd.AlipayRemark = dic["alipay_open_agent_order_query_response"]["restrict_infos"][0]["restrict_reason"].ToString();
  88. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  89. if (merchant != null)
  90. {
  91. merchant.QueryCount = -1;
  92. }
  93. }
  94. else
  95. {
  96. merchantadd.QueryCount = 2;
  97. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  98. if (merchant != null)
  99. {
  100. merchant.QueryCount = 2;
  101. }
  102. }
  103. db.SaveChanges();
  104. }
  105. }
  106. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_APPLY_ORDER_CANCELED")
  107. {
  108. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  109. if (merchantadd != null)
  110. {
  111. string reason = "";
  112. if(dic["alipay_open_agent_order_query_response"]["restrict_infos"].Count > 0)
  113. {
  114. reason = dic["alipay_open_agent_order_query_response"]["restrict_infos"][0]["restrict_reason"].ToString();
  115. }
  116. merchantadd.AlipayRemark = reason;
  117. merchantadd.QueryCount = -1;
  118. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  119. if (merchant != null)
  120. {
  121. merchant.QueryCount = -1;
  122. }
  123. db.SaveChanges();
  124. }
  125. }
  126. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_AUDITING")
  127. {
  128. return "wait";
  129. }
  130. }
  131. else
  132. {
  133. if(dic["alipay_open_agent_order_query_response"]["code"].ToString() == "40004" && dic["alipay_open_agent_order_query_response"]["sub_msg"].ToString() == "系统繁忙")
  134. {
  135. return "wait";
  136. }
  137. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  138. if (merchantadd != null)
  139. {
  140. merchantadd.QueryCount = -1;
  141. merchantadd.AlipayRemark = dic["alipay_open_agent_order_query_response"]["sub_msg"].ToString();
  142. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  143. if (merchant != null)
  144. {
  145. merchant.QueryCount = -1;
  146. }
  147. db.SaveChanges();
  148. }
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付宝签约审核队列异常");
  154. }
  155. return "";
  156. }
  157. }
  158. }