NotifyController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Essensoft.AspNetCore.Alipay;
  2. using Essensoft.AspNetCore.Alipay.Notify;
  3. using Essensoft.AspNetCore.JdPay;
  4. using Essensoft.AspNetCore.JdPay.Notify;
  5. using Essensoft.AspNetCore.QPay;
  6. using Essensoft.AspNetCore.QPay.Notify;
  7. using Essensoft.AspNetCore.WeChatPay;
  8. using Essensoft.AspNetCore.WeChatPay.Notify;
  9. using Microsoft.AspNetCore.Mvc;
  10. using System;
  11. using System.Threading.Tasks;
  12. namespace WebApplicationSample.Controllers
  13. {
  14. [Route("notify/alipay")]
  15. public class AlipayNotifyController : Controller
  16. {
  17. private readonly AlipayNotifyClient _client = null;
  18. public AlipayNotifyController(AlipayNotifyClient client)
  19. {
  20. _client = client;
  21. }
  22. /// <summary>
  23. /// 网页支付异步通知
  24. /// </summary>
  25. /// <returns></returns>
  26. [Route("pagepay")]
  27. [HttpPost]
  28. public IActionResult PagePay()
  29. {
  30. try
  31. {
  32. var notify = _client.Execute<AlipayTradePagePayNotifyResponse>(Request);
  33. if ("TRADE_SUCCESS" == notify.TradeStatus)
  34. {
  35. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  36. return Content("success", "text/plain");
  37. }
  38. return NoContent();
  39. }
  40. catch
  41. {
  42. return NoContent();
  43. }
  44. }
  45. /// <summary>
  46. /// 扫码支付异步通知
  47. /// </summary>
  48. /// <returns></returns>
  49. [Route("precreate")]
  50. [HttpPost]
  51. public IActionResult Precreate()
  52. {
  53. try
  54. {
  55. var notify = _client.Execute<AlipayTradePrecreateNotifyResponse>(Request);
  56. if ("TRADE_SUCCESS" == notify.TradeStatus)
  57. {
  58. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  59. return Content("success", "text/plain");
  60. }
  61. return NoContent();
  62. }
  63. catch
  64. {
  65. return NoContent();
  66. }
  67. }
  68. }
  69. [Route("notify/wechatpay")]
  70. public class WeChatPayNotifyController : Controller
  71. {
  72. private readonly WeChatPayNotifyClient _client = null;
  73. public WeChatPayNotifyController(WeChatPayNotifyClient client)
  74. {
  75. _client = client;
  76. }
  77. [HttpPost]
  78. public async Task<IActionResult> PostAsync()
  79. {
  80. try
  81. {
  82. var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotifyResponse>(Request);
  83. if (!notify.IsError)
  84. {
  85. if (notify.ResultCode == "SUCCESS")
  86. {
  87. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  88. return Content("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>", "text/xml");
  89. }
  90. }
  91. return NoContent();
  92. }
  93. catch
  94. {
  95. return NoContent();
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// 微信支付退款通知 需要在微信支付后台设置通知地址
  101. /// </summary>
  102. [Route("notify/wechatpay/refund")]
  103. public class WeChatPayRefundNotifyController : Controller
  104. {
  105. private readonly WeChatPayNotifyClient _client = null;
  106. public WeChatPayRefundNotifyController(WeChatPayNotifyClient client)
  107. {
  108. _client = client;
  109. }
  110. [HttpPost]
  111. public async Task<IActionResult> PostAsync()
  112. {
  113. try
  114. {
  115. var notify = await _client.ExecuteAsync<WeChatPayRefundNotifyResponse>(Request);
  116. if (!notify.IsError)
  117. {
  118. if (notify.RefundStatus == "SUCCESS")
  119. {
  120. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  121. return Content("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>", "text/xml");
  122. }
  123. }
  124. return NoContent();
  125. }
  126. catch
  127. {
  128. return NoContent();
  129. }
  130. }
  131. }
  132. [Route("notify/qpay")]
  133. public class QPayNotifyController : Controller
  134. {
  135. private readonly QPayNotifyClient _client = null;
  136. public QPayNotifyController(QPayNotifyClient client)
  137. {
  138. _client = client;
  139. }
  140. [HttpPost]
  141. public async Task<IActionResult> PostAsync()
  142. {
  143. try
  144. {
  145. var notify = await _client.ExecuteAsync<QPayUnifiedOrderNotifyResponse>(Request);
  146. if ("SUCCESS" == notify.TradeState)
  147. {
  148. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  149. return Content("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>", "text/xml");
  150. }
  151. return NoContent();
  152. }
  153. catch
  154. {
  155. return NoContent();
  156. }
  157. }
  158. }
  159. [Route("notify/jdpay")]
  160. public class JdPayNotifyController : Controller
  161. {
  162. private readonly JdPayNotifyClient _client = null;
  163. public JdPayNotifyController(JdPayNotifyClient client)
  164. {
  165. _client = client;
  166. }
  167. [HttpPost]
  168. public async Task<IActionResult> PostAsync()
  169. {
  170. try
  171. {
  172. var notify = await _client.ExecuteAsync<JdPayAsynNotifyResponse>(Request);
  173. Console.WriteLine("TradeNum: " + notify.TradeNum + " tradeType :" + notify.TradeType);
  174. return Content("success", "text/plain");
  175. }
  176. catch
  177. {
  178. return NoContent();
  179. }
  180. }
  181. }
  182. }