Dashboard 收入接口路径对齐;登录页去掉「授权管理平台」;Waffo 测试/生产密钥与 Product 映射分环境保存。 Co-authored-by: Cursor <cursoragent@cursor.com>
133 lines
3.5 KiB
Go
133 lines
3.5 KiB
Go
package payment
|
||
|
||
import (
|
||
"encoding/json"
|
||
"strings"
|
||
)
|
||
|
||
// WaffoEnvProfile 单环境下的 Product 映射(测试/生产隔离) // Logan.Li
|
||
type WaffoEnvProfile struct {
|
||
ProductID string `json:"product_id,omitempty"`
|
||
ProductIDYearly string `json:"product_id_yearly,omitempty"`
|
||
ProductIDPerpetual string `json:"product_id_perpetual,omitempty"`
|
||
ProductIDs map[string]string `json:"product_ids,omitempty"`
|
||
ProductMappings []WaffoProductMapping `json:"product_mappings,omitempty"`
|
||
}
|
||
|
||
// WaffoSecretBag 测试/生产私钥分存(写入 SecretKey 字段的 JSON) // Logan.Li
|
||
type WaffoSecretBag struct {
|
||
Test string `json:"test,omitempty"`
|
||
Live string `json:"live,omitempty"`
|
||
}
|
||
|
||
// NormalizeWaffoEnvironment 归一化环境名 // Logan.Li
|
||
func NormalizeWaffoEnvironment(env string) string {
|
||
switch strings.ToLower(strings.TrimSpace(env)) {
|
||
case "live", "prod", "production":
|
||
return "live"
|
||
default:
|
||
return "test"
|
||
}
|
||
}
|
||
|
||
// ParseWaffoSecretBag 解析 SecretKey:JSON 双钥或旧版单 PEM // Logan.Li
|
||
func ParseWaffoSecretBag(raw, fallbackEnv string) WaffoSecretBag {
|
||
raw = strings.TrimSpace(raw)
|
||
if raw == "" {
|
||
return WaffoSecretBag{}
|
||
}
|
||
if strings.HasPrefix(raw, "{") {
|
||
var bag WaffoSecretBag
|
||
if err := json.Unmarshal([]byte(raw), &bag); err == nil {
|
||
return bag
|
||
}
|
||
}
|
||
env := NormalizeWaffoEnvironment(fallbackEnv)
|
||
if env == "live" {
|
||
return WaffoSecretBag{Live: raw}
|
||
}
|
||
return WaffoSecretBag{Test: raw}
|
||
}
|
||
|
||
// EncodeWaffoSecretBag 序列化双环境私钥 // Logan.Li
|
||
func EncodeWaffoSecretBag(bag WaffoSecretBag) (string, error) {
|
||
bag.Test = strings.TrimSpace(bag.Test)
|
||
bag.Live = strings.TrimSpace(bag.Live)
|
||
b, err := json.Marshal(bag)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
return string(b), nil
|
||
}
|
||
|
||
// SecretForEnv 取指定环境私钥 // Logan.Li
|
||
func (b WaffoSecretBag) SecretForEnv(env string) string {
|
||
if NormalizeWaffoEnvironment(env) == "live" {
|
||
return strings.TrimSpace(b.Live)
|
||
}
|
||
return strings.TrimSpace(b.Test)
|
||
}
|
||
|
||
// SetSecretForEnv 写入指定环境私钥 // Logan.Li
|
||
func (b *WaffoSecretBag) SetSecretForEnv(env, secret string) {
|
||
secret = strings.TrimSpace(secret)
|
||
if NormalizeWaffoEnvironment(env) == "live" {
|
||
b.Live = secret
|
||
return
|
||
}
|
||
b.Test = secret
|
||
}
|
||
|
||
// ClearSecretForEnv 清除指定环境私钥 // Logan.Li
|
||
func (b *WaffoSecretBag) ClearSecretForEnv(env string) {
|
||
b.SetSecretForEnv(env, "")
|
||
}
|
||
|
||
// ApplyEnvProfile 将当前环境的 Profile 叠到顶层字段供运行时使用 // Logan.Li
|
||
func (m *WaffoPublicMeta) ApplyEnvProfile(env string) {
|
||
if m == nil {
|
||
return
|
||
}
|
||
env = NormalizeWaffoEnvironment(env)
|
||
if m.EnvProfiles == nil {
|
||
return
|
||
}
|
||
p, ok := m.EnvProfiles[env]
|
||
if !ok {
|
||
return
|
||
}
|
||
if len(p.ProductMappings) > 0 {
|
||
m.ProductMappings = p.ProductMappings
|
||
}
|
||
if len(p.ProductIDs) > 0 {
|
||
m.ProductIDs = p.ProductIDs
|
||
}
|
||
if p.ProductID != "" {
|
||
m.ProductID = p.ProductID
|
||
}
|
||
if p.ProductIDYearly != "" {
|
||
m.ProductIDYearly = p.ProductIDYearly
|
||
}
|
||
if p.ProductIDPerpetual != "" {
|
||
m.ProductIDPerpetual = p.ProductIDPerpetual
|
||
}
|
||
}
|
||
|
||
// CaptureEnvProfile 把顶层映射写入指定环境 Profile // Logan.Li
|
||
func (m *WaffoPublicMeta) CaptureEnvProfile(env string) {
|
||
if m == nil {
|
||
return
|
||
}
|
||
env = NormalizeWaffoEnvironment(env)
|
||
if m.EnvProfiles == nil {
|
||
m.EnvProfiles = map[string]WaffoEnvProfile{}
|
||
}
|
||
m.EnvProfiles[env] = WaffoEnvProfile{
|
||
ProductID: m.ProductID,
|
||
ProductIDYearly: m.ProductIDYearly,
|
||
ProductIDPerpetual: m.ProductIDPerpetual,
|
||
ProductIDs: m.ProductIDs,
|
||
ProductMappings: m.ProductMappings,
|
||
}
|
||
}
|