forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
697 lines (597 loc) · 31 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
package goshopify
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/shopspring/decimal"
)
const (
ordersBasePath = "orders"
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(context.Context, interface{}) ([]Order, error)
ListWithPagination(context.Context, interface{}) ([]Order, *Pagination, error)
Count(context.Context, interface{}) (int, error)
Get(context.Context, uint64, interface{}) (*Order, error)
Create(context.Context, Order) (*Order, error)
Update(context.Context, Order) (*Order, error)
Cancel(context.Context, uint64, interface{}) (*Order, error)
Close(context.Context, uint64) (*Order, error)
Open(context.Context, uint64) (*Order, error)
Delete(context.Context, uint64) 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
}
type orderStatus string
// https://shopify.dev/docs/api/admin-rest/2023-07/resources/order#get-orders?status=any
const (
// Show only open orders.
OrderStatusOpen orderStatus = "open"
// Show only closed orders.
OrderStatusClosed orderStatus = "closed"
// Show only cancelled orders.
OrderStatusCancelled orderStatus = "cancelled"
// Show orders of any status, open, closed, cancellerd, or archived.
OrderStatusAny orderStatus = "any"
)
type orderFulfillmentStatus string
// https://shopify.dev/docs/api/admin-rest/2023-07/resources/order#get-orders?status=any
const (
// Show orders that have been shipped.
OrderFulfillmentStatusShipped orderFulfillmentStatus = "shipped"
// Show partially shipped orders.
OrderFulfillmentStatusPartial orderFulfillmentStatus = "partial"
// Show orders that have not yet been shipped.
OrderFulfillmentStatusUnshipped orderFulfillmentStatus = "unshipped"
// Show orders of any fulfillment status.
OrderFulfillmentStatusAny orderFulfillmentStatus = "any"
// Returns orders with fulfillment_status of null or partial.
OrderFulfillmentStatusUnfulfilled orderFulfillmentStatus = "unfulfilled"
//"fulfilled" used to be an acceptable value? Was it deprecated? It isn't noted
//in the Shopify docs at the provided URL, but it was used in tests and still
//seems to function.
OrderFulfillmentStatusFulfilled orderFulfillmentStatus = "fulfilled"
)
type orderFinancialStatus string
// https://shopify.dev/docs/api/admin-rest/2023-07/resources/order#get-orders?status=any
const (
// Show only authorized orders.
OrderFinancialStatusAuthorized orderFinancialStatus = "authorized"
// Show only pending orders.
OrderFinancialStatusPending orderFinancialStatus = "pending"
// Show only paid orders.
OrderFinancialStatusPaid orderFinancialStatus = "paid"
// Show only partially paid orders.
OrderFinancialStatusPartiallyPaid orderFinancialStatus = "partially_paid"
// Show only refunded orders.
OrderFinancialStatusRefunded orderFinancialStatus = "refunded"
// Show only voided orders.
OrderFinancialStatusVoided orderFinancialStatus = "voided"
// Show only partially refunded orders.
OrderFinancialStatusPartiallyRefunded orderFinancialStatus = "partially_refunded"
// Show orders of any financial status.
OrderFinancialStatusAny orderFinancialStatus = "any"
// Show authorized and partially paid orders.
OrderFinancialStatusUnpaid orderFinancialStatus = "unpaid"
)
type orderCancelReason string
const (
// The customer canceled the order.
OrderCancelReasonCustomer orderCancelReason = "customer"
// The order was fraudulent.
OrderCancelReasonFraud orderCancelReason = "fraud"
// Items in the order were not in inventory.
OrderCancelReasonInventory orderCancelReason = "inventory"
// The payment was declined.
OrderCancelReasonDeclined orderCancelReason = "declined"
// Cancelled for some other reason.
OrderCancelReasonOther orderCancelReason = "other"
)
// A struct for all available order count options
type OrderCountOptions struct {
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
SinceId uint64 `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 orderStatus `url:"status,omitempty"`
FinancialStatus orderFinancialStatus `url:"financial_status,omitempty"`
FulfillmentStatus orderFulfillmentStatus `url:"fulfillment_status,omitempty"`
}
// A struct for all available order list options.
// See: https://help.shopify.com/api/reference/order#index
type OrderListOptions struct {
ListOptions
Status orderStatus `url:"status,omitempty"`
FinancialStatus orderFinancialStatus `url:"financial_status,omitempty"`
FulfillmentStatus orderFulfillmentStatus `url:"fulfillment_status,omitempty"`
ProcessedAtMin time.Time `url:"processed_at_min,omitempty"`
ProcessedAtMax time.Time `url:"processed_at_max,omitempty"`
Order string `url:"order,omitempty"`
}
// A struct of all available order cancel options.
// See: https://help.shopify.com/api/reference/order#index
type OrderCancelOptions struct {
Amount *decimal.Decimal `json:"amount,omitempty"`
Currency string `json:"currency,omitempty"`
Restock bool `json:"restock,omitempty"`
Reason string `json:"reason,omitempty"`
Email bool `json:"email,omitempty"`
Refund *Refund `json:"refund,omitempty"`
}
// The behaviour to use when updating inventory.
type orderInventoryBehaviour string
const (
// Do not claim inventory.
OrderInventoryBehaviourBypass orderInventoryBehaviour = "bypass"
// Ignore the product's inventory policy and claim inventory.
OrderInventoryBehaviourDecrementIgnoringPolicy orderInventoryBehaviour = "decrement_ignoring_policy"
// Follow the product's inventory policy and claim inventory, if possible.
OrderInventoryBehaviourDecrementObeyingPolicy orderInventoryBehaviour = "decrement_obeying_policy"
)
// Order represents a Shopify order
type Order struct {
Id uint64 `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"`
TotalPriceSet *AmountSet `json:"total_price_set,omitempty"`
TotalShippingPriceSet *AmountSet `json:"total_shipping_price_set,omitempty"`
CurrentTotalPrice *decimal.Decimal `json:"current_total_price,omitempty"`
SubtotalPrice *decimal.Decimal `json:"subtotal_price,omitempty"`
CurrentSubtotalPrice *decimal.Decimal `json:"current_subtotal_price,omitempty"`
TotalDiscounts *decimal.Decimal `json:"total_discounts,omitempty"`
TotalDiscountSet *AmountSet `json:"total_discount_set,omitempty"`
CurrentTotalDiscounts *decimal.Decimal `json:"current_total_discounts,omitempty"`
CurrentTotalDiscountsSet *AmountSet `json:"current_total_discounts_set,omitempty"`
TotalLineItemsPrice *decimal.Decimal `json:"total_line_items_price,omitempty"`
TaxesIncluded bool `json:"taxes_included,omitempty"`
TotalTax *decimal.Decimal `json:"total_tax,omitempty"`
TotalTaxSet *AmountSet `json:"total_tax_set,omitempty"`
CurrentTotalTax *decimal.Decimal `json:"current_total_tax,omitempty"`
CurrentTotalTaxSet *AmountSet `json:"current_total_tax_set,omitempty"`
TaxLines []TaxLine `json:"tax_lines,omitempty"`
TotalWeight int `json:"total_weight,omitempty"`
FinancialStatus orderFinancialStatus `json:"financial_status,omitempty"`
Fulfillments []Fulfillment `json:"fulfillments,omitempty"`
FulfillmentStatus orderFulfillmentStatus `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 orderCancelReason `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 uint64 `json:"location_id,omitempty"`
PaymentGatewayNames []string `json:"payment_gateway_names,omitempty"`
ProcessingMethod string `json:"processing_method,omitempty"`
Refunds []Refund `json:"refunds,omitempty"`
UserId uint64 `json:"user_id,omitempty"`
OrderStatusUrl string `json:"order_status_url,omitempty"`
Gateway string `json:"gateway,omitempty"`
Confirmed bool `json:"confirmed,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 uint64 `json:"device_id,omitempty"`
Phone string `json:"phone,omitempty"`
LandingSiteRef string `json:"landing_site_ref,omitempty"`
CheckoutId uint64 `json:"checkout_id,omitempty"`
ContactEmail string `json:"contact_email,omitempty"`
Metafields []Metafield `json:"metafields,omitempty"`
SendReceipt bool `json:"send_receipt,omitempty"`
SendFulfillmentReceipt bool `json:"send_fulfillment_receipt,omitempty"`
PresentmentCurrency string `json:"presentment_currency,omitempty"`
InventoryBehaviour orderInventoryBehaviour `json:"inventory_behaviour,omitempty"`
}
type Address struct {
Id uint64 `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 uint64 `json:"id,omitempty"`
ProductId uint64 `json:"product_id,omitempty"`
VariantId uint64 `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 orderFulfillmentStatus `json:"fulfillment_status,omitempty"`
TaxLines []TaxLine `json:"tax_lines,omitempty"`
// Deprecated: See 2022-10 release notes: https://shopify.dev/docs/api/release-notes/2022-10
OriginLocation *Address `json:"origin_location,omitempty"`
// Deprecated: See 2022-10 release notes: https://shopify.dev/docs/api/release-notes/2022-10
DestinationLocation *Address `json:"destination_location,omitempty"`
AppliedDiscount *AppliedDiscount `json:"applied_discount,omitempty"`
DiscountAllocations []DiscountAllocations `json:"discount_allocations,omitempty"`
}
type DiscountAllocations struct {
Amount *decimal.Decimal `json:"amount,omitempty"`
DiscountApplicationIndex int `json:"discount_application_index,omitempty"`
AmountSet *AmountSet `json:"amount_set,omitempty"`
}
type AmountSet struct {
ShopMoney AmountSetEntry `json:"shop_money,omitempty"`
PresentmentMoney AmountSetEntry `json:"presentment_money,omitempty"`
}
type AmountSetEntry struct {
Amount *decimal.Decimal `json:"amount,omitempty"`
CurrencyCode string `json:"currency_code,omitempty"`
}
// UnmarshalJSON custom unmarsaller for LineItem required to mitigate some older orders having LineItem.Properies
// which are empty JSON objects rather than the expected array.
func (li *LineItem) UnmarshalJSON(data []byte) error {
type alias LineItem
aux := &struct {
Properties json.RawMessage `json:"properties"`
*alias
}{alias: (*alias)(li)}
err := json.Unmarshal(data, &aux)
if err != nil {
return err
}
if len(aux.Properties) == 0 {
return nil
} else if aux.Properties[0] == '[' { // if the first character is a '[' we unmarshal into an array
var p []NoteAttribute
err = json.Unmarshal(aux.Properties, &p)
if err != nil {
return err
}
li.Properties = p
} else { // else we unmarshal it into a struct
var p NoteAttribute
err = json.Unmarshal(aux.Properties, &p)
if err != nil {
return err
}
if p.Name == "" && p.Value == nil { // if the struct is empty we set properties to nil
li.Properties = nil
} else {
li.Properties = []NoteAttribute{p} // else we set them to an array with the property nested
}
}
return nil
}
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 uint64 `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Price *decimal.Decimal `json:"price,omitempty"`
PriceSet *AmountSet `json:"price_set,omitempty"`
DiscountedPrice *decimal.Decimal `json:"discounted_price,omitempty"`
DiscountedPriceSet *AmountSet `json:"discounted_price_set,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"`
Handle string `json:"handle,omitempty"`
}
// UnmarshalJSON custom unmarshaller for ShippingLines implemented to handle requested_fulfillment_service_id being
// returned as json numbers or json nulls instead of json strings
func (sl *ShippingLines) UnmarshalJSON(data []byte) error {
type alias ShippingLines
aux := &struct {
*alias
RequestedFulfillmentServiceId interface{} `json:"requested_fulfillment_service_id"`
}{alias: (*alias)(sl)}
err := json.Unmarshal(data, &aux)
if err != nil {
return err
}
switch aux.RequestedFulfillmentServiceId.(type) {
case nil:
sl.RequestedFulfillmentServiceId = ""
default:
sl.RequestedFulfillmentServiceId = fmt.Sprintf("%v", aux.RequestedFulfillmentServiceId)
}
return nil
}
type TaxLine struct {
Title string `json:"title,omitempty"`
Price *decimal.Decimal `json:"price,omitempty"`
Rate *decimal.Decimal `json:"rate,omitempty"`
}
type Transaction struct {
Id uint64 `json:"id,omitempty"`
OrderId uint64 `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"`
Source string `json:"source,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 uint64 `json:"id,omitempty"`
OrderId uint64 `json:"order_id,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
Note string `json:"note,omitempty"`
Restock bool `json:"restock,omitempty"`
UserId uint64 `json:"user_id,omitempty"`
RefundLineItems []RefundLineItem `json:"refund_line_items,omitempty"`
Transactions []Transaction `json:"transactions,omitempty"`
OrderAdjustments []OrderAdjustment `json:"order_adjustments,omitempty"`
}
type OrderAdjustment struct {
Id uint64 `json:"id,omitempty"`
OrderId uint64 `json:"order_id,omitempty"`
RefundId uint64 `json:"refund_id,omitempty"`
Amount *decimal.Decimal `json:"amount,omitempty"`
TaxAmount *decimal.Decimal `json:"tax_amount,omitempty"`
Kind OrderAdjustmentType `json:"kind,omitempty"`
Reason string `json:"reason,omitempty"`
AmountSet *AmountSet `json:"amount_set,omitempty"`
TaxAmountSet *AmountSet `json:"tax_amount_set,omitempty"`
}
type OrderAdjustmentType string
const (
OrderAdjustmentTypeShippingRefund OrderAdjustmentType = "shipping_refund"
OrderAdjustmentTypeRefundDiscrepancy OrderAdjustmentType = "refund_discrepancy"
)
type RefundLineItem struct {
Id uint64 `json:"id,omitempty"`
Quantity int `json:"quantity,omitempty"`
LineItemId uint64 `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(ctx context.Context, options interface{}) ([]Order, error) {
orders, _, err := s.ListWithPagination(ctx, options)
if err != nil {
return nil, err
}
return orders, nil
}
func (s *OrderServiceOp) ListWithPagination(ctx context.Context, options interface{}) ([]Order, *Pagination, error) {
path := fmt.Sprintf("%s.json", ordersBasePath)
resource := new(OrdersResource)
pagination, err := s.client.ListWithPagination(ctx, path, resource, options)
if err != nil {
return nil, nil, err
}
return resource.Orders, pagination, nil
}
// Count orders
func (s *OrderServiceOp) Count(ctx context.Context, options interface{}) (int, error) {
path := fmt.Sprintf("%s/count.json", ordersBasePath)
return s.client.Count(ctx, path, options)
}
// Get individual order
func (s *OrderServiceOp) Get(ctx context.Context, orderId uint64, options interface{}) (*Order, error) {
path := fmt.Sprintf("%s/%d.json", ordersBasePath, orderId)
resource := new(OrderResource)
err := s.client.Get(ctx, path, resource, options)
return resource.Order, err
}
// Create order
func (s *OrderServiceOp) Create(ctx context.Context, order Order) (*Order, error) {
path := fmt.Sprintf("%s.json", ordersBasePath)
wrappedData := OrderResource{Order: &order}
resource := new(OrderResource)
err := s.client.Post(ctx, path, wrappedData, resource)
return resource.Order, err
}
// Update order
func (s *OrderServiceOp) Update(ctx context.Context, order Order) (*Order, error) {
path := fmt.Sprintf("%s/%d.json", ordersBasePath, order.Id)
wrappedData := OrderResource{Order: &order}
resource := new(OrderResource)
err := s.client.Put(ctx, path, wrappedData, resource)
return resource.Order, err
}
// Cancel order
func (s *OrderServiceOp) Cancel(ctx context.Context, orderId uint64, options interface{}) (*Order, error) {
path := fmt.Sprintf("%s/%d/cancel.json", ordersBasePath, orderId)
resource := new(OrderResource)
err := s.client.Post(ctx, path, options, resource)
return resource.Order, err
}
// Close order
func (s *OrderServiceOp) Close(ctx context.Context, orderId uint64) (*Order, error) {
path := fmt.Sprintf("%s/%d/close.json", ordersBasePath, orderId)
resource := new(OrderResource)
err := s.client.Post(ctx, path, nil, resource)
return resource.Order, err
}
// Open order
func (s *OrderServiceOp) Open(ctx context.Context, orderId uint64) (*Order, error) {
path := fmt.Sprintf("%s/%d/open.json", ordersBasePath, orderId)
resource := new(OrderResource)
err := s.client.Post(ctx, path, nil, resource)
return resource.Order, err
}
// Delete order
func (s *OrderServiceOp) Delete(ctx context.Context, orderId uint64) error {
path := fmt.Sprintf("%s/%d.json", ordersBasePath, orderId)
err := s.client.Delete(ctx, path)
return err
}
// List metafields for an order
func (s *OrderServiceOp) ListMetafields(ctx context.Context, orderId uint64, options interface{}) ([]Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return metafieldService.List(ctx, options)
}
// Count metafields for an order
func (s *OrderServiceOp) CountMetafields(ctx context.Context, orderId uint64, options interface{}) (int, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return metafieldService.Count(ctx, options)
}
// Get individual metafield for an order
func (s *OrderServiceOp) GetMetafield(ctx context.Context, orderId uint64, metafieldId uint64, options interface{}) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return metafieldService.Get(ctx, metafieldId, options)
}
// Create a new metafield for an order
func (s *OrderServiceOp) CreateMetafield(ctx context.Context, orderId uint64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return metafieldService.Create(ctx, metafield)
}
// Update an existing metafield for an order
func (s *OrderServiceOp) UpdateMetafield(ctx context.Context, orderId uint64, metafield Metafield) (*Metafield, error) {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return metafieldService.Update(ctx, metafield)
}
// Delete an existing metafield for an order
func (s *OrderServiceOp) DeleteMetafield(ctx context.Context, orderId uint64, metafieldId uint64) error {
metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return metafieldService.Delete(ctx, metafieldId)
}
// List fulfillments for an order
func (s *OrderServiceOp) ListFulfillments(ctx context.Context, orderId uint64, options interface{}) ([]Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return fulfillmentService.List(ctx, options)
}
// Count fulfillments for an order
func (s *OrderServiceOp) CountFulfillments(ctx context.Context, orderId uint64, options interface{}) (int, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return fulfillmentService.Count(ctx, options)
}
// Get individual fulfillment for an order
func (s *OrderServiceOp) GetFulfillment(ctx context.Context, orderId uint64, fulfillmentId uint64, options interface{}) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return fulfillmentService.Get(ctx, fulfillmentId, options)
}
// Create a new fulfillment for an order
func (s *OrderServiceOp) CreateFulfillment(ctx context.Context, orderId uint64, fulfillment Fulfillment) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return fulfillmentService.Create(ctx, fulfillment)
}
// Update an existing fulfillment for an order
func (s *OrderServiceOp) UpdateFulfillment(ctx context.Context, orderId uint64, fulfillment Fulfillment) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return fulfillmentService.Update(ctx, fulfillment)
}
// Complete an existing fulfillment for an order
func (s *OrderServiceOp) CompleteFulfillment(ctx context.Context, orderId uint64, fulfillmentId uint64) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return fulfillmentService.Complete(ctx, fulfillmentId)
}
// Transition an existing fulfillment for an order
func (s *OrderServiceOp) TransitionFulfillment(ctx context.Context, orderId uint64, fulfillmentId uint64) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return fulfillmentService.Transition(ctx, fulfillmentId)
}
// Cancel an existing fulfillment for an order
func (s *OrderServiceOp) CancelFulfillment(ctx context.Context, orderId uint64, fulfillmentId uint64) (*Fulfillment, error) {
fulfillmentService := &FulfillmentServiceOp{client: s.client, resource: ordersResourceName, resourceId: orderId}
return fulfillmentService.Cancel(ctx, fulfillmentId)
}