f
This commit is contained in:
@@ -95,6 +95,112 @@ func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *type
|
||||
}
|
||||
}
|
||||
|
||||
// 如果订单状态是 pending 且支付平台是云印签支付,主动查询云印签订单状态
|
||||
if order.Status == "pending" && (order.PaymentPlatform == "yunyinSignPay" || order.PaymentPlatform == "yunyinSignPay_wechat" || order.PaymentPlatform == "yunyinSignPay_alipay") {
|
||||
// 检查云印签支付服务是否启用
|
||||
if l.svcCtx.YunYinSignPayService != nil {
|
||||
// 主动查询云印签订单状态
|
||||
queryResp, queryErr := l.svcCtx.YunYinSignPayService.QueryPayeeBill(l.ctx, req.OrderNo)
|
||||
if queryErr != nil {
|
||||
logx.Errorf("主动查询云印签订单状态失败,订单号: %s, 错误: %v", req.OrderNo, queryErr)
|
||||
// 查询失败不影响返回,继续返回当前订单状态
|
||||
} else {
|
||||
// 根据云印签返回的支付状态更新本地订单状态
|
||||
// 云印签 payStatus: 0-订单生成, 1-支付中, 2-支付成功, 3-支付失败, 4-已退款
|
||||
// 我们的订单状态: pending, paid, failed, refunded
|
||||
var newOrderStatus string
|
||||
var shouldUpdate bool
|
||||
|
||||
switch queryResp.PayStatus {
|
||||
case 2: // 支付成功
|
||||
newOrderStatus = "paid"
|
||||
shouldUpdate = true
|
||||
case 3: // 支付失败
|
||||
newOrderStatus = "failed"
|
||||
shouldUpdate = true
|
||||
case 4: // 已退款
|
||||
newOrderStatus = "refunded"
|
||||
shouldUpdate = true
|
||||
case 0, 1: // 订单生成或支付中,保持 pending
|
||||
// 不更新,继续返回 pending
|
||||
}
|
||||
|
||||
if shouldUpdate {
|
||||
logx.Infof("主动查询发现云印签订单状态已变更,订单号: %s, 支付状态: %d, 新订单状态: %s", req.OrderNo, queryResp.PayStatus, newOrderStatus)
|
||||
|
||||
// 重新查询订单(获取最新版本号)
|
||||
order, err = l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
logx.Errorf("更新订单状态前重新查询订单失败: %v", err)
|
||||
} else {
|
||||
// 更新云印签订单表的支付状态(无论订单状态如何,都要同步云印签的状态)
|
||||
yunyinOrder, findYunyinErr := l.svcCtx.YunyinSignPayOrderModel.FindOneByOrderId(l.ctx, order.Id)
|
||||
if findYunyinErr == nil && yunyinOrder != nil {
|
||||
// 更新支付状态
|
||||
// 云印签 payStatus: 0-订单生成, 1-支付中, 2-支付成功, 3-支付失败, 4-已退款
|
||||
// 我们的 payStatus: 0-待支付, 1-已支付, 2-已退款
|
||||
var newPayStatus int64
|
||||
if queryResp.PayStatus == 2 {
|
||||
newPayStatus = 1 // 已支付
|
||||
} else if queryResp.PayStatus == 4 {
|
||||
newPayStatus = 2 // 已退款
|
||||
} else if queryResp.PayStatus == 3 {
|
||||
// 支付失败,保持待支付状态
|
||||
newPayStatus = 0
|
||||
}
|
||||
|
||||
if newPayStatus != yunyinOrder.PayStatus {
|
||||
yunyinOrder.PayStatus = newPayStatus
|
||||
if updateYunyinErr := l.svcCtx.YunyinSignPayOrderModel.Update(l.ctx, yunyinOrder); updateYunyinErr != nil {
|
||||
logx.Errorf("更新云印签订单支付状态失败: %v", updateYunyinErr)
|
||||
} else {
|
||||
logx.Infof("成功更新云印签订单支付状态,订单ID: %s, 新支付状态: %d", order.Id, newPayStatus)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 只有在订单状态是 pending 时才更新订单状态
|
||||
if order.Status == "pending" {
|
||||
// 更新订单状态
|
||||
order.Status = newOrderStatus
|
||||
if newOrderStatus == "paid" {
|
||||
order.PayTime = lzUtils.TimeToNullTime(time.Now())
|
||||
if queryResp.ChannelOrderNo != "" {
|
||||
order.PlatformOrderId = lzUtils.StringToNullString(queryResp.ChannelOrderNo)
|
||||
}
|
||||
} else if newOrderStatus == "refunded" {
|
||||
order.RefundTime = lzUtils.TimeToNullTime(time.Now())
|
||||
}
|
||||
|
||||
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
|
||||
logx.Errorf("主动查询后更新订单状态失败: %v", updateErr)
|
||||
} else {
|
||||
logx.Infof("主动查询后成功更新订单状态,订单号: %s, 新状态: %s", req.OrderNo, newOrderStatus)
|
||||
|
||||
// 如果订单已支付,发送异步任务处理后续流程
|
||||
if newOrderStatus == "paid" {
|
||||
if asyncErr := l.svcCtx.AsynqService.SendQueryTask(order.Id); asyncErr != nil {
|
||||
logx.Errorf("主动查询后发送异步任务失败: %v", asyncErr)
|
||||
}
|
||||
}
|
||||
|
||||
// 返回更新后的状态
|
||||
return &types.PaymentCheckResp{
|
||||
Type: "query",
|
||||
Status: newOrderStatus,
|
||||
}, nil
|
||||
}
|
||||
} else {
|
||||
// 订单状态已经不是 pending,说明可能已经被其他流程处理
|
||||
// 但仍然返回当前状态
|
||||
logx.Infof("订单状态已不是 pending,当前状态: %s,跳过更新", order.Status)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &types.PaymentCheckResp{
|
||||
Type: "query",
|
||||
Status: order.Status,
|
||||
|
||||
Reference in New Issue
Block a user