1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"encoding/json" |
5
|
|
|
"fmt" |
6
|
|
|
"net/http" |
7
|
|
|
"time" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// ShipmentsService operates on shipments endpoints. |
11
|
|
|
type ShipmentsService service |
12
|
|
|
|
13
|
|
|
// Shipment contains information about a user service/product delivery and |
14
|
|
|
// is used in the figurative sense here. |
15
|
|
|
// It can also mean that a service was provided or digital content was delivered. |
16
|
|
|
type Shipment struct { |
17
|
|
|
Resource string `json:"resource,omitempty"` |
18
|
|
|
ID string `json:"id,omitempty"` |
19
|
|
|
OrderID string `json:"orderId,omitempty"` |
20
|
|
|
TestMode bool `json:"testmode,omitempty"` |
21
|
|
|
CreatedAt *time.Time `json:"createdAt,omitempty"` |
22
|
|
|
Tracking *ShipmentTracking `json:"tracking,omitempty"` |
23
|
|
|
Lines []*OrderLine `json:"lines,omitempty"` |
24
|
|
|
Links ShipmentLinks `json:"_links,omitempty"` |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// ShipmentTracking contains shipment tracking details. |
28
|
|
|
type ShipmentTracking struct { |
29
|
|
|
Carrier string `json:"carrier,omitempty"` |
30
|
|
|
Code string `json:"code,omitempty"` |
31
|
|
|
URL string `json:"url,omitempty"` |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// ShipmentLinks contains URL objects with shipment relevant |
35
|
|
|
// information for the user. |
36
|
|
|
type ShipmentLinks struct { |
37
|
|
|
Self *URL `json:"self,omitempty"` |
38
|
|
|
Order *URL `json:"order,omitempty"` |
39
|
|
|
Documentation *URL `json:"documentation,omitempty"` |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Get retrieves a single shipment and the order lines shipped by a shipment’s ID. |
43
|
|
|
// |
44
|
|
|
// See: https://docs.mollie.com/reference/v2/shipments-api/get-shipment# |
45
|
|
|
func (ss *ShipmentsService) Get(oID string, sID string) (s *Shipment, err error) { |
46
|
1 |
|
u := fmt.Sprintf("v2/orders/%s/shipments/%s", oID, sID) |
47
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodGet, u, nil) |
48
|
1 |
|
if err != nil { |
49
|
1 |
|
return |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
res, err := ss.client.Do(req) |
53
|
1 |
|
if err != nil { |
54
|
1 |
|
return |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
58
|
1 |
|
return |
59
|
|
|
} |
60
|
1 |
|
return |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// CreateShipmentRequest defines information required to create a new shipment |
64
|
|
|
type CreateShipmentRequest struct { |
65
|
|
|
Lines []OrderLine `json:"lines,omitempty"` |
66
|
|
|
Tracking ShipmentTracking `json:"tracking,omitempty"` |
67
|
|
|
TestMode bool `json:"testmode,omitempty"` |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Create can be used to ship order lines. |
71
|
|
|
// |
72
|
|
|
// See: https://docs.mollie.com/reference/v2/shipments-api/create-shipment |
73
|
|
|
func (ss *ShipmentsService) Create(oID string, cs CreateShipmentRequest) (s *Shipment, err error) { |
74
|
1 |
|
u := fmt.Sprintf("v2/orders/%s/shipments", oID) |
75
|
|
|
|
76
|
1 |
|
if ss.client.HasAccessToken() && ss.client.config.testing { |
77
|
1 |
|
cs.TestMode = true |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodPost, u, cs) |
81
|
1 |
|
if err != nil { |
82
|
1 |
|
return |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
res, err := ss.client.Do(req) |
86
|
1 |
|
if err != nil { |
87
|
1 |
|
return |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
91
|
1 |
|
return |
92
|
|
|
} |
93
|
1 |
|
return |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// ShipmentsList describes how a list of payments will be retrieved by Mollie. |
97
|
|
|
type ShipmentsList struct { |
98
|
|
|
Count int `json:"count,omitempty"` |
99
|
|
|
Embedded struct { |
100
|
|
|
Shipments []Shipment |
101
|
|
|
} `json:"_embedded,omitempty"` |
102
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// List retrieves all shipments for an order. |
106
|
|
|
// |
107
|
|
|
// See: https://docs.mollie.com/reference/v2/shipments-api/list-shipments |
108
|
|
|
func (ss *ShipmentsService) List(oID string) (sl *ShipmentsList, err error) { |
109
|
1 |
|
u := fmt.Sprintf("v2/orders/%s/shipments", oID) |
110
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodGet, u, nil) |
111
|
1 |
|
if err != nil { |
112
|
1 |
|
return |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
res, err := ss.client.Do(req) |
116
|
1 |
|
if err != nil { |
117
|
1 |
|
return |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
121
|
1 |
|
return |
122
|
|
|
} |
123
|
1 |
|
return |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Update can be used to update the tracking information of a shipment |
127
|
|
|
// |
128
|
|
|
// See: https://docs.mollie.com/reference/v2/shipments-api/update-shipment |
129
|
|
|
func (ss *ShipmentsService) Update(oID string, sID string, st ShipmentTracking) (s *Shipment, err error) { |
130
|
1 |
|
u := fmt.Sprintf("v2/orders/%s/shipments/%s", oID, sID) |
131
|
1 |
|
req, err := ss.client.NewAPIRequest(http.MethodPatch, u, st) |
132
|
1 |
|
if err != nil { |
133
|
1 |
|
return |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
res, err := ss.client.Do(req) |
137
|
1 |
|
if err != nil { |
138
|
1 |
|
return |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
142
|
1 |
|
return |
143
|
|
|
} |
144
|
1 |
|
return |
145
|
|
|
} |
146
|
|
|
|