Files
qnc-server-v3/app/main/api/internal/logic/pay/paymentchecklogic.go

128 lines
4.0 KiB
Go
Raw Normal View History

2025-12-13 17:44:18 +08:00
package pay
import (
"context"
"strings"
2026-06-06 11:52:06 +08:00
"qnc-server/app/main/api/internal/service"
2025-12-13 17:44:18 +08:00
"qnc-server/app/main/api/internal/svc"
"qnc-server/app/main/api/internal/types"
2026-06-06 11:52:06 +08:00
"qnc-server/app/main/model"
"qnc-server/common/ctxdata"
2025-12-13 17:44:18 +08:00
"qnc-server/common/xerr"
2026-06-07 14:39:21 +08:00
2025-12-13 17:44:18 +08:00
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
)
type PaymentCheckLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewPaymentCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PaymentCheckLogic {
return &PaymentCheckLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *types.PaymentCheckResp, err error) {
if strings.HasPrefix(req.OrderNo, "U_") {
2026-06-06 11:52:06 +08:00
order, findErr := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
if findErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询升级订单失败: %v", findErr)
}
return &types.PaymentCheckResp{Type: "agent_upgrade", Status: order.Status}, nil
}
order, findErr := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
if findErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询订单失败: %v", findErr)
}
2026-06-07 14:39:21 +08:00
resp = &types.PaymentCheckResp{Type: "query", Status: order.Status}
2026-06-06 17:03:08 +08:00
// xpay 轮询pending 时主动查微信单并到账(不通知微信发货)
if order.Status == model.OrderStatusPending && model.IsXpayOrder(order) &&
2026-06-06 11:52:06 +08:00
l.svcCtx.XpayService != nil && l.svcCtx.XpayService.Enabled() {
2026-06-07 14:39:21 +08:00
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)
2025-12-13 17:44:18 +08:00
}
2026-06-07 14:39:21 +08:00
order, _ = l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
resp.Status = order.Status
2026-06-06 11:52:06 +08:00
}
2026-06-07 14:39:21 +08:00
return resp, nil
}
type xpaySyncInfo struct {
Status int
RawOrder string
2026-06-06 11:52:06 +08:00
}
2026-06-07 14:39:21 +08:00
func (l *PaymentCheckLogic) syncXpayOrderStatus(order *model.Order) (xpaySyncInfo, error) {
info := xpaySyncInfo{}
2026-06-06 11:52:06 +08:00
userID, err := ctxdata.GetUidFromCtx(l.ctx)
2025-12-13 17:44:18 +08:00
if err != nil {
2026-06-07 14:39:21 +08:00
return info, err
2025-12-13 17:44:18 +08:00
}
2026-06-06 11:52:06 +08:00
if order.UserId != userID {
2026-06-07 14:39:21 +08:00
return info, errors.New("无权查询此订单")
2026-06-06 11:52:06 +08:00
}
openid, err := l.svcCtx.XpayService.GetWxMiniOpenID(l.ctx, l.svcCtx.UserAuthModel, userID)
if err != nil {
2026-06-07 14:39:21 +08:00
return info, err
2026-06-06 11:52:06 +08:00
}
2026-06-07 14:39:21 +08:00
l.Infof("[xpay] check sync start order_no=%s openid=%s env=%d", order.OrderNo, openid, l.svcCtx.XpayService.Env())
2026-06-06 17:03:08 +08:00
status, err := l.svcCtx.XpayService.QueryOrder(l.ctx, openid, order.OrderNo, "")
2026-06-06 11:52:06 +08:00
if err != nil {
2026-06-07 14:39:21 +08:00
return info, err
2026-06-06 11:52:06 +08:00
}
2026-06-07 14:39:21 +08:00
info.Status = status.Status
info.RawOrder = status.RawOrder
2026-06-06 11:52:06 +08:00
if service.IsXpayPaidStatus(status.Status) {
2026-06-06 17:03:08 +08:00
_, fulfillErr := fulfillQueryOrderPaid(l.ctx, l.svcCtx, order, status.WxOrderID, status.PaidFee)
2026-06-07 14:39:21 +08:00
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
2026-06-06 11:52:06 +08:00
}
2026-06-07 15:11:33 +08:00
if service.IsXpayRefundedStatus(status.Status) || service.IsXpayAlreadyRefunded(status) {
order.Status = model.OrderStatusRefunded
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
return info, updateErr
}
l.Infof("[xpay] check sync refunded order_no=%s wx_status=%d left_fee=%d", order.OrderNo, status.Status, status.LeftFee)
return info, nil
}
2026-06-06 11:52:06 +08:00
if service.IsXpayClosedStatus(status.Status) {
2026-06-07 15:11:33 +08:00
order.Status = model.OrderStatusClosed
2026-06-07 14:39:21 +08:00
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
2026-06-06 11:52:06 +08:00
}
2026-06-07 14:39:21 +08:00
return info, nil
2025-12-13 17:44:18 +08:00
}