I almost got it working perfectly, except for generating the labels.
I have this code to generate the rates, which works great:
//Wait for rates to be generated
$attempts = 0;
while (($shipment["object_status"] == "QUEUED" || $shipment["object_status"] == "WAITING") && $attempts < 10)
{
$shipment = Shippo_Shipment::retrieve($shipment["object_id"]);
$attempts +=1;
}
//Get all rates for shipment.
$rates = Shippo_Shipment::get_shipping_rates(array('id'=> $shipment["object_id"]));
$json = json_decode($rates, true);
foreach ($json["results"] as $key)
{
$amount = $key["amount"];
$servicelevel = $key["servicelevel_name"];
$objid = $key["object_id"];
}
As I go through each of the results, I assign them to variables for the different service levels and allow the user to select which shipping method they want to use. I pass the $objid
to the next page to produce the label using the following code:
//Write the object_id to a variable
$var = $shiparray[1];
$transaction = Shippo_Transaction::create(array('rate'=>$var));
echo $transaction["object_status"] ."<br>";
// Wait for carrier to create shipping label
$attempts = 0;
while (($transaction["object_status"] == "QUEUED" || $transaction["object_status"] == "WAITING") && $attempts < 10)
{
$transaction = Shippo_Transaction::retrieve($transaction["object_id"]);
$attempts += 1;
}
echo $transaction["object_status"] ."<br>";
// Retrieve label url and tracking number or error message
if ($transaction["object_status"] == "SUCCESS")
{
echo($transaction["label_url"]);
echo("\n");
echo($transaction["tracking_number"]);
}
else
{
echo( $transaction["messages"] );
}
This just produces an error, though. Am I passing the wrong value to produce the label? Should I be using a value produced for the shipment rather then the rate?