From 1605af1ced748313e99f38e5eb6888768fbc7d54 Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期三, 11 六月 2025 14:46:43 +0800
Subject: [PATCH] fix:支付回调和供应商回调的并发问题
---
LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs | 9 ++++
LifePayment/LifePayment.HttpApi/LifePay/ACOOLYNotifyController.cs | 6 ++
LifePayment/LifePayment.Worker/Worker/CheckUnPayOrderWork.cs | 52 +++++++++++++++-----------
LifePayment/LifePayment.Application/LifePay/LifePayService.cs | 9 ++++
LifePayment/LifePayment.HttpApi/LifePay/AliPayNotifyController.cs | 10 ++++
5 files changed, 60 insertions(+), 26 deletions(-)
diff --git a/LifePayment/LifePayment.Application/LifePay/LifePayService.cs b/LifePayment/LifePayment.Application/LifePay/LifePayService.cs
index c2774a8..5581eb3 100644
--- a/LifePayment/LifePayment.Application/LifePay/LifePayService.cs
+++ b/LifePayment/LifePayment.Application/LifePay/LifePayService.cs
@@ -18,6 +18,7 @@
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Data;
+using Volo.Abp.DistributedLocking;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using Z.EntityFramework.Plus;
@@ -44,6 +45,7 @@
private readonly ILifePayOrderService _lifePayOrderService;
private readonly IDataFilter dataFilter;
private readonly IChannelFilter _channelFilter;
+ private readonly IAbpDistributedLock distributedLock;
private readonly IAliPayApi _aliPayApi;
private readonly IAlipayInterfaceManager _alipayInterfaceManager;
private readonly IWxPayApi _wxPayApi;
@@ -73,7 +75,8 @@
IRepository<LifePayChannles, Guid> lifePayChannlesRep,
IRepository<LifePayAccount, Guid> lifePayAccount,
IDataFilter dataFilter,
- IChannelFilter channelFilter)
+ IChannelFilter channelFilter,
+ IAbpDistributedLock distributedLock)
{
_logger = logger;
_aCOOLYManager = aCOOLYManager;
@@ -95,6 +98,7 @@
_lifePayAccount = lifePayAccount;
this.dataFilter = dataFilter;
_channelFilter = channelFilter;
+ this.distributedLock = distributedLock;
_operateHistory = operateHistory;
}
@@ -1528,8 +1532,11 @@
public async Task ACOOLYOrderNotifyHandler(string orderNo, string acoolyOrderNo, LifePayOrderStatusEnum status, ACOOLYStatusEnum acoolyStatus, decimal payAmount, string refundApplyRemark, decimal? parValue = 0, decimal? actualParValue = 0)
{
var order = await _lifePayOrderRepository.Where(x => x.OrderNo == orderNo).FirstOrDefaultAsync();
+
CheckExtensions.IfTrueThrowUserFriendlyException(order == null, "璁㈠崟涓嶅瓨鍦�");
+ await using var orderLock = await distributedLock.TryAcquireAsync($"LockKey:UpdateOrder:{orderNo}", TimeSpan.FromSeconds(60));
+
if (order.ACOOLYStatus.HasValue && (int)order.ACOOLYStatus > (int)acoolyStatus)
{
_logger.LogInformation($"璁㈠崟锛坽orderNo}锛夌敱{order.ACOOLYStatus}鑷硔acoolyStatus}澶辫触锛屼笉鍙洖婊氱姸鎬�");
diff --git a/LifePayment/LifePayment.HttpApi/LifePay/ACOOLYNotifyController.cs b/LifePayment/LifePayment.HttpApi/LifePay/ACOOLYNotifyController.cs
index 02e1a4b..cae6846 100644
--- a/LifePayment/LifePayment.HttpApi/LifePay/ACOOLYNotifyController.cs
+++ b/LifePayment/LifePayment.HttpApi/LifePay/ACOOLYNotifyController.cs
@@ -10,6 +10,7 @@
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
+using Volo.Abp.DistributedLocking;
using Volo.Abp.Uow;
namespace LifePayment.HttpApi
@@ -20,14 +21,17 @@
public class ACOOLYNotifyController : AbpController
{
private readonly ILifePayService _lifePayService;
+ private readonly IAbpDistributedLock distributedLock;
private readonly ILogger<ACOOLYNotifyController> _logger;
public ACOOLYNotifyController(
ILogger<ACOOLYNotifyController> logger,
- ILifePayService lifePayService)
+ ILifePayService lifePayService,
+ IAbpDistributedLock distributedLock)
{
_logger = logger;
_lifePayService = lifePayService;
+ this.distributedLock = distributedLock;
}
/// <summary>
diff --git a/LifePayment/LifePayment.HttpApi/LifePay/AliPayNotifyController.cs b/LifePayment/LifePayment.HttpApi/LifePay/AliPayNotifyController.cs
index 1dc7f00..3ecc609 100644
--- a/LifePayment/LifePayment.HttpApi/LifePay/AliPayNotifyController.cs
+++ b/LifePayment/LifePayment.HttpApi/LifePay/AliPayNotifyController.cs
@@ -2,6 +2,7 @@
using Castle.Core.Internal;
using LifePayment.Application.Contracts;
using LifePayment.Domain.Shared;
+using Medallion.Threading;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
@@ -11,6 +12,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
+using Volo.Abp.DistributedLocking;
using Volo.Abp.Uow;
using ZeroD.Util;
@@ -27,15 +29,17 @@
private readonly ILifePayOrderService _lifePayOrderService;
private readonly ILogger<AliPayNotifyController> _logger;
+ private readonly IAbpDistributedLock distributedLock;
public AliPayNotifyController(
ILogger<AliPayNotifyController> logger,
-
+ IAbpDistributedLock distributedLock,
ILifePayService lifePayService,
ILifePayOrderService lifePayOrderService)
{
_logger = logger;
+ this.distributedLock = distributedLock;
_lifePayService = lifePayService;
_lifePayOrderService = lifePayOrderService;
}
@@ -61,6 +65,8 @@
{
if (input.TradeStatus == LifePaymentConstant.AliPayStatus.鏀粯鎴愬姛 && input.OutBizNo.IsNullOrEmpty())
{
+ await using var orderLock = await distributedLock.TryAcquireAsync($"LockKey:UpdateOrder:{input.OutTradeNo}", TimeSpan.FromSeconds(60));
+
await _lifePayService.LifePaySuccessHandler(input.OutTradeNo, input.TradeNo);
// 鎻掑叆鏀舵敮娴佹按
await _lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput()
@@ -75,6 +81,8 @@
&& input.OutBizNo.IsNotNullOrEmpty()
&& (input.RefundFee.HasValue && input.RefundFee > 0)))
{
+ await using var orderLock = await distributedLock.TryAcquireAsync($"LockKey:UpdateOrder:{input.OutTradeNo}", TimeSpan.FromSeconds(60));
+
await _lifePayService.LifePayRefundsHandler(input.OutTradeNo, LifePayRefundStatusEnum.宸查��娆�);
// 鎻掑叆鏀舵敮娴佹按
await _lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput()
diff --git a/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs b/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs
index de9e04b..3ff1a0d 100644
--- a/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs
+++ b/LifePayment/LifePayment.HttpApi/LifePay/WxPayNotifyController.cs
@@ -1,6 +1,7 @@
锘縰sing LifePayment.Application.Contracts;
using LifePayment.Domain;
using LifePayment.Domain.Shared;
+using Medallion.Threading;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
@@ -10,6 +11,7 @@
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Caching;
+using Volo.Abp.DistributedLocking;
using Volo.Abp.Uow;
using ZeroD.Util;
@@ -26,6 +28,7 @@
private readonly ILifePayService _lifePayService;
private readonly ILifePayOrderService _lifePayOrderService;
private readonly IDistributedCache<string> distributedCache;
+ private readonly IAbpDistributedLock distributedLock;
private readonly ILogger<WxPayNotifyController> _logger;
public WxPayNotifyController(
@@ -33,13 +36,15 @@
ILogger<WxPayNotifyController> logger,
ILifePayService lifePayService,
ILifePayOrderService lifePayOrderService,
- IDistributedCache<string> distributedCache)
+ IDistributedCache<string> distributedCache,
+ IAbpDistributedLock distributedLock)
{
_wxPayApi = wxPayApi;
_logger = logger;
_lifePayService = lifePayService;
_lifePayOrderService = lifePayOrderService;
this.distributedCache = distributedCache;
+ this.distributedLock = distributedLock;
}
/// <summary>
@@ -63,6 +68,8 @@
{
if (wxPayNotice.TradeState == LifePaymentConstant.WxPayStatus.鏀粯鎴愬姛)
{
+ await using var orderLock = await distributedLock.TryAcquireAsync($"LockKey:UpdateOrder:{wxPayNotice.OutTradeNo}", TimeSpan.FromSeconds(60));
+
var key = $"WxRechargeNotify_{wxPayNotice.OutTradeNo}";
if (string.IsNullOrWhiteSpace(distributedCache.Get(key)))
{
diff --git a/LifePayment/LifePayment.Worker/Worker/CheckUnPayOrderWork.cs b/LifePayment/LifePayment.Worker/Worker/CheckUnPayOrderWork.cs
index b16edd2..5bd2dec 100644
--- a/LifePayment/LifePayment.Worker/Worker/CheckUnPayOrderWork.cs
+++ b/LifePayment/LifePayment.Worker/Worker/CheckUnPayOrderWork.cs
@@ -6,16 +6,19 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nest;
+using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.BackgroundWorkers;
+using Volo.Abp.DistributedLocking;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Threading;
using Volo.Abp.Uow;
using ZeroD.Util;
+using ZeroD.Util.Fadd;
namespace LifePayment.Worker.Worker
{
@@ -25,6 +28,7 @@
private readonly int _doWorkMinute = 1;
private readonly ILifePayOrderService lifePayOrderService;
private readonly ILifePayService lifePayService;
+ private readonly IAbpDistributedLock distributedLock;
private readonly IRepository<LifePayOrder, Guid> lifePayOrderRepository;
public CheckUnPayOrderWork(
@@ -32,11 +36,13 @@
IServiceScopeFactory serviceScopeFactory,
ILifePayOrderService lifePayOrderService,
ILifePayService lifePayService,
+ IAbpDistributedLock distributedLock,
IRepository<LifePayOrder, Guid> lifePayOrderRepository) : base(timer, serviceScopeFactory)
{
- timer.Period = (int)TimeSpan.FromMinutes(10).TotalMilliseconds;
+ timer.Period = (int)TimeSpan.FromSeconds(5).TotalMilliseconds;
this.lifePayOrderService = lifePayOrderService;
this.lifePayService = lifePayService;
+ this.distributedLock = distributedLock;
this.lifePayOrderRepository = lifePayOrderRepository;
}
@@ -51,30 +57,32 @@
var orders = lifePayOrderRepository.Where(it => it.PayStatus == LifePayStatusEnum.鏈敮浠� && it.CreationTime >= start && it.CreationTime <= end).ToList();
foreach (var order in orders)
{
+ await using var orderLock = await distributedLock.TryAcquireAsync($"LockKey:UpdateOrder:{order.OrderNo}", TimeSpan.FromSeconds(60));
+
Logger.LogInformation($"璁㈠崟锛歿order.OrderNo}-{order.ToJson()}");
- //var wxPayNotice = await lifePayService.WxPayTradeQuery(order.OrderNo);
- //var json = wxPayNotice.ToJson();
- //Logger.LogInformation($"璁㈠崟锛坽wxPayNotice.OutTradeNo}锛変俊鎭�: {json}");
- //if (wxPayNotice.OutTradeNo.Contains("JF") && wxPayNotice.TradeState == LifePaymentConstant.WxPayStatus.鏀粯鎴愬姛)
- //{
- // await lifePayService.LifePaySuccessHandler(wxPayNotice.OutTradeNo, wxPayNotice.TransactionId);
+ var wxPayNotice = await lifePayService.WxPayTradeQuery(order.OrderNo);
+ var json = wxPayNotice.ToJson();
+ Logger.LogInformation($"璁㈠崟锛坽wxPayNotice.OutTradeNo}锛変俊鎭�: {json}");
+ if (wxPayNotice.OutTradeNo.Contains("JF") && wxPayNotice.TradeState == LifePaymentConstant.WxPayStatus.鏀粯鎴愬姛)
+ {
+ await lifePayService.LifePaySuccessHandler(wxPayNotice.OutTradeNo, wxPayNotice.TransactionId);
- // // 鎻掑叆鏀舵敮娴佹按
- // await lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput()
- // {
- // OrderNo = wxPayNotice.OutTradeNo,
- // OutOrderNo = wxPayNotice.TransactionId,
- // LifePayType = LifePayTypeEnum.WxPay,
- // ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Expenses,
- // Amount = wxPayNotice.Amount.Total
- // });
+ // 鎻掑叆鏀舵敮娴佹按
+ await lifePayOrderService.AddLifePayExpensesReceipts(new AddLifePayExpensesReceiptsInput()
+ {
+ OrderNo = wxPayNotice.OutTradeNo,
+ OutOrderNo = wxPayNotice.TransactionId,
+ LifePayType = LifePayTypeEnum.WxPay,
+ ExpensesReceiptsType = ExpensesReceiptsTypeEnum.Expenses,
+ Amount = wxPayNotice.Amount.Total
+ });
- // Logger.LogInformation("宸叉敼涓烘敮浠樻垚鍔�");
- //}
- //else
- //{
- // Logger.LogInformation("鏈洿鏂�");
- //}
+ Logger.LogInformation("宸叉敼涓烘敮浠樻垚鍔�");
+ }
+ else
+ {
+ Logger.LogInformation("鏈洿鏂�");
+ }
}
}
catch (Exception ex)
--
Gitblit v1.9.1