provider

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrExchangeRateUnavailable indicates the exchange rate service is unreachable or down.
	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

View Source
var (
	ErrProviderUnavailable = errors.New("provider unavailable")
	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 NewCache added in v1.3.0

func NewCache(ttl time.Duration) *Cache

NewCache creates a new cache with the given TTL

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

func (*Cache) GetRate added in v1.3.0

func (c *Cache) GetRate(ctx context.Context, from, to string) (*RateInfo, error)

GetRate gets a rate from the cache

func (*Cache) StoreRate added in v1.3.0

func (c *Cache) StoreRate(ctx context.Context, rate *RateInfo) error

StoreRate stores a rate in 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

type RateStats struct {
	Min     float64
	Max     float64
	Average float64
	Count   int
}

RateStats contains statistics about exchange rates

func CalculateStats added in v1.3.0

func CalculateStats(rates []float64) RateStats

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL