WeChatPayV3NotifyController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Threading.Tasks;
  2. using Essensoft.Paylink.WeChatPay;
  3. using Essensoft.Paylink.WeChatPay.V3;
  4. using Essensoft.Paylink.WeChatPay.V3.Notify;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. namespace WebApplicationSample.Controllers
  9. {
  10. [Route("wechatpay/v3/notify")]
  11. public class WeChatPayV3NotifyController : Controller
  12. {
  13. private readonly ILogger<WeChatPayV3NotifyController> _logger;
  14. private readonly IWeChatPayClient _client;
  15. private readonly IOptions<WeChatPayOptions> _optionsAccessor;
  16. public WeChatPayV3NotifyController(ILogger<WeChatPayV3NotifyController> logger, IWeChatPayClient client, IOptions<WeChatPayOptions> optionsAccessor)
  17. {
  18. _logger = logger;
  19. _client = client;
  20. _optionsAccessor = optionsAccessor;
  21. }
  22. /// <summary>
  23. /// 支付结果通知
  24. /// </summary>
  25. [Route("transactions")]
  26. [HttpPost]
  27. public async Task<IActionResult> Transactions()
  28. {
  29. try
  30. {
  31. var notify = await _client.ExecuteAsync<WeChatPayTransactionsNotify>(Request, _optionsAccessor.Value);
  32. if (notify.TradeState == WeChatPayTradeState.Success)
  33. {
  34. _logger.LogInformation("支付结果通知 => OutTradeNo: " + notify.OutTradeNo);
  35. return WeChatPayNotifyResult.Success;
  36. }
  37. return WeChatPayNotifyResult.Failure;
  38. }
  39. catch (WeChatPayException ex)
  40. {
  41. _logger.LogWarning("出现异常: " + ex.Message);
  42. return WeChatPayNotifyResult.Failure;
  43. }
  44. }
  45. /// <summary>
  46. /// 退款结果通知
  47. /// </summary>
  48. [Route("refund")]
  49. [HttpPost]
  50. public async Task<IActionResult> Refund()
  51. {
  52. try
  53. {
  54. var notify = await _client.ExecuteAsync<WeChatPayRefundDomesticRefundsNotify>(Request, _optionsAccessor.Value);
  55. if (notify.RefundStatus == WeChatPayRefundStatus.Success)
  56. {
  57. _logger.LogInformation("退款结果通知 => OutTradeNo: " + notify.OutTradeNo);
  58. return WeChatPayNotifyResult.Success;
  59. }
  60. return WeChatPayNotifyResult.Failure;
  61. }
  62. catch (WeChatPayException ex)
  63. {
  64. _logger.LogWarning("出现异常: " + ex.Message);
  65. return WeChatPayNotifyResult.Failure;
  66. }
  67. }
  68. #region 微信支付分
  69. /// <summary>
  70. /// 开启/解除授权服务回调通知
  71. /// </summary>
  72. [Route("score/permissions")]
  73. [HttpPost]
  74. public async Task<IActionResult> Permissions()
  75. {
  76. try
  77. {
  78. var notify = await _client.ExecuteAsync<WeChatPayScoreUserOpenOrCloseNotify>(Request, _optionsAccessor.Value);
  79. if (notify.UserServiceStatus == WeChatPayScoreUserServiceStatus.Opened ||
  80. notify.UserServiceStatus == WeChatPayScoreUserServiceStatus.Closed)
  81. {
  82. _logger.LogInformation("开启/解除授权服务回调通知 => " + notify.Body);
  83. return WeChatPayNotifyResult.Success;
  84. }
  85. return WeChatPayNotifyResult.Failure;
  86. }
  87. catch (WeChatPayException ex)
  88. {
  89. _logger.LogWarning("出现异常: " + ex.Message);
  90. return WeChatPayNotifyResult.Failure;
  91. }
  92. }
  93. /// <summary>
  94. /// 确认订单回调通知
  95. /// </summary>
  96. [Route("score/orderconfirm")]
  97. [HttpPost]
  98. public async Task<IActionResult> OrderConfirm()
  99. {
  100. try
  101. {
  102. var notify = await _client.ExecuteAsync<WeChatPayScoreUserConfirmNotify>(Request, _optionsAccessor.Value);
  103. if (notify.State == WeChatPayServiceOrderState.Doing)
  104. {
  105. _logger.LogInformation("确认订单回调通知 => " + notify.Body);
  106. return WeChatPayNotifyResult.Success;
  107. }
  108. return WeChatPayNotifyResult.Failure;
  109. }
  110. catch (WeChatPayException ex)
  111. {
  112. _logger.LogWarning("出现异常: " + ex.Message);
  113. return WeChatPayNotifyResult.Failure;
  114. }
  115. }
  116. /// <summary>
  117. /// 订单支付成功回调通知
  118. /// </summary>
  119. [Route("score/orderpaid")]
  120. [HttpPost]
  121. public async Task<IActionResult> OrderPaid()
  122. {
  123. try
  124. {
  125. var notify = await _client.ExecuteAsync<WeChatPayScoreUserPaidNotify>(Request, _optionsAccessor.Value);
  126. if (notify.State == WeChatPayServiceOrderState.Done)
  127. {
  128. _logger.LogInformation("订单支付成功回调通知 => " + notify.Body);
  129. return WeChatPayNotifyResult.Success;
  130. }
  131. return WeChatPayNotifyResult.Failure;
  132. }
  133. catch (WeChatPayException ex)
  134. {
  135. _logger.LogWarning("出现异常: " + ex.Message);
  136. return WeChatPayNotifyResult.Failure;
  137. }
  138. }
  139. /// <summary>
  140. /// 订单确认 或 支付成功 回调通知
  141. /// </summary>
  142. [Route("score/confirmorpaid")]
  143. [HttpPost]
  144. public async Task<IActionResult> OrderConfirmOrPaid()
  145. {
  146. try
  147. {
  148. var notify = await _client.ExecuteAsync<WeChatPayScoreUserPaidNotify>(Request, _optionsAccessor.Value);
  149. if (notify.State == WeChatPayServiceOrderState.Doing || notify.State == WeChatPayServiceOrderState.Done)
  150. {
  151. _logger.LogInformation("订单确认或支付成功回调通知: " + notify.Body);
  152. return WeChatPayNotifyResult.Success;
  153. }
  154. return WeChatPayNotifyResult.Failure;
  155. }
  156. catch (WeChatPayException ex)
  157. {
  158. _logger.LogWarning("出现异常: " + ex.Message);
  159. return WeChatPayNotifyResult.Failure;
  160. }
  161. }
  162. #endregion
  163. }
  164. }