forked from TimmyGuy/bigcommerce-api-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcart.go
230 lines (219 loc) · 7.27 KB
/
cart.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package bigcommerce
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Cart is a BigCommerce cart object
type Cart struct {
ID string `json:"id"`
CheckoutURL string `json:"checkout_url,omitempty"`
CustomerID int64 `json:"customer_id,omitempty"`
ChannelID int64 `json:"channel_id,omitempty"`
Email string `json:"email,omitempty"`
Currency struct {
Code string `json:"code,omitempty"`
} `json:"currency,omitempty"`
TaxIncluded bool `json:"tax_included,omitempty"`
BaseAmount float64 `json:"base_amount,omitempty"`
DiscountAmount float64 `json:"discount_amount,omitempty"`
CartAmount float64 `json:"cart_amount,omitempty"`
Discounts []Discount `json:"discounts,omitempty"`
Coupons []CartCoupon `json:"coupons,omitempty"`
LineItems struct {
PhysicalItems []LineItem `json:"physical_items,omitempty"`
DigitalItems []LineItem `json:"digital_items,omitempty"`
GiftCertificates []LineItem `json:"gift_certificates,omitempty"`
CustomItems []LineItem `json:"custom_items,omitempty"`
} `json:"line_items"`
CreatedTime time.Time `json:"created_time,omitempty"`
UpdatedTime time.Time `json:"updated_time,omitempty"`
RedirectUrls struct {
CartURL string `json:"cart_url"`
CheckoutURL string `json:"checkout_url"`
EmbeddedCheckoutURL string `json:"embedded_checkout_url"`
} `json:"redirect_urls,omitempty"`
Locale string `json:"locale,omitempty"`
}
// LineItem is a BigCommerce line item object for cart
type LineItem struct {
ID string `json:"id,omitempty"`
ParentID int64 `json:"parent_id,omitempty"`
VariantID int64 `json:"variant_id,omitempty"`
ProductID int64 `json:"product_id,omitempty"`
Sku string `json:"sku,omitempty"`
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Quantity float64 `json:"quantity,omitempty"`
Taxable bool `json:"taxable,omitempty"`
ImageURL string `json:"image_url,omitempty"`
Discounts []Discount `json:"discounts,omitempty"`
Coupons interface{} `json:"coupons,omitempty"`
DiscountAmount float64 `json:"discount_amount,omitempty"`
CouponAmount float64 `json:"coupon_amount,omitempty"`
OriginalPrice float64 `json:"original_price,omitempty"`
ListPrice float64 `json:"list_price,omitempty"`
SalePrice float64 `json:"sale_price,omitempty"`
ExtendedListPrice float64 `json:"extended_list_price,omitempty"`
ExtendedSalePrice float64 `json:"extended_sale_price,omitempty"`
IsRequireShipping bool `json:"is_require_shipping,omitempty"`
IsMutable bool `json:"is_mutable,omitempty"`
}
type CartURLs struct {
CartURL string `json:"cart_url,omitempty"`
CheckoutURL string `json:"checkout_url,omitempty"`
EmbeddedCheckoutURL string `json:"embedded_checkout_url,omitempty"`
}
// CreateCart creates a new cart in BigCommerce and returns it
func (bc *Client) CreateCart(items []LineItem) (*Cart, error) {
var body []byte
body, _ = json.Marshal(map[string]interface{}{
"channel_id": bc.ChannelID,
"line_items": items,
})
req := bc.getAPIRequest(http.MethodPost, "/v3/carts?include=redirect_urls", bytes.NewReader(body))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
b, err := processBody(res)
if err != nil {
return nil, err
}
var cartResponse struct {
Data Cart `json:"data,omitempty"`
Meta struct {
} `json:"meta,omitempty"`
}
err = json.Unmarshal(b, &cartResponse)
if err != nil {
return nil, err
}
return &cartResponse.Data, nil
}
// GetCart gets a cart by ID from BigCommerce and returns it
func (bc *Client) GetCart(cartID string) (*Cart, error) {
req := bc.getAPIRequest(http.MethodGet, "/v3/carts/"+cartID+"?include=redirect_urls", nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
b, err := processBody(res)
if err != nil {
return nil, err
}
var cartResponse struct {
Data Cart `json:"data,omitempty"`
Meta struct {
} `json:"meta,omitempty"`
}
err = json.Unmarshal(b, &cartResponse)
if err != nil {
return nil, err
}
return &cartResponse.Data, nil
}
// CartAddItem adds line items to a cart
func (bc *Client) CartAddItems(cartID string, items []LineItem) (*Cart, error) {
var body []byte
body, _ = json.Marshal(map[string]interface{}{
"line_items": items,
})
req := bc.getAPIRequest(http.MethodPost, "/v3/carts/"+cartID+"/items?include=redirect_urls", bytes.NewReader(body))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
b, err := processBody(res)
if err != nil {
return nil, fmt.Errorf("%s", string(b))
}
var cartResponse struct {
Data Cart `json:"data,omitempty"`
Meta struct {
} `json:"meta,omitempty"`
}
err = json.Unmarshal(b, &cartResponse)
if err != nil {
return nil, err
}
return &cartResponse.Data, nil
}
// EditItem edits a line item in a cart, returns the updated cart
// Arguments:
// cartID: the cart ID
// item: the line item to edit. Must have an ID, quantity, and product ID
func (bc *Client) CartEditItem(cartID string, item LineItem) (*Cart, error) {
var body []byte
body, _ = json.Marshal(map[string]interface{}{
"line_item": item,
})
req := bc.getAPIRequest(http.MethodPut, "/v3/carts/"+cartID+"/items/"+item.ID+"?include=redirect_urls", bytes.NewReader(body))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
b, err := processBody(res)
if err != nil {
return nil, fmt.Errorf("%s", string(b))
}
return bc.GetCart(cartID)
}
// DeleteItem deletes a line item from a cart, returns the updated cart
// Arguments:
// cartID: the cart ID
// item: the line item, must have an existing line item ID
// returns nil for empty cart
func (bc *Client) CartDeleteItem(cartID string, item LineItem) (*Cart, error) {
req := bc.getAPIRequest(http.MethodDelete, "/v3/carts/"+cartID+"/items/"+item.ID+"?include=redirect_urls", nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode == 204 {
return nil, nil
}
_, err = processBody(res)
if err != nil {
return nil, err
}
return bc.GetCart(cartID)
}
// CartUpdateCustomerID updates the customer ID for a cart
// Arguments:
// cartID: the BigCommerce cart ID
// customerID: the new BigCommerce customer ID
func (bc *Client) CartUpdateCustomerID(cartID, customerID string) (*Cart, error) {
req := bc.getAPIRequest(http.MethodPut, "/v3/carts/"+cartID+"?include=redirect_urls",
bytes.NewReader([]byte(fmt.Sprintf(`{"customer_id": %s}`, customerID))))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}
b, err := processBody(res)
if err != nil {
return nil, err
}
var cartResponse struct {
Data Cart `json:"data,omitempty"`
}
err = json.Unmarshal(b, &cartResponse)
if err != nil {
return nil, err
}
return &cartResponse.Data, nil
}
// DeleteCart deletes a cart by ID from BigCommerce
func (bc *Client) DeleteCart(cartID string) error {
req := bc.getAPIRequest(http.MethodDelete, "/v3/carts/"+cartID, nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return err
}
if res.StatusCode != http.StatusNoContent {
return fmt.Errorf("unexpected status code: %d", res.StatusCode)
}
return nil
}