WeChatPayNotifyController.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Threading.Tasks;
  2. using Essensoft.Paylink.WeChatPay;
  3. using Essensoft.Paylink.WeChatPay.V2;
  4. using Essensoft.Paylink.WeChatPay.V2.Notify;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. namespace WebApplicationSample.Controllers
  9. {
  10. [Route("wechatpay/notify")]
  11. public class WeChatPayNotifyController : Controller
  12. {
  13. private readonly ILogger<WeChatPayNotifyController> _logger;
  14. private readonly IWeChatPayClient _client;
  15. private readonly IOptions<WeChatPayOptions> _optionsAccessor;
  16. public WeChatPayNotifyController(ILogger<WeChatPayNotifyController> logger, IWeChatPayClient client, IOptions<WeChatPayOptions> optionsAccessor)
  17. {
  18. _logger = logger;
  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. var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
  32. if (notify.ReturnCode == WeChatPayCode.Success)
  33. {
  34. if (notify.ResultCode == WeChatPayCode.Success)
  35. {
  36. _logger.LogInformation("统一下单支付结果通知 => OutTradeNo: " + notify.OutTradeNo);
  37. return WeChatPayNotifyResult.Success;
  38. }
  39. }
  40. return WeChatPayNotifyResult.Failure;
  41. }
  42. catch (WeChatPayException ex)
  43. {
  44. _logger.LogWarning("出现异常: " + ex.Message);
  45. return WeChatPayNotifyResult.Failure;
  46. }
  47. }
  48. /// <summary>
  49. /// 退款结果通知
  50. /// </summary>
  51. [Route("refund")]
  52. [HttpPost]
  53. public async Task<IActionResult> Refund()
  54. {
  55. try
  56. {
  57. var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request, _optionsAccessor.Value);
  58. if (notify.ReturnCode == WeChatPayCode.Success)
  59. {
  60. if (notify.RefundStatus == WeChatPayRefundStatus.Success)
  61. {
  62. _logger.LogInformation("退款结果通知 => OutTradeNo: " + notify.OutTradeNo);
  63. return WeChatPayNotifyResult.Success;
  64. }
  65. }
  66. return WeChatPayNotifyResult.Failure;
  67. }
  68. catch (WeChatPayException ex)
  69. {
  70. _logger.LogWarning("出现异常: " + ex.Message);
  71. return WeChatPayNotifyResult.Failure;
  72. }
  73. }
  74. }
  75. }