123 lines
4.0 KiB
Go
123 lines
4.0 KiB
Go
package pay
|
|
|
|
import (
|
|
"aedata-server/app/main/api/internal/svc"
|
|
"aedata-server/app/main/api/internal/types"
|
|
"aedata-server/app/main/model"
|
|
"aedata-server/common/ctxdata"
|
|
"aedata-server/common/xerr"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/redis/go-redis/v9"
|
|
"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
|
|
}
|
|
type PaymentTypeResp struct {
|
|
amount float64
|
|
outTradeNo string
|
|
description string
|
|
}
|
|
|
|
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) {
|
|
var paymentTypeResp *PaymentTypeResp
|
|
var prepayData interface{}
|
|
l.svcCtx.OrderModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
|
|
switch req.PayType {
|
|
case "query":
|
|
paymentTypeResp, err = l.QueryOrderPayment(req, session)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
var createOrderErr error
|
|
if req.PayMethod == "wechat" {
|
|
prepayData, createOrderErr = l.svcCtx.WechatPayService.CreateWechatOrder(l.ctx, paymentTypeResp.amount, paymentTypeResp.description, paymentTypeResp.outTradeNo)
|
|
} else if req.PayMethod == "alipay" {
|
|
prepayData, createOrderErr = l.svcCtx.AlipayService.CreateAlipayOrder(l.ctx, paymentTypeResp.amount, paymentTypeResp.description, paymentTypeResp.outTradeNo)
|
|
}
|
|
if createOrderErr != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 创建支付订单失败: %+v", createOrderErr)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
switch v := prepayData.(type) {
|
|
case string:
|
|
// 如果 prepayData 是字符串类型,直接返回
|
|
return &types.PaymentResp{PrepayId: v, OrderNo: paymentTypeResp.outTradeNo}, nil
|
|
default:
|
|
return &types.PaymentResp{PrepayData: prepayData, OrderNo: paymentTypeResp.outTradeNo}, nil
|
|
}
|
|
}
|
|
|
|
func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Session) (resp *PaymentTypeResp, err error) {
|
|
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
|
|
if getUidErr != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取用户信息失败, %+v", getUidErr)
|
|
}
|
|
outTradeNo := req.Id
|
|
redisKey := fmt.Sprintf(types.QueryCacheKey, userID, outTradeNo)
|
|
cache, cacheErr := l.svcCtx.Redis.GetCtx(l.ctx, redisKey)
|
|
if cacheErr != nil {
|
|
if cacheErr == redis.Nil {
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("订单已过期"), "生成订单, 缓存不存在, %+v", cacheErr)
|
|
}
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取缓存失败, %+v", 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 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)
|
|
}
|
|
amount = product.SellPrice
|
|
if user.Inside == 1 {
|
|
amount = 0.01
|
|
}
|
|
order := model.Order{
|
|
OrderNo: outTradeNo,
|
|
UserId: userID,
|
|
ProductId: product.Id,
|
|
PaymentPlatform: req.PayMethod,
|
|
PaymentScene: "app",
|
|
Amount: amount,
|
|
Status: "pending",
|
|
}
|
|
_, insertOrderErr := l.svcCtx.OrderModel.Insert(l.ctx, session, &order)
|
|
if insertOrderErr != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存订单失败: %+v", insertOrderErr)
|
|
}
|
|
|
|
return &PaymentTypeResp{amount: amount, outTradeNo: outTradeNo, description: product.ProductName}, nil
|
|
}
|