21
This commit is contained in:
parent
c7bf419208
commit
8a6635bc52
|
|
@ -13,13 +13,16 @@ namespace MiAssessment.Api.Controllers;
|
|||
public class NotifyController : ControllerBase
|
||||
{
|
||||
private readonly IPaymentNotifyService _paymentNotifyService;
|
||||
private readonly IOrderService _orderService;
|
||||
private readonly ILogger<NotifyController> _logger;
|
||||
|
||||
public NotifyController(
|
||||
IPaymentNotifyService paymentNotifyService,
|
||||
IOrderService orderService,
|
||||
ILogger<NotifyController> logger)
|
||||
{
|
||||
_paymentNotifyService = paymentNotifyService;
|
||||
_orderService = orderService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
|
@ -64,6 +67,19 @@ public class NotifyController : ControllerBase
|
|||
_logger.LogInformation("微信支付回调处理完成: Success={Success}, Message={Message}",
|
||||
result.Success, result.Message);
|
||||
|
||||
// 支付成功时更新订单状态
|
||||
if (result.Success && !string.IsNullOrEmpty(result.OrderNo) && result.NotifyData != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _orderService.HandlePaymentSuccessAsync(result.OrderNo, result.NotifyData.TransactionId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "更新订单状态失败,orderNo: {OrderNo}", result.OrderNo);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据回调版本返回对应格式的响应
|
||||
if (!string.IsNullOrEmpty(result.JsonResponse))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -85,4 +85,16 @@ public interface IOrderService
|
|||
/// <param name="orderId">订单ID</param>
|
||||
/// <returns>支付结果,包含是否已支付、订单状态、关联的测评记录ID等</returns>
|
||||
Task<PayResultDto?> GetPayResultAsync(long userId, long orderId);
|
||||
|
||||
/// <summary>
|
||||
/// 处理支付成功回调
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 更新订单状态为已支付,记录支付时间和交易号。
|
||||
/// 测评订单会同步更新关联的测评记录状态为待测评。
|
||||
/// </remarks>
|
||||
/// <param name="orderNo">订单编号</param>
|
||||
/// <param name="transactionId">微信支付交易号</param>
|
||||
/// <returns>是否处理成功</returns>
|
||||
Task<bool> HandlePaymentSuccessAsync(string orderNo, string transactionId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -274,6 +274,52 @@ public class OrderService : IOrderService
|
|||
return payResult;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> HandlePaymentSuccessAsync(string orderNo, string transactionId)
|
||||
{
|
||||
_logger.LogInformation("处理支付成功回调,orderNo: {OrderNo}, transactionId: {TransactionId}", orderNo, transactionId);
|
||||
|
||||
var order = await _dbContext.Orders
|
||||
.FirstOrDefaultAsync(o => o.OrderNo == orderNo && !o.IsDeleted);
|
||||
|
||||
if (order == null)
|
||||
{
|
||||
_logger.LogWarning("支付回调订单不存在,orderNo: {OrderNo}", orderNo);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 幂等:已支付的订单不重复处理
|
||||
if (order.Status >= 2)
|
||||
{
|
||||
_logger.LogInformation("订单已处理,跳过,orderNo: {OrderNo}, status: {Status}", orderNo, order.Status);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 更新订单状态为已支付
|
||||
order.Status = 2;
|
||||
order.PayTime = DateTime.Now;
|
||||
order.TransactionId = transactionId;
|
||||
order.PayType = 1; // 微信支付
|
||||
order.UpdateTime = DateTime.Now;
|
||||
|
||||
// 测评订单:更新关联的测评记录状态为待测评
|
||||
if (order.OrderType == 1)
|
||||
{
|
||||
var record = await _dbContext.AssessmentRecords
|
||||
.FirstOrDefaultAsync(r => r.OrderId == order.Id && !r.IsDeleted);
|
||||
if (record != null)
|
||||
{
|
||||
record.Status = 1; // 待测评
|
||||
record.UpdateTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("订单支付状态更新成功,orderNo: {OrderNo}, orderId: {OrderId}", orderNo, order.Id);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// 创建订单
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ export function usePayment() {
|
|||
try {
|
||||
const res = await getPayResult(orderId)
|
||||
if (res.code === 0 && res.data) {
|
||||
if (res.data.paid) {
|
||||
if (res.data.isPaid) {
|
||||
return res.data
|
||||
}
|
||||
}
|
||||
|
|
@ -140,7 +140,7 @@ export function usePayment() {
|
|||
|
||||
// 3. 查询支付结果
|
||||
const result = await checkPayResult(order.orderId)
|
||||
if (result && result.paid) {
|
||||
if (result && result.isPaid) {
|
||||
return { success: true, orderId: order.orderId, assessmentRecordId: order.assessmentRecordId }
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user