优化支付宝支付状态查询处理

This commit is contained in:
jianweie
2023-04-26 13:14:28 +08:00
parent 23781304d8
commit f1b0576e78
5 changed files with 109 additions and 58 deletions

View File

@@ -36,6 +36,9 @@ using Newtonsoft.Json;
using NPOI.HSSF.UserModel;
using SqlSugar;
using CoreCms.Net.Services;
using Essensoft.Paylink.Alipay;
using Essensoft.Paylink.Alipay.Domain;
using Essensoft.Paylink.Alipay.Request;
namespace CoreCms.Net.Web.Admin.Controllers
{
@@ -51,25 +54,26 @@ namespace CoreCms.Net.Web.Admin.Controllers
{
private readonly ICoreCmsBillPaymentsServices _coreCmsBillPaymentsServices;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IWeChatPayClient _client;
private readonly IWeChatPayClient _clientWeCaht;
private readonly IAlipayClient _clientAlipay;
private readonly IWeChatPayConfigServices _weChatPayConfigServices;
private readonly IAlipayConfigServices _alipayConfigServices;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="webHostEnvironment"></param>
/// <param name="coreCmsBillPaymentsServices"></param>
/// <param name="client"></param>
/// <param name="weChatPayConfigServices"></param>
public CoreCmsBillPaymentsController(IWebHostEnvironment webHostEnvironment
, ICoreCmsBillPaymentsServices coreCmsBillPaymentsServices, IWeChatPayClient client, IWeChatPayConfigServices weChatPayConfigServices)
, ICoreCmsBillPaymentsServices coreCmsBillPaymentsServices, IWeChatPayClient client, IWeChatPayConfigServices weChatPayConfigServices, IAlipayConfigServices alipayConfigServices, IAlipayClient clientAlipay)
{
_webHostEnvironment = webHostEnvironment;
_coreCmsBillPaymentsServices = coreCmsBillPaymentsServices;
_client = client;
_clientWeCaht = client;
_weChatPayConfigServices = weChatPayConfigServices;
_alipayConfigServices = alipayConfigServices;
_clientAlipay = clientAlipay;
}
#region ============================================================
@@ -563,7 +567,7 @@ namespace CoreCms.Net.Web.Admin.Controllers
//商户订单号
OutTradeNo = model.paymentId
};
var response = await _client.ExecuteAsync(request, payOptions);
var response = await _clientWeCaht.ExecuteAsync(request, payOptions);
if (response is { ReturnCode: WeChatPayCode.Success, ResultCode: WeChatPayCode.Success, TradeState: WeChatPayCode.Success })
{
@@ -575,22 +579,79 @@ namespace CoreCms.Net.Web.Admin.Controllers
jm.code = 0;
jm.msg = "刷新成功";
jm.data = response;
//jm.data = response;
}
else
{
jm.code = 1;
jm.data = response;
//jm.data = response;
jm.msg = response.TradeState == "NOTPAY" ? response.TradeStateDesc : response.ErrCodeDes;
}
}
else if (model.paymentCode == GlobalEnumVars.PaymentsTypes.alipay.ToString())
{
var tradeType = GlobalEnumVars.AliPayPayTradeType.ScanQRCodes.ToString();
if (!string.IsNullOrEmpty(model.parameters))
{
var jObj = (JObject)JsonConvert.DeserializeObject(model.parameters);
if (jObj != null && jObj.TryGetValue("trade_type", out var value))
tradeType = PayHelper.GetAliPayPayTradeType(value.ObjectToString());
}
var config = await _alipayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true && p.appType == tradeType);
if (config == null)
{
jm.msg = "未获取到你的支付配置信息";
return jm;
}
//构建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 request = new AlipayTradeQueryModel
{
OutTradeNo = model.paymentId,
//TradeNo = viewMode.TradeNo
};
var req = new AlipayTradeQueryRequest();
req.SetBizModel(request);
var response = await _clientAlipay.ExecuteAsync(req, payOptions);
if (response is { Code: "10000", IsError: false })
{
var money = Convert.ToDecimal(response.PayAmount);
await _coreCmsBillPaymentsServices.ToUpdate(response.OutTradeNo,
(int)GlobalEnumVars.BillPaymentsStatus.Payed,
GlobalEnumVars.PaymentsTypes.wechatpay.ToString(), money, response.Code,
response.TradeNo);
jm.code = 0;
jm.msg = "刷新成功";
//jm.data = response;
}
else
{
jm.code = 1;
//jm.data = response;
jm.msg = !string.IsNullOrEmpty(response.SubMsg) ? response.SubMsg : response.Msg;
}
jm.otherData = response;
return jm;
}
else