Documentation
¶
Index ¶
- Variables
- func AllHealthy(ctx context.Context, providers []HealthChecker) error
- func HealthCheckAll(ctx context.Context, providers []HealthChecker) map[string]error
- type Cache
- type ExchangeInfo
- type ExchangeRate
- type ExchangeRateProviderdeprecated
- type GetPaymentStatusParams
- type HealthChecker
- type InitiatePaymentParams
- type InitiatePaymentResponse
- type Payment
- type PaymentEvent
- type PaymentProviderdeprecated
- type PaymentStatus
- type Provider
- type ProviderMetadata
- type RateFetcher
- type RateHistory
- type RateInfo
- type RateStats
- type SupportedChecker
- type UpdatePaymentStatusParams
Constants ¶
This section is empty.
Variables ¶
var ( ErrExchangeRateUnavailable = errors.New("exchange rate service unavailable") // ErrUnsupportedCurrencyPair indicates the currency pair is not supported. ErrUnsupportedCurrencyPair = errors.New("unsupported currency pair") // ErrExchangeRateExpired indicates the exchange rate data is stale or expired. ErrExchangeRateExpired = errors.New("exchange rate has expired") // ErrExchangeRateInvalid indicates the received exchange rate is invalid. ErrExchangeRateInvalid = errors.New("invalid exchange rate received") )
Common errors for exchange rate operations
var ( ErrUnsupportedPair = errors.New("unsupported currency pair") )
Common errors for provider operations
Functions ¶
func AllHealthy ¶ added in v1.3.0
func AllHealthy( ctx context.Context, providers []HealthChecker, ) error
AllHealthy checks if all providers are healthy
func HealthCheckAll ¶ added in v1.3.0
func HealthCheckAll( ctx context.Context, providers []HealthChecker, ) map[string]error
HealthCheckAll checks the health of all providers and returns a map of results
Types ¶
type Cache ¶ added in v1.3.0
type Cache struct {
// contains filtered or unexported fields
}
Cache provides an in-memory cache for exchange rates
func (*Cache) BatchGetRates ¶ added in v1.3.0
func (c *Cache) BatchGetRates( ctx context.Context, from string, to []string, ) (map[string]*RateInfo, error)
BatchGetRates gets multiple rates from the cache
func (*Cache) Clear ¶ added in v1.3.0
func (c *Cache) Clear()
Clear removes all entries from the cache
type ExchangeInfo ¶ added in v1.3.0
type ExchangeInfo struct {
OriginalAmount float64
OriginalCurrency string
ConvertedAmount float64
ConvertedCurrency string
ConversionRate float64
Timestamp time.Time
Source string
}
ExchangeInfo holds details about a currency conversion performed during a transaction.
type ExchangeRate ¶ added in v1.3.0
type ExchangeRate interface {
// GetRate fetches the current exchange rate for a currency pair.
GetRate(ctx context.Context, from, to string) (*ExchangeInfo, error)
// GetRates fetches multiple exchange rates in a single request.
GetRates(ctx context.Context, from string) (map[string]*ExchangeInfo, error)
// IsSupported checks if a currency pair is supported by the provider.
IsSupported(from, to string) bool
// Name returns the provider's name for logging and identification.
Name() string
// IsHealthy checks if the provider is currently available.
IsHealthy() bool
}
ExchangeRate defines the interface for external exchange rate providers.
type ExchangeRateProvider
deprecated
type ExchangeRateProvider ExchangeRate
Deprecated: Use ExchangeRateP directly.
type GetPaymentStatusParams ¶
type GetPaymentStatusParams struct {
PaymentID string
}
GetPaymentStatusParams holds the parameters for the GetPaymentStatus method.
type HealthChecker ¶ added in v1.3.0
type HealthChecker interface {
// CheckHealth checks if the provider is healthy
CheckHealth(ctx context.Context) error
}
HealthChecker defines the interface for checking provider health
func FirstHealthy ¶ added in v1.3.0
func FirstHealthy( ctx context.Context, providers []HealthChecker, ) (HealthChecker, error)
FirstHealthy returns the first healthy provider from the list
type InitiatePaymentParams ¶
type InitiatePaymentParams struct {
UserID uuid.UUID
AccountID uuid.UUID
TransactionID uuid.UUID
Amount int64
Currency string
}
InitiatePaymentParams holds the parameters for the InitiatePayment method.
type InitiatePaymentResponse ¶
type InitiatePaymentResponse struct {
Status PaymentStatus
// PaymentID is the ID of the payment in the payment provider
// (e.g., Stripe Checkout Session ID)
PaymentID string
}
type Payment ¶ added in v1.3.0
type Payment interface {
InitiatePayment(
ctx context.Context,
params *InitiatePaymentParams,
) (*InitiatePaymentResponse, error)
HandleWebhook(
ctx context.Context,
payload []byte,
signature string,
) (*PaymentEvent, error)
}
Payment is a interface for payment provider
type PaymentEvent ¶
type PaymentEvent struct {
ID string
Status PaymentStatus
Amount int64
Currency string
UserID uuid.UUID
AccountID uuid.UUID
Metadata map[string]string
}
PaymentEvent represents a payment event from Stripe.
type PaymentProvider
deprecated
type PaymentProvider Payment
Deprecated: Use Payment directly.
type PaymentStatus ¶
type PaymentStatus string
PaymentStatus represents the status of a payment in the mock provider.
const ( // PaymentPending indicates the payment is still pending. PaymentPending PaymentStatus = "pending" // PaymentCompleted indicates the payment has completed successfully. PaymentCompleted PaymentStatus = "completed" // PaymentFailed indicates the payment has failed. PaymentFailed PaymentStatus = "failed" )
type Provider ¶ added in v1.3.0
type Provider interface {
RateFetcher
HealthChecker
SupportedChecker
// Metadata returns the provider's metadata
Metadata() ProviderMetadata
}
Provider defines the complete interface for a rate provider
type ProviderMetadata ¶ added in v1.3.0
type ProviderMetadata struct {
Name string `json:"name"`
Version string `json:"version"`
LastUpdated time.Time `json:"last_updated"`
IsActive bool `json:"is_active"`
}
ProviderMetadata contains metadata about a provider
type RateFetcher ¶ added in v1.3.0
type RateFetcher interface {
// FetchRate gets the exchange rate for a currency pair
FetchRate(ctx context.Context, from, to string) (*RateInfo, error)
// FetchRates gets multiple exchange rates in a single request
FetchRates(ctx context.Context, from string, to []string) (map[string]*RateInfo, error)
}
RateFetcher defines the interface for fetching exchange rates
type RateHistory ¶ added in v1.3.0
type RateHistory struct {
// contains filtered or unexported fields
}
RateHistory tracks historical rate data
func NewRateHistory ¶ added in v1.3.0
func NewRateHistory(size int) *RateHistory
NewRateHistory creates a new RateHistory with the specified maximum size
func (*RateHistory) Add ¶ added in v1.3.0
func (h *RateHistory) Add(rate RateInfo)
Add adds a new rate to the history
func (*RateHistory) Average ¶ added in v1.3.0
func (h *RateHistory) Average(since time.Time) float64
Average calculates the average rate over the specified duration
func (*RateHistory) Get ¶ added in v1.3.0
func (h *RateHistory) Get() []RateInfo
Get returns the rate history
type RateInfo ¶ added in v1.3.0
type RateInfo struct {
FromCurrency string `json:"from_currency"`
ToCurrency string `json:"to_currency"`
Rate float64 `json:"rate"`
Timestamp time.Time `json:"timestamp"`
Provider string `json:"provider"`
IsDerived bool `json:"is_derived"`
BaseCurrency string `json:"base_currency,omitempty"`
OriginalRate float64 `json:"original_rate,omitempty"`
}
RateInfo contains information about an exchange rate This is the core data structure used by the provider package
type RateStats ¶ added in v1.3.0
RateStats contains statistics about exchange rates
func CalculateStats ¶ added in v1.3.0
CalculateStats calculates statistics for a set of rates
type SupportedChecker ¶ added in v1.3.0
type SupportedChecker interface {
// IsSupported checks if a currency pair is supported by the provider
IsSupported(from, to string) bool
// SupportedPairs returns all supported currency pairs
SupportedPairs() []string
}
SupportedChecker defines the interface for checking supported currency pairs
type UpdatePaymentStatusParams ¶
type UpdatePaymentStatusParams struct {
TransactionID uuid.UUID
PaymentID string
Status PaymentStatus
}
UpdatePaymentStatusParams holds the parameters for updating a payment status