forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
409 lines (366 loc) · 19.5 KB
/
order.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package goshopify
import (
"fmt"
"time"
"github.com/shopspring/decimal"
)
const ordersBasePath = "orders"
const ordersResourceName = "orders"
// OrderService is an interface for interfacing with the orders endpoints of
// the Shopify API.
// See: https://help.shopify.com/api/reference/order
type OrderService interface {
List(interface{}) ([]Order, error)
Count(interface{}) (int, error)
Get(int64, interface{}) (*Order, error)
Create(Order) (*Order, error)
Update(Order) (*Order, error)
// MetafieldsService used for Order resource to communicate with Metafields resource
MetafieldsService
// FulfillmentsService used for Order resource to communicate with Fulfillments resource
FulfillmentsService
}
// OrderServiceOp handles communication with the order related methods of the
// Shopify API.
type OrderServiceOp struct {
client *Client
}
// A struct for all available order count options
type OrderCountOptions struct {
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
SinceID int64 `url:"since_id,omitempty"`
CreatedAtMin time.Time `url:"created_at_min,omitempty"`
CreatedAtMax time.Time `url:"created_at_max,omitempty"`
UpdatedAtMin time.Time `url:"updated_at_min,omitempty"`
UpdatedAtMax time.Time `url:"updated_at_max,omitempty"`
Order string `url:"order,omitempty"`
Fields string `url:"fields,omitempty"`
Status string `url:"status,omitempty"`
FinancialStatus string `url:"financial_status,omitempty"`
FulfillmentStatus string `url:"fulfillment_status,omitempty"`
}
// A struct for all available order list options.
// See: https://help.shopify.com/api/reference/order#index
type OrderListOptions struct {
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
SinceID int64 `url:"since_id,omitempty"`
Status string `url:"status,omitempty"`
FinancialStatus string `url:"financial_status,omitempty"`
FulfillmentStatus string `url:"fulfillment_status,omitempty"`
CreatedAtMin time.Time `url:"created_at_min,omitempty"`
CreatedAtMax time.Time `url:"created_at_max,omitempty"`
UpdatedAtMin time.Time `url:"updated_at_min,omitempty"`
UpdatedAtMax time.Time `url:"updated_at_max,omitempty"`
ProcessedAtMin time.Time `url:"processed_at_min,omitempty"`
ProcessedAtMax time.Time `url:"processed_at_max,omitempty"`
Fields string `url:"fields,omitempty"`
Order string `url:"order,omitempty"`
}
// Order represents a Shopify order
type Order struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
CancelledAt *time.Time `json:"cancelled_at,omitempty"`
ClosedAt *time.Time `json:"closed_at,omitempty"`
ProcessedAt *time.Time `json:"processed_at,omitempty"`
Customer *Customer `json:"customer,omitempty"`
BillingAddress *Address `json:"billing_address,omitempty"`
ShippingAddress *Address `json:"shipping_address,omitempty"`
Currency string `json:"currency,omitempty"`
TotalPrice *decimal.Decimal `json:"total_price,omitempty"`
SubtotalPrice *decimal.Decimal `json:"subtotal_price,omitempty"`
TotalDiscounts *decimal.Decimal `json:"total_discounts,omitempty"`
TotalLineItemsPrice *decimal.Decimal `json:"total_line_items_price,omitempty"`
TaxesIncluded bool `json:"taxes_included,omitempty"`
TotalTax *decimal.Decimal `json:"total_tax,omitempty"`
TaxLines []TaxLine `json:"tax_lines,omitempty"`
TotalWeight int `json:"total_weight,omitempty"`
FinancialStatus string `json:"financial_status,omitempty"`
Fulfillments []Fulfillment `json:"fulfillments,omitempty"`
FulfillmentStatus string `json:"fulfillment_status,omitempty"`
Token string `json:"token,omitempty"`
CartToken string `json:"cart_token,omitempty"`
Number int `json:"number,omitempty"`
OrderNumber int `json:"order_number,omitempty"`
Note string `json:"note,omitempty"`
Test bool `json:"test,omitempty"`
BrowserIp string `json:"browser_ip,omitempty"`
BuyerAcceptsMarketing bool `json:"buyer_accepts_marketing,omitempty"`
CancelReason string `json:"cancel_reason,omitempty"`
NoteAttributes []NoteAttribute `json:"note_attributes,omitempty"`
DiscountCodes []DiscountCode `json:"discount_codes,omitempty"`
LineItems []LineItem `json:"line_items,omitempty"`
ShippingLines []ShippingLines `json:"shipping_lines,omitempty"`
Transactions []Transaction `json:"transactions,omitempty"`
AppID int `json:"app_id,omitempty"`
CustomerLocale string `json:"customer_locale,omitempty"`
LandingSite string `json:"landing_site,omitempty"`
ReferringSite string `json:"referring_site,omitempty"`
SourceName string `json:"source_name,omitempty"`
ClientDetails *ClientDetails `json:"client_details,omitempty"`
Tags string `json:"tags,omitempty"`
LocationId int64 `json:"location_id,omitempty"`
PaymentGatewayNames []string `json:"payment_gateway_names,omitempty"`
ProcessingMethod string `json:"processing_method,omitempty"`
Refunds []Refund `json:"refunds,omitempty"`
UserId int64 `json:"user_id,omitempty"`
OrderStatusUrl string `json:"order_status_url,omitempty"`
Gateway string `json:"gateway,omitempty"`
Confirmed bool `json:"confirmed,omitempty"`
TotalPriceUSD *decimal.Decimal `json:"total_price_usd,omitempty"`
CheckoutToken string `json:"checkout_token,omitempty"`
Reference string `json:"reference,omitempty"`
SourceIdentifier string `json:"source_identifier,omitempty"`
SourceURL string `json:"source_url,omitempty"`
DeviceID int64 `json:"device_id,omitempty"`
Phone string `json:"phone,omitempty"`
LandingSiteRef string `json:"landing_site_ref,omitempty"`
CheckoutID int64 `json:"checkout_id,omitempty"`
ContactEmail string `json:"contact_email,omitempty"`
Metafields []Metafield `json:"metafields,omitempty"`
}
type Address struct {
ID int64 `json:"id,omitempty"`
Address1 string `json:"address1,omitempty"`
Address2 string `json:"address2,omitempty"`
City string `json:"city,omitempty"`
Company string `json:"company,omitempty"`
Country string `json:"country,omitempty"`
CountryCode string `json:"country_code,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
Name string `json:"name,omitempty"`
Phone string `json:"phone,omitempty"`
Province string `json:"province,omitempty"`
ProvinceCode string `json:"province_code,omitempty"`
Zip string `json:"zip,omitempty"`
}
type DiscountCode struct {
Amount *decimal.Decimal `json:"amount,omitempty"`
Code string `json:"code,omitempty"`
Type string `json:"type,omitempty"`
}
type LineItem struct {
ID int64 `json:"id,omitempty"`
ProductID int64 `json:"product_id,omitempty"`
VariantID int64 `json:"variant_id,omitempty"`
Quantity int `json:"quantity,omitempty"`
Price *decimal.Decimal `json:"price,omitempty"`
TotalDiscount *decimal.Decimal `json:"total_discount,omitempty"`
Title string `json:"title,omitempty"`
VariantTitle string `json:"variant_title,omitempty"`
Name string `json:"name,omitempty"`
SKU string `json:"sku,omitempty"`
Vendor string `json:"vendor,omitempty"`
GiftCard bool `json:"gift_card,omitempty"`
Taxable bool `json:"taxable,omitempty"`
FulfillmentService string `json:"fulfillment_service,omitempty"`
RequiresShipping bool `json:"requires_shipping,omitempty"`
VariantInventoryManagement string `json:"variant_inventory_management,omitempty"`
PreTaxPrice *decimal.Decimal `json:"pre_tax_price,omitempty"`
Properties []NoteAttribute `json:"properties,omitempty"`
ProductExists bool `json:"product_exists,omitempty"`
FulfillableQuantity int `json:"fulfillable_quantity,omitempty"`
Grams int `json:"grams,omitempty"`
FulfillmentStatus string `json:"fulfillment_status,omitempty"`
TaxLines []TaxLine `json:"tax_lines,omitempty"`
OriginLocation *Address `json:"origin_location,omitempty"`
DestinationLocation *Address `json:"destination_location,omitempty"`
}
type LineItemProperty struct {
Message string `json:"message"`
}
type NoteAttribute struct {
Name string `json:"name,omitempty"`
Value interface{} `json:"value,omitempty"`
}
// Represents the result from the orders/X.json endpoint
type OrderResource struct {
Order *Order `json:"order"`
}
// Represents the result from the orders.json endpoint
type OrdersResource struct {
Orders []Order `json:"orders"`
}
type PaymentDetails struct {
AVSResultCode string `json:"avs_result_code,omitempty"`
CreditCardBin string `json:"credit_card_bin,omitempty"`
CVVResultCode string `json:"cvv_result_code,omitempty"`
CreditCardNumber string `json:"credit_card_number,omitempty"`
CreditCardCompany string `json:"credit_card_company,omitempty"`
}
type ShippingLines struct {
ID int64 `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Price *decimal.Decimal `json:"price,omitempty"`
Code string `json:"code,omitempty"`
Source string `json:"source,omitempty"`
Phone string `json:"phone,omitempty"`
RequestedFulfillmentServiceID string `json:"requested_fulfillment_service_id,omitempty"`
DeliveryCategory string `json:"delivery_category,omitempty"`
CarrierIdentifier string `json:"carrier_identifier,omitempty"`
TaxLines []TaxLine `json:"tax_lines,omitempty"`
}
type TaxLine struct {
Title string `json:"title,omitempty"`
Price *decimal.Decimal `json:"price,omitempty"`
Rate *decimal.Decimal `json:"rate,omitempty"`
}
type Transaction struct {
ID int64 `json:"id,omitempty"`
OrderID int64 `json:"order_id,omitempty"`
Amount *decimal.Decimal `json:"amount,omitempty"`
Kind string `json:"kind,omitempty"`
Gateway string `json:"gateway,omitempty"`
Status string `json:"status,omitempty"`
Message string `json:"message,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
Test bool `json:"test,omitempty"`
Authorization string `json:"authorization,omitempty"`
Currency string `json:"currency,omitempty"`
LocationID *int64 `json:"location_id,omitempty"`
UserID *int64 `json:"user_id,omitempty"`
ParentID *int64 `json:"parent_id,omitempty"`
DeviceID *int64 `json:"device_id,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
SourceName string `json:"source_name,omitempty"`
PaymentDetails *PaymentDetails `json:"payment_details,omitempty"`
}
type ClientDetails struct {
AcceptLanguage string `json:"accept_language,omitempty"`
BrowserHeight int `json:"browser_height,omitempty"`
BrowserIp string `json:"browser_ip,omitempty"`
BrowserWidth int `json:"browser_width,omitempty"`
SessionHash string `json:"session_hash,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
}
type Refund struct {
Id int64 `json:"id,omitempty"`
OrderId int64 `json:"order_id,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
Note string `json:"note,omitempty"`
Restock bool `json:"restock,omitempty"`
UserId int64 `json:"user_id,omitempty"`
RefundLineItems []RefundLineItem `json:"refund_line_items,omitempty"`
Transactions []Transaction `json:"transactions,omitempty"`
}
type RefundLineItem struct {
Id int64 `json:"id,omitempty"`
Quantity int `json:"quantity,omitempty"`
LineItemId int64 `json:"line_item_id,omitempty"`
LineItem *LineItem `json:"line_item,omitempty"`
Subtotal *decimal.Decimal `json:"subtotal,omitempty"`
TotalTax *decimal.Decimal `json:"total_tax,omitempty"`
}
// List orders
func (s *OrderServiceOp) List(options interface{}) ([]Order, error) {
path := fmt.Sprintf("%s/%s.json", globalApiPathPrefix, ordersBasePath)
resource := new(OrdersResource)
err := s.client.Get(path, resource, options)
return resource.Orders, err
}
// Count orders
func (s *OrderServiceOp) Count(options interface{}) (int, error) {
path := fmt.Sprintf("%s/%s/count.json", globalApiPathPrefix, ordersBasePath)
return s.client.Count(path, options)
}
// Get individual order
func (s *OrderServiceOp) Get(orderID int64, options interface{}) (*Order, error) {
path := fmt.Sprintf("%s/%s/%d.json", globalApiPathPrefix, ordersBasePath, orderID)
resource := new(OrderResource)
err := s.client.Get(path, resource, options)
return resource.Order, err
}
// Create order
func (s *OrderServiceOp) Create(order Order) (*Order, error) {
path := fmt.Sprintf("%s/%s.json", globalApiPathPrefix, ordersBasePath)
wrappedData := OrderResource{Order: &order}
resource := new(OrderResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Order, err
}
// Update order
func (s *OrderServiceOp) Update(order Order) (*Order, error) {
path := fmt.Sprintf("%s/%s/%d.json", globalApiPathPrefix, ordersBasePath, order.ID)
wrappedData := OrderResource{Order: &order}
resource := new(OrderResource)
err := s.client.Put(path, wrappedData, resource)
return resource.Order, err
}
// List metafields for an order
func (s *OrderServiceOp) ListMetafields(orderID int64, options interface{}) ([]Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return metafieldService.List(options)
}
// Count metafields for an order
func (s *OrderServiceOp) CountMetafields(orderID int64, options interface{}) (int, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return metafieldService.Count(options)
}
// Get individual metafield for an order
func (s *OrderServiceOp) GetMetafield(orderID int64, metafieldID int64, options interface{}) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return metafieldService.Get(metafieldID, options)
}
// Create a new metafield for an order
func (s *OrderServiceOp) CreateMetafield(orderID int64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return metafieldService.Create(metafield)
}
// Update an existing metafield for an order
func (s *OrderServiceOp) UpdateMetafield(orderID int64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return metafieldService.Update(metafield)
}
// Delete an existing metafield for an order
func (s *OrderServiceOp) DeleteMetafield(orderID int64, metafieldID int64) error {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return metafieldService.Delete(metafieldID)
}
// List fulfillments for an order
func (s *OrderServiceOp) ListFulfillments(orderID int64, options interface{}) ([]Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return fulfillmentService.List(options)
}
// Count fulfillments for an order
func (s *OrderServiceOp) CountFulfillments(orderID int64, options interface{}) (int, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return fulfillmentService.Count(options)
}
// Get individual fulfillment for an order
func (s *OrderServiceOp) GetFulfillment(orderID int64, fulfillmentID int64, options interface{}) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return fulfillmentService.Get(fulfillmentID, options)
}
// Create a new fulfillment for an order
func (s *OrderServiceOp) CreateFulfillment(orderID int64, fulfillment Fulfillment) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return fulfillmentService.Create(fulfillment)
}
// Update an existing fulfillment for an order
func (s *OrderServiceOp) UpdateFulfillment(orderID int64, fulfillment Fulfillment) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return fulfillmentService.Update(fulfillment)
}
// Complete an existing fulfillment for an order
func (s *OrderServiceOp) CompleteFulfillment(orderID int64, fulfillmentID int64) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return fulfillmentService.Complete(fulfillmentID)
}
// Transition an existing fulfillment for an order
func (s *OrderServiceOp) TransitionFulfillment(orderID int64, fulfillmentID int64) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return fulfillmentService.Transition(fulfillmentID)
}
// Cancel an existing fulfillment for an order
func (s *OrderServiceOp) CancelFulfillment(orderID int64, fulfillmentID int64) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID}
return fulfillmentService.Cancel(fulfillmentID)
}