NotifyController.cs 5.8 KB

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