products

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrProductNotFound = errors.New("product not found")

ErrProductNotFound is returned when a product doesn't exist.

Functions

This section is empty.

Types

type CachedRepository

type CachedRepository struct {
	// contains filtered or unexported fields
}

CachedRepository wraps a Repository with caching for ListProducts and lookups.

func NewCachedRepository

func NewCachedRepository(underlying Repository, cacheTTL time.Duration) *CachedRepository

NewCachedRepository wraps a repository with a caching layer. cacheTTL determines how long the product list cache is valid. Set to 0 to disable caching (pass-through mode).

func (*CachedRepository) Close

func (r *CachedRepository) Close() error

Close closes the underlying repository.

func (*CachedRepository) CreateProduct

func (r *CachedRepository) CreateProduct(ctx context.Context, product Product) error

CreateProduct creates a new product and invalidates the cache.

func (*CachedRepository) DeleteProduct

func (r *CachedRepository) DeleteProduct(ctx context.Context, id string) error

DeleteProduct soft-deletes a product and invalidates the cache.

func (*CachedRepository) GetProduct

func (r *CachedRepository) GetProduct(ctx context.Context, id string) (Product, error)

GetProduct retrieves a product by ID with caching.

func (*CachedRepository) GetProductByStripePriceID

func (r *CachedRepository) GetProductByStripePriceID(ctx context.Context, stripePriceID string) (Product, error)

GetProductByStripePriceID retrieves a product by its Stripe Price ID with caching.

func (*CachedRepository) InvalidateCache

func (r *CachedRepository) InvalidateCache()

InvalidateCache forces the next ListProducts call to fetch fresh data and clears all caches.

func (*CachedRepository) ListProducts

func (r *CachedRepository) ListProducts(ctx context.Context) ([]Product, error)

ListProducts returns all active products with TTL-based caching.

func (*CachedRepository) UpdateProduct

func (r *CachedRepository) UpdateProduct(ctx context.Context, product Product) error

UpdateProduct updates an existing product and invalidates the cache.

type MongoDBRepository

type MongoDBRepository struct {
	// contains filtered or unexported fields
}

MongoDBRepository implements Repository using MongoDB.

func NewMongoDBRepository

func NewMongoDBRepository(connectionString, database, collection string) (*MongoDBRepository, error)

NewMongoDBRepository creates a MongoDB-backed repository.

func (*MongoDBRepository) Close

func (r *MongoDBRepository) Close() error

Close closes the MongoDB connection.

func (*MongoDBRepository) CreateProduct

func (r *MongoDBRepository) CreateProduct(ctx context.Context, p Product) error

CreateProduct creates a new product.

func (*MongoDBRepository) DeleteProduct

func (r *MongoDBRepository) DeleteProduct(ctx context.Context, id string) error

DeleteProduct soft-deletes a product (sets active = false).

func (*MongoDBRepository) GetProduct

func (r *MongoDBRepository) GetProduct(ctx context.Context, id string) (Product, error)

GetProduct retrieves a product by ID.

func (*MongoDBRepository) GetProductByStripePriceID

func (r *MongoDBRepository) GetProductByStripePriceID(ctx context.Context, stripePriceID string) (Product, error)

GetProductByStripePriceID retrieves a product by its Stripe Price ID.

func (*MongoDBRepository) ListProducts

func (r *MongoDBRepository) ListProducts(ctx context.Context) ([]Product, error)

ListProducts returns all active products.

func (*MongoDBRepository) UpdateProduct

func (r *MongoDBRepository) UpdateProduct(ctx context.Context, p Product) error

UpdateProduct updates an existing product.

type PostgresRepository

type PostgresRepository struct {
	// contains filtered or unexported fields
}

PostgresRepository implements Repository using PostgreSQL.

func NewPostgresRepository

func NewPostgresRepository(connectionString string, poolConfig config.PostgresPoolConfig) (*PostgresRepository, error)

NewPostgresRepository creates a PostgreSQL-backed repository.

func NewPostgresRepositoryWithDB

func NewPostgresRepositoryWithDB(db *sql.DB) *PostgresRepository

NewPostgresRepositoryWithDB creates a PostgreSQL-backed repository using an existing connection pool. This allows sharing a single connection pool across multiple repositories.

func (*PostgresRepository) Close

func (r *PostgresRepository) Close() error

Close closes the database connection only if this repository owns it.

func (*PostgresRepository) CreateProduct

func (r *PostgresRepository) CreateProduct(ctx context.Context, p Product) error

CreateProduct creates a new product.

func (*PostgresRepository) DeleteProduct

func (r *PostgresRepository) DeleteProduct(ctx context.Context, id string) error

DeleteProduct soft-deletes a product (sets active = false).

func (*PostgresRepository) GetProduct

func (r *PostgresRepository) GetProduct(ctx context.Context, id string) (Product, error)

GetProduct retrieves a product by ID.

func (*PostgresRepository) GetProductByStripePriceID

func (r *PostgresRepository) GetProductByStripePriceID(ctx context.Context, stripePriceID string) (Product, error)

GetProductByStripePriceID retrieves a product by its Stripe Price ID.

func (*PostgresRepository) ListProducts

func (r *PostgresRepository) ListProducts(ctx context.Context) ([]Product, error)

ListProducts returns all active products.

func (*PostgresRepository) UpdateProduct

