-
Notifications
You must be signed in to change notification settings - Fork 5
/
CreateOrder.go
81 lines (74 loc) · 3.61 KB
/
CreateOrder.go
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
package taxjar
import "encoding/json"
// CreateOrderParams should be passed to `CreateOrder` to create an order․
type CreateOrderParams struct {
TransactionID string `json:"transaction_id,omitempty"`
TransactionDate string `json:"transaction_date,omitempty"`
Provider string `json:"provider,omitempty"`
FromCountry string `json:"from_country,omitempty"`
FromZip string `json:"from_zip,omitempty"`
FromState string `json:"from_state,omitempty"`
FromCity string `json:"from_city,omitempty"`
FromStreet string `json:"from_street,omitempty"`
ToCountry string `json:"to_country,omitempty"`
ToZip string `json:"to_zip,omitempty"`
ToState string `json:"to_state,omitempty"`
ToCity string `json:"to_city,omitempty"`
ToStreet string `json:"to_street,omitempty"`
Amount float64 `json:"amount"`
Shipping float64 `json:"shipping"`
SalesTax float64 `json:"sales_tax"`
CustomerID string `json:"customer_id,omitempty"`
ExemptionType string `json:"exemption_type,omitempty"`
LineItems []OrderLineItem `json:"line_items,omitempty"`
}
// Order is the structure for an order returned within `CreateOrderResponse`, `ShowOrderResponse`, `UpdateOrderResponse`, and `DeleteOrderResponse`․
type Order struct {
TransactionID string `json:"transaction_id"`
UserID int `json:"user_id"`
TransactionDate string `json:"transaction_date"`
TransactionReferenceID string `json:"transaction_reference_id"`
Provider string `json:"provider"`
ExemptionType string `json:"exemption_type,omitempty"`
FromCountry string `json:"from_country"`
FromZip string `json:"from_zip"`
FromState string `json:"from_state"`
FromCity string `json:"from_city"`
FromStreet string `json:"from_street"`
ToCountry string `json:"to_country"`
ToZip string `json:"to_zip"`
ToState string `json:"to_state"`
ToCity string `json:"to_city"`
ToStreet string `json:"to_street"`
Amount float64 `json:"amount,string"`
Shipping float64 `json:"shipping,string"`
SalesTax float64 `json:"sales_tax,string"`
LineItems []OrderLineItem `json:"line_items"`
}
// CreateOrderResponse is the structure returned from `CreateOrder`․
//
// Access the created order with `CreateOrderResponse.Order`․
type CreateOrderResponse struct {
Order Order `json:"order"`
}
// CreateOrder creates a new order in TaxJar․
//
// See https://developers.taxjar.com/api/reference/?go#post-create-an-order-transaction for more details․
func (client *Config) CreateOrder(params CreateOrderParams) (*CreateOrderResponse, error) {
res, err := client.post("transactions/orders", params)
if err != nil {
return nil, err
}
order := new(CreateOrderResponse)
if err := json.Unmarshal(res.([]byte), &order); err != nil {
if typeError, ok := err.(*json.UnmarshalTypeError); ok {
// Ignores JSON line_item.id type errors due to API's conversion of numeric strings to integers
if !(typeError.Field == "order.line_items.id" && typeError.Value == "number") {
return nil, err
}
} else {
return nil, err
}
}
return order, nil
}