WeChatPayNotifyController.cs 2.6 KB

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