calculator

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2019 License: MIT Imports: 6 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 DiscountItem added in v1.6.0

type DiscountItem struct {
	Type       DiscountType `json:"type"`
	Percentage uint64       `json:"percentage"`
	Fixed      uint64       `json:"fixed"`
}

DiscountItem provides details about a discount that was applied

type DiscountType added in v1.6.0

type DiscountType int

DiscountType indicates what type of discount was given

const (
	DiscountTypeCoupon DiscountType = iota + 1
	DiscountTypeMember
)

possible types for a discount item

func (DiscountType) MarshalJSON added in v1.6.0

func (t DiscountType) MarshalJSON() ([]byte, error)

MarshalJSON marshals the enum as a quoted json string

func (DiscountType) String added in v1.6.0

func (t DiscountType) String() string

func (*DiscountType) UnmarshalJSON added in v1.6.0

func (t *DiscountType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmashals a quoted json string to the enum value

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
	NetTotal uint64
	Taxes    uint64
	Total    int64

	DiscountItems []DiscountItem
}

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
	NetTotal 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