payment

package
v1.29.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package payment provides subscription and payment processing with support for multiple payment providers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Address

type Address struct {
	Line1      string `json:"line1"`
	Line2      string `json:"line2,omitempty"`
	City       string `json:"city"`
	State      string `json:"state"`
	PostalCode string `json:"postal_code"`
	Country    string `json:"country"`
}

Address represents a billing address

type BillingInterval

type BillingInterval string

BillingInterval represents billing frequency

const (
	IntervalMonthly BillingInterval = "monthly"
	IntervalYearly  BillingInterval = "yearly"
	IntervalWeekly  BillingInterval = "weekly"
	IntervalOneTime BillingInterval = "one_time"
)

type CardDetails

type CardDetails struct {
	Brand       string `json:"brand"`
	Last4       string `json:"last4"`
	ExpMonth    int    `json:"exp_month"`
	ExpYear     int    `json:"exp_year"`
	Fingerprint string `json:"fingerprint,omitempty"`
}

CardDetails represents credit card details

type Charge

type Charge struct {
	ID             string            `json:"id"`
	ProviderID     string            `json:"provider_id"`
	CustomerID     string            `json:"customer_id"`
	Amount         int64             `json:"amount"` // In cents
	Currency       string            `json:"currency"`
	Description    string            `json:"description"`
	Status         ChargeStatus      `json:"status"`
	PaymentMethod  string            `json:"payment_method"`
	FailureMessage string            `json:"failure_message,omitempty"`
	Metadata       map[string]string `json:"metadata,omitempty"`
	CreatedAt      time.Time         `json:"created_at"`
}

Charge represents a payment charge

type ChargeStatus

type ChargeStatus string

ChargeStatus represents charge status

const (
	ChargeSucceeded ChargeStatus = "succeeded"
	ChargePending   ChargeStatus = "pending"
	ChargeFailed    ChargeStatus = "failed"
)

type Customer

type Customer struct {
	ID            string            `json:"id"`
	ProviderID    string            `json:"provider_id"`
	Email         string            `json:"email"`
	Name          string            `json:"name"`
	Company       string            `json:"company,omitempty"`
	Phone         string            `json:"phone,omitempty"`
	Address       *Address          `json:"address,omitempty"`
	Metadata      map[string]string `json:"metadata,omitempty"`
	PaymentMethod *PaymentMethod    `json:"payment_method,omitempty"`
	Balance       int64             `json:"balance"` // In cents
	Currency      string            `json:"currency"`
	CreatedAt     time.Time         `json:"created_at"`
	UpdatedAt     time.Time         `json:"updated_at"`
}

Customer represents a customer

type Invoice

type Invoice struct {
	ID             string        `json:"id"`
	ProviderID     string        `json:"provider_id"`
	CustomerID     string        `json:"customer_id"`
	SubscriptionID string        `json:"subscription_id,omitempty"`
	Number         string        `json:"number"`
	Status         InvoiceStatus `json:"status"`
	Amount         int64         `json:"amount"` // In cents
	Currency       string        `json:"currency"`
	DueDate        time.Time     `json:"due_date"`
	PaidAt         *time.Time    `json:"paid_at,omitempty"`
	Lines          []InvoiceLine `json:"lines"`
	PDFUrl         string        `json:"pdf_url,omitempty"`
	CreatedAt      time.Time     `json:"created_at"`
}

Invoice represents an invoice

type InvoiceLine

type InvoiceLine struct {
	Description string `json:"description"`
	Quantity    int    `json:"quantity"`
	UnitPrice   int64  `json:"unit_price"` // In cents
	Amount      int64  `json:"amount"`     // In cents
}

InvoiceLine represents an invoice line item

type InvoiceStatus

type InvoiceStatus string

InvoiceStatus represents invoice status

const (
	InvoiceDraft         InvoiceStatus = "draft"
	InvoiceOpen          InvoiceStatus = "open"
	InvoicePaid          InvoiceStatus = "paid"
	InvoiceVoid          InvoiceStatus = "void"
	InvoiceUncollectible InvoiceStatus = "uncollectible"
)

