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