123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Essensoft.AspNetCore.Payment.Alipay;
- using Essensoft.AspNetCore.Payment.Alipay.Notify;
- using Essensoft.AspNetCore.Payment.JDPay;
- using Essensoft.AspNetCore.Payment.JDPay.Notify;
- using Essensoft.AspNetCore.Payment.LianLianPay;
- using Essensoft.AspNetCore.Payment.LianLianPay.Notify;
- using Essensoft.AspNetCore.Payment.QPay;
- using Essensoft.AspNetCore.Payment.QPay.Notify;
- using Essensoft.AspNetCore.Payment.UnionPay;
- using Essensoft.AspNetCore.Payment.UnionPay.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<AlipayOptions> _optionsAccessor;
- public AlipayNotifyController(IAlipayNotifyClient client, IOptions<AlipayOptions> optionsAccessor)
- {
- _client = client;
- _optionsAccessor = optionsAccessor;
- }
- /// <summary>
- /// 扫码支付异步通知
- /// </summary>
- /// <returns></returns>
- [Route("precreate")]
- [HttpPost]
- public async Task<IActionResult> Precreate()
- {
- try
- {
- var parameters = new Dictionary<string, string>();
- foreach (var iter in Request.Form)
- {
- parameters.Add(iter.Key, iter.Value);
- }
- var notify = await _client.ExecuteAsync<AlipayTradePrecreateNotify>(parameters, _optionsAccessor.Value);
- if ("TRADE_SUCCESS" == notify.TradeStatus)
- {
- Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
- return AlipayNotifyResult.Success;
- }
- return NoContent();
- }
- catch
- {
- return NoContent();
- }
- }
- /// <summary>
- /// APP支付异步通知
- /// </summary>
- /// <returns></returns>
- [Route("apppay")]
- [HttpPost]
- public async Task<IActionResult> AppPay()
- {
- try
- {
- var parameters = new Dictionary<string, string>();
- foreach (var iter in Request.Form)
- {
- parameters.Add(iter.Key, iter.Value);
- }
- var notify = await _client.ExecuteAsync<AlipayTradeAppPayNotify>(parameters, _optionsAccessor.Value);
- if ("TRADE_SUCCESS" == notify.TradeStatus)
- {
- Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
- return AlipayNotifyResult.Success;
- }
- return NoContent();
- }
- catch
- {
- return NoContent();
- }
- }
- /// <summary>
- /// 电脑网站支付异步通知
- /// </summary>
- /// <returns></returns>
- [Route("pagepay")]
- [HttpPost]
- public async Task<IActionResult> PagePay()
- {
- try
- {
- var parameters = new Dictionary<string, string>();
- foreach (var iter in Request.Form)
- {
- parameters.Add(iter.Key, iter.Value);
- }
- var notify = await _client.ExecuteAsync<AlipayTradePagePayNotify>(parameters, _optionsAccessor.Value);
- if ("TRADE_SUCCESS" == notify.TradeStatus)
- {
- Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
- return AlipayNotifyResult.Success;
- }
- return NoContent();
- }
- catch
- {
- return NoContent();
- }
- }
- /// <summary>
- /// 手机网站支付异步通知
- /// </summary>
- /// <returns></returns>
- [Route("wappay")]
- [HttpPost]
- public async Task<IActionResult> WapPay()
- {
- try
- {
- var parameters = new Dictionary<string, string>();
- foreach (var iter in Request.Form)
- {
- parameters.Add(iter.Key, iter.Value);
- }
- var notify = await _client.ExecuteAsync<AlipayTradeWapPayNotify>(parameters, _optionsAccessor.Value);
- if ("TRADE_SUCCESS" == notify.TradeStatus)
- {
- 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;
- public WeChatPayNotifyController(IWeChatPayNotifyClient client)
- {
- _client = client;
- }
- /// <summary>
- /// 统一下单支付结果通知
- /// </summary>
- /// <returns></returns>
- [Route("unifiedorder")]
- [HttpPost]
- public async Task<IActionResult> Unifiedorder()
- {
- try
- {
- var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request);
- if (notify.ReturnCode == "SUCCESS")
- {
- if (notify.ResultCode == "SUCCESS")
- {
- Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
- return WeChatPayNotifyResult.Success;
- }
- }
- return NoContent();
- }
- catch
- {
- return NoContent();
- }
- }
- /// <summary>
- /// 退款结果通知
- /// </summary>
- /// <returns></returns>
- [Route("refund")]
- [HttpPost]
- public async Task<IActionResult> Refund()
- {
- try
- {
- var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request);
- if (notify.ReturnCode == "SUCCESS")
- {
- if (notify.RefundStatus == "SUCCESS")
- {
- Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
- return WeChatPayNotifyResult.Success;
- }
- }
- return NoContent();
- }
- catch
- {
- return NoContent();
- }
- }
- }
- #endregion
- #region QQ钱包异步通知
- [Route("notify/qpay")]
- public class QPayNotifyController : Controller
- {
- private readonly IQPayNotifyClient _client;
- private readonly IOptions<QPayOptions> _optionsAccessor;
- public QPayNotifyController(IQPayNotifyClient client, IOptions<QPayOptions> optionsAccessor)
- {
- _client = client;
- _optionsAccessor = optionsAccessor;
- }
- /// <summary>
- /// 统一下单支付结果通知
- /// </summary>
- /// <returns></returns>
- [Route("unifiedorder")]
- [HttpPost]
- public async Task<IActionResult> Unifiedorder()
- {
- try
- {
- var notify = await _client.ExecuteAsync<QPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
- if ("SUCCESS" == notify.TradeState)
- {
- Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
- return QPayNotifyResult.Success;
- }
- return NoContent();
- }
- catch
- {
- return NoContent();
- }
- }
- /// <summary>
- /// 付款码支付结果通知
- /// </summary>
- /// <returns></returns>
- [Route("micropay")]
- [HttpPost]
- public async Task<IActionResult> MicroPay()
- {
- try
- {
- var notify = await _client.ExecuteAsync<QPayMicroPayNotify>(Request, _optionsAccessor.Value);
- if ("SUCCESS" == notify.TradeState)
- {
- Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
- return QPayNotifyResult.Success;
- }
- return NoContent();
- }
- catch
- {
- return NoContent();
- }
- }
- /// <summary>
- /// 企业付款 - 用户到账通知
- /// </summary>
- /// <returns></returns>
- [Route("b2cpay")]
- [HttpPost]
- public async Task<IActionResult> B2CPay()
- {
- try
- {
- var notify = await _client.ExecuteAsync<QPayEPayB2CNotify>(Request, _optionsAccessor.Value);
- Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
- return QPayNotifyResult.Success;
- }
- catch
- {
- return NoContent();
- }
- }
- }
- #endregion
- #region 京东支付异步通知
- [Route("notify/jdpay")]
- public class JDPayNotifyController : Controller
- {
- private readonly IJDPayNotifyClient _client;
- private readonly IOptions<JDPayOptions> _optionsAccessor;
- public JDPayNotifyController(IJDPayNotifyClient client, IOptions<JDPayOptions> optionsAccessor)
- {
- _client = client;
- _optionsAccessor = optionsAccessor;
- }
- [Route("async")]
- [HttpPost]
- public async Task<IActionResult> Async()
- {
- try
- {
- var notify = await _client.ExecuteAsync<JDPayAsyncNotify>(Request, _optionsAccessor.Value);
- Console.WriteLine("TradeNum: " + notify.TradeNum + " tradeType :" + notify.TradeType);// notify.TradeType 0-消费 1-退款
- return JDPayNotifyResult.Success;
- }
- catch
- {
- return NoContent();
- }
- }
- [Route("defraypay")]
- [HttpPost]
- public async Task<IActionResult> DefrayPay()
- {
- try
- {
- var notify = await _client.ExecuteAsync<JDPayDefrayPayNotify>(Request, _optionsAccessor.Value);
- Console.WriteLine("trade_no: " + notify.TradeNo + " trade_amount :" + notify.TradeAmount);
- return JDPayNotifyResult.Success;
- }
- catch
- {
- return NoContent();
- }
- }
- }
- #endregion
- #region 连连支付异步通知
- [Route("notify/lianlianpay")]
- public class LianLianPayNotifyController : Controller
- {
- private readonly ILianLianPayNotifyClient _client;
- private readonly IOptions<LianLianPayOptions> _optionsAccessor;
- public LianLianPayNotifyController(ILianLianPayNotifyClient client, IOptions<LianLianPayOptions> optionsAccessor)
- {
- _client = client;
- _optionsAccessor = optionsAccessor;
- }
- [Route("receivemoney")]
- [HttpPost]
- public async Task<IActionResult> ReceiveMoney()
- {
- try
- {
- var notify = await _client.ExecuteAsync<LianLianPayReceiveMoneyNotify>(Request, _optionsAccessor.Value);
- Console.WriteLine("NoOrder: " + notify.NoOrder);
- return LianLianPayNotifyResult.Success;
- }
- catch
- {
- return NoContent();
- }
- }
- [Route("refund")]
- [HttpPost]
- public async Task<IActionResult> Refund()
- {
- try
- {
- var notify = await _client.ExecuteAsync<LianLianPayRefundNotify>(Request, _optionsAccessor.Value);
- Console.WriteLine("NoRefund: " + notify.NoRefund);
- return LianLianPayNotifyResult.Success;
- }
- catch
- {
- return NoContent();
- }
- }
- }
- #endregion
- #region 银联支付异步通知
- [Route("notify/unionpay")]
- public class UnionPayNotifyController : Controller
- {
- private readonly IUnionPayNotifyClient _client;
- public UnionPayNotifyController(IUnionPayNotifyClient client)
- {
- _client = client;
- }
- /// <summary>
- /// 网关支付 - 跳转网关页面支付通知
- /// </summary>
- /// <returns></returns>
- [Route("gatewaypayfrontconsume")]
- [HttpPost]
- public async Task<IActionResult> GatewayPayFrontConsume()
- {
- try
- {
- var notify = await _client.ExecuteAsync<UnionPayGatewayPayFrontConsumeNotify>(Request);
- Console.WriteLine("OrderId: " + notify.OrderId + " respCode :" + notify.RespCode);
- return UnionPayNotifyResult.Success;
- }
- catch
- {
- return NoContent();
- }
- }
- }
- #endregion
- }
|