新版本,代理功能上线
This commit is contained in:
		| @@ -8,6 +8,7 @@ import ( | ||||
| 	"github.com/pkg/errors" | ||||
| 	"github.com/zeromicro/go-zero/core/logx" | ||||
| 	"github.com/zeromicro/go-zero/core/stores/sqlx" | ||||
| 	"os" | ||||
| 	"tydata-server/app/user/cmd/api/internal/svc" | ||||
| 	"tydata-server/app/user/cmd/api/internal/types" | ||||
| 	"tydata-server/app/user/model" | ||||
| @@ -48,12 +49,12 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, | ||||
| 	var data types.QueryCacheLoad | ||||
| 	err = json.Unmarshal([]byte(cache), &data) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 解析缓存内容失败, %+v", err) | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 解析缓存内容失败, %v", err) | ||||
| 	} | ||||
|  | ||||
| 	product, err := l.svcCtx.ProductModel.FindOneByProductEn(l.ctx, data.Product) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 查找产品错误: %+v", err) | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 查找产品错误: %v", err) | ||||
| 	} | ||||
| 	secretKey := l.svcCtx.Config.Encrypt.SecretKey | ||||
| 	key, decodeErr := hex.DecodeString(secretKey) | ||||
| @@ -71,16 +72,28 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, | ||||
| 	var prepayData interface{} | ||||
| 	var outTradeNo string | ||||
| 	var amount float64 | ||||
| 	var orderAmount float64 | ||||
| 	user, err := l.svcCtx.UserModel.FindOne(l.ctx, userID) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 获取用户信息失败: %+v", err) | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 获取用户信息失败: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if data.AgentIdentifier != "" { | ||||
| 		agentLinkModel, findAgentLinkErr := l.svcCtx.AgentLinkModel.FindOneByLinkIdentifier(l.ctx, data.AgentIdentifier) | ||||
| 		if findAgentLinkErr != nil { | ||||
| 			return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 获取代理订单失败: %+v", findAgentLinkErr) | ||||
| 		} | ||||
| 		amount = agentLinkModel.Price | ||||
| 		orderAmount = agentLinkModel.Price | ||||
| 	} else { | ||||
| 		amount = product.SellPrice | ||||
| 		orderAmount = product.SellPrice | ||||
| 	} | ||||
|  | ||||
| 	if user.Inside == 1 { | ||||
| 		amount = 0.01 | ||||
| 	} else { | ||||
| 		amount = product.SellPrice | ||||
| 	} | ||||
|  | ||||
| 	var createOrderErr error | ||||
| 	if req.PayMethod == "wechat" { | ||||
| 		outTradeNo = l.svcCtx.WechatPayService.GenerateOutTradeNo() | ||||
| @@ -103,16 +116,16 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, | ||||
| 			ProductId:       product.Id, | ||||
| 			PaymentPlatform: req.PayMethod, | ||||
| 			PaymentScene:    "app", | ||||
| 			Amount:          amount, | ||||
| 			Amount:          orderAmount, | ||||
| 			Status:          "pending", | ||||
| 		} | ||||
| 		orderInsertResult, insertOrderErr := l.svcCtx.OrderModel.Insert(ctx, session, &order) | ||||
| 		if insertOrderErr != nil { | ||||
| 			return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 保存订单失败: %+v", insertOrderErr) | ||||
| 			return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存订单失败: %+v", insertOrderErr) | ||||
| 		} | ||||
| 		insertedOrderID, lastInsertIdErr := orderInsertResult.LastInsertId() | ||||
| 		if lastInsertIdErr != nil { | ||||
| 			return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取保存订单ID失败: %+v", lastInsertIdErr) | ||||
| 			return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 获取保存订单ID失败: %+v", lastInsertIdErr) | ||||
| 		} | ||||
| 		orderID = insertedOrderID | ||||
| 		query := model.Query{ | ||||
| @@ -124,14 +137,32 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, | ||||
| 		} | ||||
| 		_, insertQueryErr := l.svcCtx.QueryModel.Insert(l.ctx, session, &query) | ||||
| 		if insertQueryErr != nil { | ||||
| 			return insertQueryErr | ||||
| 			return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存查询失败: %+v", lastInsertIdErr) | ||||
| 		} | ||||
| 		if data.AgentIdentifier != "" { | ||||
| 			agent, parsingErr := l.agentParsing(data.AgentIdentifier) | ||||
| 			if parsingErr != nil { | ||||
| 				return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 解析代理标识符失败: %+v", parsingErr) | ||||
| 			} | ||||
| 			var agentOrder model.AgentOrder | ||||
| 			agentOrder.OrderId = orderID | ||||
| 			agentOrder.AgentId = agent.AgentID | ||||
| 			_, agentOrderInsert := l.svcCtx.AgentOrderModel.Insert(ctx, session, &agentOrder) | ||||
| 			if agentOrderInsert != nil { | ||||
| 				return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存代理订单失败: %+v", agentOrderInsert) | ||||
| 			} | ||||
| 		} | ||||
| 		return nil | ||||
| 	}) | ||||
| 	if transErr != nil { | ||||
| 		return nil, transErr | ||||
| 	} | ||||
|  | ||||
| 	env := os.Getenv("ENV") | ||||
| 	if env == "development" { | ||||
| 		if asyncErr := l.svcCtx.AsynqService.SendQueryTask(orderID); asyncErr != nil { | ||||
| 			return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 异步订单调用失败: %+v", asyncErr) | ||||
| 		} | ||||
| 	} | ||||
| 	switch v := prepayData.(type) { | ||||
| 	case string: | ||||
| 		// 如果 prepayData 是字符串类型,直接返回 | ||||
| @@ -140,3 +171,21 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, | ||||
| 		return &types.PaymentResp{PrepayData: prepayData, OrderID: orderID}, nil | ||||
| 	} | ||||
| } | ||||
| func (l *PaymentLogic) agentParsing(agentIdentifier string) (*types.AgentIdentifier, error) { | ||||
| 	key, decodeErr := hex.DecodeString("8e3e7a2f60edb49221e953b9c029ed10") | ||||
| 	if decodeErr != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 获取AES密钥失败: %+v", decodeErr) | ||||
| 	} | ||||
| 	// Encrypt the params | ||||
|  | ||||
| 	encrypted, err := crypto.AesDecryptURL(agentIdentifier, key) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, %v", err) | ||||
| 	} | ||||
| 	var agentIdentifierStruct types.AgentIdentifier | ||||
| 	err = json.Unmarshal(encrypted, &agentIdentifierStruct) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务,反序列化失败 %v", err) | ||||
| 	} | ||||
| 	return &agentIdentifierStruct, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user