forked from bold-commerce/go-shopify
/
fulfillment.go
194 lines (171 loc) · 7.19 KB
/
fulfillment.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
package shopify
import (
"fmt"
"time"
)
const fulfillmentsResourceName = "fulfillments"
// FulfillmentService is an interface for interfacing with the fulfillment endpoints
// of the Shopify API.
// https://help.shopify.com/api/reference/fulfillment
type FulfillmentService interface {
List(interface{}) ([]Fulfillment, error)
Count(interface{}) (int, error)
Get(int64, interface{}) (*Fulfillment, error)
Create(FulfillmentInfo) (*Fulfillment, error)
Update(Fulfillment) (*Fulfillment, error)
Complete(int64) (*Fulfillment, error)
Transition(int64) (*Fulfillment, error)
Cancel(int64) (*Fulfillment, error)
}
// FulfillmentsService is an interface for other Shopify resources
// to interface with the fulfillment endpoints of the Shopify API.
// https://help.shopify.com/api/reference/fulfillment
type FulfillmentsService interface {
ListFulfillments(int64, interface{}) ([]Fulfillment, error)
CountFulfillments(int64, interface{}) (int, error)
GetFulfillment(int64, int64, interface{}) (*Fulfillment, error)
CreateFulfillment(FulfillmentInfo) (*Fulfillment, error)
UpdateFulfillment(int64, Fulfillment) (*Fulfillment, error)
CompleteFulfillment(int64, int64) (*Fulfillment, error)
TransitionFulfillment(int64, int64) (*Fulfillment, error)
CancelFulfillment(int64, int64) (*Fulfillment, error)
}
// FulfillmentServiceOp handles communication with the fulfillment
// related methods of the Shopify API.
type FulfillmentServiceOp struct {
client *Client
resource string
resourceID int64
}
// Fulfillment represents a Shopify fulfillment.
type Fulfillment struct {
ID int64 `json:"id"`
OrderID int64 `json:"order_id"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
Service string `json:"service"`
UpdatedAt time.Time `json:"updated_at"`
TrackingCompany string `json:"tracking_company"`
ShipmentStatus interface{} `json:"shipment_status"`
LocationID int64 `json:"location_id"`
OriginAddress interface{} `json:"origin_address"`
LineItems []LineItem `json:"line_items"`
TrackingNumber string `json:"tracking_number"`
TrackingNumbers []string `json:"tracking_numbers"`
TrackingURL string `json:"tracking_url"`
TrackingURLs []string `json:"tracking_urls"`
Receipt Receipt `json:"receipt"`
Name string `json:"name"`
AdminGraphqlAPIID string `json:"admin_graphql_api_id"`
}
type FulfillmentInfo struct {
LineItemsByFulfillmentOrder []FulfillmentOrderItem `json:"line_items_by_fulfillment_order,omitempty"`
Message string `json:"message,omitempty"`
NotifyCustomer bool `json:"notify_customer,omitempty"`
OriginAddress *OriginAddress `json:"origin_address,omitempty"`
TrackingInfo TrackingInfo `json:"tracking_info,omitempty"`
}
type FulfillmentOrderItem struct {
FulfillmentOrderID int64 `json:"fulfillment_order_id,omitempty"`
FulfillmentOrderLineItems []FulfillmentLineItem `json:"fulfillment_order_line_items,omitempty"`
}
// The line items in the fulfillment.
type FulfillmentLineItem struct {
ID int64 `json:"id,omitempty"`
Quantity int64 `json:"quantity,omitempty"`
}
// The address of the fulfillment location.
type OriginAddress struct {
Address1 string `json:"address1"`
Address2 string `json:"address2"`
City string `json:"city"`
CountryCode string `json:"country_code"`
ProvinceCode string `json:"province_code"`
Zip string `json:"zip"`
}
// The tracking information for the fulfillment.
type TrackingInfo struct {
Company string `json:"company,omitempty"`
Number string `json:"number,omitempty"`
Url string `json:"url,omitempty"`
}
// Receipt represents a Shopify receipt.
type Receipt struct {
TestCase bool `json:"testcase,omitempty"`
Authorization string `json:"authorization,omitempty"`
}
// FulfillmentResource represents the result from the fulfillments/X.json endpoint
type FulfillmentResource struct {
Fulfillment *Fulfillment `json:"fulfillment"`
}
type FulfillmentInfoResource struct {
Fulfillment *FulfillmentInfo `json:"fulfillment"`
}
// FulfillmentsResource represents the result from the fullfilments.json endpoint
type FulfillmentsResource struct {
Fulfillments []Fulfillment `json:"fulfillments"`
}
// List fulfillments
func (s *FulfillmentServiceOp) List(options interface{}) ([]Fulfillment, error) {
prefix := FulfillmentPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s.json", prefix)
resource := new(FulfillmentsResource)
err := s.client.Get(path, resource, options)
return resource.Fulfillments, err
}
// Count fulfillments
func (s *FulfillmentServiceOp) Count(options interface{}) (int, error) {
prefix := FulfillmentPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/count.json", prefix)
return s.client.Count(path, options)
}
// Get individual fulfillment
func (s *FulfillmentServiceOp) Get(fulfillmentID int64, options interface{}) (*Fulfillment, error) {
prefix := FulfillmentPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/%d.json", prefix, fulfillmentID)
resource := new(FulfillmentResource)
err := s.client.Get(path, resource, options)
return resource.Fulfillment, err
}
// Create a new fulfillment
func (s *FulfillmentServiceOp) Create(fulfillment FulfillmentInfo) (*Fulfillment, error) {
prefix := FulfillmentPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s.json", prefix)
wrappedData := FulfillmentInfoResource{Fulfillment: &fulfillment}
resource := new(FulfillmentResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Fulfillment, err
}
// Update an existing fulfillment
func (s *FulfillmentServiceOp) Update(fulfillment Fulfillment) (*Fulfillment, error) {
prefix := FulfillmentPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/%d.json", prefix, fulfillment.ID)
wrappedData := FulfillmentResource{Fulfillment: &fulfillment}
resource := new(FulfillmentResource)
err := s.client.Put(path, wrappedData, resource)
return resource.Fulfillment, err
}
// Complete an existing fulfillment
func (s *FulfillmentServiceOp) Complete(fulfillmentID int64) (*Fulfillment, error) {
prefix := FulfillmentPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/%d/complete.json", prefix, fulfillmentID)
resource := new(FulfillmentResource)
err := s.client.Post(path, nil, resource)
return resource.Fulfillment, err
}
// Transition an existing fulfillment
func (s *FulfillmentServiceOp) Transition(fulfillmentID int64) (*Fulfillment, error) {
prefix := FulfillmentPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/%d/open.json", prefix, fulfillmentID)
resource := new(FulfillmentResource)
err := s.client.Post(path, nil, resource)
return resource.Fulfillment, err
}
// Cancel an existing fulfillment
func (s *FulfillmentServiceOp) Cancel(fulfillmentID int64) (*Fulfillment, error) {
prefix := FulfillmentPathPrefix(s.resource, s.resourceID)
path := fmt.Sprintf("%s/%d/cancel.json", prefix, fulfillmentID)
resource := new(FulfillmentResource)
err := s.client.Post(path, nil, resource)
return resource.Fulfillment, err
}