CheckAlipaySignService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. merchant.QueryCount = 2;
  104. if (merchant.SignDate == null)
  105. {
  106. merchant.SignDate = DateTime.Now;
  107. }
  108. }
  109. if (merchant.Latitude == 0)
  110. {
  111. //签约成功调用腾讯地图接口获取地址
  112. RedisDbconn.Instance.AddList("GetTencentAddressInfoQueue", "{\"MerChantId\":\"" + merchant.Id + "\",\"Address\":\"" + merchant.Areas + merchant.Address + "\"}");
  113. }
  114. }
  115. }
  116. else
  117. {
  118. merchantadd.QueryCount = 2;
  119. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  120. if (merchant != null)
  121. {
  122. merchant.QueryCount = 2;
  123. if (merchant.SignDate == null)
  124. {
  125. merchant.SignDate = DateTime.Now;
  126. }
  127. }
  128. if (merchant.Latitude == 0)
  129. {
  130. //签约成功调用腾讯地图接口获取地址
  131. RedisDbconn.Instance.AddList("GetTencentAddressInfoQueue", "{\"MerChantId\":\"" + merchant.Id + "\",\"Address\":\"" + merchant.Areas + merchant.Address + "\"}");
  132. }
  133. }
  134. db.SaveChanges();
  135. }
  136. }
  137. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_APPLY_ORDER_CANCELED")
  138. {
  139. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  140. if (merchantadd != null)
  141. {
  142. string reason = "";
  143. if (dic["alipay_open_agent_order_query_response"]["restrict_infos"].Count > 0)
  144. {
  145. reason = dic["alipay_open_agent_order_query_response"]["restrict_infos"][0]["restrict_reason"].ToString();
  146. }
  147. merchantadd.AlipayRemark = reason;
  148. merchantadd.QueryCount = -1;
  149. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  150. if (merchant != null)
  151. {
  152. merchant.QueryCount = -1;
  153. }
  154. db.SaveChanges();
  155. }
  156. }
  157. else if (dic["alipay_open_agent_order_query_response"]["order_status"].ToString() == "MERCHANT_AUDITING")
  158. {
  159. return "wait";
  160. }
  161. }
  162. else
  163. {
  164. if (dic["alipay_open_agent_order_query_response"]["code"].ToString() == "40004" && dic["alipay_open_agent_order_query_response"]["sub_msg"].ToString() == "系统繁忙")
  165. {
  166. return "wait";
  167. }
  168. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  169. if (merchantadd != null)
  170. {
  171. merchantadd.QueryCount = -1;
  172. merchantadd.AlipayRemark = dic["alipay_open_agent_order_query_response"]["sub_msg"].ToString();
  173. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == sign.MerchantAddInfoId);
  174. if (merchant != null)
  175. {
  176. merchant.QueryCount = -1;
  177. }
  178. db.SaveChanges();
  179. }
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "支付宝签约审核队列异常");
  185. }
  186. return "";
  187. }
  188. }
  189. }