2024-11-23 16:13:23 +08:00
|
|
|
package pay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2025-05-27 18:35:01 +08:00
|
|
|
"tyc-server/app/main/api/internal/service"
|
2025-04-27 12:17:18 +08:00
|
|
|
"tyc-server/app/main/api/internal/svc"
|
|
|
|
"tyc-server/app/main/api/internal/types"
|
2025-04-09 15:58:06 +08:00
|
|
|
"tyc-server/common/ctxdata"
|
|
|
|
"tyc-server/common/xerr"
|
2025-03-21 15:47:11 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
2024-11-23 16:13:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2025-05-27 18:35:01 +08:00
|
|
|
// 从缓存中获取订单信息
|
2025-04-09 17:27:40 +08:00
|
|
|
outTradeNo := req.Id
|
|
|
|
redisKey := fmt.Sprintf("%d:%s", userID, outTradeNo)
|
2024-11-23 16:13:23 +08:00
|
|
|
cache, cacheErr := l.svcCtx.Redis.GetCtx(l.ctx, redisKey)
|
|
|
|
if cacheErr != nil {
|
|
|
|
return nil, cacheErr
|
|
|
|
}
|
2025-05-27 18:35:01 +08:00
|
|
|
|
2024-12-24 11:37:25 +08:00
|
|
|
var data types.QueryCacheLoad
|
2024-11-23 16:13:23 +08:00
|
|
|
err = json.Unmarshal([]byte(cache), &data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 解析缓存内容失败, %+v", err)
|
|
|
|
}
|
2025-05-27 18:35:01 +08:00
|
|
|
var payMethod service.PayMethod
|
|
|
|
switch req.PayMethod {
|
|
|
|
case "alipay":
|
|
|
|
payMethod = service.PayMethodAlipay
|
|
|
|
case "wechat":
|
|
|
|
payMethod = service.PayMethodWechat
|
|
|
|
// case "apple":
|
|
|
|
// payMethod = service.PayMethodApple
|
|
|
|
default:
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 不支持的支付方式: %+v", req.PayMethod)
|
2024-12-28 17:13:18 +08:00
|
|
|
}
|
2025-01-01 02:00:06 +08:00
|
|
|
|
2025-05-27 18:35:01 +08:00
|
|
|
// 创建支付订单
|
|
|
|
orderReq := &service.CreateOrderRequest{
|
|
|
|
PayMethod: payMethod,
|
|
|
|
ProductEn: data.Product,
|
2024-12-28 17:13:18 +08:00
|
|
|
}
|
2025-01-18 22:34:27 +08:00
|
|
|
|
2025-05-27 18:35:01 +08:00
|
|
|
orderResp, err := l.svcCtx.PayService.CreateOrder(l.ctx, orderReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单失败: %+v", err)
|
2024-11-23 16:13:23 +08:00
|
|
|
}
|
2025-05-09 17:54:28 +08:00
|
|
|
|
2025-05-27 18:35:01 +08:00
|
|
|
return &types.PaymentResp{
|
|
|
|
PrepayData: orderResp.PayData,
|
|
|
|
PrepayId: orderResp.OrderNo,
|
|
|
|
OrderID: orderResp.OrderID,
|
|
|
|
}, nil
|
2024-11-23 16:13:23 +08:00
|
|
|
}
|