This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathexample_batch.js
187 lines (176 loc) · 5.88 KB
/
example_batch.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
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
/**
This example demonstrates how to create, retrieve, and purchase batch shipments
as well as adding and removing shipments from a batch
**/
// replace <YOUR_PRIVATE_KEY> with your ShippoToken key
var shippo = require('shippo')('<YOUR_PRIVATE_KEY>');
//Maximum time we'll wait for batch to clear
const BATCH_WAIT_TIMEOUT = 10000;
const POLLING_INTERVAL = 1000;
// example Batch object for batch shipment creation
var myBatch = {
"default_carrier_account": "<YOUR_USPS_ACCOUNT_OBJECT_ID>",
"default_servicelevel_token": "usps_priority",
"label_filetype": "PDF_4x6",
"metadata": "BATCH #170",
"batch_shipments": [
{
"shipment": {
"address_from": {
"name": "Mr Hippo",
"street1": "965 Mission St",
"street2": "Ste 201",
"city": "San Francisco",
"state": "CA",
"zip": "94103",
"country": "US",
"phone": "4151234567",
"email": "[email protected]"
},
"address_to": {
"name": "Mrs Hippo",
"company": "",
"street1": "Broadway 1",
"street2": "",
"city": "New York",
"state": "NY",
"zip": "10007",
"country": "US",
"phone": "4151234567",
"email": "[email protected]"
},
"parcels": [{
"length": "5",
"width": "5",
"height": "5",
"distance_unit": "in",
"weight": "2",
"mass_unit": "oz"
}]}
},
{
"shipment": {
"address_from": {
"name": "Mr Hippo",
"street1": "1092 Indian Summer Ct",
"city": "San Jose",
"state": "CA",
"zip": "95122",
"country": "US",
"phone": "4151234567",
"email": "[email protected]"
},
"address_to": {
"name": "Mrs Hippo",
"company": "",
"street1": "Broadway 1",
"street2": "",
"city": "New York",
"state": "NY",
"zip": "10007",
"country": "US",
"phone": "4151234567",
"email": "[email protected]"
},
"parcels": [{
"length": "5",
"width": "5",
"height": "5",
"distance_unit": "in",
"weight": "20",
"mass_unit": "lb"
}]
}
}
]
}
var addressFrom = {
"name":"Ms Hippo",
"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 = {
"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" : "Hippo T-Shirt Order #1043"
}
// parcel object dict
var parcel = {
"length":"5",
"width":"5",
"height":"5",
"distance_unit":"in",
"weight":"2",
"mass_unit":"lb",
}
var shipments = []
shippo.shipment.create({
"address_from": addressFrom,
"address_to": addressTo,
"parcels": [parcel],
"async": false
}).catch(function(shipmentErr) {
console.log("There was an error creating a shipment: %s", shipmentErr);
}).then(function(shipmentResponse) {
console.log("Shipment created with object id: %s", shipmentResponse.object_id);
shipments.push({"shipment" : shipmentResponse.object_id});
// example of creating a batch shipment
return shippo.batch.create(myBatch);
}).catch(function(createErr) {
console.log("There was an error creating the batch shipment: %s", createErr);
}).then(function(createResponse) {
console.log("Batch shipment creation response: %s", JSON.stringify(createResponse, null, 4));
//Poll a batch object to check for a VALID status before adding/removing shipments or purchasing
checkBatchStatus(createResponse.object_id);
});
//Using Batch::retrieve to poll for the batch's VALID status is done for demo purposes only
//In practice, it is recommended to register a Batch Create webhook for status updates
var timeout = 0;
function checkBatchStatus(object_id) {
shippo.batch.retrieve(object_id)
.catch(function(retrieveErr) {
console.log("There was an error retrieving the batch information: %s", retrieveErr);
}).then(function(response) {
if (response.status === "VALID") {
//Example of adding a shipment to a batch object
shippo.batch.add(response.object_id, shipments)
.catch(function(addErr) {
console.log("There was an error adding shipments to the batch: %s", addErr);
}).then(function(addResponse) {
console.log("Response from adding shipments to batch: %s", JSON.stringify(addResponse, null, 4));
//Example of removing a shipment from a batch object
return shippo.batch.remove(addResponse.object_id, [addResponse.batch_shipments.results[0].object_id]);
}).catch(function(removeErr) {
console.log("There was an error removing shipments from the batch: %s", removeErr);
}).then(function(removeResponse) {
console.log("Response from removing shipments from the batch: %s", JSON.stringify(removeResponse, null, 4));
//Example of purchasing a batch shipment
return shippo.batch.purchase(response.object_id);
}).catch(function(purchaseErr) {
console.log("There was an error purchasing the batch shipment: %s", purchaseErr);
}).then(function(purchaseResponse) {
console.log("Batch shipment purchase response: %s", JSON.stringify(purchaseResponse, null, 4));
});
} else if (timeout < BATCH_WAIT_TIMEOUT){
timeout = timeout + POLLING_INTERVAL;
setTimeout(checkBatchStatus, POLLING_INTERVAL, response.object_id);
} else {
console.log("Batch purchase timed out on batch %s", response.object_id);
}
});
}