public static function getDefaultShipment() { parent::setTestApiKey(); $addressFrom = Shippo_AddressTest::getDefaultAddress(); $addressTo = Shippo_AddressTest::getDefaultAddress(); $parcel = Shippo_ParcelTest::getDefaultParcel(); return Shippo_Shipment::create(array('object_purpose' => 'QUOTE', 'address_from' => $addressFrom->object_id, 'address_to' => $addressTo->object_id, 'parcel' => $parcel->object_id, 'submission_type' => 'PICKUP', 'submission_date' => '2013-12-03T12:00:00.000Z', 'insurance_amount' => '30', 'insurance_currency' => 'USD', 'extra' => '{signature_confirmation: true}', 'customs_declaration' => '', 'reference_1' => '', 'reference_2' => '', 'metadata' => 'Customer ID 123456')); }
public static function getDefaultRate() { parent::setTestApiKey(); $shipment = Shippo_ShipmentTest::getDefaultShipment(); try { Shippo_Shipment::get_shipping_rates(array('id' => $shipment->object_id, 'currency' => 'USD')); } catch (Exception $e) { // Expected Exception, rates not ready, prompting to server to generate } sleep(5); return Shippo_Shipment::get_shipping_rates(array('id' => $shipment->object_id, 'currency' => 'USD')); }
$customs_item = array('description' => 'T-Shirt', 'quantity' => '2', 'net_weight' => '1', 'mass_unit' => 'lb', 'value_amount' => '20', 'value_currency' => 'USD', 'origin_country' => 'US'); #Creating the CustomsDeclaration #(CustomsDeclarations are only required for international shipments) $customs_declaration = Shippo_CustomsDeclaration::create(array('contents_type' => 'MERCHANDISE', 'contents_explanation' => 'T-Shirt purchase', 'non_delivery_option' => 'RETURN', 'certify' => 'true', 'certify_signer' => 'Laura Behrens Wu', 'items' => array($customs_item))); //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. $shipment = Shippo_Shipment::create(array('object_purpose' => 'PURCHASE', 'address_from' => $fromAddress, 'address_to' => $toAddress, 'parcel' => $parcel, 'submission_type' => 'PICKUP', 'insurance_amount' => '30', 'insurance_currency' => 'USD', 'extra' => '{signature_confirmation: true}', 'customs_declaration' => $customs_declaration["object_id"])); //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"])); //Get the first rate in the rates results. $rate = $rates["results"][0]; //Purchase the desired rate. sync=True indicates that the function will wait until the //carrier returns a shipping label before it returns $transaction = Shippo_Transaction::create(array('rate' => $rate["object_id"])); //Wait for transaction to be proccessed $attempts = 0; while (($transaction["object_status"] == "QUEUED" || $transaction["object_status"] == "WAITING") && $attempts < 10) { $transaction = Shippo_Transaction::retrieve($transaction["object_id"]); $attempts += 1; } //label_url and tracking_number if ($transaction["object_status"] == "SUCCESS") { echo $transaction["label_url"]; echo "\n";