using System.Threading.Tasks; using Essensoft.Paylink.WeChatPay; using Essensoft.Paylink.WeChatPay.V2; using Essensoft.Paylink.WeChatPay.V2.Notify; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace WebApplicationSample.Controllers { [Route("wechatpay/notify")] public class WeChatPayNotifyController : Controller { private readonly ILogger _logger; private readonly IWeChatPayNotifyClient _client; private readonly IOptions _optionsAccessor; public WeChatPayNotifyController(ILogger logger, IWeChatPayNotifyClient client, IOptions optionsAccessor) { _logger = logger; _client = client; _optionsAccessor = optionsAccessor; } /// /// 统一下单支付结果通知 /// [Route("unifiedorder")] [HttpPost] public async Task Unifiedorder() { try { var notify = await _client.ExecuteAsync(Request, _optionsAccessor.Value); if (notify.ReturnCode == WeChatPayCode.Success) { if (notify.ResultCode == WeChatPayCode.Success) { _logger.LogInformation("统一下单支付结果通知 => OutTradeNo: " + notify.OutTradeNo); return WeChatPayNotifyResult.Success; } } return WeChatPayNotifyResult.Failure; } catch (WeChatPayException ex) { _logger.LogWarning("出现异常: " + ex.Message); return WeChatPayNotifyResult.Failure; } } /// /// 退款结果通知 /// [Route("refund")] [HttpPost] public async Task Refund() { try { var notify = await _client.ExecuteAsync(Request, _optionsAccessor.Value); if (notify.ReturnCode == WeChatPayCode.Success) { if (notify.RefundStatus == WeChatPayRefundStatus.Success) { _logger.LogInformation("退款结果通知 => OutTradeNo: " + notify.OutTradeNo); return WeChatPayNotifyResult.Success; } } return WeChatPayNotifyResult.Failure; } catch (WeChatPayException ex) { _logger.LogWarning("出现异常: " + ex.Message); return WeChatPayNotifyResult.Failure; } } } }