mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 17:13:26 +08:00
接口【完善】完善接口端商家转账回调通知。
This commit is contained in:
@@ -17,12 +17,15 @@ using CoreCms.Net.Model.FromBody;
|
||||
using Essensoft.Paylink.WeChatPay;
|
||||
using Essensoft.Paylink.WeChatPay.V2;
|
||||
using Essensoft.Paylink.WeChatPay.V2.Notify;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
||||
|
||||
namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
|
||||
{
|
||||
@@ -39,18 +42,24 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
|
||||
private readonly IWeChatPayConfigServices _weChatPayConfigServices;
|
||||
private readonly ICoreCmsUserTocashWeChatNotifyServices _userTocashWeChatNotifyServices;
|
||||
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly IWechatTenpayClientFactory _wechatTenpayClientFactory;
|
||||
private readonly ICoreCmsUserTocashServices _userTocashServices;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public WeChatPayController(
|
||||
IWeChatPayNotifyClient client, ICoreCmsBillRefundServices billRefundServices, IRedisOperationRepository redisOperationRepository, IWeChatPayConfigServices weChatPayConfigServices, ICoreCmsUserTocashWeChatNotifyServices userTocashWeChatNotifyServices)
|
||||
IWeChatPayNotifyClient client, ICoreCmsBillRefundServices billRefundServices, IRedisOperationRepository redisOperationRepository, IWeChatPayConfigServices weChatPayConfigServices, ICoreCmsUserTocashWeChatNotifyServices userTocashWeChatNotifyServices, IHttpContextAccessor httpContextAccessor, IWechatTenpayClientFactory wechatTenpayClientFactory, ICoreCmsUserTocashServices userTocashServices)
|
||||
{
|
||||
_client = client;
|
||||
_billRefundServices = billRefundServices;
|
||||
_redisOperationRepository = redisOperationRepository;
|
||||
_weChatPayConfigServices = weChatPayConfigServices;
|
||||
_userTocashWeChatNotifyServices = userTocashWeChatNotifyServices;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_wechatTenpayClientFactory = wechatTenpayClientFactory;
|
||||
_userTocashServices = userTocashServices;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -153,12 +162,17 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> TransferBillsCallBack([FromBody] FMTransferBillsCallBack entity)
|
||||
public async Task<IActionResult> TransferBillsCallBack(
|
||||
[FromHeader(Name = "Wechatpay-Timestamp")] string timestamp,
|
||||
[FromHeader(Name = "Wechatpay-Nonce")] string nonce,
|
||||
[FromHeader(Name = "Wechatpay-Signature")] string signature,
|
||||
[FromHeader(Name = "Wechatpay-Serial")] string serialNumber,
|
||||
[FromBody] FMTransferBillsCallBack entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
NLogUtil.WriteAll(LogLevel.Trace, LogType.Refund, "商家转账回调通知", JsonConvert.SerializeObject(entity));
|
||||
|
||||
// 检查请求头是否包含必要的字段
|
||||
if (entity == null)
|
||||
{
|
||||
var obj = new
|
||||
@@ -169,25 +183,108 @@ namespace CoreCms.Net.Web.WebApi.Controllers.PayNotify
|
||||
return new JsonResult(obj);
|
||||
}
|
||||
|
||||
var log = new CoreCmsUserTocashWeChatNotify();
|
||||
|
||||
log.callBackId = entity.id;
|
||||
log.create_time = entity.create_time;
|
||||
log.resource_type = entity.resource_type;
|
||||
log.event_type = entity.event_type;
|
||||
log.summary = entity.summary;
|
||||
log.resource = JsonConvert.SerializeObject(entity.resource);
|
||||
log.createTime = DateTime.Now;
|
||||
|
||||
await _userTocashWeChatNotifyServices.InsertAsync(log);
|
||||
|
||||
|
||||
if (entity.event_type== "MCHTRANSFER.BILL.FINISHED")
|
||||
// 存储回调数据
|
||||
var log = new CoreCmsUserTocashWeChatNotify
|
||||
{
|
||||
|
||||
wechatpayNonce = nonce,
|
||||
wechatpaySerial = serialNumber,
|
||||
wechatpaySignature = signature,
|
||||
wechatpayTimestamp = timestamp,
|
||||
callBackId = entity.id,
|
||||
create_time = entity.create_time,
|
||||
resource_type = entity.resource_type,
|
||||
event_type = entity.event_type,
|
||||
summary = entity.summary,
|
||||
resource = JsonConvert.SerializeObject(entity.resource),
|
||||
createTime = DateTime.Now
|
||||
};
|
||||
// 获取返回序列
|
||||
var id = await _userTocashWeChatNotifyServices.ExecuteReturnIdentityAsync(log);
|
||||
|
||||
// 获取支付配置
|
||||
var payConfig = await _weChatPayConfigServices.QueryByClauseAsync(p =>
|
||||
p.isDefault == true && p.isEnable == true &&
|
||||
p.appType == GlobalEnumVars.WeiChatPayTradeType.JSAPI.ToString());
|
||||
|
||||
// 构建WeChatPay客户端
|
||||
var client = await _wechatTenpayClientFactory.Create(payConfig.mchId);
|
||||
|
||||
// 验证签名
|
||||
bool valid = await client.VerifyEventSignatureAsync(
|
||||
webhookTimestamp: timestamp,
|
||||
webhookNonce: nonce,
|
||||
webhookBody: JsonConvert.SerializeObject(entity),
|
||||
webhookSignature: signature,
|
||||
webhookSerialNumber: serialNumber
|
||||
);
|
||||
|
||||
// 如果验签失败,返回错误信息
|
||||
if (!valid)
|
||||
{
|
||||
return new JsonResult(new { code = "FAIL", message = "验签失败" });
|
||||
}
|
||||
|
||||
// 反序列化回调模型
|
||||
var callbackModel = client.DeserializeEvent(JsonConvert.SerializeObject(entity));
|
||||
var eventType = callbackModel.EventType?.ToUpper();
|
||||
|
||||
// 处理不同的事件类型
|
||||
if (eventType == "MCHTRANSFER.BILL.FINISHED")
|
||||
{
|
||||
var callbackResource = client.DecryptEventResource<SKIT.FlurlHttpClient.Wechat.TenpayV3.Events.MerchantTransferBillFinishedResource>(callbackModel);
|
||||
|
||||
if (callbackResource != null)
|
||||
{
|
||||
//更新解密数据
|
||||
await _userTocashWeChatNotifyServices.UpdateAsync(p => new CoreCmsUserTocashWeChatNotify
|
||||
{
|
||||
decryptedData = JsonConvert.SerializeObject(callbackResource)
|
||||
}, p => p.id == id);
|
||||
|
||||
//业务处理
|
||||
var usertocash = callbackResource.OutBillNumber.Replace("usertocash", "");
|
||||
var userTocashId = Convert.ToInt32(usertocash);
|
||||
|
||||
switch (callbackResource.State)
|
||||
{
|
||||
//转账成功
|
||||
case "SUCCESS":
|
||||
await _userTocashServices.UpdateAsync(p => new CoreCmsUserTocash()
|
||||
{
|
||||
needUserGet = false,
|
||||
}, p => p.id == userTocashId);
|
||||
break;
|
||||
|
||||
//单据已受理
|
||||
case "ACCEPTED":
|
||||
break;
|
||||
|
||||
//单据处理中,转账结果尚未明确,如一直处于此状态,建议检查账户余额是否足够
|
||||
case "PROCESSING":
|
||||
break;
|
||||
|
||||
//待收款用户确认,可拉起微信收款确认页面进行收款确认
|
||||
case "WAIT_USER_CONFIRM":
|
||||
break;
|
||||
|
||||
//转账失败
|
||||
case "FAIL":
|
||||
break;
|
||||
|
||||
//撤销中
|
||||
case "CANCELING":
|
||||
break;
|
||||
|
||||
//撤销
|
||||
case "CANCELLED":
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 其他情况略
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user