feat(v2): 账户配置注册表(config.accounts + env 取凭证 + 渠道/区域筛选)
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// Package accounts loads the payment-account registry from config. Credentials
|
||||
// never live in the config file — only an env-var prefix is stored; the real
|
||||
// secret is read from the environment at call time.
|
||||
package accounts
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/wangjia/pay/config"
|
||||
)
|
||||
|
||||
type Registry struct{ accts []config.AccountConfig }
|
||||
|
||||
func New(accts []config.AccountConfig) *Registry { return &Registry{accts: accts} }
|
||||
|
||||
// EnabledFor returns enabled accounts for a channel (+region if non-empty).
|
||||
func (r *Registry) EnabledFor(channel, region string) []config.AccountConfig {
|
||||
var out []config.AccountConfig
|
||||
for _, a := range r.accts {
|
||||
if a.Channel != channel || !a.Enabled {
|
||||
continue
|
||||
}
|
||||
if region != "" && a.Region != "" && a.Region != region {
|
||||
continue
|
||||
}
|
||||
out = append(out, a)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Credential reads <CredentialEnvPrefix>_<KEY> from the environment.
|
||||
func (r *Registry) Credential(accountID, key string) string {
|
||||
for _, a := range r.accts {
|
||||
if a.AccountID == accountID {
|
||||
return os.Getenv(strings.ToUpper(a.CredentialEnvPrefix + "_" + key))
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user