优化微信支付、支付宝支付多种支付终端情况下的一一对应处理。

This commit is contained in:
jianweie
2023-04-26 10:27:06 +08:00
parent 04a8b30413
commit 8a12ab7ea5
5 changed files with 149 additions and 49 deletions

View File

@@ -12,10 +12,12 @@ using System.Threading.Tasks;
using System.Xml;
using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using Essensoft.Paylink.Alipay;
using Essensoft.Paylink.Alipay.Notify;
using Essensoft.Paylink.Alipay.Utility;
using Google.Protobuf.WellKnownTypes;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
@@ -30,33 +32,49 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
public class AliPayController : ControllerBase
{
private readonly IAlipayNotifyClient _client;
private readonly IOptions<AlipayOptions> _optionsAccessor;
private readonly IRedisOperationRepository _redisOperationRepository;
private readonly IAlipayConfigServices _alipayConfigServices;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="client"></param>
/// <param name="optionsAccessor"></param>
/// <param name="redisOperationRepository"></param>
public AliPayController(IAlipayNotifyClient client, IOptions<AlipayOptions> optionsAccessor, IRedisOperationRepository redisOperationRepository)
/// <param name="alipayConfigServices"></param>
public AliPayController(IAlipayNotifyClient client, IRedisOperationRepository redisOperationRepository, IAlipayConfigServices alipayConfigServices)
{
_client = client;
_optionsAccessor = optionsAccessor;
_redisOperationRepository = redisOperationRepository;
_alipayConfigServices = alipayConfigServices;
}
#region
/// <summary>
/// APP支付统一下单支付结果通知
/// https://api.pro.coreshop.cn/Notify/AliPay/Unifiedorder
/// </summary>
[HttpPost]
public async Task<IActionResult> Unifiedorder()
[Route("m-{appid}")]
public async Task<IActionResult> Unifiedorder([FromRoute(Name = "appid")] string appid)
{
NLogUtil.WriteAll(LogLevel.Trace, LogType.Order, "支付宝支付回调开始", "支付宝回调开始数据标记");
try
{
var notify = await _client.ExecuteAsync<AlipayTradeAppPayNotify>(Request, _optionsAccessor.Value);
var config = await _alipayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true && p.appId == appid);
//构建linkPay请求配置实体
var payOptions = new AlipayOptions
{
AppId = config.appId,
AlipayPublicKey = config.publicKey,
AppPrivateKey = config.privateKey,
ServerUrl = config.serverUrl,
SignType = config.signType,
AppPublicCert = config.appPublicCert,
AlipayPublicCert = config.alipayPublicCert,
AlipayRootCert = config.alipayRootCert
};
var notify = await _client.ExecuteAsync<AlipayTradeAppPayNotify>(Request, payOptions);
NLogUtil.WriteAll(LogLevel.Trace, LogType.Order, "支付宝支付回调支付宝返回的参数", JsonConvert.SerializeObject(notify));
if (notify.TradeStatus == AlipayTradeStatus.Success)
{
@@ -82,17 +100,34 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> Gateway()
[Route("m-{appid}")]
public async Task<IActionResult> Gateway([FromRoute(Name = "appid")] string appid)
{
try
{
var config = await _alipayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true && p.appId == appid);
//构建linkPay请求配置实体
var payOptions = new AlipayOptions
{
AppId = config.appId,
AlipayPublicKey = config.publicKey,
AppPrivateKey = config.privateKey,
ServerUrl = config.serverUrl,
SignType = config.signType,
AppPublicCert = config.appPublicCert,
AlipayPublicCert = config.alipayPublicCert,
AlipayRootCert = config.alipayRootCert
};
var service = Request.Form["service"].ToString();
switch (service)
{
// 激活开发者模式
case "alipay.service.check":
{
var options = _optionsAccessor.Value;
var options = payOptions;
// 获取参数
var parameters = await _client.GetParametersAsync(Request);
@@ -121,49 +156,49 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
// 资金单据状态变更通知
case "alipay.fund.trans.order.changed":
{
var notify = await _client.CertificateExecuteAsync<AlipayFundTransOrderChangedNotify>(Request, _optionsAccessor.Value);
var notify = await _client.CertificateExecuteAsync<AlipayFundTransOrderChangedNotify>(Request, payOptions);
return AlipayNotifyResult.Success;
}
// 第三方应用授权取消消息
case "alipay.open.auth.appauth.cancelled":
{
var notify = await _client.CertificateExecuteAsync<AlipayOpenAuthAppauthCancelledNotify>(Request, _optionsAccessor.Value);
var notify = await _client.CertificateExecuteAsync<AlipayOpenAuthAppauthCancelledNotify>(Request, payOptions);
return AlipayNotifyResult.Success;
}
// 用户授权取消消息
case "alipay.open.auth.userauth.cancelled":
{
var notify = await _client.CertificateExecuteAsync<AlipayOpenAuthUserauthCancelledNotify>(Request, _optionsAccessor.Value);
var notify = await _client.CertificateExecuteAsync<AlipayOpenAuthUserauthCancelledNotify>(Request, payOptions);
return AlipayNotifyResult.Success;
}
// 小程序审核通过通知
case "alipay.open.mini.version.audit.passed":
{
var notify = await _client.CertificateExecuteAsync<AlipayOpenMiniVersionAuditPassedNotify>(Request, _optionsAccessor.Value);
var notify = await _client.CertificateExecuteAsync<AlipayOpenMiniVersionAuditPassedNotify>(Request, payOptions);
return AlipayNotifyResult.Success;
}
// 用户授权取消消息
case "alipay.open.mini.version.audit.rejected":
{
var notify = await _client.CertificateExecuteAsync<AlipayOpenMiniVersionAuditRejectedNotify>(Request, _optionsAccessor.Value);
var notify = await _client.CertificateExecuteAsync<AlipayOpenMiniVersionAuditRejectedNotify>(Request, payOptions);
return AlipayNotifyResult.Success;
}
// 收单资金结算到银行账户,结算退票的异步通知
case "alipay.trade.settle.dishonoured":
{
var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleDishonouredNotify>(Request, _optionsAccessor.Value);
var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleDishonouredNotify>(Request, payOptions);
return AlipayNotifyResult.Success;
}
// 收单资金结算到银行账户,结算失败的异步通知
case "alipay.trade.settle.fail":
{
var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleFailNotify>(Request, _optionsAccessor.Value);
var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleFailNotify>(Request, payOptions);
return AlipayNotifyResult.Success;
}
// 收单资金结算到银行账户,结算成功的异步通知
case "alipay.trade.settle.success":
{
var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleSuccessNotify>(Request, _optionsAccessor.Value);
var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleSuccessNotify>(Request, payOptions);
return AlipayNotifyResult.Success;
}
}
@@ -180,11 +215,28 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
/// 扫码支付异步通知
/// </summary>
[HttpPost]
public async Task<IActionResult> Precreate()
[Route("m-{appid}")]
public async Task<IActionResult> Precreate([FromRoute(Name = "appid")] string appid)
{
try
{
var notify = await _client.ExecuteAsync<AlipayTradeAppPayNotify>(Request, _optionsAccessor.Value);
var config = await _alipayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true && p.appId == appid);
//构建linkPay请求配置实体
var payOptions = new AlipayOptions
{
AppId = config.appId,
AlipayPublicKey = config.publicKey,
AppPrivateKey = config.privateKey,
ServerUrl = config.serverUrl,
SignType = config.signType,
AppPublicCert = config.appPublicCert,
AlipayPublicCert = config.alipayPublicCert,
AlipayRootCert = config.alipayRootCert
};
var notify = await _client.ExecuteAsync<AlipayTradeAppPayNotify>(Request, payOptions);
if (notify.TradeStatus == AlipayTradeStatus.Success)
{
await _redisOperationRepository.ListLeftPushAsync(RedisMessageQueueKey.AliPayNotice, JsonConvert.SerializeObject(notify));
@@ -204,11 +256,26 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
/// APP支付异步通知
/// </summary>
[HttpPost]
public async Task<IActionResult> AppPay()
[Route("m-{appid}")]
public async Task<IActionResult> AppPay([FromRoute(Name = "appid")] string appid)
{
try
{
var notify = await _client.CertificateExecuteAsync<AlipayTradeAppPayNotify>(Request, _optionsAccessor.Value);
var config = await _alipayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true && p.appId == appid);
//构建linkPay请求配置实体
var payOptions = new AlipayOptions
{
AppId = config.appId,
AlipayPublicKey = config.publicKey,
AppPrivateKey = config.privateKey,
ServerUrl = config.serverUrl,
SignType = config.signType,
AppPublicCert = config.appPublicCert,
AlipayPublicCert = config.alipayPublicCert,
AlipayRootCert = config.alipayRootCert
};
var notify = await _client.CertificateExecuteAsync<AlipayTradeAppPayNotify>(Request, payOptions);
if (notify.TradeStatus == AlipayTradeStatus.Success)
{
Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
@@ -222,15 +289,31 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
return NoContent();
}
}
/// <summary>
/// 电脑网站支付异步通知
/// </summary>
[HttpPost]
public async Task<IActionResult> PagePay()
[Route("m-{appid}")]
public async Task<IActionResult> PagePay([FromRoute(Name = "appid")] string appid)
{
try
{
var notify = await _client.CertificateExecuteAsync<AlipayTradePagePayNotify>(Request, _optionsAccessor.Value);
var config = await _alipayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true && p.appId == appid);
//构建linkPay请求配置实体
var payOptions = new AlipayOptions
{
AppId = config.appId,
AlipayPublicKey = config.publicKey,
AppPrivateKey = config.privateKey,
ServerUrl = config.serverUrl,
SignType = config.signType,
AppPublicCert = config.appPublicCert,
AlipayPublicCert = config.alipayPublicCert,
AlipayRootCert = config.alipayRootCert
};
var notify = await _client.CertificateExecuteAsync<AlipayTradePagePayNotify>(Request, payOptions);
if (notify.TradeStatus == AlipayTradeStatus.Success)
{
Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
@@ -249,11 +332,26 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
/// 手机网站支付异步通知
/// </summary>
[HttpPost]
public async Task<IActionResult> WapPay()
[Route("m-{appid}")]
public async Task<IActionResult> WapPay([FromRoute(Name = "appid")] string appid)
{
try
{
var notify = await _client.CertificateExecuteAsync<AlipayTradeWapPayNotify>(Request, _optionsAccessor.Value);
var config = await _alipayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true && p.appId == appid);
//构建linkPay请求配置实体
var payOptions = new AlipayOptions
{
AppId = config.appId,
AlipayPublicKey = config.publicKey,
AppPrivateKey = config.privateKey,
ServerUrl = config.serverUrl,
SignType = config.signType,
AppPublicCert = config.appPublicCert,
AlipayPublicCert = config.alipayPublicCert,
AlipayRootCert = config.alipayRootCert
};
var notify = await _client.CertificateExecuteAsync<AlipayTradeWapPayNotify>(Request, payOptions);
if (notify.TradeStatus == AlipayTradeStatus.Success)
{
Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);

View File

@@ -34,7 +34,6 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
private readonly ICoreCmsBillPaymentsServices _billPaymentsServices;
private readonly ICoreCmsBillRefundServices _billRefundServices;
private readonly IWeChatPayNotifyClient _client;
//private readonly IOptions<WeChatPayOptions> _optionsAccessor;
private readonly IRedisOperationRepository _redisOperationRepository;
private readonly IWeChatPayConfigServices _weChatPayConfigServices;
@@ -45,11 +44,9 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
/// </summary>
public WeChatPayController(
IWeChatPayNotifyClient client
//, IOptions<WeChatPayOptions> optionsAccessor
, ICoreCmsBillPaymentsServices billPaymentsServices, ICoreCmsBillRefundServices billRefundServices, IRedisOperationRepository redisOperationRepository, IWeChatPayConfigServices weChatPayConfigServices)
{
_client = client;
//_optionsAccessor = optionsAccessor;
_billPaymentsServices = billPaymentsServices;
_billRefundServices = billRefundServices;
_redisOperationRepository = redisOperationRepository;
@@ -60,12 +57,14 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
/// 统一下单支付结果通知
/// </summary>
[HttpPost]
public async Task<IActionResult> Unifiedorder()
[Route("m-{appid}")]
public async Task<IActionResult> Unifiedorder([FromRoute(Name = "appid")] string appid)
{
try
{
//NLogUtil.WriteAll(LogLevel.Trace, LogType.Order, "微信支付成功接收appid", JsonConvert.SerializeObject(appid));
var config = await _weChatPayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true);
var config = await _weChatPayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true && p.appId == appid);
//构建linkPay请求配置实体
var payOptions = new WeChatPayOptions
{
@@ -105,8 +104,6 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
{
try
{
var config = await _weChatPayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true);
//构建linkPay请求配置实体
var payOptions = new WeChatPayOptions
@@ -121,7 +118,6 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
SubMchId = config.subMchId
};
var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request, payOptions);
NLogUtil.WriteAll(LogLevel.Trace, LogType.Refund, "微信退款结果通知", JsonConvert.SerializeObject(notify));