type Manager

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

Manager handles payment operations

func NewManager

func NewManager(provider Provider) *Manager

NewManager creates a new payment manager

func (*Manager) AddPlan

func (m *Manager) AddPlan(plan *Plan)

AddPlan adds a plan to the manager

func (*Manager) CancelSubscription

func (m *Manager) CancelSubscription(ctx context.Context, subscriptionID string, immediately bool) error

CancelSubscription cancels a subscription

func (*Manager) ChangePlan

func (m *Manager) ChangePlan(ctx context.Context, subscriptionID, newPlanID string) error

ChangePlan changes subscription plan

func (*Manager) ChargeOneTime

func (m *Manager) ChargeOneTime(ctx context.Context, customerID string, amount int64, description string) (*Charge, error)

ChargeOneTime processes a one-time payment

func (*Manager) CreateCustomer

func (m *Manager) CreateCustomer(ctx context.Context, email, name string) (*Customer, error)

CreateCustomer creates a new customer

func (*Manager) GetPlan

func (m *Manager) GetPlan(planID string) (*Plan, bool)

GetPlan retrieves a plan

func (*Manager) GetUsage

func (m *Manager) GetUsage(ctx context.Context, customerID string, start, end time.Time) ([]*UsageRecord, error)

GetUsage retrieves usage for a period

func (*Manager) HandleWebhook

func (m *Manager) HandleWebhook(ctx context.Context, payload []byte, signature string) error

HandleWebhook processes payment provider webhooks

func (*Manager) ListPlans

func (m *Manager) ListPlans() []*Plan

ListPlans returns all available plans

func (*Manager) Subscribe

func (m *Manager) Subscribe(ctx context.Context, customerID, planID string) (*Subscription, error)

Subscribe creates a subscription for a customer

func (*Manager) TrackUsage

func (m *Manager) TrackUsage(ctx context.Context, record *UsageRecord) error

TrackUsage records usage for metered billing

type PaymentMethod

type PaymentMethod struct {
	ID         string            `json:"id"`
	ProviderID string            `json:"provider_id"`
	CustomerID string            `json:"customer_id"`
	Type       PaymentMethodType `json:"type"`
	Card       *CardDetails      `json:"card,omitempty"`
	IsDefault  bool              `json:"is_default"`
	CreatedAt  time.Time         `json:"created_at"`
}

PaymentMethod represents a payment method

type PaymentMethodType

type PaymentMethodType string

PaymentMethodType represents payment method type

const (
	PaymentCard   PaymentMethodType = "card"
	PaymentBank   PaymentMethodType = "bank"
	PaymentPayPal PaymentMethodType = "paypal"
)

type Plan

type Plan struct {
	ID          string            `json:"id"`
	ProviderID  string            `json:"provider_id"`
	Name        string            `json:"name"`
	Description string            `json:"description"`
	Amount      int64             `json:"amount"` // In cents
	Currency    string            `json:"currency"`
	Interval    BillingInterval   `json:"interval"`
	Features    []string          `json:"features,omitempty"`
	Metadata    map[string]string `json:"metadata,omitempty"`
	Active      bool              `json:"active"`
	TrialDays   int               `json:"trial_days"`
}

Plan represents a subscription plan

type Provider

