mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 17:43:26 +08:00
优化后台查询微信支付单支付状态
This commit is contained in:
@@ -31,8 +31,11 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using NPOI.HSSF.UserModel;
|
||||
using SqlSugar;
|
||||
using CoreCms.Net.Services;
|
||||
|
||||
namespace CoreCms.Net.Web.Admin.Controllers
|
||||
{
|
||||
@@ -49,7 +52,8 @@ namespace CoreCms.Net.Web.Admin.Controllers
|
||||
private readonly ICoreCmsBillPaymentsServices _coreCmsBillPaymentsServices;
|
||||
private readonly IWebHostEnvironment _webHostEnvironment;
|
||||
private readonly IWeChatPayClient _client;
|
||||
private readonly IOptions<WeChatPayOptions> _optionsAccessor;
|
||||
|
||||
private readonly IWeChatPayConfigServices _weChatPayConfigServices;
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -58,14 +62,14 @@ namespace CoreCms.Net.Web.Admin.Controllers
|
||||
/// <param name="webHostEnvironment"></param>
|
||||
/// <param name="coreCmsBillPaymentsServices"></param>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="optionsAccessor"></param>
|
||||
/// <param name="weChatPayConfigServices"></param>
|
||||
public CoreCmsBillPaymentsController(IWebHostEnvironment webHostEnvironment
|
||||
, ICoreCmsBillPaymentsServices coreCmsBillPaymentsServices, IWeChatPayClient client, IOptions<WeChatPayOptions> optionsAccessor)
|
||||
, ICoreCmsBillPaymentsServices coreCmsBillPaymentsServices, IWeChatPayClient client, IWeChatPayConfigServices weChatPayConfigServices)
|
||||
{
|
||||
_webHostEnvironment = webHostEnvironment;
|
||||
_coreCmsBillPaymentsServices = coreCmsBillPaymentsServices;
|
||||
_client = client;
|
||||
_optionsAccessor = optionsAccessor;
|
||||
_weChatPayConfigServices = weChatPayConfigServices;
|
||||
}
|
||||
|
||||
#region 获取列表============================================================
|
||||
@@ -508,9 +512,9 @@ namespace CoreCms.Net.Web.Admin.Controllers
|
||||
jm.msg = "不存在此信息";
|
||||
return jm;
|
||||
}
|
||||
if (model.paymentCode != GlobalEnumVars.PaymentsTypes.wechatpay.ToString())
|
||||
if (model.paymentCode != GlobalEnumVars.PaymentsTypes.wechatpay.ToString() && model.paymentCode != GlobalEnumVars.PaymentsTypes.alipay.ToString())
|
||||
{
|
||||
jm.msg = "此更新暂时仅支持微信支付查询";
|
||||
jm.msg = "此更新暂时仅支持微信支付及支付宝支付查询";
|
||||
return jm;
|
||||
}
|
||||
|
||||
@@ -521,32 +525,79 @@ namespace CoreCms.Net.Web.Admin.Controllers
|
||||
}
|
||||
|
||||
|
||||
var request = new WeChatPayOrderQueryRequest
|
||||
if (model.paymentCode == GlobalEnumVars.PaymentsTypes.wechatpay.ToString())
|
||||
{
|
||||
//微信订单号
|
||||
//TransactionId = model.TransactionId,
|
||||
//商户订单号
|
||||
OutTradeNo = model.paymentId
|
||||
};
|
||||
var response = await _client.ExecuteAsync(request, _optionsAccessor.Value);
|
||||
var tradeType = GlobalEnumVars.WeiChatPayTradeType.JSAPI.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.GetWeiChatPayTradeType(value.ObjectToString());
|
||||
}
|
||||
|
||||
if (response is { ReturnCode: WeChatPayCode.Success, ResultCode: WeChatPayCode.Success, TradeState: WeChatPayCode.Success })
|
||||
var config = await _weChatPayConfigServices.QueryByClauseAsync(p => p.isDefault == true && p.isEnable == true && p.appType == tradeType);
|
||||
|
||||
if (config == null)
|
||||
{
|
||||
jm.msg = "未获取到你的支付配置信息";
|
||||
return jm;
|
||||
}
|
||||
|
||||
//构建linkPay请求配置实体
|
||||
var payOptions = new WeChatPayOptions
|
||||
{
|
||||
AppId = config.appId,
|
||||
MchId = config.mchId,
|
||||
APIKey = config.apiKey,
|
||||
APIv3Key = config.apiV3Key,
|
||||
Certificate = config.certificate,
|
||||
RsaPublicKey = config.rsaPublicKey,
|
||||
SubAppId = config.subAppId,
|
||||
SubMchId = config.subMchId
|
||||
};
|
||||
|
||||
var request = new WeChatPayOrderQueryRequest
|
||||
{
|
||||
//微信订单号
|
||||
//TransactionId = model.TransactionId,
|
||||
//商户订单号
|
||||
OutTradeNo = model.paymentId
|
||||
};
|
||||
var response = await _client.ExecuteAsync(request, payOptions);
|
||||
|
||||
if (response is { ReturnCode: WeChatPayCode.Success, ResultCode: WeChatPayCode.Success, TradeState: WeChatPayCode.Success })
|
||||
{
|
||||
var money = Math.Round((decimal)response.TotalFee / 100, 2);
|
||||
await _coreCmsBillPaymentsServices.ToUpdate(response.OutTradeNo,
|
||||
(int)GlobalEnumVars.BillPaymentsStatus.Payed,
|
||||
GlobalEnumVars.PaymentsTypes.wechatpay.ToString(), money, response.ResultCode,
|
||||
response.TransactionId);
|
||||
|
||||
jm.code = 0;
|
||||
jm.msg = "刷新成功";
|
||||
jm.data = response;
|
||||
}
|
||||
else
|
||||
{
|
||||
jm.code = 1;
|
||||
jm.data = response;
|
||||
jm.msg = response.TradeState == "NOTPAY" ? response.TradeStateDesc : response.ErrCodeDes;
|
||||
}
|
||||
|
||||
}
|
||||
else if (model.paymentCode == GlobalEnumVars.PaymentsTypes.alipay.ToString())
|
||||
{
|
||||
var money = Math.Round((decimal)response.TotalFee / 100, 2);
|
||||
await _coreCmsBillPaymentsServices.ToUpdate(response.OutTradeNo,
|
||||
(int)GlobalEnumVars.BillPaymentsStatus.Payed,
|
||||
GlobalEnumVars.PaymentsTypes.wechatpay.ToString(), money, response.ResultCode,
|
||||
response.TransactionId);
|
||||
|
||||
jm.code = 0;
|
||||
jm.msg = "刷新成功";
|
||||
jm.data = response;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
jm.code = 1;
|
||||
jm.msg = response.TradeStateDesc;
|
||||
jm.msg = "错误类型的支付方式";
|
||||
}
|
||||
|
||||
return jm;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user