59 lines
2.2 KiB
Go
59 lines
2.2 KiB
Go
package cart
|
|
|
|
// TaxProvider computes taxes for orders and line items.
|
|
// It mirrors the PaymentProvider and ShippingProvider seam patterns: one
|
|
// interface, interchangeable implementations.
|
|
//
|
|
// All tax rates are expressed as raw percent (e.g. 25 = 25 %). This matches the
|
|
// OrderLine.tax_rate proto convention.
|
|
type TaxProvider interface {
|
|
// Name returns the provider name for identification (logging, metrics).
|
|
Name() string
|
|
|
|
// ComputeTax computes the tax portion from a gross (inc-VAT) total and tax
|
|
// rate. Both total and return value are in minor currency units (ore).
|
|
// taxRate is expressed as raw percent (e.g. 25 = 25 %).
|
|
//
|
|
// Formula: tax = total x rate / (100 + rate)
|
|
//
|
|
// When taxRate <= 0 the result is 0 (untaxed / zero-rated items).
|
|
ComputeTax(total int64, taxRate int) int64
|
|
|
|
// DefaultTaxRate returns the default VAT rate for a country, expressed as
|
|
// raw percent (e.g. 25 = 25 %). Used for deliveries and items where no
|
|
// explicit rate was recorded at add-to-cart time.
|
|
//
|
|
// Return 0 if the country is unknown or untaxed.
|
|
DefaultTaxRate(country string) int
|
|
}
|
|
|
|
// ComputeTax is the shared tax formula: tax = total x rate / (100 + rate).
|
|
// taxRate is raw percent (e.g. 25 = 25 %). When taxRate <= 0 the result is 0.
|
|
//
|
|
// This differs from GetTaxAmount which uses the CartItem convention
|
|
// (percent x 100, e.g. 2500 = 25.00 %) with formula total x rate / (10000 + rate).
|
|
func ComputeTax(total int64, taxRate int) int64 {
|
|
if taxRate <= 0 {
|
|
return 0
|
|
}
|
|
return total * int64(taxRate) / int64(100+taxRate)
|
|
}
|
|
|
|
// StaticTaxProvider is a stateless, always-available TaxProvider that simply
|
|
// runs the formula without any country-specific rates. Use for local dev,
|
|
// tests, and as a no-dependency default.
|
|
type StaticTaxProvider struct{}
|
|
|
|
var _ TaxProvider = (*StaticTaxProvider)(nil)
|
|
|
|
func NewStaticTaxProvider() *StaticTaxProvider { return &StaticTaxProvider{} }
|
|
|
|
func (p *StaticTaxProvider) Name() string { return "static" }
|
|
|
|
func (p *StaticTaxProvider) ComputeTax(total int64, taxRate int) int64 {
|
|
return ComputeTax(total, taxRate)
|
|
}
|
|
|
|
// DefaultTaxRate returns 0 for any country (no awareness).
|
|
func (p *StaticTaxProvider) DefaultTaxRate(_ string) int { return 0 }
|