-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathbasic-batch-shipment.php
217 lines (203 loc) · 7.93 KB
/
basic-batch-shipment.php
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
<?php
/*
In this tutorial we will create a batch shipment,
add and remove shipments from the batch, and purchase the batch shipment.
Sample output:
--> Batch created with id: 6590eecf10f04c79a41bc6ffc6406b6f
--> Shipment created with id: a881b47ee9b04089b543588051ad49b5
--> Batch object 6590eecf10f04c79a41bc6ffc6406b6f has status VALIDATING
Waiting for the batch to validate
--> Batch object 6590eecf10f04c79a41bc6ffc6406b6f has status VALID
--> batch now contains shipments: {
"next": null,
"previous": null,
"results": [
{
"metadata": "",
"carrier_account": null,
"servicelevel_token": null,
"shipment": "188f0891ee3e44888b577985c3cafb0f",
"transaction": null,
"object_id": "8940f9546c7040f58d84465dad496051",
"status": "VALID",
"messages": []
},
{
"metadata": "",
"carrier_account": null,
"servicelevel_token": null,
"shipment": "a881b47ee9b04089b543588051ad49b5",
"transaction": null,
"object_id": "ecd6e8248ac54ec9821fb25e3398f815",
"status": "VALID",
"messages": []
}
]
}
--> batch now contains shipments: {
"next": null,
"previous": null,
"results": [
{
"metadata": "",
"carrier_account": null,
"servicelevel_token": null,
"shipment": "a881b47ee9b04089b543588051ad49b5",
"transaction": null,
"object_id": "ecd6e8248ac54ec9821fb25e3398f815",
"status": "VALID",
"messages": []
}
]
}
--> Batch object 6590eecf10f04c79a41bc6ffc6406b6f has status PURCHASING
Before running it, remember to do
composer install
*/
require_once(__DIR__ . '../../vendor/autoload.php');
// or if you do not have or want the composer autoload feature do
// require_once('path/to/shippo/library/folder/' . 'lib/Shippo.php');
// Replace <API-KEY> with your credentials from https://app.goshippo.com/api/
Shippo::setApiKey('<API-KEY>');
//Example data to create a batch shipment
//The complete reference to batch shipment creation is available here: https://goshippo.com/docs/reference#batches-create
$carrier = '<YOUR CARRIER ACCOUNT PRIVATE KEY>';
$data = array(
'default_carrier_account' => $carrier,
'default_servicelevel_token' => 'usps_priority',
'label_filetype' => 'PDF_4x6',
'metadata' => '',
'batch_shipments' => array(
array(
'shipment' => array(
'address_from' => array(
'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' => array(
'name' => 'Mrs Hippo',
'company' => '',
'street1' => 'Broadway 1',
'street2' => '',
'city' => 'New York',
'state' => 'NY',
'zip' => '10007',
'country' => 'US',
'phone' => '4151234567',
'email' => '[email protected]'
),
'parcels' => array(
array(
'length' => '5',
'width' => '5',
'height' => '5',
'distance_unit' => 'in',
'weight' => '2',
'mass_unit' => 'oz'
)
)
)
)
)
);
//Example of batch shipment creation
$batch = Shippo_Batch::create($data);
echo "--> " . "Batch created with id: " . $batch['object_id'] . "\n";
//Now we create a shipment to add to the newly created batch
// Example from_address array
// The complete refence for the address object is available here: https://goshippo.com/docs/reference#addresses
$from_address = array(
'name' => 'Mr Hippo',
'company' => 'Shippo',
'street1' => '215 Clayton St.',
'city' => 'San Francisco',
'state' => 'CA',
'zip' => '94117',
'country' => 'US',
'phone' => '+1 555 341 9393',
'email' => '[email protected]',
);
// Example to_address array
// The complete refence for the address object is available here: https://goshippo.com/docs/reference#addresses
$to_address = array(
'name' => 'Ms Hippo',
'company' => 'San Diego Zoo',
'street1' => '2920 Zoo Drive',
'city' => 'San Diego',
'state' => 'CA',
'zip' => '92101',
'country' => 'US',
'phone' => '+1 555 341 9393',
'email' => '[email protected]',
);
// Parcel information array
// The complete reference for parcel object is here: https://goshippo.com/docs/reference#parcels
$parcel = array(
'length'=> '5',
'width'=> '5',
'height'=> '5',
'distance_unit'=> 'in',
'weight'=> '2',
'mass_unit'=> 'lb',
);
// Example shipment object
// For complete reference to the shipment object: https://goshippo.com/docs/reference#shipments
// This object has async=false, indicating that the function will wait until all rates are generated before it returns.
// By default, Shippo handles responses asynchronously. However this will be depreciated soon. Learn more: https://goshippo.com/docs/async
$shipment = Shippo_Shipment::create(
array(
'address_from'=> $from_address,
'address_to'=> $to_address,
'parcels'=> array($parcel),
'async'=> false,
));
echo "--> " . "Shipment created with id: " . $shipment['object_id'] . "\n";
//Example of retrieving a batch object to check its validation status
//For complete reference to the retrieve endpoint: https://goshippo.com/docs/reference#batches-retrieve
//This method of polling the batch validation status is for demo purposes only
//In practice it is advised to use the batch-create webhook in the user api dashboard: https://app.goshippo.com/api/
$MAX_TIMEOUT = 10;
$counter = 0;
while ($counter < $MAX_TIMEOUT) {
$retrieved_batch = Shippo_Batch::retrieve($batch['object_id']);
if ($retrieved_batch['status'] == 'VALID') {
break;
} else {
$counter = $counter + 1;
sleep(1);
}
}
$retrieved_batch2 = Shippo_Batch::retrieve($batch['object_id']);
if ($retrieved_batch2['status'] == 'VALID') {
echo "--> " . "Batch object " . $retrieved_batch2['object_id'] . " has status " . $retrieved_batch2['status'] . "\n";
//example shipment to add to the batch
$shipments_to_add = array(
array('shipment' => $shipment['object_id'])
);
//add an array of shipment objects to the batch
//For complete reference to the batch-add endpoint: https://goshippo.com/docs/reference#batches-add-shipments
$added_batch = Shippo_Batch::add($batch['object_id'], $shipments_to_add);
echo "--> " . "batch now contains shipments: " . $added_batch['batch_shipments'] . "\n";
//example shipment to remove from the batch
$shipments_to_remove = array(
$added_batch['batch_shipments']['results'][0]
);
//remove an array of shipment objects from the batch
//For complete reference to the batch-remove endpoint: https://goshippo.com/docs/reference#batches-remove-shipments
$removed_batch = Shippo_Batch::remove($batch['object_id'], $shipments_to_remove);
echo "--> " . "batch now contains shipments: " . $removed_batch['batch_shipments'] . "\n";
//purchase the batch shipment
//For complete reference to the batch-purchase endpoint: https://goshippo.com/docs/reference#batches-purchase
$purchased_batch = Shippo_Batch::purchase($removed_batch['object_id']);
echo "--> " . "Batch object " . $purchased_batch['object_id'] . " has status " . $purchased_batch['status'] . "\n";
} else {
echo 'Batch shipment validation timed out' . "\n";
}
?>