function testApiKey() { Postmaster::setApiKey("example-api-key"); $result = Postmaster::getApiKey(); $expected = "example-api-key"; $this->assertEquals($expected, $result); }
protected function setUp() { Postmaster::setApiKey(getenv('PM_API_KEY')); if (getenv('PM_API_HOST')) { Postmaster::$apiBase = getenv('PM_API_HOST'); } else { Postmaster::$apiBase = 'https://api.postmaster.io'; } }
function testNoAuthError() { Postmaster::setApiKey(''); try { $result = Postmaster_AddressValidation::validate(array()); } catch (Authentication_Error $expected) { $msg = $expected->getMessage(); $this->assertStringStartsWith('You must authorize', $msg); return; } $this->fail('An expected exception has not been raised.'); }
public function request($meth, $url, $params = null, $headers = null) { $absUrl = self::apiUrl($url); $apiKey = Postmaster::getApiKey(); if (!$params) { $params = array(); } $ua = array('bindings_version' => Postmaster::VERSION, 'lang' => 'php', 'lang_version' => phpversion(), 'publisher' => 'stripe', 'uname' => php_uname()); $allHeaders = array('X-Postmaster-Client-User-Agent: ' . json_encode($ua), 'User-Agent: Postmaster/v1 PhpBindings/' . Postmaster::VERSION); if ($headers) { $allHeaders = array_merge($allHeaders, $headers); } list($rbody, $rcode) = $this->_curlRequest($meth, $absUrl, $allHeaders, $params, $apiKey); if ($rbody == 'OK') { $resp = $rbody; } else { try { $resp = json_decode($rbody, true); } catch (Exception $e) { throw new Postmaster_Error("Invalid response body from API: {$rbody} (HTTP response code was {$rcode})", $rcode, $rbody); } } if ($rcode < 200 || $rcode >= 300) { if (is_array($resp) && array_key_exists('message', $resp)) { $msg = $resp['message']; } else { $msg = "Unknown API error"; } if ($rcode == 400) { throw new InvalidData_Error($msg, $rbody, $rcode, $resp); } else { if ($rcode == 401) { throw new Authentication_Error($msg, $rbody, $rcode, $resp); } else { if ($rcode == 403) { throw new Permission_Error($msg, $rbody, $rcode, $resp); } } } throw new API_Error($msg, $rbody, $rcode, $resp); } return $resp; }
public static function setApiKey($apiKey) { self::$apiKey = $apiKey; }
<?php /* at startup set API key */ require_once './lib/Postmaster.php'; Postmaster::setApiKey("example-api-key"); /* at first validate recipient address */ $result = Postmaster_AddressValidation::validate(array("company" => "Postmaster Inc.", "contact" => "Joe Smith", "line1" => "701 Brazos St. Suite 1616", "city" => "Austin", "state" => "TX", "zip_code" => "78701", "country" => "US")); //var_dump($result); /* if address is ok you can ask for time and rates for it */ $result = Postmaster_TransitTimes::get(array("from_zip" => "78701", "to_zip" => "78704", "weight" => 1.5, "carrier" => "fedex")); //var_dump($result); $result = Postmaster_Rates::get(array("from_zip" => "78701", "to_zip" => "78704", "weight" => 1.5, "carrier" => "fedex")); //var_dump($result); /* when user will choose delivery type you create shipment */ $result = Postmaster_Shipment::create(array("to" => array("company" => "Postmaster Inc.", "contact" => "Joe Smith", "line1" => "701 Brazos St. Suite 1616", "city" => "Austin", "state" => "TX", "zip_code" => "78701", "phone_no" => "512-693-4040"), "from" => array("company" => "Postmaster Inc.", "contact" => "Joe Smith", "line1" => "701 Brazos St. Suite 1616", "city" => "Austin", "state" => "TX", "zip_code" => "78701", "phone_no" => "512-693-4040"), "carrier" => "fedex", "service" => "2DAY", "package" => array("weight" => 1.5, "length" => 10, "width" => 6, "height" => 8))); //var_dump($result); /* store in your DB shipment ID for later use */ $shipment_id = $result->id; /* anytime you can extract shipment data */ $sm = Postmaster_Shipment::retrieve($shipment_id); //var_dump($sm); /* or check delivery status */ $result = $sm->track(); //var_dump($result); /* you can cancel shipment, but only before being picked up by the carrier */ $result = $sm->void(); //var_dump($result); /* list all shipments */ $result = Postmaster_Shipment::all(); //var_dump($result); /* list 3 newest shipments */