forked from goshippo/shippo-node-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
139 lines (127 loc) · 4.25 KB
/
example.js
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
/**
This example demonstrates how to purchase a label for an international shipment.
Creating domestic shipment would follow a similiar proccess but would not require
the creation of CustomsItems and CustomsDeclaration objects.
**/
// replace <YOUR_PRIVATE_KEY> with your ShippoToken key
var shippo = require('shippo')('<YOUR_PRIVATE_KEY>');
var addressFrom = {
"object_purpose":"PURCHASE",
"name":"Laura Behrens Wu",
"company":"Shippo",
"street1":"215 Clayton St.",
"city":"San Francisco",
"state":"CA",
"zip":"94117",
"country":"US", //iso2 country code
"phone":"+1 555 341 9393",
"email":"[email protected]",
}
// example address_to object dict
var addressTo = {
"object_purpose":"PURCHASE",
"name":"Mr Hippo",
"company":"London Zoo",
"street1":"Regent's Park",
"street2":"Outer Cir",
"city":"LONDON",
"state":"",
"zip":"NW1 4RY",
"country":"GB", //iso2 country code
"phone":"+1 555 341 9393",
"email":"[email protected]",
"metadata" : "Hippos dont lie"
}
// parcel object dict
var parcel = {
"length":"5",
"width":"5",
"height":"5",
"distance_unit":"in",
"weight":"2",
"mass_unit":"lb",
}
// example CustomsItems object. This is required for int'l shipment only.
var customsItem = {
"description":"T-Shirt",
"quantity":2,
"net_weight":"0.3",
"mass_unit":"lb",
"value_amount":"20",
"value_currency":"USD",
"origin_country":"US",
}
// Creating the CustomsDeclaration
// (CustomsDeclaration are NOT required for domestic shipments)
shippo.customsdeclaration.create({
"contents_type": "MERCHANDISE",
"contents_explanation": "T-Shirt purchase",
"non_delivery_option": "RETURN",
"certify": true,
"certify_signer": "Laura Behrens Wu",
"items": [customsItem],
}).then(function(customs_declaration){
console.log("customs Declaration : %s", JSON.stringify(customs_declaration, null, 4));
// Creating the shipment object. In this example, the objects are directly passed to the
// shipment.create method, Alternatively, the Address and Parcel objects could be created
// using address.create(..) and parcel.create(..) functions respectively.
return shippo.shipment.create({
"object_purpose": "PURCHASE",
"address_from": addressFrom,
"address_to": addressTo,
"parcel": parcel,
"submission_type": "DROPOFF",
"customs_declaration": customs_declaration.object_id
})
}, function(err) {
// Deal with an error
console.log("There was an error creating customs declaration: %s", err);
}).then(function(shipment){
console.log("shipment : %s", JSON.stringify(shipment, null, 4));
//Wait for rates to be generated
function ratesReady(shipment,attempts){
if ((shipment.object_status == "QUEUED" || shipment.object_status == "WAITING") && attempts < 10){
shippo.shipment.retrieve(shipment.object_id).then(function(val){
ratesReady(val,attempts+1);
});
}else{
shippo.shipment.rates(shipment.object_id).then(function(rates){
console.log("rates : %s", JSON.stringify(rates, null, 4));
// Get the first rate in the rates results for demo purposes.
rate = rates.results[0]
// Purchase the desired rate.
return shippo.transaction.create({"rate": rate.object_id})
}, function(err) {
// Deal with an error
console.log("There was an error retrieving rates : %s", err);
}).then(function(transaction){
console.log("transaction : %s", JSON.stringify(transaction, null, 4));
//Wait for transaction to be proccessed
transactionReady(transaction,0);
}, function(err) {
// Deal with an error
console.log("There was an error creating transaction : %s", err);
});
}
}
//Wait for transaction to be proccessed
function transactionReady(transaction,attempts){
if ((transaction.object_status == "QUEUED" || transaction.object_status == "WAITING") && attempts < 10){
shippo.transaction.retrieve(transaction.object_id).then(function(val){
transactionReady(val,attempts+1);
});
}else{
// print label_url and tracking_number
if(transaction.object_status == "SUCCESS"){
console.log("Label URL: %s",transaction.label_url);
console.log("Tracking Number: %s",transaction.tracking_number);
}else{
console.log("Message: %s",transaction.messages);
}
}
}
ratesReady(shipment,0);
},function(err){
// Deal with an error
console.log("There was an error creating shipment: %s", err);
});