183 lines
5.3 KiB
Go
183 lines
5.3 KiB
Go
package service
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"gateway/internal/config"
|
||
"gateway/internal/entities/supplier"
|
||
"gateway/internal/models/merchant"
|
||
"gateway/internal/schema/response"
|
||
"github.com/beego/beego/v2/core/logs"
|
||
"strconv"
|
||
)
|
||
|
||
// GetMerchantInfoByUID 获取商户信息
|
||
func GetMerchantInfoByUID(params map[string]string) *response.PayBaseResp {
|
||
c := new(response.PayBaseResp)
|
||
c.Params = make(map[string]string)
|
||
c.Params = params
|
||
merchantInfo := merchant.GetMerchantByUid(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(payKey string) (merchantInfo merchant.MerchantInfo, err error) {
|
||
merchantInfo = merchant.GetMerchantByPasskey(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(params map[string]string) *response.PayBaseResp {
|
||
c := new(response.PayBaseResp)
|
||
c.Params = make(map[string]string)
|
||
c.Params = params
|
||
merchantInfo := merchant.GetMerchantByPasskey(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(c *response.PayBaseResp) *response.PayBaseResp {
|
||
c = OrderIsValid(c)
|
||
c = NotifyUrlIsValid(c)
|
||
c = OrderPeriodIsValid(c)
|
||
c = OrderPriceIsValid(c)
|
||
c = ExValueIsValid(c)
|
||
return c
|
||
}
|
||
|
||
func ExValueIsValid(c *response.PayBaseResp) *response.PayBaseResp {
|
||
if c.Params["exValue"] == "" || len(c.Params["exValue"]) == 0 {
|
||
c.Code = -1
|
||
c.Msg = "扩展参数不能为空"
|
||
}
|
||
isRedeemValid := true
|
||
exRedeemValue := supplier.RedeemCardInfo{}
|
||
if err := json.Unmarshal([]byte(c.Params["exValue"]), &exRedeemValue); err != nil {
|
||
logs.Error("提交卡密格式错误,请检查", c.Params["exValue"], err)
|
||
isRedeemValid = false
|
||
}
|
||
if exRedeemValue.Data == "" || len(exRedeemValue.Data) == 0 {
|
||
isRedeemValid = false
|
||
}
|
||
isRechargeValid := true
|
||
exRechargeValue := supplier.RechargeCardInfo{}
|
||
if err := json.Unmarshal([]byte(c.Params["exValue"]), &exRechargeValue); err != nil {
|
||
logs.Error("提交卡密格式错误,请检查", c.Params["exValue"], err)
|
||
isRechargeValid = false
|
||
}
|
||
if exRechargeValue.AccountNumber == "" {
|
||
isRechargeValid = false
|
||
}
|
||
if !isRedeemValid && !isRechargeValid {
|
||
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 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(c *response.PayBaseResp) *response.PayBaseResp {
|
||
if c.Params["notifyUrl"] == "" || len(c.Params["notifyUrl"]) == 0 {
|
||
c.Code = -1
|
||
c.Msg = "订单回调不能为空"
|
||
}
|
||
return c
|
||
}
|
||
|
||
// OsTypeIsValid 判断设备类型是否符合规则
|
||
func OsTypeIsValid(c *response.PayBaseResp) *response.PayBaseResp {
|
||
if c.Params["osType"] == "" || len(c.Params["osType"]) == 0 {
|
||
c.Code = -1
|
||
c.Msg = "支付设备系统类型不能为空,默认填写\"1\"即可"
|
||
}
|
||
return c
|
||
}
|
||
|
||
func OrderPeriodIsValid(c *response.PayBaseResp) *response.PayBaseResp {
|
||
if c.Params["orderPeriod"] == "" || len(c.Params["orderPeriod"]) == 0 {
|
||
c.Code = -1
|
||
c.Msg = "订单过期时间不能为空,默认填写\"1\"即可"
|
||
}
|
||
|
||
return c
|
||
}
|
||
|
||
// OrderPriceIsValid 判断订单金额
|
||
func OrderPriceIsValid(c *response.PayBaseResp) *response.PayBaseResp {
|
||
if c.Params["orderPrice"] == "" || len(c.Params["orderPrice"]) == 0 {
|
||
c.Code = -1
|
||
c.Msg = "订单金额不能为空"
|
||
return c
|
||
}
|
||
|
||
a, err := strconv.ParseFloat(c.Params["orderPrice"], 64)
|
||
if err != nil {
|
||
logs.Error("order price is invalid: ", c.Params["orderPrice"])
|
||
c.Code = -1
|
||
c.Msg = "订单金额非法"
|
||
}
|
||
c.OrderAmount = a
|
||
|
||
return c
|
||
}
|
||
|
||
// OrderIsValid 判断金额订单号是否为空或者有重复
|
||
func OrderIsValid(c *response.PayBaseResp) *response.PayBaseResp {
|
||
if c.Params["orderNo"] == "" || len(c.Params["orderNo"]) == 0 {
|
||
c.Code = -1
|
||
c.Msg = "商户订单号不能为空"
|
||
return c
|
||
}
|
||
return c
|
||
}
|
||
|
||
// IpIsWhite 判断ip是否在白名单中
|
||
func IpIsWhite() bool {
|
||
// TODO
|
||
return true
|
||
}
|