WeChatPayV3NotifyController.cs 7.5 KB

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