calculator

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2018 License: MIT Imports: 4 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Round added in v1.3.0

func Round(x float64) float64

Round returns the nearest integer, rounding half away from zero.

Special cases are:

Round(±0) = ±0
Round(±Inf) = ±Inf
Round(NaN) = NaN

Types

type Coupon

type Coupon interface {
	ValidForType(string) bool
	ValidForPrice(string, uint64) bool
	ValidForProduct(string) bool
	PercentageDiscount() uint64
	FixedDiscount(string) uint64
}

Coupon is the interface for a coupon needed to do price calculation.

type FixedMemberDiscount

type FixedMemberDiscount struct {
	Amount   string `json:"amount"`
	Currency string `json:"currency"`
}

FixedMemberDiscount represents a fixed discount given to members.

type Item

type Item interface {
	ProductSku() string
	PriceInLowestUnit() uint64
	ProductType() string
	FixedVAT() uint64
	TaxableItems() []Item
	GetQuantity() uint64
}

Item is the interface for a single line item needed to do price calculation.

type ItemPrice

type ItemPrice struct {
	Quantity uint64

	Subtotal uint64
	Discount uint64
	Taxes    uint64
	Total    int64
}

ItemPrice is the price of a single line item.

type MemberDiscount

type MemberDiscount struct {
	Claims       map[string]string      `json:"claims"`
	Percentage   uint64                 `json:"percentage"`
	FixedAmount  []*FixedMemberDiscount `json:"fixed"`
	ProductTypes []string               `json:"product_types"`
	Products     []string               `json:"products"`
}

MemberDiscount represents a discount given to members, either fixed or a percentage.

func (*MemberDiscount) FixedDiscount

func (d *MemberDiscount) FixedDiscount(currency string) uint64

FixedDiscount returns what the fixed discount amount is for a particular currency.

func (*MemberDiscount) ValidForProduct added in v1.2.0

func (d *MemberDiscount) ValidForProduct(productSku string) bool

ValidForProduct returns whether a member discount is valid for a product sku

func (*MemberDiscount) ValidForType

func (d *MemberDiscount) ValidForType(productType string) bool

ValidForType returns whether a member discount is valid for a product type.

type PaymentMethods added in v1.3.0

type PaymentMethods struct {
	Stripe struct {
		Enabled   bool   `json:"enabled"`
		PublicKey string `json:"public_key,omitempty"`
	} `json:"stripe"`
	PayPal struct {
		Enabled     bool   `json:"enabled"`
		ClientID    string `json:"client_id,omitempty"`
		Environment string `json:"environment,omitempty"`
	} `json:"paypal"`
}

PaymentMethods settings

type Price

type Price struct {
	Items []ItemPrice

	Subtotal uint64
	Discount uint64
	Taxes    uint64
	Total    int64
}

Price represents the total price of all line items.

func CalculatePrice

func CalculatePrice(settings *Settings, jwtClaims map[string]interface{}, params PriceParameters, log logrus.FieldLogger) Price

CalculatePrice will calculate the final total price. It takes into account currency, country, coupons, and discounts.

type PriceParameters added in v1.2.2

type PriceParameters struct {
	Country  string
	Currency string
	Coupon   Coupon
	Items    []Item
}

PriceParameters represents the order information to calculate prices.

type Settings

type Settings struct {
	PricesIncludeTaxes bool              `json:"prices_include_taxes"`
	Taxes              []*Tax            `json:"taxes,omitempty"`
	MemberDiscounts    []*MemberDiscount `json:"member_discounts,omitempty"`
	PaymentMethods     *PaymentMethods   `json:"payment_methods,omitempty"`
}

Settings represent the site-wide settings for price calculation.

type Tax

type Tax struct {
	Percentage   uint64   `json:"percentage"`
	ProductTypes []string `json:"product_types"`
	Countries    []string `json:"countries"`
}

Tax represents a tax, potentially specific to countries and product types.

func (*Tax) AppliesTo

func (t *Tax) AppliesTo(country, productType string) bool

AppliesTo determines if the tax applies to the country AND product type provided.

Jump to

Keyboard shortcuts

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