This commit is contained in:
Mrx
2026-06-07 14:39:21 +08:00
parent f9ec2e51fd
commit 0dada87a54
6 changed files with 113 additions and 29 deletions

View File

@@ -10,6 +10,7 @@ import (
"qnc-server/app/main/model"
"qnc-server/common/ctxdata"
"qnc-server/common/xerr"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -42,46 +43,76 @@ func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *type
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询订单失败: %v", findErr)
}
resp = &types.PaymentCheckResp{Type: "query", Status: order.Status}
// xpay 轮询pending 时主动查微信单并到账(不通知微信发货)
if order.Status == model.OrderStatusPending && model.IsXpayOrder(order) &&
l.svcCtx.XpayService != nil && l.svcCtx.XpayService.Enabled() {
if syncErr := l.syncXpayOrderStatus(order); syncErr != nil {
l.Errorf("[xpay] 轮询查单失败 order_no=%s err=%v", req.OrderNo, syncErr)
} else {
order, _ = l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
wxInfo, syncErr := l.syncXpayOrderStatus(order)
resp.WxOrderStatus = wxInfo.Status
resp.WxOrderDetail = wxInfo.RawOrder
if syncErr != nil {
resp.WxSyncError = syncErr.Error()
l.Errorf("[xpay] 轮询查单失败 order_no=%s local_status=%s wx_status=%d wx_detail=%s err=%v",
req.OrderNo, order.Status, wxInfo.Status, wxInfo.RawOrder, syncErr)
} else if wxInfo.Status > 0 && !service.IsXpayPaidStatus(wxInfo.Status) {
l.Infof("[xpay] 轮询查单未到账 order_no=%s wx_status=%d wx_detail=%s",
req.OrderNo, wxInfo.Status, wxInfo.RawOrder)
}
order, _ = l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
resp.Status = order.Status
}
return &types.PaymentCheckResp{Type: "query", Status: order.Status}, nil
return resp, nil
}
func (l *PaymentCheckLogic) syncXpayOrderStatus(order *model.Order) error {
type xpaySyncInfo struct {
Status int
RawOrder string
}
func (l *PaymentCheckLogic) syncXpayOrderStatus(order *model.Order) (xpaySyncInfo, error) {
info := xpaySyncInfo{}
userID, err := ctxdata.GetUidFromCtx(l.ctx)
if err != nil {
return err
return info, err
}
if order.UserId != userID {
return errors.New("无权查询此订单")
return info, errors.New("无权查询此订单")
}
openid, err := l.svcCtx.XpayService.GetWxMiniOpenID(l.ctx, l.svcCtx.UserAuthModel, userID)
if err != nil {
return err
return info, err
}
l.Infof("[xpay] check sync start order_no=%s openid=%s env=%d", order.OrderNo, openid, l.svcCtx.XpayService.Env())
status, err := l.svcCtx.XpayService.QueryOrder(l.ctx, openid, order.OrderNo, "")
if err != nil {
return err
return info, err
}
info.Status = status.Status
info.RawOrder = status.RawOrder
if service.IsXpayPaidStatus(status.Status) {
_, fulfillErr := fulfillQueryOrderPaid(l.ctx, l.svcCtx, order, status.WxOrderID, status.PaidFee)
return fulfillErr
if fulfillErr != nil {
return info, fulfillErr
}
l.Infof("[xpay] check sync paid order_no=%s wx_order_id=%s paid_fee=%d",
order.OrderNo, status.WxOrderID, status.PaidFee)
return info, nil
}
if service.IsXpayClosedStatus(status.Status) {
order.Status = "closed"
return l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order)
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
return info, updateErr
}
l.Infof("[xpay] check sync closed order_no=%s wx_status=%d", order.OrderNo, status.Status)
return info, nil
}
return nil
return info, nil
}