115 lines
3.8 KiB
Go
115 lines
3.8 KiB
Go
package pay
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"tyc-server/app/user/cmd/api/internal/svc"
|
|
"tyc-server/app/user/cmd/api/internal/types"
|
|
"tyc-server/app/user/model"
|
|
"tyc-server/common/ctxdata"
|
|
"tyc-server/common/xerr"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
)
|
|
|
|
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)
|
|
}
|
|
outTradeNo := req.Id
|
|
redisKey := fmt.Sprintf("%d:%s", userID, outTradeNo)
|
|
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)
|
|
}
|
|
var prepayData interface{}
|
|
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 == "wechat" {
|
|
prepayData, createOrderErr = l.svcCtx.WechatPayService.CreateWechatOrder(l.ctx, amount, product.ProductName, outTradeNo)
|
|
} else if req.PayMethod == "alipay" {
|
|
prepayData, createOrderErr = l.svcCtx.AlipayService.CreateAlipayOrder(l.ctx, amount, product.ProductName, outTradeNo, brand)
|
|
} else if req.PayMethod == "appleiap" {
|
|
prepayData = 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: "tyc",
|
|
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
|
|
return nil
|
|
})
|
|
if transErr != nil {
|
|
return nil, transErr
|
|
}
|
|
switch v := prepayData.(type) {
|
|
case string:
|
|
// 如果 prepayData 是字符串类型,直接返回
|
|
return &types.PaymentResp{PrepayId: v, OrderID: orderID}, nil
|
|
default:
|
|
return &types.PaymentResp{PrepayData: prepayData, OrderID: orderID}, nil
|
|
}
|
|
}
|