WeChatPayNotifyController.cs 2.4 KB

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