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

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

@@ -85,6 +85,9 @@ namespace CoreCms.Net.Services
return jm;
}
var notifyUrl = config.notifyUrl.EndsWith("/") ? config.notifyUrl + "m-" + config.appId : config.notifyUrl + "/m-" + config.appId;
//构建linkPay请求配置实体
var payOptions = new AlipayOptions
{
@@ -112,7 +115,7 @@ namespace CoreCms.Net.Services
};
var req = new AlipayTradePrecreateRequest();
req.SetBizModel(model);
req.SetNotifyUrl(config.notifyUrl);
req.SetNotifyUrl(notifyUrl);
//req.SetReturnUrl("https://pc.pro.demo.corecms.cn/order/payment/result");
Loging.NLogUtil.WriteAll(LogLevel.Trace, Loging.LogType.Order, "支付宝ScanQRCodes支付拼接APP入参", JsonConvert.SerializeObject(model));
@@ -139,7 +142,7 @@ namespace CoreCms.Net.Services
};
var req = new AlipayTradePagePayRequest();
req.SetBizModel(model);
req.SetNotifyUrl(config.notifyUrl);
req.SetNotifyUrl(notifyUrl);
req.SetReturnUrl(config.jumpUrl);
Loging.NLogUtil.WriteAll(LogLevel.Trace, Loging.LogType.Order, "支付宝JSAPI_PC支付拼接APP入参", JsonConvert.SerializeObject(model));
@@ -166,7 +169,7 @@ namespace CoreCms.Net.Services
};
var req = new AlipayTradeAppPayRequest();
req.SetBizModel(model);
req.SetNotifyUrl(config.notifyUrl);
req.SetNotifyUrl(notifyUrl);
//req.SetReturnUrl(config.jumpUrl);
Loging.NLogUtil.WriteAll(LogLevel.Trace, Loging.LogType.Order, "支付宝APP支付拼接APP入参", JsonConvert.SerializeObject(model));
@@ -194,7 +197,7 @@ namespace CoreCms.Net.Services
};
var req = new AlipayTradeWapPayRequest();
req.SetBizModel(model);
req.SetNotifyUrl(config.notifyUrl);
req.SetNotifyUrl(notifyUrl);
req.SetReturnUrl(config.jumpUrl);
Loging.NLogUtil.WriteAll(LogLevel.Trace, Loging.LogType.Order, "支付宝MWEB支付拼接APP入参", JsonConvert.SerializeObject(model));

View File

@@ -180,13 +180,16 @@ namespace CoreCms.Net.Services
openId = user.openid;
}
var notifyUrl = config.notifyUrl.EndsWith("/") ? config.notifyUrl + "m-" + config.appId : config.notifyUrl + "/m-" + config.appId;
var orderRequest = new WeChatPayUnifiedOrderRequest
{
Body = entity.payTitle.Length > 40 ? entity.payTitle[..40] : entity.payTitle,
OutTradeNo = entity.paymentId,
TotalFee = Convert.ToInt32(entity.money * 100),
SpBillCreateIp = entity.ip,
NotifyUrl = config.notifyUrl,
NotifyUrl = notifyUrl,
TradeType = tradeType,
//OpenId = openId
};
@@ -233,7 +236,7 @@ namespace CoreCms.Net.Services
jm.status = true;
jm.msg = "创建JSAPI支付环境成功";
jm.data = parameter;
//jm.otherData = response;
jm.otherData = response;
}
//扫码支付
else if (tradeType == GlobalEnumVars.WeiChatPayTradeType.NATIVE.ToString())

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));

View File

@@ -730,42 +730,42 @@
支付宝异步通知
</summary>
</member>
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.#ctor(Essensoft.Paylink.Alipay.IAlipayNotifyClient,Microsoft.Extensions.Options.IOptions{Essensoft.Paylink.Alipay.AlipayOptions},CoreCms.Net.Caching.AutoMate.RedisCache.IRedisOperationRepository)">
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.#ctor(Essensoft.Paylink.Alipay.IAlipayNotifyClient,CoreCms.Net.Caching.AutoMate.RedisCache.IRedisOperationRepository,CoreCms.Net.IServices.IAlipayConfigServices)">
<summary>
构造函数
</summary>
<param name="client"></param>
<param name="optionsAccessor"></param>
<param name="redisOperationRepository"></param>
<param name="alipayConfigServices"></param>
</member>
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.Unifiedorder">
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.Unifiedorder(System.String)">
<summary>
APP支付统一下单支付结果通知
https://api.pro.coreshop.cn/Notify/AliPay/Unifiedorder
</summary>
</member>
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.Gateway">
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.Gateway(System.String)">
<summary>
应用网关
</summary>
<returns></returns>
</member>
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.Precreate">
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.Precreate(System.String)">
<summary>
扫码支付异步通知
</summary>
</member>
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.AppPay">
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.AppPay(System.String)">
<summary>
APP支付异步通知
</summary>
</member>
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.PagePay">
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.PagePay(System.String)">
<summary>
电脑网站支付异步通知
</summary>
</member>
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.WapPay">
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.AliPayController.WapPay(System.String)">
<summary>
手机网站支付异步通知
</summary>
@@ -775,12 +775,12 @@
微信支付异步通知
</summary>
</member>
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.WeChatPayController.#ctor(Essensoft.Paylink.WeChatPay.V2.IWeChatPayNotifyClient,Microsoft.Extensions.Options.IOptions{Essensoft.Paylink.WeChatPay.WeChatPayOptions},CoreCms.Net.IServices.ICoreCmsBillPaymentsServices,CoreCms.Net.IServices.ICoreCmsBillRefundServices,CoreCms.Net.Caching.AutoMate.RedisCache.IRedisOperationRepository)">
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.WeChatPayController.#ctor(Essensoft.Paylink.WeChatPay.V2.IWeChatPayNotifyClient,CoreCms.Net.IServices.ICoreCmsBillPaymentsServices,CoreCms.Net.IServices.ICoreCmsBillRefundServices,CoreCms.Net.Caching.AutoMate.RedisCache.IRedisOperationRepository,CoreCms.Net.IServices.IWeChatPayConfigServices)">
<summary>
构造函数
</summary>
</member>
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.WeChatPayController.Unifiedorder">
<member name="M:CoreCms.Net.Web.WebApi.Controllers.PayNotify.WeChatPayController.Unifiedorder(System.String)">
<summary>
统一下单支付结果通知
</summary>