MicroPay.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Threading;
  5. namespace LYFZ.WxPayAPI
  6. {
  7. public class MicroPay
  8. {
  9. /**
  10. * 刷卡支付完整业务流程逻辑
  11. * @param body 商品描述
  12. * @param total_fee 总金额
  13. * @param auth_code 支付授权码
  14. * @throws WxPayException
  15. * @return 刷卡支付结果
  16. */
  17. public static Result<WxPayData> Run(string body, string total_fee, string auth_code, string AppId = "", string MacId = "", string key = "", string sub_mch_id = "",bool bMySalt=false)
  18. {
  19. Result<WxPayData> resultData = new Result<WxPayData>();
  20. Log.Info("MicroPay", "Micropay is processing...");
  21. WxPayData data = new WxPayData();
  22. data.SetValue("auth_code", auth_code);//授权码
  23. data.SetValue("body", body);//商品描述
  24. data.SetValue("total_fee", int.Parse(total_fee));//总金额
  25. data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());//产生随机的商户订单号
  26. if (!bMySalt)
  27. {
  28. data.SetValue("sub_mch_id", sub_mch_id);//绑定子商户号
  29. }
  30. else
  31. {
  32. sub_mch_id = "";
  33. //data.SetValue("sub_mch_id", sub_mch_id);//绑定子商户号
  34. }
  35. WxPayData result = WxPayApi.Micropay(data, 120, AppId, MacId, key); //提交被扫支付,接收返回结果
  36. //如果提交被扫支付接口调用失败,则抛异常
  37. if (!result.IsSet("return_code") || result.GetValue("return_code").ToString() == "FAIL")
  38. {
  39. string returnMsg = result.IsSet("return_msg") ? result.GetValue("return_msg").ToString() : "";
  40. Log.Error("MicroPay", "Micropay API interface call failure, result : " + result.ToXml());
  41. //throw new WxPayException("Micropay API interface call failure, return_msg : " + returnMsg);
  42. resultData.Status = false;
  43. resultData.StatusCode = 0;
  44. resultData.Message = returnMsg;
  45. resultData.Data = result;
  46. return resultData;
  47. }
  48. //签名验证
  49. result.CheckSign(key);
  50. Log.Debug("MicroPay", "Micropay response check sign success");
  51. //刷卡支付直接成功
  52. if (result.GetValue("return_code").ToString() == "SUCCESS" &&
  53. result.GetValue("result_code").ToString() == "SUCCESS")
  54. {
  55. Log.Debug("MicroPay", "Micropay business success, result : " + result.ToXml());
  56. resultData.Status = true;
  57. resultData.StatusCode = 1;
  58. resultData.Message = result.ToXml();
  59. resultData.Data = result;
  60. return resultData;
  61. }
  62. /******************************************************************
  63. * 剩下的都是接口调用成功,业务失败的情况
  64. * ****************************************************************/
  65. //1)业务结果明确失败
  66. if (result.GetValue("err_code").ToString() != "USERPAYING" &&
  67. result.GetValue("err_code").ToString() != "SYSTEMERROR")
  68. {
  69. Log.Error("MicroPay", "micropay API interface call success, business failure, result : " + result.ToXml());
  70. resultData.Status = false;
  71. resultData.StatusCode = 0;
  72. resultData.Message = result.ToXml();
  73. resultData.Data = result;
  74. return resultData;
  75. }
  76. //2)不能确定是否失败,需查单
  77. //用商户订单号去查单
  78. string out_trade_no = data.GetValue("out_trade_no").ToString();
  79. //确认支付是否成功,每隔一段时间查询一次订单,共查询10次
  80. int queryTimes = 10;//查询次数计数器
  81. while (queryTimes-- > 0)
  82. {
  83. int succResult = 0;//查询结果
  84. WxPayData queryResult = Query(out_trade_no, out succResult, AppId, MacId, key, sub_mch_id);
  85. //如果需要继续查询,则等待2s后继续
  86. if (succResult == 2)
  87. {
  88. Thread.Sleep(2000);
  89. continue;
  90. }
  91. //查询成功,返回订单查询接口返回的数据
  92. else if (succResult == 1)
  93. {
  94. Log.Debug("MicroPay", "Mircopay success, return order query result : " + queryResult.ToXml());
  95. resultData.Status = true;
  96. resultData.StatusCode = 1;
  97. resultData.Message = queryResult.ToXml();
  98. resultData.Data = queryResult;
  99. return resultData;
  100. }
  101. //订单交易失败,直接返回刷卡支付接口返回的结果,失败原因会在err_code中描述
  102. else
  103. {
  104. Log.Error("MicroPay", "Micropay failure, return micropay result : " + result.ToXml());
  105. resultData.Status = false;
  106. resultData.StatusCode = 0;
  107. resultData.Message = result.ToXml();
  108. resultData.Data = result;
  109. return resultData;
  110. }
  111. }
  112. //确认失败,则撤销订单
  113. Log.Error("MicroPay", "Micropay failure, Reverse order is processing...");
  114. if (!Cancel(out_trade_no))
  115. {
  116. Log.Error("MicroPay", "Reverse order failure");
  117. throw new WxPayException("Reverse order failure!");
  118. }
  119. if (result.GetValue("return_msg").ToString().ToUpper() == "OK")
  120. {
  121. resultData.Status = true;
  122. resultData.StatusCode = 1;
  123. }
  124. else
  125. {
  126. resultData.Status = false;
  127. resultData.StatusCode = 0;
  128. }
  129. resultData.Message = result.ToXml();
  130. resultData.Data = result;
  131. return resultData;
  132. }
  133. /**
  134. *
  135. * 查询订单情况
  136. * @param string out_trade_no 商户订单号
  137. * @param int succCode 查询订单结果:0表示订单不成功,1表示订单成功,2表示继续查询
  138. * @return 订单查询接口返回的数据,参见协议接口
  139. */
  140. public static WxPayData Query(string out_trade_no, out int succCode, string AppId = "", string MacId = "", string key = "", string sub_mch_id = "")
  141. {
  142. WxPayData queryOrderInput = new WxPayData();
  143. queryOrderInput.SetValue("out_trade_no",out_trade_no);
  144. if (!string.IsNullOrEmpty(sub_mch_id))
  145. {
  146. queryOrderInput.SetValue("sub_mch_id", sub_mch_id);
  147. }
  148. WxPayData result = WxPayApi.OrderQuery(queryOrderInput, 20, AppId, MacId, key);
  149. if(result.GetValue("return_code").ToString() == "SUCCESS"
  150. && result.GetValue("result_code").ToString() == "SUCCESS")
  151. {
  152. //支付成功
  153. if (result.GetValue("trade_state") != null && result.GetValue("trade_state").ToString() == "SUCCESS")
  154. {
  155. succCode = 1;
  156. return result;
  157. }
  158. //用户支付中,需要继续查询
  159. else if (result.GetValue("trade_state") != null && result.GetValue("trade_state").ToString() == "USERPAYING")
  160. {
  161. succCode = 2;
  162. return result;
  163. }
  164. else if (result.GetValue("trade_state") != null && result.GetValue("trade_state").ToString() == "NOTPAY")
  165. {
  166. succCode = 3;
  167. return result;
  168. }
  169. else if (result.GetValue("trade_state") != null && result.GetValue("trade_state").ToString() == "PAYERROR")
  170. {
  171. succCode = 4;
  172. return result;
  173. }
  174. }
  175. //如果返回错误码为“此交易订单号不存在”则直接认定失败
  176. if (result.GetValue("err_code") != null && result.GetValue("err_code").ToString() == "ORDERNOTEXIST")
  177. {
  178. succCode = 0;
  179. }
  180. else
  181. {
  182. //如果是系统错误,则后续继续
  183. succCode = 2;
  184. }
  185. return result;
  186. }
  187. /**
  188. *
  189. * 撤销订单,如果失败会重复调用10次
  190. * @param string out_trade_no 商户订单号
  191. * @param depth 调用次数,这里用递归深度表示
  192. * @return false表示撤销失败,true表示撤销成功
  193. */
  194. public static bool Cancel(string out_trade_no, int depth = 0)
  195. {
  196. if(depth > 10)
  197. {
  198. return false;
  199. }
  200. WxPayData reverseInput = new WxPayData();
  201. reverseInput.SetValue("out_trade_no",out_trade_no);
  202. WxPayData result = WxPayApi.Reverse(reverseInput);
  203. //接口调用失败
  204. if(result.GetValue("return_code").ToString() != "SUCCESS")
  205. {
  206. return false;
  207. }
  208. //如果结果为success且不需要重新调用撤销,则表示撤销成功
  209. if(result.GetValue("result_code").ToString() != "SUCCESS" && result.GetValue("recall").ToString() == "N")
  210. {
  211. return true;
  212. }
  213. else if(result.GetValue("recall").ToString() == "Y")
  214. {
  215. return Cancel(out_trade_no, ++depth);
  216. }
  217. return false;
  218. }
  219. }
  220. }