189 lines
5.8 KiB
Go
189 lines
5.8 KiB
Go
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
|
||
}
|