WeChatPayNotifyController.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Threading.Tasks;
  3. using Essensoft.Paylink.WeChatPay;
  4. using Essensoft.Paylink.WeChatPay.V2;
  5. using Essensoft.Paylink.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 (WeChatPayException ex)
  41. {
  42. Console.WriteLine("出现异常: " + ex.Message);
  43. return WeChatPayNotifyResult.Failure;
  44. }
  45. }
  46. /// <summary>
  47. /// 退款结果通知
  48. /// </summary>
  49. [Route("refund")]
  50. [HttpPost]
  51. public async Task<IActionResult> Refund()
  52. {
  53. try
  54. {
  55. var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request, _optionsAccessor.Value);
  56. if (notify.ReturnCode == WeChatPayCode.Success)
  57. {
  58. if (notify.RefundStatus == WeChatPayCode.Success)
  59. {
  60. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  61. return WeChatPayNotifyResult.Success;
  62. }
  63. }
  64. return WeChatPayNotifyResult.Failure;
  65. }
  66. catch (WeChatPayException ex)
  67. {
  68. Console.WriteLine("出现异常: " + ex.Message);
  69. return WeChatPayNotifyResult.Failure;
  70. }
  71. }
  72. }
  73. }