CheckAlipaySignService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. //{"alipay_open_agent_order_query_response":{"code":"10000","msg":"Success","order_no":"20230704034000100000030319581766","product_agent_status_infos":[{"product_code":"FACE_TO_FACE_PAYMENT","product_name":"当面付","status":"EFFECTED"}],"order_status":"MERCHANT_CONFIRM_SUCCESS","merchant_pid":"2088641525908640"},"sign":"UyyrAf+Q+2jd5VVsaKUyWTMW8gFadUWWQIoYSTLzYFx0NOetjbN4XE9xq2kYvoJIYgQQOhohDKyAZZ7FOnOz9J849AXaqW1VYfVY9FSDvnSCdcUrAdyXGQEp7TcbnMtP5RTXZO+VCKRpvvUc/LVdXMPMetUbzhyaahQf/T512aQNuYFjxH5QfMdzGDwevt47iNoANITwXqns1A+ETBCuxHlx2Ps7WXpPxzqvFYK9CBD5MQCaq7IrDityzAUcMFiMjE3LlXb1zgg6PlsHnJBZm0Zde1PYzKZcHKnx2bxWl2upwpC3uj6drxzkGqzcZ4D21cTlndm8GIRLo2XHpd79Hw=="}
  81. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  82. if (merchantadd != null)
  83. {
  84. merchantadd.AlipayPid = dic["alipay_open_agent_order_query_response"]["merchant_pid"].ToString();
  85. if (content.Contains("restrict_infos"))
  86. {
  87. if (dic["alipay_open_agent_order_query_response"]["restrict_infos"].Count > 0)
  88. {
  89. merchantadd.QueryCount = -1;
  90. merchantadd.AlipayRemark = dic["alipay_open_agent_order_query_response"]["restrict_infos"][0]["restrict_reason"].ToString();
  91. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  92. if (merchant != null)
  93. {
  94. merchant.QueryCount = -1;
  95. }
  96. }
  97. else
  98. {
  99. merchantadd.QueryCount = 2;
  100. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  101. if (merchant != null)
  102. {
  103. if(merchant.Status < 2 && merchant.QueryCount < 2)
  104. {
  105. RedisDbconn.Instance.AddList("MpMerchantActQueue", "{\"userId\":\"" + merchant.UserId + "\",\"statDate\":\"" + DateTime.Now.ToString("yyyyMMdd") + "\",\"statMonth\":\"" + DateTime.Now.ToString("yyyyMM") + "\"}");
  106. Utils.Instance.SendActData(merchant);
  107. }
  108. merchant.QueryCount = 2;
  109. if (merchant.SignDate == null)
  110. {
  111. merchant.SignDate = DateTime.Now;
  112. }
  113. }
  114. if (merchant.Latitude == 0)
  115. {
  116. //签约成功调用腾讯地图接口获取地址
  117. RedisDbconn.Instance.AddList("GetTencentAddressInfoQueue", "{\"MerChantId\":\"" + merchant.Id + "\",\"Address\":\"" + merchant.Areas + merchant.Address + "\"}");
  118. }
  119. }
  120. }
  121. else
  122. {
  123. merchantadd.QueryCount = 2;
  124. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  125. if (merchant != null)
  126. {
  127. if(merchant.Status < 2 && merchant.QueryCount < 2)
  128. {
  129. RedisDbconn.Instance.AddList("MpMerchantActQueue", "{\"userId\":\"" + merchant.UserId + "\",\"statDate\":\"" + DateTime.Now.ToString("yyyyMMdd") + "\",\"statMonth\":\"" + DateTime.Now.ToString("yyyyMM") + "\"}");
  130. Utils.Instance.SendActData(merchant);
  131. }
  132. merchant.QueryCount = 2;
  133. if (merchant.SignDate == null)
  134. {
  135. merchant.SignDate = DateTime.Now;
  136. }
  137. }
  138. if (merchant.Latitude == 0)
  139. {
  140. //签约成功调用腾讯地图接口获取地址
  141. RedisDbconn.Instance.AddList("GetTencentAddressInfoQueue", "{\"MerChantId\":\"" + merchant.Id + "\",\"Address\":\"" + merchant.Areas + merchant.Address + "\"}");
  142. }
  143. }
  144. db.SaveChanges();
  145. }
  146. }
  147. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_APPLY_ORDER_CANCELED")
  148. {
  149. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  150. if (merchantadd != null)
  151. {
  152. string reason = "";
  153. if (dic["alipay_open_agent_order_query_response"]["restrict_infos"].Count > 0)
  154. {
  155. reason = dic["alipay_open_agent_order_query_response"]["restrict_infos"][0]["restrict_reason"].ToString();
  156. }
  157. merchantadd.AlipayRemark = reason;
  158. merchantadd.QueryCount = -1;
  159. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  160. if (merchant != null)
  161. {
  162. merchant.QueryCount = -1;
  163. }
  164. db.SaveChanges();
  165. }
  166. }
  167. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_AUDITING")
  168. {
  169. return "wait";
  170. }
  171. }
  172. else
  173. {
  174. if (dic["alipay_open_agent_order_query_response"]["code"].ToString() == "40004" && dic["alipay_open_agent_order_query_response"]["sub_msg"].ToString() == "系统繁忙")
  175. {
  176. return "wait";
  177. }
  178. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  179. if (merchantadd != null)
  180. {
  181. merchantadd.QueryCount = -1;
  182. merchantadd.AlipayRemark = dic["alipay_open_agent_order_query_response"]["sub_msg"].ToString();
  183. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  184. if (merchant != null)
  185. {
  186. merchant.QueryCount = -1;
  187. }
  188. db.SaveChanges();
  189. }
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付宝签约审核队列异常");
  195. }
  196. return "";
  197. }
  198. }
  199. }