79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package monetix
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
DefaultRequestTimeout = 15 * time.Second
|
|
DefaultStatusSuccess = "success"
|
|
DefaultStatusProcessing = "processing"
|
|
|
|
OutcomeSuccess = "success"
|
|
OutcomeProcessing = "processing"
|
|
OutcomeDecline = "decline"
|
|
)
|
|
|
|
// Config holds resolved settings for communicating with Monetix.
|
|
type Config struct {
|
|
BaseURL string
|
|
ProjectID int64
|
|
SecretKey string
|
|
AllowedCurrencies []string
|
|
RequireCustomerAddress bool
|
|
RequestTimeout time.Duration
|
|
StatusSuccess string
|
|
StatusProcessing string
|
|
}
|
|
|
|
func DefaultConfig() Config {
|
|
return Config{
|
|
RequestTimeout: DefaultRequestTimeout,
|
|
StatusSuccess: DefaultStatusSuccess,
|
|
StatusProcessing: DefaultStatusProcessing,
|
|
}
|
|
}
|
|
|
|
func (c Config) timeout() time.Duration {
|
|
if c.RequestTimeout <= 0 {
|
|
return DefaultRequestTimeout
|
|
}
|
|
return c.RequestTimeout
|
|
}
|
|
|
|
// Timeout exposes the configured HTTP timeout for external callers.
|
|
func (c Config) Timeout() time.Duration {
|
|
return c.timeout()
|
|
}
|
|
|
|
func (c Config) CurrencyAllowed(code string) bool {
|
|
code = strings.ToUpper(strings.TrimSpace(code))
|
|
if code == "" {
|
|
return false
|
|
}
|
|
if len(c.AllowedCurrencies) == 0 {
|
|
return true
|
|
}
|
|
for _, allowed := range c.AllowedCurrencies {
|
|
if strings.EqualFold(strings.TrimSpace(allowed), code) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (c Config) SuccessStatus() string {
|
|
if strings.TrimSpace(c.StatusSuccess) == "" {
|
|
return DefaultStatusSuccess
|
|
}
|
|
return strings.ToLower(strings.TrimSpace(c.StatusSuccess))
|
|
}
|
|
|
|
func (c Config) ProcessingStatus() string {
|
|
if strings.TrimSpace(c.StatusProcessing) == "" {
|
|
return DefaultStatusProcessing
|
|
}
|
|
return strings.ToLower(strings.TrimSpace(c.StatusProcessing))
|
|
}
|