|
| 1 | +package goshopify |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +// FulfillmentService is an interface for interfacing with the fulfillment endpoints |
| 9 | +// of the Shopify API. |
| 10 | +// https://help.shopify.com/api/reference/fulfillment |
| 11 | +type FulfillmentService interface { |
| 12 | + List(interface{}) ([]Fulfillment, error) |
| 13 | + Count(interface{}) (int, error) |
| 14 | + Get(int, interface{}) (*Fulfillment, error) |
| 15 | + Create(Fulfillment) (*Fulfillment, error) |
| 16 | + Update(Fulfillment) (*Fulfillment, error) |
| 17 | + Complete(int) (*Fulfillment, error) |
| 18 | + Transition(int) (*Fulfillment, error) |
| 19 | + Cancel(int) (*Fulfillment, error) |
| 20 | +} |
| 21 | + |
| 22 | +// FulfillmentsService is an interface for other Shopify resources |
| 23 | +// to interface with the fulfillment endpoints of the Shopify API. |
| 24 | +// https://help.shopify.com/api/reference/fulfillment |
| 25 | +type FulfillmentsService interface { |
| 26 | + ListFulfillments(int, interface{}) ([]Fulfillment, error) |
| 27 | + CountFulfillments(int, interface{}) (int, error) |
| 28 | + GetFulfillment(int, int, interface{}) (*Fulfillment, error) |
| 29 | + CreateFulfillment(int, Fulfillment) (*Fulfillment, error) |
| 30 | + UpdateFulfillment(int, Fulfillment) (*Fulfillment, error) |
| 31 | + CompleteFulfillment(int, int) (*Fulfillment, error) |
| 32 | + TransitionFulfillment(int, int) (*Fulfillment, error) |
| 33 | + CancelFulfillment(int, int) (*Fulfillment, error) |
| 34 | +} |
| 35 | + |
| 36 | +// FulfillmentServiceOp handles communication with the fulfillment |
| 37 | +// related methods of the Shopify API. |
| 38 | +type FulfillmentServiceOp struct { |
| 39 | + client *Client |
| 40 | + resource string |
| 41 | + resourceID int |
| 42 | +} |
| 43 | + |
| 44 | +// Fulfillment represents a Shopify fulfillment. |
| 45 | +type Fulfillment struct { |
| 46 | + ID int `json:"id,omitempty"` |
| 47 | + OrderID int `json:"order_id,omitempty"` |
| 48 | + LocationID int `json:"location_id,omitempty"` |
| 49 | + Status string `json:"status,omitempty"` |
| 50 | + CreatedAt *time.Time `json:"created_at,omitempty"` |
| 51 | + Service string `json:"service,omitempty"` |
| 52 | + UpdatedAt *time.Time `json:"updated_at,omitempty"` |
| 53 | + TrackingCompany string `json:"tracking_company,omitempty"` |
| 54 | + ShipmentStatus string `json:"shipment_status,omitempty"` |
| 55 | + TrackingNumber string `json:"tracking_number,omitempty"` |
| 56 | + TrackingNumbers []string `json:"tracking_numbers,omitempty"` |
| 57 | + TrackingUrl string `json:"tracking_url,omitempty"` |
| 58 | + TrackingUrls []string `json:"tracking_urls,omitempty"` |
| 59 | + Receipt Receipt `json:"receipt,omitempty"` |
| 60 | + LineItems []LineItem `json:"line_items,omitempty"` |
| 61 | + NotifyCustomer bool `json:"notify_customer,omitempty"` |
| 62 | +} |
| 63 | + |
| 64 | +// Receipt represents a Shopify receipt. |
| 65 | +type Receipt struct { |
| 66 | + TestCase bool `json:"testcase,omitempty"` |
| 67 | + Authorization string `json:"authorization,omitempty"` |
| 68 | +} |
| 69 | + |
| 70 | +// FulfillmentResource represents the result from the fulfillments/X.json endpoint |
| 71 | +type FulfillmentResource struct { |
| 72 | + Fulfillment *Fulfillment `json:"fulfillment"` |
| 73 | +} |
| 74 | + |
| 75 | +// FulfillmentsResource represents the result from the fullfilments.json endpoint |
| 76 | +type FulfillmentsResource struct { |
| 77 | + Fulfillments []Fulfillment `json:"fulfillments"` |
| 78 | +} |
| 79 | + |
| 80 | +// List fulfillments |
| 81 | +func (s *FulfillmentServiceOp) List(options interface{}) ([]Fulfillment, error) { |
| 82 | + prefix := FulfillmentPathPrefix(s.resource, s.resourceID) |
| 83 | + path := fmt.Sprintf("%s.json", prefix) |
| 84 | + resource := new(FulfillmentsResource) |
| 85 | + err := s.client.Get(path, resource, options) |
| 86 | + return resource.Fulfillments, err |
| 87 | +} |
| 88 | + |
| 89 | +// Count fulfillments |
| 90 | +func (s *FulfillmentServiceOp) Count(options interface{}) (int, error) { |
| 91 | + prefix := FulfillmentPathPrefix(s.resource, s.resourceID) |
| 92 | + path := fmt.Sprintf("%s/count.json", prefix) |
| 93 | + return s.client.Count(path, options) |
| 94 | +} |
| 95 | + |
| 96 | +// Get individual fulfillment |
| 97 | +func (s *FulfillmentServiceOp) Get(fulfillmentID int, options interface{}) (*Fulfillment, error) { |
| 98 | + prefix := FulfillmentPathPrefix(s.resource, s.resourceID) |
| 99 | + path := fmt.Sprintf("%s/%d.json", prefix, fulfillmentID) |
| 100 | + resource := new(FulfillmentResource) |
| 101 | + err := s.client.Get(path, resource, options) |
| 102 | + return resource.Fulfillment, err |
| 103 | +} |
| 104 | + |
| 105 | +// Create a new fulfillment |
| 106 | +func (s *FulfillmentServiceOp) Create(fulfillment Fulfillment) (*Fulfillment, error) { |
| 107 | + prefix := FulfillmentPathPrefix(s.resource, s.resourceID) |
| 108 | + path := fmt.Sprintf("%s.json", prefix) |
| 109 | + wrappedData := FulfillmentResource{Fulfillment: &fulfillment} |
| 110 | + resource := new(FulfillmentResource) |
| 111 | + err := s.client.Post(path, wrappedData, resource) |
| 112 | + return resource.Fulfillment, err |
| 113 | +} |
| 114 | + |
| 115 | +// Update an existing fulfillment |
| 116 | +func (s *FulfillmentServiceOp) Update(fulfillment Fulfillment) (*Fulfillment, error) { |
| 117 | + prefix := FulfillmentPathPrefix(s.resource, s.resourceID) |
| 118 | + path := fmt.Sprintf("%s/%d.json", prefix, fulfillment.ID) |
| 119 | + wrappedData := FulfillmentResource{Fulfillment: &fulfillment} |
| 120 | + resource := new(FulfillmentResource) |
| 121 | + err := s.client.Put(path, wrappedData, resource) |
| 122 | + return resource.Fulfillment, err |
| 123 | +} |
| 124 | + |
| 125 | +// Complete an existing fulfillment |
| 126 | +func (s *FulfillmentServiceOp) Complete(fulfillmentID int) (*Fulfillment, error) { |
| 127 | + prefix := FulfillmentPathPrefix(s.resource, s.resourceID) |
| 128 | + path := fmt.Sprintf("%s/%d/complete.json", prefix, fulfillmentID) |
| 129 | + resource := new(FulfillmentResource) |
| 130 | + err := s.client.Post(path, nil, resource) |
| 131 | + return resource.Fulfillment, err |
| 132 | +} |
| 133 | + |
| 134 | +// Transition an existing fulfillment |
| 135 | +func (s *FulfillmentServiceOp) Transition(fulfillmentID int) (*Fulfillment, error) { |
| 136 | + prefix := FulfillmentPathPrefix(s.resource, s.resourceID) |
| 137 | + path := fmt.Sprintf("%s/%d/open.json", prefix, fulfillmentID) |
| 138 | + resource := new(FulfillmentResource) |
| 139 | + err := s.client.Post(path, nil, resource) |
| 140 | + return resource.Fulfillment, err |
| 141 | +} |
| 142 | + |
| 143 | +// Cancel an existing fulfillment |
| 144 | +func (s *FulfillmentServiceOp) Cancel(fulfillmentID int) (*Fulfillment, error) { |
| 145 | + prefix := FulfillmentPathPrefix(s.resource, s.resourceID) |
| 146 | + path := fmt.Sprintf("%s/%d/cancel.json", prefix, fulfillmentID) |
| 147 | + resource := new(FulfillmentResource) |
| 148 | + err := s.client.Post(path, nil, resource) |
| 149 | + return resource.Fulfillment, err |
| 150 | +} |
0 commit comments