NotifyController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Threading.Tasks;
  3. using Essensoft.AspNetCore.Payment.Alipay;
  4. using Essensoft.AspNetCore.Payment.Alipay.Notify;
  5. using Essensoft.AspNetCore.Payment.WeChatPay;
  6. using Essensoft.AspNetCore.Payment.WeChatPay.Notify;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Options;
  9. namespace WebApplicationSample.Controllers
  10. {
  11. #region 支付宝异步通知
  12. [Route("notify/alipay")]
  13. public class AlipayNotifyController : Controller
  14. {
  15. private readonly IAlipayNotifyClient _client;
  16. private readonly IOptions<AlipayOptions> _optionsAccessor;
  17. public AlipayNotifyController(IAlipayNotifyClient client, IOptions<AlipayOptions> optionsAccessor)
  18. {
  19. _client = client;
  20. _optionsAccessor = optionsAccessor;
  21. }
  22. /// <summary>
  23. /// 扫码支付异步通知
  24. /// </summary>
  25. /// <returns></returns>
  26. [Route("precreate")]
  27. [HttpPost]
  28. public async Task<IActionResult> Precreate()
  29. {
  30. try
  31. {
  32. var notify = await _client.ExecuteAsync<AlipayTradePrecreateNotify>(Request, _optionsAccessor.Value);
  33. if (notify.TradeStatus == AlipayTradeStatus.Success)
  34. {
  35. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  36. return AlipayNotifyResult.Success;
  37. }
  38. return NoContent();
  39. }
  40. catch
  41. {
  42. return NoContent();
  43. }
  44. }
  45. /// <summary>
  46. /// APP支付异步通知
  47. /// </summary>
  48. /// <returns></returns>
  49. [Route("apppay")]
  50. [HttpPost]
  51. public async Task<IActionResult> AppPay()
  52. {
  53. try
  54. {
  55. var notify = await _client.ExecuteAsync<AlipayTradeAppPayNotify>(Request, _optionsAccessor.Value);
  56. if (notify.TradeStatus == AlipayTradeStatus.Success)
  57. {
  58. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  59. return AlipayNotifyResult.Success;
  60. }
  61. return NoContent();
  62. }
  63. catch
  64. {
  65. return NoContent();
  66. }
  67. }
  68. /// <summary>
  69. /// 电脑网站支付异步通知
  70. /// </summary>
  71. /// <returns></returns>
  72. [Route("pagepay")]
  73. [HttpPost]
  74. public async Task<IActionResult> PagePay()
  75. {
  76. try
  77. {
  78. var notify = await _client.ExecuteAsync<AlipayTradePagePayNotify>(Request, _optionsAccessor.Value);
  79. if (notify.TradeStatus == AlipayTradeStatus.Success)
  80. {
  81. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  82. return AlipayNotifyResult.Success;
  83. }
  84. return NoContent();
  85. }
  86. catch
  87. {
  88. return NoContent();
  89. }
  90. }
  91. /// <summary>
  92. /// 手机网站支付异步通知
  93. /// </summary>
  94. /// <returns></returns>
  95. [Route("wappay")]
  96. [HttpPost]
  97. public async Task<IActionResult> WapPay()
  98. {
  99. try
  100. {
  101. var notify = await _client.ExecuteAsync<AlipayTradeWapPayNotify>(Request, _optionsAccessor.Value);
  102. if (notify.TradeStatus == AlipayTradeStatus.Success)
  103. {
  104. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  105. return AlipayNotifyResult.Success;
  106. }
  107. return NoContent();
  108. }
  109. catch
  110. {
  111. return NoContent();
  112. }
  113. }
  114. }
  115. #endregion
  116. #region 微信支付异步通知
  117. [Route("notify/wechatpay")]
  118. public class WeChatPayNotifyController : Controller
  119. {
  120. private readonly IWeChatPayNotifyClient _client;
  121. private readonly IOptions<WeChatPayOptions> _optionsAccessor;
  122. public WeChatPayNotifyController(IWeChatPayNotifyClient client, IOptions<WeChatPayOptions> optionsAccessor)
  123. {
  124. _client = client;
  125. _optionsAccessor = optionsAccessor;
  126. }
  127. /// <summary>
  128. /// 统一下单支付结果通知
  129. /// </summary>
  130. /// <returns></returns>
  131. [Route("unifiedorder")]
  132. [HttpPost]
  133. public async Task<IActionResult> Unifiedorder()
  134. {
  135. try
  136. {
  137. var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
  138. if (notify.ReturnCode == WeChatPayCode.Success)
  139. {
  140. if (notify.ResultCode == WeChatPayCode.Success)
  141. {
  142. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  143. return WeChatPayNotifyResult.Success;
  144. }
  145. }
  146. return NoContent();
  147. }
  148. catch
  149. {
  150. return NoContent();
  151. }
  152. }
  153. /// <summary>
  154. /// 退款结果通知
  155. /// </summary>
  156. /// <returns></returns>
  157. [Route("refund")]
  158. [HttpPost]
  159. public async Task<IActionResult> Refund()
  160. {
  161. try
  162. {
  163. var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request, _optionsAccessor.Value);
  164. if (notify.ReturnCode == WeChatPayCode.Success)
  165. {
  166. if (notify.RefundStatus == WeChatPayCode.Success)
  167. {
  168. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  169. return WeChatPayNotifyResult.Success;
  170. }
  171. }
  172. return NoContent();
  173. }
  174. catch
  175. {
  176. return NoContent();
  177. }
  178. }
  179. }
  180. #endregion
  181. }