Files
kami_gateway/internal/service/base_service.go
danial e88ff05a14 refactor(trace): 重命名 otel 包为 otelTrace并更新相关引用
- 将内部使用的 otel 包重命名为 otelTrace
- 更新了所有引用该包的文件中的导入路径
- 修改了部分函数和变量名称以适应新的包名
2025-02-23 21:56:29 +08:00

189 lines
5.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"context"
"encoding/json"
"errors"
"gateway/internal/config"
"gateway/internal/otelTrace"
"gateway/internal/service/supplier"
"gateway/internal/models/merchant"
"gateway/internal/schema/response"
"strconv"
"github.com/duke-git/lancet/v2/convertor"
"go.uber.org/zap"
)
// GetMerchantInfoByUID 获取商户信息
func GetMerchantInfoByUID(ctx context.Context, params map[string]any) *response.PayBaseResp {
c := new(response.PayBaseResp)
c.Params = make(map[string]any)
c.Params = params
merchantInfo := merchant.GetMerchantByUid(ctx, convertor.ToString(params["MerchantUid"]))
if merchantInfo.MerchantUid == "" || len(merchantInfo.MerchantUid) == 0 {
c.Code = -1
c.Msg = "商户不存在或者paykey有误请联系管理员"
} else if merchantInfo.Status != config.ACTIVE {
c.Code = -1
c.Msg = "商户状态已经被冻结或者被删除,请联系管理员!"
} else {
c.MerchantInfo = merchantInfo
}
return c
}
// GetMerchantInfoByPayKey 通过payKey获取商户信息
func GetMerchantInfoByPayKey(ctx context.Context, payKey string) (merchantInfo merchant.MerchantInfo, err error) {
merchantInfo = merchant.GetMerchantByPasskey(ctx, payKey)
if merchantInfo.MerchantUid == "" || len(merchantInfo.MerchantUid) == 0 {
err = errors.New("商户不存在,请联系商户")
return
} else if merchantInfo.Status != config.ACTIVE {
err = errors.New("商户状态已经被冻结或者被删除,请联系商户!")
return
}
return
}
// GetMerchantInfo 获取商户信息
func GetMerchantInfo(ctx context.Context, params map[string]any) *response.PayBaseResp {
c := new(response.PayBaseResp)
c.Params = make(map[string]any)
c.Params = params
merchantInfo := merchant.GetMerchantByPasskey(ctx, convertor.ToString(params["payKey"]))
if merchantInfo.MerchantUid == "" || len(merchantInfo.MerchantUid) == 0 {
c.Code = -1
c.Msg = "商户不存在或者paykey有误请联系管理员"
} else if merchantInfo.Status != config.ACTIVE {
c.Code = -1
c.Msg = "商户状态已经被冻结或者被删除,请联系管理员!"
} else {
c.MerchantInfo = merchantInfo
}
return c
}
func JudgeParams(ctx context.Context, c *response.PayBaseResp) *response.PayBaseResp {
c = OrderIsValid(ctx, c)
c = NotifyUrlIsValid(ctx, c)
c = OrderPeriodIsValid(ctx, c)
c = OrderPriceIsValid(ctx, c)
c = ExValueIsValid(ctx, c)
return c
}
func ExValueIsValid(ctx context.Context, c *response.PayBaseResp) *response.PayBaseResp {
if c.Params["exValue"] == "" || len(convertor.ToString(c.Params["exValue"])) == 0 {
c.Code = -1
c.Msg = "扩展参数不能为空"
}
isRedeemValid := true
exRedeemValue := supplier.RedeemCardInfo{}
if err := json.Unmarshal([]byte(convertor.ToString(c.Params["exValue"])), &exRedeemValue); err != nil {
otelTrace.Logger.WithContext(ctx).Error("提交卡密格式错误,请检查", zap.Any("exValue", c.Params["exValue"]), zap.Error(err))
isRedeemValid = false
}
if exRedeemValue.Data == "" || len(exRedeemValue.Data) == 0 {
isRedeemValid = false
}
if !isRedeemValid {
c.Code = -1
c.Msg = "提交卡密格式错误,请检查"
}
return c
}
func CompleteRedeemExValue(exValueStr string, faceValue string) (string, error) {
exValue := supplier.RedeemCardInfo{}
if err := json.Unmarshal([]byte(exValueStr), &exValue); err != nil {
return "", err
}
exValue.FaceType = faceValue
res, err := json.Marshal(&exValue)
return string(res), err
}
func GetCompleteRedeemExValue(exValueStr string, faceValue string) (exValue supplier.RedeemCardInfo, err error) {
exValue = supplier.RedeemCardInfo{}
if err = json.Unmarshal([]byte(exValueStr), &exValue); err != nil {
return exValue, err
}
exValue.FaceType = faceValue
return
}
func CompleteRechargeExValue(exValueStr string, faceValue string) (string, error) {
exValue := supplier.RechargeCardInfo{}
if err := json.Unmarshal([]byte(exValueStr), &exValue); err != nil {
return "", err
}
exValue.FaceType = faceValue
res, err := json.Marshal(&exValue)
return string(res), err
}
// NotifyUrlIsValid 判断回调地址是否符合规则
func NotifyUrlIsValid(ctx context.Context, c *response.PayBaseResp) *response.PayBaseResp {
if c.Params["notifyUrl"] == "" || len(convertor.ToString(c.Params["notifyUrl"])) == 0 {
c.Code = -1
c.Msg = "订单回调不能为空"
}
return c
}
// OsTypeIsValid 判断设备类型是否符合规则
func OsTypeIsValid(c *response.PayBaseResp) *response.PayBaseResp {
if c.Params["osType"] == "" || len(convertor.ToString(c.Params["osType"])) == 0 {
c.Code = -1
c.Msg = "支付设备系统类型不能为空,默认填写\"1\"即可"
}
return c
}
func OrderPeriodIsValid(ctx context.Context, c *response.PayBaseResp) *response.PayBaseResp {
if c.Params["orderPeriod"] == "" || len(convertor.ToString(c.Params["orderPeriod"])) == 0 {
c.Code = -1
c.Msg = "订单过期时间不能为空,默认填写\"1\"即可"
}
return c
}
// OrderPriceIsValid 判断订单金额
func OrderPriceIsValid(ctx context.Context, c *response.PayBaseResp) *response.PayBaseResp {
if c.Params["orderPrice"] == "" || len(convertor.ToString(c.Params["orderPrice"])) == 0 {
c.Code = -1
c.Msg = "订单金额不能为空"
return c
}
a, err := strconv.ParseFloat(convertor.ToString(c.Params["orderPrice"]), 64)
if err != nil {
otelTrace.Logger.WithContext(ctx).Error("order price is invalid ", zap.Any("orderPrice", c.Params["orderPrice"]), zap.Error(err))
c.Code = -1
c.Msg = "订单金额非法"
}
c.OrderAmount = a
return c
}
// OrderIsValid 判断金额订单号是否为空或者有重复
func OrderIsValid(ctx context.Context, c *response.PayBaseResp) *response.PayBaseResp {
if c.Params["orderNo"] == "" || len(convertor.ToString(c.Params["orderNo"])) == 0 {
c.Code = -1
c.Msg = "商户订单号不能为空"
return c
}
return c
}
// IpIsWhite 判断ip是否在白名单中
func IpIsWhite() bool {
// TODO
return true
}