WeChatPayNotifyController.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Threading.Tasks;
  3. using Essensoft.AspNetCore.Payment.WeChatPay;
  4. using Essensoft.AspNetCore.Payment.WeChatPay.V2;
  5. using Essensoft.AspNetCore.Payment.WeChatPay.V2.Notify;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Options;
  9. namespace WebApplicationSample.Controllers
  10. {
  11. [Route("wechatpay/notify")]
  12. public class WeChatPayNotifyController : Controller
  13. {
  14. private readonly IWeChatPayNotifyClient _client;
  15. private readonly IOptions<WeChatPayOptions> _optionsAccessor;
  16. public WeChatPayNotifyController(IWeChatPayNotifyClient client, IOptions<WeChatPayOptions> optionsAccessor)
  17. {
  18. _client = client;
  19. _optionsAccessor = optionsAccessor;
  20. }
  21. /// <summary>
  22. /// 统一下单支付结果通知
  23. /// </summary>
  24. [Route("unifiedorder")]
  25. [HttpPost]
  26. public async Task<IActionResult> Unifiedorder()
  27. {
  28. try
  29. {
  30. Request.EnableBuffering();
  31. var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
  32. if (notify.ReturnCode == WeChatPayCode.Success)
  33. {
  34. if (notify.ResultCode == WeChatPayCode.Success)
  35. {
  36. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  37. return WeChatPayNotifyResult.Success;
  38. }
  39. }
  40. return WeChatPayNotifyResult.Failure;
  41. }
  42. catch
  43. {
  44. return WeChatPayNotifyResult.Failure;
  45. }
  46. }
  47. /// <summary>
  48. /// 退款结果通知
  49. /// </summary>
  50. [Route("refund")]
  51. [HttpPost]
  52. public async Task<IActionResult> Refund()
  53. {
  54. try
  55. {
  56. Request.EnableBuffering();
  57. var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request, _optionsAccessor.Value);
  58. if (notify.ReturnCode == WeChatPayCode.Success)
  59. {
  60. if (notify.RefundStatus == WeChatPayCode.Success)
  61. {
  62. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  63. return WeChatPayNotifyResult.Success;
  64. }
  65. }
  66. return WeChatPayNotifyResult.Failure;
  67. }
  68. catch
  69. {
  70. return WeChatPayNotifyResult.Failure;
  71. }
  72. }
  73. }
  74. }