func (r *PostgresRepository) UpdateProduct(ctx context.Context, p Product) error

UpdateProduct updates an existing product.

func (*PostgresRepository) WithMetrics

WithMetrics adds metrics collection to the repository.

func (*PostgresRepository) WithTableName

func (r *PostgresRepository) WithTableName(tableName string) *PostgresRepository

WithTableName sets a custom table name (for schema_mapping support). Validates the table name to prevent SQL injection.

type Product

type Product struct {
	ID            string            // Resource ID (e.g., "demo-content")
	Description   string            // Human-readable description
	FiatPrice     *money.Money      // Stripe price (optional, nil if not available)
	StripePriceID string            // Stripe Price ID (optional)
	CryptoPrice   *money.Money      // Crypto price (optional, nil if not available)
	CryptoAccount string            // Override token account (optional)
	MemoTemplate  string            // Transaction memo template
	Metadata      map[string]string // Custom key-value pairs
	Active        bool              // Enable/disable product

	// Subscription configuration (nil = one-time purchase only)
	Subscription *SubscriptionConfig

	CreatedAt time.Time // Creation timestamp
	UpdatedAt time.Time // Last update timestamp
}

Product represents a product/resource with pricing information.

func (Product) IsSubscription

func (p Product) IsSubscription() bool

IsSubscription returns true if this product requires a subscription.

func (Product) ToPaywallResource

func (p Product) ToPaywallResource() config.PaywallResource

ToPaywallResource converts a Product to a PaywallResource.

type Repository

type Repository interface {
	// GetProduct retrieves a product by ID.
	GetProduct(ctx context.Context, id string) (Product, error)

	// GetProductByStripePriceID retrieves a product by its Stripe Price ID.
	// Returns ErrProductNotFound if no product matches the given price ID.
	GetProductByStripePriceID(ctx context.Context, stripePriceID string) (Product, error)

	// ListProducts returns all active products.
	ListProducts(ctx context.Context) ([]Product, error)

	// CreateProduct creates a new product.
	CreateProduct(ctx context.Context, product Product) error

	// UpdateProduct updates an existing product.
	UpdateProduct(ctx context.Context, product Product) error

	// DeleteProduct soft-deletes a product (sets active = false).
	DeleteProduct(ctx context.Context, id string) error

	// Close closes any open connections.
	Close() error
}

Repository defines the interface for product storage.

func NewRepository

func NewRepository(cfg config.PaywallConfig) (Repository, error)

NewRepository creates a product repository based on config with optional caching.

func NewRepositoryWithDB

func NewRepositoryWithDB(cfg config.PaywallConfig, sharedDB *sql.DB) (Repository, error)

NewRepositoryWithDB creates a product repository with an optional shared database pool. If sharedDB is provided (non-nil) for postgres sources, it will be used instead of creating a new connection. Pass nil to create a new connection pool.

type SubscriptionConfig

type SubscriptionConfig struct {
	BillingPeriod    string `json:"billingPeriod" yaml:"billing_period"`            // "day", "week", "month", "year"
	BillingInterval  int    `json:"billingInterval" yaml:"billing_interval"`        // e.g., 1 for monthly, 3 for quarterly
	TrialDays        int    `json:"trialDays,omitempty" yaml:"trial_days"`          // Free trial period in days
	StripePriceID    string `json:"stripePriceId,omitempty" yaml:"stripe_price_id"` // Stripe recurring price ID
	AllowX402        bool   `json:"allowX402" yaml:"allow_x402"`                    // Allow x402 payments for subscription
	GracePeriodHours int    `json:"gracePeriodHours" yaml:"grace_period_hours"`     // Hours after expiry before blocking
}

SubscriptionConfig defines subscription billing for a product.

type YAMLRepository

type YAMLRepository struct {
	// contains filtered or unexported fields
}

YAMLRepository implements Repository using in-memory YAML config.

func NewYAMLRepository

func NewYAMLRepository(resources map[string]config.PaywallResource) *YAMLRepository

NewYAMLRepository creates a repository from YAML config.

func (*YAMLRepository) Close

func (r *YAMLRepository) Close() error

Close is a no-op for YAML repository.

func (*YAMLRepository) CreateProduct

func (r *YAMLRepository) CreateProduct(_ context.Context, _ Product) error

CreateProduct is not supported for YAML repository (read-only).

func (*YAMLRepository) DeleteProduct

func (r *YAMLRepository) DeleteProduct(_ context.Context, _ string) error

DeleteProduct is not supported for YAML repository (read-only).

func (*YAMLRepository) GetProduct

func (r *YAMLRepository) GetProduct(_ context.Context, id string) (Product, error)

GetProduct retrieves a product by ID.

func (*YAMLRepository) GetProductByStripePriceID

func (r *YAMLRepository) GetProductByStripePriceID(ctx context.Context, stripePriceID string) (Product, error)

GetProductByStripePriceID retrieves a product by its Stripe Price ID.

func (*YAMLRepository) ListProducts

func (r *YAMLRepository) ListProducts(ctx context.Context) ([]Product, error)

ListProducts returns all active products.

func (*YAMLRepository) UpdateProduct

func (r *YAMLRepository) UpdateProduct(_ context.Context, _ Product) error

UpdateProduct is not supported for YAML repository (read-only).

Jump to

Keyboard shortcuts

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