aboutsummaryrefslogtreecommitdiffstats
path: root/cost.go
blob: c0d340abea22130378090785a61ec1a55c642f8c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package lulu

import (
	"encoding/json"

	"github.com/shopspring/decimal"
)

type printJobCostReq struct {
	LineItems []PrintJobCostLineItem  `json:"line_items"`
	ShipAddr  printJobCostReqShipAddr `json:"shipping_address"`
	ShipOpt   ShippingLevel           `json:"shipping_option"`
}

type PrintJobCostLineItem struct {
	NPages   uint  `json:"page_count"`
	Mfg      PkgId `json:"pod_package_id"`
	Quantity uint  `json:"quantity"`
}

type printJobCostReqShipAddr struct {
	City     string      `json:"city"`
	Country  string      `json:"country_code"`
	PostCode string      `json:"postcode"`
	State    string      `json:"state_code"`
	Street1  string      `json:"street1"`
	Phone    PhoneNumber `json:"phone_number"`
}

// PrintJobCost is the response from /print-job-cost-calculations/.
type PrintJobCost struct {
	Addr, SuggestedAddr                                         ShippingAddress
	AddrWarnings                                                []ShippingAddressWarning
	Fees                                                        []Fee
	LineItemCosts                                               []LineItemCost
	ShipCost                                                    FulfillmentCost
	FulfillmentCost                                             FulfillmentCost
	TotalTax, TotalCostExclTax, TotalCostInclTax, TotalDiscount decimal.Decimal
	Currency                                                    string
}

type ShippingAddressWarning struct {
	Type string `json:"type"`    // eg "validation_warning"
	Path string `json:"path"`    // eg "external"
	Code string `json:"code"`    // eg "REPLACED"
	Msg  string `json:"message"` // eg "street1: Holstenstr. 40 -> Holstenstraße 40"
}

type Fee struct {
	Currency         string          `json:"currency"`
	Type             string          `json:"fee_type"`
	Sku              string          `json:"sku"`
	TaxRate          decimal.Decimal `json:"tax_rate"`
	TotalCostExclTax decimal.Decimal `json:"total_cost_excl_tax"`
	TotalCostInclTax decimal.Decimal `json:"total_cost_incl_tax"`
	TotalTax         decimal.Decimal `json:"total_tax"`
}

type FulfillmentCost struct {
	TotalCostExclTax decimal.Decimal `json:"total_cost_excl_tax"`
	TotalCostInclTax decimal.Decimal `json:"total_cost_incl_tax"`
	TotalTax         decimal.Decimal `json:"total_tax"`
	TaxRate          decimal.Decimal `json:"tax_rate"`
}

type LineItemCost struct {
	CostExclDiscounts      decimal.Decimal `json:"cost_excl_discounts"`
	TotalTax               decimal.Decimal `json:"total_tax"`
	TaxRate                decimal.Decimal `json:"tax_rate"`
	Quantity               uint            `json:"quantity"`
	TotalCostExclTax       decimal.Decimal `json:"total_cost_excl_tax"`
	TotalCostExclDiscounts decimal.Decimal `json:"total_cost_excl_discounts"`
	TotalCostInclTax       decimal.Decimal `json:"total_cost_incl_tax"`
	Discounts              []Discount      `json:"discounts"`
	UnitTierCost           decimal.Decimal `json:"unit_tier_cost"`
}

type Discount struct {
	Amount      decimal.Decimal `json:"amount"`
	Description string          `json:"description"`
}

type printJobCostResp struct {
	Addr             json.RawMessage `json:"shipping_address"`
	Fees             []Fee           `json:"fees"`
	LineItemCosts    []LineItemCost  `json:"line_item_costs"`
	ShipCost         FulfillmentCost `json:"shipping_cost"`
	FulfillmentCost  FulfillmentCost `json:"fulfillment_cost"`
	TotalTax         decimal.Decimal `json:"total_tax"`
	TotalCostExclTax decimal.Decimal `json:"total_cost_excl_tax"`
	TotalCostInclTax decimal.Decimal `json:"total_cost_incl_tax"`
	TotalDiscount    decimal.Decimal `json:"total_discount_amount"`
	Currency         string          `json:"currency"`
}

func (c *PrintJobCost) UnmarshalJSON(data []byte) error {
	var resp printJobCostResp
	if err := json.Unmarshal(data, &resp); err != nil {
		return err
	}
	if err := json.Unmarshal(resp.Addr, &c.Addr); err != nil {
		return err
	}
	var warnsAndSugg struct {
		Warnings  []ShippingAddressWarning `json:"warnings"`
		Suggested ShippingAddress          `json:"suggested_address"`
	}
	if err := json.Unmarshal(resp.Addr, &warnsAndSugg); err != nil {
		return err
	}
	c.SuggestedAddr = warnsAndSugg.Suggested
	c.AddrWarnings = warnsAndSugg.Warnings
	c.Fees = resp.Fees
	c.LineItemCosts = resp.LineItemCosts
	c.ShipCost = resp.ShipCost
	c.FulfillmentCost = resp.FulfillmentCost
	c.TotalTax = resp.TotalTax
	c.TotalCostExclTax = resp.TotalCostExclTax
	c.TotalCostInclTax = resp.TotalCostInclTax
	c.TotalDiscount = resp.TotalDiscount
	c.Currency = resp.Currency
	return nil
}