From 8a6635bc52c263cd35e4a0b2b783fb41bbbba25b Mon Sep 17 00:00:00 2001 From: zpc Date: Thu, 19 Mar 2026 06:05:12 +0800 Subject: [PATCH] 21 --- .../Controllers/NotifyController.cs | 16 +++++++ .../Interfaces/IOrderService.cs | 12 +++++ .../Services/OrderService.cs | 46 +++++++++++++++++++ uniapp/composables/usePayment.js | 4 +- 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/server/MiAssessment/src/MiAssessment.Api/Controllers/NotifyController.cs b/server/MiAssessment/src/MiAssessment.Api/Controllers/NotifyController.cs index 830ba70..9118603 100644 --- a/server/MiAssessment/src/MiAssessment.Api/Controllers/NotifyController.cs +++ b/server/MiAssessment/src/MiAssessment.Api/Controllers/NotifyController.cs @@ -13,13 +13,16 @@ namespace MiAssessment.Api.Controllers; public class NotifyController : ControllerBase { private readonly IPaymentNotifyService _paymentNotifyService; + private readonly IOrderService _orderService; private readonly ILogger _logger; public NotifyController( IPaymentNotifyService paymentNotifyService, + IOrderService orderService, ILogger 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)) { diff --git a/server/MiAssessment/src/MiAssessment.Core/Interfaces/IOrderService.cs b/server/MiAssessment/src/MiAssessment.Core/Interfaces/IOrderService.cs index 36c68d7..6e1a7b2 100644 --- a/server/MiAssessment/src/MiAssessment.Core/Interfaces/IOrderService.cs +++ b/server/MiAssessment/src/MiAssessment.Core/Interfaces/IOrderService.cs @@ -85,4 +85,16 @@ public interface IOrderService /// 订单ID /// 支付结果,包含是否已支付、订单状态、关联的测评记录ID等 Task GetPayResultAsync(long userId, long orderId); + + /// + /// 处理支付成功回调 + /// + /// + /// 更新订单状态为已支付,记录支付时间和交易号。 + /// 测评订单会同步更新关联的测评记录状态为待测评。 + /// + /// 订单编号 + /// 微信支付交易号 + /// 是否处理成功 + Task HandlePaymentSuccessAsync(string orderNo, string transactionId); } diff --git a/server/MiAssessment/src/MiAssessment.Core/Services/OrderService.cs b/server/MiAssessment/src/MiAssessment.Core/Services/OrderService.cs index 1f14c9e..7c06aa8 100644 --- a/server/MiAssessment/src/MiAssessment.Core/Services/OrderService.cs +++ b/server/MiAssessment/src/MiAssessment.Core/Services/OrderService.cs @@ -274,6 +274,52 @@ public class OrderService : IOrderService return payResult; } + /// + public async Task 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; + } + /// /// /// 创建订单 diff --git a/uniapp/composables/usePayment.js b/uniapp/composables/usePayment.js index fb3244d..3f8e860 100644 --- a/uniapp/composables/usePayment.js +++ b/uniapp/composables/usePayment.js @@ -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 } }