f
This commit is contained in:
@@ -5,13 +5,13 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/model"
|
||||
"qnc-server/pkg/lzkit/lzUtils"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
@@ -45,6 +45,9 @@ func (l *AlipayCallbackLogic) AlipayCallback(w http.ResponseWriter, r *http.Requ
|
||||
} else if strings.HasPrefix(orderNo, "U_") {
|
||||
// 代理升级订单处理
|
||||
return l.handleAgentUpgradeOrderPayment(w, notification)
|
||||
} else if strings.HasPrefix(orderNo, "W_") {
|
||||
// 白名单下架订单
|
||||
return l.handleWhitelistOrderPayment(w, notification)
|
||||
} else if strings.HasPrefix(orderNo, "A_") {
|
||||
// 旧系统会员充值订单(已废弃,新系统使用升级功能)
|
||||
// return l.handleAgentVipOrderPayment(w, notification)
|
||||
@@ -226,6 +229,51 @@ func (l *AlipayCallbackLogic) handleAgentUpgradeOrderPayment(w http.ResponseWrit
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleWhitelistOrderPayment 处理白名单下架订单支付
|
||||
func (l *AlipayCallbackLogic) handleWhitelistOrderPayment(w http.ResponseWriter, notification *alipay.Notification) error {
|
||||
wo, err := l.svcCtx.WhitelistOrderModel.FindOneByOrderNo(l.ctx, notification.OutTradeNo)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
logx.Errorf("支付宝支付回调,白名单订单不存在: %s", notification.OutTradeNo)
|
||||
} else {
|
||||
logx.Errorf("支付宝支付回调,查找白名单订单失败: %+v", err)
|
||||
}
|
||||
alipay.ACKNotification(w)
|
||||
return nil
|
||||
}
|
||||
if wo.Status != 1 {
|
||||
alipay.ACKNotification(w)
|
||||
return nil
|
||||
}
|
||||
amount := lzUtils.ToAlipayAmount(wo.TotalAmount)
|
||||
if amount != notification.TotalAmount {
|
||||
logx.Errorf("支付宝支付回调,白名单订单金额不一致")
|
||||
alipay.ACKNotification(w)
|
||||
return nil
|
||||
}
|
||||
if notification.TradeStatus != alipay.TradeStatusSuccess {
|
||||
alipay.ACKNotification(w)
|
||||
return nil
|
||||
}
|
||||
wo.Status = 2
|
||||
wo.PaymentMethod = lzUtils.StringToNullString("alipay")
|
||||
wo.PayTime = lzUtils.TimeToNullTime(time.Now())
|
||||
wo.PlatformOrderId = lzUtils.StringToNullString(notification.TradeNo)
|
||||
if updateErr := l.svcCtx.WhitelistOrderModel.UpdateWithVersion(l.ctx, nil, wo); updateErr != nil {
|
||||
logx.Errorf("支付宝支付回调,更新白名单订单失败: %+v", updateErr)
|
||||
alipay.ACKNotification(w)
|
||||
return nil
|
||||
}
|
||||
err = l.svcCtx.WhitelistOrderModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
return l.svcCtx.WhitelistService.ProcessPaidWhitelistOrder(ctx, session, nil, wo)
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("支付宝支付回调,处理白名单订单失败: %+v", err)
|
||||
}
|
||||
alipay.ACKNotification(w)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 处理代理会员订单支付(已废弃,新系统使用升级功能)
|
||||
/*
|
||||
func (l *AlipayCallbackLogic) handleAgentVipOrderPayment(w http.ResponseWriter, notification *alipay.Notification) error {
|
||||
|
||||
@@ -3,6 +3,7 @@ package pay
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/api/internal/types"
|
||||
"qnc-server/common/xerr"
|
||||
@@ -27,8 +28,33 @@ func NewPaymentCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Paym
|
||||
|
||||
func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *types.PaymentCheckResp, err error) {
|
||||
// 根据订单号前缀判断订单类型
|
||||
// 白名单下架订单:前缀 W_
|
||||
if strings.HasPrefix(req.OrderNo, "W_") {
|
||||
whitelistOrder, err := l.svcCtx.WhitelistOrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询白名单订单失败: %v", err)
|
||||
}
|
||||
|
||||
status := "pending"
|
||||
switch whitelistOrder.Status {
|
||||
case 1:
|
||||
status = "pending"
|
||||
case 2:
|
||||
status = "paid"
|
||||
case 3:
|
||||
status = "closed"
|
||||
default:
|
||||
status = "pending"
|
||||
}
|
||||
|
||||
return &types.PaymentCheckResp{
|
||||
Type: "whitelist",
|
||||
Status: status,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 升级订单:前缀 U_
|
||||
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)
|
||||
@@ -38,8 +64,8 @@ func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *type
|
||||
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)
|
||||
|
||||
@@ -76,6 +76,12 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case "whitelist":
|
||||
paymentTypeResp, err = l.WhitelistOrderPayment(req, session)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 开发环境测试支付模式:跳过实际支付流程
|
||||
@@ -462,6 +468,67 @@ func (l *PaymentLogic) AgentUpgradeOrderPayment(req *types.PaymentReq, session s
|
||||
}, nil
|
||||
}
|
||||
|
||||
// WhitelistOrderPayment 白名单下架订单支付(id 格式:id_card|feature_api_id)
|
||||
func (l *PaymentLogic) WhitelistOrderPayment(req *types.PaymentReq, session sqlx.Session) (resp *PaymentTypeResp, err error) {
|
||||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err)
|
||||
}
|
||||
parts := strings.SplitN(req.Id, "|", 2)
|
||||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("白名单支付参数无效,id 格式应为 id_card|feature_api_id"), "")
|
||||
}
|
||||
idCard := parts[0]
|
||||
featureApiId := parts[1]
|
||||
feature, err := l.svcCtx.FeatureModel.FindOneByApiId(l.ctx, featureApiId)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("模块不存在"), "")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询模块失败: %v", err)
|
||||
}
|
||||
if feature.WhitelistPrice <= 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrMsg("该模块不支持付费下架"), "")
|
||||
}
|
||||
base := l.svcCtx.AlipayService.GenerateOutTradeNo()
|
||||
outTradeNo := "W_" + base
|
||||
if len(outTradeNo) > 32 {
|
||||
outTradeNo = outTradeNo[:32]
|
||||
}
|
||||
amount := feature.WhitelistPrice
|
||||
wo := &model.WhitelistOrder{
|
||||
Id: uuid.NewString(),
|
||||
OrderNo: outTradeNo,
|
||||
UserId: userID,
|
||||
IdCard: idCard,
|
||||
TotalAmount: amount,
|
||||
Status: 1,
|
||||
}
|
||||
_, err = l.svcCtx.WhitelistOrderModel.Insert(l.ctx, session, wo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建白名单订单失败: %v", err)
|
||||
}
|
||||
item := &model.WhitelistOrderItem{
|
||||
Id: uuid.NewString(),
|
||||
OrderId: wo.Id,
|
||||
FeatureId: feature.Id,
|
||||
FeatureApiId: feature.ApiId,
|
||||
FeatureName: feature.Name,
|
||||
Price: amount,
|
||||
}
|
||||
_, err = l.svcCtx.WhitelistOrderItemModel.Insert(l.ctx, session, item)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "创建白名单订单明细失败: %v", err)
|
||||
}
|
||||
description := fmt.Sprintf("%s 模块下架", feature.Name)
|
||||
return &PaymentTypeResp{
|
||||
amount: amount,
|
||||
outTradeNo: outTradeNo,
|
||||
description: description,
|
||||
orderID: wo.Id,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// getLevelBonus 获取等级加成(从配置表读取)
|
||||
func (l *PaymentLogic) getLevelBonus(level int64) (int64, error) {
|
||||
var configKey string
|
||||
|
||||
@@ -5,10 +5,11 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"qnc-server/app/main/api/internal/service"
|
||||
"qnc-server/pkg/lzkit/lzUtils"
|
||||
|
||||
"qnc-server/app/main/api/internal/service"
|
||||
"qnc-server/app/main/api/internal/svc"
|
||||
"qnc-server/app/main/model"
|
||||
"qnc-server/pkg/lzkit/lzUtils"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
|
||||
@@ -45,6 +46,9 @@ func (l *WechatPayCallbackLogic) WechatPayCallback(w http.ResponseWriter, r *htt
|
||||
} else if strings.HasPrefix(orderNo, "U_") {
|
||||
// 代理升级订单处理
|
||||
return l.handleAgentUpgradeOrderPayment(w, notification)
|
||||
} else if strings.HasPrefix(orderNo, "W_") {
|
||||
// 白名单下架订单
|
||||
return l.handleWhitelistOrderPayment(w, notification)
|
||||
} else if strings.HasPrefix(orderNo, "A_") {
|
||||
// 旧系统会员充值订单(已废弃,新系统使用升级功能)
|
||||
// return l.handleAgentVipOrderPayment(w, notification)
|
||||
@@ -222,6 +226,57 @@ func (l *WechatPayCallbackLogic) handleAgentUpgradeOrderPayment(w http.ResponseW
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleWhitelistOrderPayment 处理白名单下架订单支付
|
||||
func (l *WechatPayCallbackLogic) handleWhitelistOrderPayment(w http.ResponseWriter, notification *payments.Transaction) error {
|
||||
wo, err := l.svcCtx.WhitelistOrderModel.FindOneByOrderNo(l.ctx, *notification.OutTradeNo)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
logx.Errorf("微信支付回调,白名单订单不存在: %s", *notification.OutTradeNo)
|
||||
} else {
|
||||
logx.Errorf("微信支付回调,查找白名单订单失败: %+v", err)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
if wo.Status != 1 {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
amount := lzUtils.ToWechatAmount(wo.TotalAmount)
|
||||
if amount != *notification.Amount.Total {
|
||||
logx.Errorf("微信支付回调,白名单订单金额不一致")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
if *notification.TradeState != service.TradeStateSuccess {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
wo.Status = 2
|
||||
wo.PaymentMethod = lzUtils.StringToNullString("wechat")
|
||||
wo.PayTime = lzUtils.TimeToNullTime(time.Now())
|
||||
wo.PlatformOrderId = lzUtils.StringToNullString(*notification.TransactionId)
|
||||
if updateErr := l.svcCtx.WhitelistOrderModel.UpdateWithVersion(l.ctx, nil, wo); updateErr != nil {
|
||||
logx.Errorf("微信支付回调,更新白名单订单失败: %+v", updateErr)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
err = l.svcCtx.WhitelistOrderModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
return l.svcCtx.WhitelistService.ProcessPaidWhitelistOrder(ctx, session, nil, wo)
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("微信支付回调,处理白名单订单失败: %+v", err)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
|
||||
// 处理代理会员订单支付(已废弃,新系统使用升级功能)
|
||||
/*
|
||||
func (l *WechatPayCallbackLogic) handleAgentVipOrderPayment(w http.ResponseWriter, notification *payments.Transaction) error {
|
||||
|
||||
Reference in New Issue
Block a user