NotifyController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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("wappay")]
  50. [HttpPost]
  51. public IActionResult WapPay()
  52. {
  53. try
  54. {
  55. var notify = _client.Execute<AlipayTradeWapPayNotifyResponse>(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. /// <summary>
  69. /// 扫码支付异步通知
  70. /// </summary>
  71. /// <returns></returns>
  72. [Route("precreate")]
  73. [HttpPost]
  74. public IActionResult Precreate()
  75. {
  76. try
  77. {
  78. var notify = _client.Execute<AlipayTradePrecreateNotifyResponse>(Request);
  79. if ("TRADE_SUCCESS" == notify.TradeStatus)
  80. {
  81. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  82. return Content("success", "text/plain");
  83. }
  84. return NoContent();
  85. }
  86. catch
  87. {
  88. return NoContent();
  89. }
  90. }
  91. }
  92. [Route("notify/wechatpay")]
  93. public class WeChatPayNotifyController : Controller
  94. {
  95. private readonly WeChatPayNotifyClient _client = null;
  96. public WeChatPayNotifyController(WeChatPayNotifyClient client)
  97. {
  98. _client = client;
  99. }
  100. [HttpPost]
  101. public async Task<IActionResult> PostAsync()
  102. {
  103. try
  104. {
  105. var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotifyResponse>(Request);
  106. if (!notify.IsError)
  107. {
  108. if (notify.ResultCode == "SUCCESS")
  109. {
  110. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  111. return Content("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>", "text/xml");
  112. }
  113. }
  114. return NoContent();
  115. }
  116. catch
  117. {
  118. return NoContent();
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// 微信支付退款通知 需要在微信支付后台设置通知地址
  124. /// </summary>
  125. [Route("notify/wechatpay/refund")]
  126. public class WeChatPayRefundNotifyController : Controller
  127. {
  128. private readonly WeChatPayNotifyClient _client = null;
  129. public WeChatPayRefundNotifyController(WeChatPayNotifyClient client)
  130. {
  131. _client = client;
  132. }
  133. [HttpPost]
  134. public async Task<IActionResult> PostAsync()
  135. {
  136. try
  137. {
  138. var notify = await _client.ExecuteAsync<WeChatPayRefundNotifyResponse>(Request);
  139. if (!notify.IsError)
  140. {
  141. if (notify.RefundStatus == "SUCCESS")
  142. {
  143. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  144. return Content("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>", "text/xml");
  145. }
  146. }
  147. return NoContent();
  148. }
  149. catch
  150. {
  151. return NoContent();
  152. }
  153. }
  154. }
  155. [Route("notify/qpay")]
  156. public class QPayNotifyController : Controller
  157. {
  158. private readonly QPayNotifyClient _client = null;
  159. public QPayNotifyController(QPayNotifyClient client)
  160. {
  161. _client = client;
  162. }
  163. [HttpPost]
  164. public async Task<IActionResult> PostAsync()
  165. {
  166. try
  167. {
  168. var notify = await _client.ExecuteAsync<QPayUnifiedOrderNotifyResponse>(Request);
  169. if ("SUCCESS" == notify.TradeState)
  170. {
  171. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  172. return Content("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>", "text/xml");
  173. }
  174. return NoContent();
  175. }
  176. catch
  177. {
  178. return NoContent();
  179. }
  180. }
  181. }
  182. [Route("notify/jdpay")]
  183. public class JdPayNotifyController : Controller
  184. {
  185. private readonly JdPayNotifyClient _client = null;
  186. public JdPayNotifyController(JdPayNotifyClient client)
  187. {
  188. _client = client;
  189. }
  190. [HttpPost]
  191. public async Task<IActionResult> PostAsync()
  192. {
  193. try
  194. {
  195. var notify = await _client.ExecuteAsync<JdPayAsynNotifyResponse>(Request);
  196. Console.WriteLine("TradeNum: " + notify.TradeNum + " tradeType :" + notify.TradeType);
  197. return Content("success", "text/plain");
  198. }
  199. catch
  200. {
  201. return NoContent();
  202. }
  203. }
  204. }
  205. }