f
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -125,11 +125,15 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
}
|
||||
xpayParams, buildErr := l.svcCtx.XpayService.BuildPayParams(l.ctx, userID, paymentTypeResp.outTradeNo, paymentTypeResp.productEn, paymentTypeResp.amount)
|
||||
if buildErr != nil {
|
||||
logx.WithContext(l.ctx).Errorf("[xpay] BuildPayParams FAIL user=%s order_no=%s amount=%.2f product_en=%s env=%d err=%v",
|
||||
userID, paymentTypeResp.outTradeNo, paymentTypeResp.amount, paymentTypeResp.productEn, l.svcCtx.XpayService.Env(), buildErr)
|
||||
if strings.Contains(buildErr.Error(), "过期") || strings.Contains(buildErr.Error(), "会话") {
|
||||
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, buildErr.Error()), "")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "构造虚拟支付参数失败: %v", buildErr)
|
||||
}
|
||||
logx.WithContext(l.ctx).Infof("[xpay] BuildPayParams OK user=%s order_no=%s env=%d signData=%s",
|
||||
userID, paymentTypeResp.outTradeNo, l.svcCtx.XpayService.Env(), xpayParams.SignData)
|
||||
prepayData = xpayParams
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user