WeChatPayV3NotifyController.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.ExecuteV3Async<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
  38. {
  39. return WeChatPayNotifyResult.Failure;
  40. }
  41. }
  42. }
  43. }