1
0

WeChatPayV3NotifyController.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Threading.Tasks;
  3. using Essensoft.AspNetCore.Payment.WeChatPay;
  4. using Essensoft.AspNetCore.Payment.WeChatPay.V3;
  5. using Essensoft.AspNetCore.Payment.WeChatPay.V3.Notify;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Options;
  8. namespace WebApplicationSample.Controllers
  9. {
  10. [Route("wechatpay/v3/notify")]
  11. public class WeChatPayV3NotifyController : Controller
  12. {
  13. private readonly IWeChatPayNotifyClient _client;
  14. private readonly IOptions<WeChatPayOptions> _optionsAccessor;
  15. public WeChatPayV3NotifyController(IWeChatPayNotifyClient client, IOptions<WeChatPayOptions> optionsAccessor)
  16. {
  17. _client = client;
  18. _optionsAccessor = optionsAccessor;
  19. }
  20. /// <summary>
  21. /// 支付结果通知
  22. /// </summary>
  23. [Route("transactions")]
  24. [HttpPost]
  25. public async Task<IActionResult> Transactions()
  26. {
  27. try
  28. {
  29. var notify = await _client.ExecuteAsync<WeChatPayTransactionsNotify>(Request, _optionsAccessor.Value);
  30. if (notify.TradeState == WeChatPayTradeState.Success)
  31. {
  32. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  33. return WeChatPayNotifyResult.Success;
  34. }
  35. return WeChatPayNotifyResult.Failure;
  36. }
  37. catch (WeChatPayException ex)
  38. {
  39. Console.WriteLine("出现异常: " + ex.Message);
  40. return WeChatPayNotifyResult.Failure;
  41. }
  42. }
  43. /// <summary>
  44. /// 退款结果通知
  45. /// </summary>
  46. [Route("refund")]
  47. [HttpPost]
  48. public async Task<IActionResult> Refund()
  49. {
  50. try
  51. {
  52. var notify = await _client.ExecuteAsync<WeChatPayRefundDomesticRefundsNotify>(Request, _optionsAccessor.Value);
  53. if (notify.RefundStatus == WeChatPayRefundStatus.Success)
  54. {
  55. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  56. return WeChatPayNotifyResult.Success;
  57. }
  58. return WeChatPayNotifyResult.Failure;
  59. }
  60. catch (WeChatPayException ex)
  61. {
  62. Console.WriteLine("出现异常: " + ex.Message);
  63. return WeChatPayNotifyResult.Failure;
  64. }
  65. }
  66. }
  67. }