type Provider interface {
	// CreateCustomer creates a new customer
	CreateCustomer(ctx context.Context, customer *Customer) error

	// GetCustomer retrieves customer details
	GetCustomer(ctx context.Context, customerID string) (*Customer, error)

	// UpdateCustomer updates customer information
	UpdateCustomer(ctx context.Context, customer *Customer) error

	// CreateSubscription creates a new subscription
	CreateSubscription(ctx context.Context, sub *Subscription) error

	// GetSubscription retrieves subscription details
	GetSubscription(ctx context.Context, subscriptionID string) (*Subscription, error)

	// CancelSubscription cancels a subscription
	CancelSubscription(ctx context.Context, subscriptionID string, immediately bool) error

	// UpdateSubscription updates subscription (e.g., change plan)
	UpdateSubscription(ctx context.Context, sub *Subscription) error

	// CreatePaymentMethod adds a payment method
	CreatePaymentMethod(ctx context.Context, method *PaymentMethod) error

	// ChargePayment processes a one-time payment
	ChargePayment(ctx context.Context, charge *Charge) error

	// RefundPayment issues a refund
	RefundPayment(ctx context.Context, refund *Refund) error

	// ListInvoices lists customer invoices
	ListInvoices(ctx context.Context, customerID string, limit int) ([]*Invoice, error)

	// HandleWebhook processes provider webhooks
	HandleWebhook(ctx context.Context, payload []byte, signature string) (*WebhookEvent, error)
}

Provider defines the payment provider interface

type Refund

type Refund struct {
	ID         string            `json:"id"`
	ProviderID string            `json:"provider_id"`
	ChargeID   string            `json:"charge_id"`
	Amount     int64             `json:"amount"` // In cents
	Currency   string            `json:"currency"`
	Reason     string            `json:"reason"`
	Status     RefundStatus      `json:"status"`
	Metadata   map[string]string `json:"metadata,omitempty"`
	CreatedAt  time.Time         `json:"created_at"`
}

Refund represents a refund

type RefundStatus

type RefundStatus string

RefundStatus represents refund status

const (
	RefundSucceeded RefundStatus = "succeeded"
	RefundPending   RefundStatus = "pending"
	RefundFailed    RefundStatus = "failed"
)

type Subscription

type Subscription struct {
	ID                 string             `json:"id"`
	ProviderID         string             `json:"provider_id"`
	CustomerID         string             `json:"customer_id"`
	PlanID             string             `json:"plan_id"`
	Status             SubscriptionStatus `json:"status"`
	Quantity           int                `json:"quantity"`
	CurrentPeriodStart time.Time          `json:"current_period_start"`
	CurrentPeriodEnd   time.Time          `json:"current_period_end"`
	CancelAt           *time.Time         `json:"cancel_at,omitempty"`
	CanceledAt         *time.Time         `json:"canceled_at,omitempty"`
	TrialStart         *time.Time         `json:"trial_start,omitempty"`
	TrialEnd           *time.Time         `json:"trial_end,omitempty"`
	Metadata           map[string]string  `json:"metadata,omitempty"`
	Items              []SubscriptionItem `json:"items,omitempty"`
	CreatedAt          time.Time          `json:"created_at"`
	UpdatedAt          time.Time          `json:"updated_at"`
}

Subscription represents a subscription

type SubscriptionItem

type SubscriptionItem struct {
	ID       string `json:"id"`
	PriceID  string `json:"price_id"`
	Quantity int    `json:"quantity"`
}

SubscriptionItem represents a subscription line item

type SubscriptionStatus

type SubscriptionStatus string

SubscriptionStatus represents subscription status

const (
	StatusActive   SubscriptionStatus = "active"
	StatusTrialing SubscriptionStatus = "trialing"
	StatusPastDue  SubscriptionStatus = "past_due"
	StatusCanceled SubscriptionStatus = "canceled"
	StatusUnpaid   SubscriptionStatus = "unpaid"
	StatusPaused   SubscriptionStatus = "paused"
)

type UsageRecord

type UsageRecord struct {
	CustomerID     string    `json:"customer_id"`
	SubscriptionID string    `json:"subscription_id"`
	Metric         string    `json:"metric"`
	Quantity       int64     `json:"quantity"`
	Timestamp      time.Time `json:"timestamp"`
}

UsageRecord represents usage data

type WebhookEvent

type WebhookEvent struct {
	ID        string                 `json:"id"`
	Type      string                 `json:"type"`
	Data      map[string]interface{} `json:"data"`
	CreatedAt time.Time              `json:"created_at"`
}

WebhookEvent represents a webhook event

Jump to

Keyboard shortcuts

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