first commit
This commit is contained in:
		
							
								
								
									
										136
									
								
								app/user/cmd/api/internal/logic/pay/paymentlogic.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										136
									
								
								app/user/cmd/api/internal/logic/pay/paymentlogic.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,136 @@ | ||||
| package pay | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"encoding/hex" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"github.com/pkg/errors" | ||||
| 	"github.com/zeromicro/go-zero/core/logx" | ||||
| 	"github.com/zeromicro/go-zero/core/stores/sqlx" | ||||
| 	"tydata-server/app/user/cmd/api/internal/svc" | ||||
| 	"tydata-server/app/user/cmd/api/internal/types" | ||||
| 	"tydata-server/app/user/model" | ||||
| 	"tydata-server/common/ctxdata" | ||||
| 	"tydata-server/common/xerr" | ||||
| 	"tydata-server/pkg/lzkit/crypto" | ||||
| ) | ||||
|  | ||||
| type PaymentLogic struct { | ||||
| 	logx.Logger | ||||
| 	ctx    context.Context | ||||
| 	svcCtx *svc.ServiceContext | ||||
| } | ||||
|  | ||||
| func NewPaymentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PaymentLogic { | ||||
| 	return &PaymentLogic{ | ||||
| 		Logger: logx.WithContext(ctx), | ||||
| 		ctx:    ctx, | ||||
| 		svcCtx: svcCtx, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, err error) { | ||||
| 	userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx) | ||||
| 	if getUidErr != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取用户信息失败, %+v", getUidErr) | ||||
| 	} | ||||
|  | ||||
| 	brand, ok := l.ctx.Value("brand").(string) | ||||
| 	if !ok { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取平台失败, %+v", getUidErr) | ||||
| 	} | ||||
| 	redisKey := fmt.Sprintf("%d:%s", userID, req.Id) | ||||
| 	cache, cacheErr := l.svcCtx.Redis.GetCtx(l.ctx, redisKey) | ||||
| 	if cacheErr != nil { | ||||
| 		return nil, cacheErr | ||||
| 	} | ||||
| 	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) | ||||
| 	} | ||||
|  | ||||
| 	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) | ||||
| 	} | ||||
| 	secretKey := l.svcCtx.Config.Encrypt.SecretKey | ||||
| 	key, decodeErr := hex.DecodeString(secretKey) | ||||
| 	if decodeErr != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取AES密钥失败: %+v", decodeErr) | ||||
| 	} | ||||
| 	params, marshalErr := json.Marshal(data.Params) | ||||
| 	if marshalErr != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 序列化参数失败: %+v", marshalErr) | ||||
| 	} | ||||
| 	encryptParams, aesEncryptErr := crypto.AesEncrypt(params, key) | ||||
| 	if aesEncryptErr != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 加密参数失败: %+v", aesEncryptErr) | ||||
| 	} | ||||
| 	var prepayID string | ||||
| 	var outTradeNo string | ||||
| 	var amount float64 | ||||
| 	user, err := l.svcCtx.UserModel.FindOne(l.ctx, userID) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 获取用户信息失败: %+v", err) | ||||
| 	} | ||||
|  | ||||
| 	if user.Inside == 1 { | ||||
| 		amount = 0.01 | ||||
| 	} else { | ||||
| 		amount = product.SellPrice | ||||
| 	} | ||||
| 	var createOrderErr error | ||||
| 	if req.PayMethod == "wechatpay" { | ||||
| 		outTradeNo = l.svcCtx.WechatPayService.GenerateOutTradeNo() | ||||
| 		prepayID, createOrderErr = l.svcCtx.WechatPayService.CreateWechatOrder(l.ctx, amount, product.ProductName, outTradeNo) | ||||
| 	} else if req.PayMethod == "alipay" { | ||||
| 		outTradeNo = l.svcCtx.AlipayService.GenerateOutTradeNo() | ||||
| 		prepayID, createOrderErr = l.svcCtx.AlipayService.CreateAlipayOrder(l.ctx, amount, product.ProductName, outTradeNo, brand) | ||||
| 	} else if req.PayMethod == "appleiap" { | ||||
| 		outTradeNo = l.svcCtx.ApplePayService.GenerateOutTradeNo() | ||||
| 		prepayID = l.svcCtx.ApplePayService.GetIappayAppID(product.ProductEn) | ||||
| 	} | ||||
| 	if createOrderErr != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 创建支付订单失败: %+v", createOrderErr) | ||||
| 	} | ||||
| 	var orderID int64 | ||||
| 	transErr := l.svcCtx.OrderModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error { | ||||
| 		order := model.Order{ | ||||
| 			OrderNo:         outTradeNo, | ||||
| 			UserId:          userID, | ||||
| 			ProductId:       product.Id, | ||||
| 			PaymentPlatform: req.PayMethod, | ||||
| 			PaymentScene:    "app", | ||||
| 			Amount:          amount, | ||||
| 			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) | ||||
| 		} | ||||
| 		insertedOrderID, lastInsertIdErr := orderInsertResult.LastInsertId() | ||||
| 		if lastInsertIdErr != nil { | ||||
| 			return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取保存订单ID失败: %+v", lastInsertIdErr) | ||||
| 		} | ||||
| 		orderID = insertedOrderID | ||||
| 		query := model.Query{ | ||||
| 			OrderId:     orderID, | ||||
| 			UserId:      userID, | ||||
| 			ProductId:   product.Id, | ||||
| 			QueryParams: encryptParams, | ||||
| 			QueryState:  "pending", | ||||
| 		} | ||||
| 		_, insertQueryErr := l.svcCtx.QueryModel.Insert(l.ctx, session, &query) | ||||
| 		if insertQueryErr != nil { | ||||
| 			return insertQueryErr | ||||
| 		} | ||||
| 		return nil | ||||
| 	}) | ||||
| 	if transErr != nil { | ||||
| 		return nil, transErr | ||||
| 	} | ||||
|  | ||||
| 	return &types.PaymentResp{PrepayID: prepayID, OrderID: orderID}, nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user