-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathparcel.go
348 lines (315 loc) · 12.2 KB
/
parcel.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package sendcloud
import (
"encoding/json"
"time"
)
type LabelData []byte
type CustomsShipmentType int
const (
CustomsShipmentTypeGift CustomsShipmentType = iota
CustomsShipmentTypeDocuments
CustomsShipmentTypeCommercialGoods
CustomsShipmentTypeCommercialSample
CustomsShipmentTypeReturnedGoods
)
type ParcelParams struct {
Name string
CompanyName string
Street string
HouseNumber string
City string
PostalCode string
State string
CountryCode string
AdditionalInfo string
IsLabelRequested bool
Method int64
EmailAddress string
PhoneNumber string
ExternalID string
ToServicePointID int64
Weight string
OrderNumber string
SenderID int64
Items []CreateParcelItemRequest
// The currency of the total order value. Validated against a format of
// “XYZ” (ISO 4217).
TotalOrderValueCurrency *string
// The value paid by the buyer (via various payment methods supported by the
// shop(cash on delivery, pre-paid or post-paid), it will also be used for
// the cash on delivery amount for example “99.99”.
TotalOrderValue *string
// Shipping method name selected by buyer during the checkout
ShippingMethodCheckoutName *string
// Customs invoice number
CustomsInvoiceNr *string
// Customs shipment type
CustomsShipmentType *CustomsShipmentType
// When set to true configured shipping rules will be applied before creating the label and announcing the Parcel
ApplyShippingRules *bool
}
type CreateParcelItemRequest struct {
// Harmonized System Code Wikipedia Link. Providing a complete HS code with 8 characters increases the delivery rate.
HsCode string `json:"hs_code"`
// Weight of a single item in kilograms.
Weight string `json:"weight"`
// Quantity of items shipped.
Quantity int `json:"quantity"`
// Description of the item.
Description string `json:"description"`
// ISO-2 code of the country where the items were originally produced. External Link.
OriginCountry string `json:"origin_country,omitempty"`
// Value of a single item.
Value float64 `json:"value"`
// The SKU of the product.
SKU string `json:"sku,omitempty"`
// External ID of the item generated by a shop system or similar.
ItemId string `json:"item_id,omitempty"`
// The list of properties of the product. Used as a JSON object with {‘key’: ‘value’}.
Properties map[string]interface{} `json:"properties,omitempty"`
}
type CreateParcelShipmentRequest struct {
ID int64 `json:"id"`
}
type Parcel struct {
ID int64 `json:"id"`
ExternalID *string `json:"external_id"`
Name string `json:"name"`
CompanyName string `json:"company_name"`
Email string `json:"email"`
Street string `json:"street"`
HouseNumber string `json:"house_number"`
Address string `json:"address"`
Address2 string `json:"address_2"`
City string `json:"city"`
PostalCode string `json:"postal_code"`
CountryCode string `json:"country_code"`
Method int64 `json:"method"`
PhoneNumber *string `json:"phone_number"`
TrackingNumber string `json:"tracking_number"`
TrackingUrl string `json:"tracking_url"`
ServicePointID *int64 `json:"to_service_point"`
Weight string `json:"weight"`
Label string `json:"label"`
OrderNumber string `json:"order_number"`
IsReturn bool `json:"is_return"`
Note *string `json:"note"`
CarrierCode string `json:"carrier"`
Data interface{} `json:"data"`
CreatedAt time.Time `json:"created_at"`
}
type ParcelRequestContainer struct {
Parcel ParcelRequest `json:"parcel"`
}
type ParcelRequest struct {
Name string `json:"name"`
CompanyName string `json:"company_name"`
Address string `json:"address"`
Address2 string `json:"address_2"`
HouseNumber string `json:"house_number"`
City string `json:"city"`
PostalCode string `json:"postal_code"`
CountryState string `json:"country_state"`
Country string `json:"country"`
Weight string `json:"weight,omitempty"`
Telephone string `json:"telephone"`
Email string `json:"email"`
RequestLabel bool `json:"request_label"`
ToServicePointID *int64 `json:"to_service_point,omitempty"`
OrderNumber string `json:"order_number"`
ExternalID *string `json:"external_reference,omitempty"`
SenderID *int64 `json:"sender_address,omitempty"`
Shipment *CreateParcelShipmentRequest `json:"shipment,omitempty"`
Items []CreateParcelItemRequest `json:"parcel_items,omitempty"`
TotalOrderValueCurrency *string `json:"total_order_value_currency,omitempty"`
TotalOrderValue *string `json:"total_order_value,omitempty"`
ShippingMethodCheckoutName *string `json:"shipping_method_checkout_name,omitempty"`
CustomsInvoiceNr *string `json:"customs_invoice_nr,omitempty"`
CustomsShipmentType *CustomsShipmentType `json:"customs_shipment_type,omitempty"`
ApplyShippingRules *bool `json:"apply_shipping_rules,omitempty"`
}
type LabelResponseContainer struct {
Label LabelResponse `json:"label"`
}
type ParcelResponseContainer struct {
Parcel ParcelResponse `json:"parcel"`
}
type ParcelListResponseContainer struct {
Parcels []*ParcelResponse `json:"parcel"`
}
type LabelResponse struct {
NormalPrinter []string `json:"normal_printer"`
LabelPrinter string `json:"label_printer"`
}
type ParcelResponse struct {
ID int64 `json:"id"`
Address string `json:"address"`
Address2 string `json:"address_2"`
AddressDivided AddressDivided `json:"address_divided"`
City string `json:"city"`
CompanyName string `json:"company_name"`
Country CountryResponse `json:"country"`
Data interface{} `json:"data"`
DateCreated string `json:"date_created"`
Email string `json:"email"`
Name string `json:"name"`
PostalCode string `json:"postal_code"`
Reference string `json:"reference"`
Shipment Shipment `json:"shipment"`
Status Status `json:"status"`
ToServicePointID *int64 `json:"to_service_point"`
Telephone *string `json:"telephone"`
TrackingNumber string `json:"tracking_number"`
TrackingUrl string `json:"tracking_url"`
Weight string `json:"weight"`
Label LabelResponse `json:"label"`
OrderNumber string `json:"order_number"`
InsuredValue int64 `json:"insured_value"`
TotalInsuredValue int64 `json:"total_insured_value"`
ToState interface{} `json:"to_state"`
CustomsInvoiceNr string `json:"customs_invoice_nr"`
CustomsShipmentType interface{} `json:"customs_shipment_type"`
Type interface{} `json:"type"`
ShipmentUUID *string `json:"shipment_uuid"`
ShippingMethod int64 `json:"shipping_method"`
ExternalOrderID *string `json:"external_order_id"`
ExternalShipmentID *string `json:"external_shipment_id"`
ExternalReference *string `json:"external_reference"`
IsReturn bool `json:"is_return"`
Note *string `json:"note"`
Carrier Carrier `json:"carrier"`
}
type Carrier struct {
Code string `json:"code"`
}
type AddressDivided struct {
Street string `json:"street"`
HouseNumber string `json:"house_number"`
}
type Shipment struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
type Status struct {
ID int `json:"id"`
Message string `json:"message"`
}
// Translate the params into an actual request body
func (p *ParcelParams) GetPayload() interface{} {
parcel := ParcelRequest{
Name: p.Name,
CompanyName: p.CompanyName,
Address: p.Street,
Address2: p.AdditionalInfo,
HouseNumber: p.HouseNumber,
City: p.City,
PostalCode: p.PostalCode,
CountryState: p.State,
Country: p.CountryCode,
Telephone: p.PhoneNumber,
Email: p.EmailAddress,
RequestLabel: p.IsLabelRequested,
Items: p.Items,
TotalOrderValueCurrency: p.TotalOrderValueCurrency,
TotalOrderValue: p.TotalOrderValue,
ShippingMethodCheckoutName: p.ShippingMethodCheckoutName,
CustomsInvoiceNr: p.CustomsInvoiceNr,
CustomsShipmentType: p.CustomsShipmentType,
ApplyShippingRules: p.ApplyShippingRules,
}
if p.Method != 0 {
parcel.Shipment = &CreateParcelShipmentRequest{ID: p.Method}
}
if p.SenderID != 0 {
parcel.SenderID = &p.SenderID
}
if p.ExternalID != "" {
parcel.ExternalID = &p.ExternalID
}
if p.OrderNumber != "" {
parcel.OrderNumber = p.OrderNumber
}
if p.ToServicePointID != 0 {
parcel.ToServicePointID = &p.ToServicePointID
}
if p.Weight != "" {
parcel.Weight = p.Weight
}
ar := ParcelRequestContainer{Parcel: parcel}
return ar
}
// Handle the response and return it as a Parcel{}
func (p *ParcelResponseContainer) GetResponse() interface{} {
parcel := Parcel{
ID: p.Parcel.ID,
ExternalID: p.Parcel.ExternalReference,
Name: p.Parcel.Name,
CompanyName: p.Parcel.CompanyName,
Email: p.Parcel.Email,
Street: p.Parcel.AddressDivided.Street,
HouseNumber: p.Parcel.AddressDivided.HouseNumber,
Address: p.Parcel.Address,
Address2: p.Parcel.Address2,
City: p.Parcel.City,
Method: p.Parcel.Shipment.ID,
PostalCode: p.Parcel.PostalCode,
CountryCode: p.Parcel.Country.Iso2,
PhoneNumber: p.Parcel.Telephone,
TrackingNumber: p.Parcel.TrackingNumber,
TrackingUrl: p.Parcel.TrackingUrl,
ServicePointID: p.Parcel.ToServicePointID,
Label: p.Parcel.Label.LabelPrinter,
OrderNumber: p.Parcel.OrderNumber,
IsReturn: p.Parcel.IsReturn,
Note: p.Parcel.Note,
CarrierCode: p.Parcel.Carrier.Code,
Data: p.Parcel.Data,
Weight: p.Parcel.Weight,
}
layout := "02-01-2006 15:04:05"
createdAt, _ := time.Parse(layout, p.Parcel.DateCreated)
parcel.CreatedAt = createdAt
return &parcel
}
// Set the response
func (p *ParcelResponseContainer) SetResponse(body []byte) error {
err := json.Unmarshal(body, &p)
if err != nil {
return err
}
return nil
}
// Get formatted response
func (l LabelData) GetResponse() interface{} {
return l
}
// Set the response
func (l *LabelData) SetResponse(body []byte) error {
*l = body
return nil
}
// DocumentFormat is any of the formats a Document can be in.
type DocumentFormat string
func (df DocumentFormat) String() string { return string(df) }
func (df DocumentFormat) Name() string {
switch df {
case DocumentPdf:
return "pdf"
case DocumentZpl:
return "zpl"
case DocumentPng:
return "png"
default:
return "unknown"
}
}
const (
DocumentPdf DocumentFormat = "application/pdf"
DocumentZpl DocumentFormat = "application/zpl"
DocumentPng DocumentFormat = "image/png"
)
// Document represents a document file that can be downloaded from the api.
type Document struct {
Format DocumentFormat
Body []byte
}