f
This commit is contained in:
@@ -3,10 +3,13 @@ package pay
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"qnc-server/app/main/api/internal/service"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
"qnc-server/app/main/model"
|
||||
"qnc-server/common/ctxdata"
|
||||
"qnc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -26,26 +29,72 @@ func NewPaymentCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Paym
|
||||
}
|
||||
|
||||
func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *types.PaymentCheckResp, err error) {
|
||||
// 根据订单号前缀判断订单类型
|
||||
if strings.HasPrefix(req.OrderNo, "U_") {
|
||||
// 升级订单
|
||||
order, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询升级订单失败: %v", err)
|
||||
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
|
||||
return &types.PaymentCheckResp{Type: "agent_upgrade", Status: order.Status}, nil
|
||||
}
|
||||
|
||||
// 查询订单(包括代理订单)
|
||||
order, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询订单失败: %v", err)
|
||||
|
||||
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: "query",
|
||||
Status: order.Status,
|
||||
}, nil
|
||||
|
||||
// xpay 轮询:pending 时主动查微信单
|
||||
if order.Status == "pending" && order.PaymentScene == "wxmini" &&
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return &types.PaymentCheckResp{Type: "query", Status: order.Status}, nil
|
||||
}
|
||||
|
||||
func (l *PaymentCheckLogic) syncXpayOrderStatus(order *model.Order) error {
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if order.UserId != userID {
|
||||
return errors.New("无权查询此订单")
|
||||
}
|
||||
|
||||
openid, err := l.svcCtx.XpayService.GetWxMiniOpenID(l.ctx, l.svcCtx.UserAuthModel, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sessionKey, err := l.svcCtx.XpayService.GetSessionKey(l.ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
status, err := l.svcCtx.XpayService.QueryOrder(l.ctx, openid, order.OrderNo, sessionKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if service.IsXpayPaidStatus(status.Status) {
|
||||
credited, fulfillErr := fulfillQueryOrderPaid(l.ctx, l.svcCtx, order, "", status.PaidFee)
|
||||
if fulfillErr != nil {
|
||||
return fulfillErr
|
||||
}
|
||||
if credited {
|
||||
wxOrderID := ""
|
||||
_ = l.svcCtx.XpayService.NotifyProvideGoods(l.ctx, openid, order.OrderNo, wxOrderID, sessionKey)
|
||||
_ = l.svcCtx.XpayService.MarkNotified(l.ctx, order.OrderNo)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if service.IsXpayClosedStatus(status.Status) {
|
||||
order.Status = "closed"
|
||||
return l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user