billing

package
v0.0.0-...-5b9858d Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: AGPL-3.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCancelled = fmt.Errorf("billing subscription cancelled")
)

Functions

func ApplyTax

func ApplyTax(conf ini.File, inv *model.Invoice, addr *model.BillingAddress)

func ChargeRenewalPayment

func ChargeRenewalPayment(
	ctx context.Context,
	sub *model.BillingSubscription,
	addr *model.BillingAddress,
	method *model.PaymentMethod,
) (model.PaymentIntent, error)

Creates and immediately confirms an off-session (i.e. non-interactive) Stripe payment intent to charge the user's subscription renewal fee.

func CreatePaymentIntent

func CreatePaymentIntent(
	ctx context.Context,
	sub *model.BillingSubscription,
	addr *model.BillingAddress,
	idempotencyKey *string,
) (model.PaymentIntent, error)

Creates an on-session Stripe payment intent (i.e. one the user will pay for interactively) for the given billing subscription.

func CreateSetupIntent

func CreateSetupIntent(
	ctx context.Context,
	sub *model.BillingSubscription,
) (model.SetupIntent, error)

func DeletePaymentMethod

func DeletePaymentMethod(
	ctx context.Context,
	method *model.PaymentMethod,
) error

Removes a payment method from a customer's account at the payment processor.

func DownloadInvoice

func DownloadInvoice(
	ctx context.Context,
	invoice *model.Invoice,
) (io.ReadCloser, error)

func FinalizeCancellation

func FinalizeCancellation(
	ctx context.Context,
	tx *sql.Tx,
	user *model.User,
	sub *model.BillingSubscription,
) error

Finalizes a cancelled subscription whose service term is completed by setting the user to a non-paying account.

The subscription may be nil to convert a subsidized user to non-paying.

func ForContext

func ForContext(ctx context.Context, currency model.Currency) *client.API

Returns the Stripe client associated with a currency on this context.

func GenerateInvoice

func GenerateInvoice(
	ctx context.Context,
	w io.Writer,
	invoice *model.Invoice,
) error

Generates a PDF from a model.Invoice.

func Get

func Get(conf ini.File, currency model.Currency, key string) (string, bool)

Retrieves an item from the billing configuration. Uses a currency-specific config option if relevant, or falls back to the general billing configuration if not.

func GetPaymentIntent

func GetPaymentIntent(
	ctx context.Context,
	sub *model.BillingSubscription,
) (model.PaymentIntent, error)

Retrieves a payment intent from its ID.

func GetSetupIntent

func GetSetupIntent(
	ctx context.Context,
	sub *model.BillingSubscription,
	intentID string,
) (model.SetupIntent, error)

func Init

func Init(conf ini.File)

Initiailze the billing system from a config file

func InvoiceFor

func InvoiceFor(
	ctx context.Context,
	sub *model.BillingSubscription,
	addr *model.BillingAddress,
) (*model.Invoice, error)

Calculates the payment details for a given billing subscription and returns an incomplete invoice record with all of the monetary values set accordingly (e.g. tax rate), to facilitate calculating the amount to charge the user.

func IsEnabled

func IsEnabled(conf ini.File) bool

Returns true if billing is enabled in this configuration.

func IsProhibitedCountry

func IsProhibitedCountry(ctx context.Context, c country.Country) bool

Returns true if the given country code is not allowed to receive paid services.

func Middleware

func Middleware(conf ini.File) func(next http.Handler) http.Handler

func RequireEnabled

func RequireEnabled(ctx context.Context) error

Returns an error if billing is not enabled in this configuration.

func UpdateCustomerEmail

func UpdateCustomerEmail(
	ctx context.Context,
	user *model.User,
) error

Updates the email address on record at payment processor for each of this user's customer accounts.

Returns `nil` if success. Returns a error message if contains failure.

func UploadInvoice

func UploadInvoice(
	ctx context.Context,
	invoice *model.Invoice,
) error

Uploads an invoice to S3.

func ValidateSubscription

func ValidateSubscription(
	ctx context.Context,
	tx *sql.Tx,
	user *model.User,
	sub *model.BillingSubscription,
	intent model.PaymentIntent,
) error

Validate a subscription after a payment intent has been processed to update the subscription based on the payment outcome, issuing an invoice/receipt and setting the user payment status appropriately, etc.

Types

type TemplateContext

type TemplateContext struct {
	Invoice   *model.Invoice
	User      *model.User
	Address   *model.BillingAddress
	Products  []TemplateProduct
	Discounts []TemplateDiscount
	Sample    bool
}

type TemplateDiscount

type TemplateDiscount struct {
	Reason string
	Amount int
}

type TemplateProduct

type TemplateProduct struct {
	Product   *model.Product
	UnitPrice *model.ProductPrice
	Units     int
	Total     int
}

Jump to

Keyboard shortcuts

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