Files
pay/internal/accounts/registry.go
T

41 lines
1.1 KiB
Go

// 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 ""
}