using System; using System.Threading.Tasks; using Essensoft.AspNetCore.Payment.Alipay; using Essensoft.AspNetCore.Payment.Alipay.Notify; using Essensoft.AspNetCore.Payment.WeChatPay; using Essensoft.AspNetCore.Payment.WeChatPay.Notify; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; namespace WebApplicationSample.Controllers { #region 支付宝异步通知 [Route("notify/alipay")] public class AlipayNotifyController : Controller { private readonly IAlipayNotifyClient _client; private readonly IOptions _optionsAccessor; public AlipayNotifyController(IAlipayNotifyClient client, IOptions optionsAccessor) { _client = client; _optionsAccessor = optionsAccessor; } /// /// 扫码支付异步通知 /// [Route("precreate")] [HttpPost] public async Task Precreate() { try { var notify = await _client.CertificateExecuteAsync(Request, _optionsAccessor.Value); if (notify.TradeStatus == AlipayTradeStatus.Success) { Console.WriteLine("OutTradeNo: " + notify.OutTradeNo); return AlipayNotifyResult.Success; } return NoContent(); } catch { return NoContent(); } } /// /// APP支付异步通知 /// [Route("apppay")] [HttpPost] public async Task AppPay() { try { var notify = await _client.CertificateExecuteAsync(Request, _optionsAccessor.Value); if (notify.TradeStatus == AlipayTradeStatus.Success) { Console.WriteLine("OutTradeNo: " + notify.OutTradeNo); return AlipayNotifyResult.Success; } return NoContent(); } catch { return NoContent(); } } /// /// 电脑网站支付异步通知 /// [Route("pagepay")] [HttpPost] public async Task PagePay() { try { var notify = await _client.CertificateExecuteAsync(Request, _optionsAccessor.Value); if (notify.TradeStatus == AlipayTradeStatus.Success) { Console.WriteLine("OutTradeNo: " + notify.OutTradeNo); return AlipayNotifyResult.Success; } return NoContent(); } catch { return NoContent(); } } /// /// 手机网站支付异步通知 /// [Route("wappay")] [HttpPost] public async Task WapPay() { try { var notify = await _client.CertificateExecuteAsync(Request, _optionsAccessor.Value); if (notify.TradeStatus == AlipayTradeStatus.Success) { Console.WriteLine("OutTradeNo: " + notify.OutTradeNo); return AlipayNotifyResult.Success; } return NoContent(); } catch { return NoContent(); } } } #endregion #region 微信支付异步通知 [Route("notify/wechatpay")] public class WeChatPayNotifyController : Controller { private readonly IWeChatPayNotifyClient _client; private readonly IOptions _optionsAccessor; public WeChatPayNotifyController(IWeChatPayNotifyClient client, IOptions optionsAccessor) { _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) { Console.WriteLine("OutTradeNo: " + notify.OutTradeNo); return WeChatPayNotifyResult.Success; } } return NoContent(); } catch { return NoContent(); } } /// /// 退款结果通知 /// [Route("refund")] [HttpPost] public async Task Refund() { try { var notify = await _client.ExecuteAsync(Request, _optionsAccessor.Value); if (notify.ReturnCode == WeChatPayCode.Success) { if (notify.RefundStatus == WeChatPayCode.Success) { Console.WriteLine("OutTradeNo: " + notify.OutTradeNo); return WeChatPayNotifyResult.Success; } } return NoContent(); } catch { return NoContent(); } } } #endregion }