1
|
|
|
package mollie |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"fmt" |
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(ctx context.Context, oID string, sID string) (res *Response, s *Shipment, err error) { |
46
|
1 |
|
u := fmt.Sprintf("v2/orders/%s/shipments/%s", oID, sID) |
47
|
|
|
|
48
|
1 |
|
res, err = ss.client.get(ctx, u, nil) |
49
|
1 |
|
if err != nil { |
50
|
1 |
|
return |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
54
|
1 |
|
return |
55
|
|
|
} |
56
|
1 |
|
return |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// CreateShipmentRequest defines information required to create a new shipment. |
60
|
|
|
type CreateShipmentRequest struct { |
61
|
|
|
Lines []OrderLine `json:"lines,omitempty"` |
62
|
|
|
Tracking ShipmentTracking `json:"tracking,omitempty"` |
63
|
|
|
TestMode bool `json:"testmode,omitempty"` |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Create can be used to ship order lines. |
67
|
|
|
// |
68
|
|
|
// See: https://docs.mollie.com/reference/v2/shipments-api/create-shipment |
69
|
|
|
func (ss *ShipmentsService) Create(ctx context.Context, oID string, cs CreateShipmentRequest) (res *Response, s *Shipment, err error) { |
70
|
1 |
|
uri := fmt.Sprintf("v2/orders/%s/shipments", oID) |
71
|
|
|
|
72
|
1 |
|
if ss.client.HasAccessToken() && ss.client.config.testing { |
73
|
1 |
|
cs.TestMode = true |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
res, err = ss.client.post(ctx, uri, cs, nil) |
77
|
1 |
|
if err != nil { |
78
|
1 |
|
return |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
82
|
1 |
|
return |
83
|
|
|
} |
84
|
1 |
|
return |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// ShipmentsList describes how a list of payments will be retrieved by Mollie. |
88
|
|
|
type ShipmentsList struct { |
89
|
|
|
Count int `json:"count,omitempty"` |
90
|
|
|
Embedded struct { |
91
|
|
|
Shipments []Shipment |
92
|
|
|
} `json:"_embedded,omitempty"` |
93
|
|
|
Links PaginationLinks `json:"_links,omitempty"` |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// List retrieves all shipments for an order. |
97
|
|
|
// |
98
|
|
|
// See: https://docs.mollie.com/reference/v2/shipments-api/list-shipments |
99
|
|
|
func (ss *ShipmentsService) List(ctx context.Context, oID string) (res *Response, sl *ShipmentsList, err error) { |
100
|
1 |
|
u := fmt.Sprintf("v2/orders/%s/shipments", oID) |
101
|
|
|
|
102
|
1 |
|
res, err = ss.client.get(ctx, u, nil) |
103
|
1 |
|
if err != nil { |
104
|
1 |
|
return |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
if err = json.Unmarshal(res.content, &sl); err != nil { |
108
|
1 |
|
return |
109
|
|
|
} |
110
|
1 |
|
return |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Update can be used to update the tracking information of a shipment |
114
|
|
|
// |
115
|
|
|
// See: https://docs.mollie.com/reference/v2/shipments-api/update-shipment |
116
|
|
|
func (ss *ShipmentsService) Update(ctx context.Context, oID string, sID string, st ShipmentTracking) (res *Response, s *Shipment, err error) { |
117
|
1 |
|
u := fmt.Sprintf("v2/orders/%s/shipments/%s", oID, sID) |
118
|
|
|
|
119
|
1 |
|
res, err = ss.client.patch(ctx, u, st, nil) |
120
|
1 |
|
if err != nil { |
121
|
1 |
|
return |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
if err = json.Unmarshal(res.content, &s); err != nil { |
125
|
1 |
|
return |
126
|
|
|
} |
127
|
1 |
|
return |
128
|
|
|
} |
129
|
|
|
|