WeChatPayNotifyController.cs 2.3 KB

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