diff --git a/lib/ShippingEasy.php b/lib/ShippingEasy.php index bee24dc..03cb969 100644 --- a/lib/ShippingEasy.php +++ b/lib/ShippingEasy.php @@ -4,13 +4,13 @@ // This snippet (and some of the curl code) due to the Facebook SDK. if (!function_exists('curl_init')) { - throw new Exception('ShippingEasy needs the CURL PHP extension.'); + throw new Exception('ShippingEasy needs the CURL PHP extension.'); } if (!function_exists('json_decode')) { - throw new Exception('ShippingEasy needs the JSON PHP extension.'); + throw new Exception('ShippingEasy needs the JSON PHP extension.'); } if (!function_exists('mb_detect_encoding')) { - throw new Exception('ShippingEasy needs the Multibyte String PHP extension.'); + throw new Exception('ShippingEasy needs the Multibyte String PHP extension.'); } require(dirname(__FILE__) . '/ShippingEasy/ShippingEasy.php'); @@ -30,4 +30,4 @@ require(dirname(__FILE__) . '/ShippingEasy/PartnerAccount.php'); require(dirname(__FILE__) . '/ShippingEasy/Signature.php'); require(dirname(__FILE__) . '/ShippingEasy/SignedUrl.php'); -require(dirname(__FILE__) . '/ShippingEasy/Cancellation.php'); \ No newline at end of file +require(dirname(__FILE__) . '/ShippingEasy/Cancellation.php'); diff --git a/lib/ShippingEasy/ApiRequestor.php b/lib/ShippingEasy/ApiRequestor.php index 34480a8..77487f1 100644 --- a/lib/ShippingEasy/ApiRequestor.php +++ b/lib/ShippingEasy/ApiRequestor.php @@ -2,59 +2,63 @@ class ShippingEasy_ApiRequestor { - public $apiKey; - public $apiSecret; - - public static function utf8($value) - { - if (is_string($value) && mb_detect_encoding($value, "UTF-8", TRUE) != "UTF-8") - return utf8_encode($value); - else - return $value; - } - - public static function encode($arr, $prefix=null) - { - if (!is_array($arr)) - return $arr; - - $r = array(); - foreach ($arr as $k => $v) { - if (is_null($v)) - continue; - - if ($prefix && $k && !is_int($k)) - $k = $prefix."[".$k."]"; - else if ($prefix) - $k = $prefix."[]"; - - if (is_array($v)) { - $r[] = self::encode($v, $k, true); - } else { - $r[] = urlencode($k)."=".urlencode($v); - } + public $apiKey; + public $apiSecret; + + public static function utf8($value) + { + if (is_string($value) && mb_detect_encoding($value, "UTF-8", true) != "UTF-8") { + return utf8_encode($value); + } else { + return $value; + } } - return implode("&", $r); - } - - public function request($meth, $path, $params=null, $payload = null, $apiKey = null, $apiSecret = null) - { - list($rbody, $rcode) = $this->_requestRaw($meth, $path, $params, $payload, $apiKey, $apiSecret); - $resp = $this->_interpretResponse($rbody, $rcode); - return $resp; - } + public static function encode($arr, $prefix=null) + { + if (!is_array($arr)) { + return $arr; + } + + $r = array(); + foreach ($arr as $k => $v) { + if (is_null($v)) { + continue; + } + + if ($prefix && $k && !is_int($k)) { + $k = $prefix."[".$k."]"; + } elseif ($prefix) { + $k = $prefix."[]"; + } + + if (is_array($v)) { + $r[] = self::encode($v, $k, true); + } else { + $r[] = urlencode($k)."=".urlencode($v); + } + } + + return implode("&", $r); + } - public function handleApiError($rbody, $rcode, $resp) - { + public function request($meth, $path, $params=null, $payload = null, $apiKey = null, $apiSecret = null) + { + list($rbody, $rcode) = $this->_requestRaw($meth, $path, $params, $payload, $apiKey, $apiSecret); + $resp = $this->_interpretResponse($rbody, $rcode); + return $resp; + } - if (!is_array($resp) || !isset($resp['errors'])) - throw new ShippingEasy_ApiError("Invalid response object from API: $rbody (HTTP response code was $rcode)", $rcode, $rbody, $resp); + public function handleApiError($rbody, $rcode, $resp) + { + if (!is_array($resp) || !isset($resp['errors'])) { + throw new ShippingEasy_ApiError("Invalid response object from API: $rbody (HTTP response code was $rcode)", $rcode, $rbody, $resp); + } - $error = $resp['errors']; - $message = isset($error[0]['message']) ? $error[0]['message'] : null; + $error = $resp['errors']; + $message = isset($error[0]['message']) ? $error[0]['message'] : null; - switch ($rcode) { + switch ($rcode) { case 400: throw new ShippingEasy_InvalidRequestError(json_encode($error), $rcode, $rbody, $resp); case 404: @@ -64,109 +68,112 @@ public function handleApiError($rbody, $rcode, $resp) default: throw new ShippingEasy_ApiError($message, $rcode, $rbody, $resp); } - } + } - private function _requestRaw($http_method, $path, $params, $payload, $apiKey, $apiSecret) - { - $url = new ShippingEasy_SignedUrl($http_method, $path, $params, $payload, null, $apiKey, $apiSecret); - $absUrl = $url->toString(); + private function _requestRaw($http_method, $path, $params, $payload, $apiKey, $apiSecret) + { + $url = new ShippingEasy_SignedUrl($http_method, $path, $params, $payload, null, $apiKey, $apiSecret); + $absUrl = $url->toString(); - $langVersion = phpversion(); - $uname = php_uname(); + $langVersion = phpversion(); + $uname = php_uname(); - $ua = array('bindings_version' => ShippingEasy::VERSION, - 'lang' => 'php', - 'lang_version' => $langVersion, - 'publisher' => 'ShippingEasy', - 'uname' => $uname); + $ua = array('bindings_version' => ShippingEasy::VERSION, + 'lang' => 'php', + 'lang_version' => $langVersion, + 'publisher' => 'ShippingEasy', + 'uname' => $uname); - $headers = array('X-ShippingEasy-Client-User-Agent: ' . json_encode($ua), + $headers = array('X-ShippingEasy-Client-User-Agent: ' . json_encode($ua), 'User-Agent: ShippingEasy/v1 PhpBindings/' . ShippingEasy::VERSION, 'Authorization: Bearer ' . $apiKey); - if (ShippingEasy::$apiVersion) - $headers[] = 'ShippingEasy-Version: ' . ShippingEasy::$apiVersion; - - list($rbody, $rcode) = $this->_curlRequest($http_method, $absUrl, $headers, $payload); - return array($rbody, $rcode); - } + if (ShippingEasy::$apiVersion) { + $headers[] = 'ShippingEasy-Version: ' . ShippingEasy::$apiVersion; + } - private function _interpretResponse($rbody, $rcode) - { - try { - $resp = json_decode($rbody, true); - } catch (Exception $e) { - throw new ShippingEasy_ApiError("Invalid response body from API: $rbody (HTTP response code was $rcode)", $rcode, $rbody); + list($rbody, $rcode) = $this->_curlRequest($http_method, $absUrl, $headers, $payload); + return array($rbody, $rcode); } - if ($rcode < 200 || $rcode >= 300) { - $this->handleApiError($rbody, $rcode, $resp); - } - return $resp; - } - - private function _curlRequest($meth, $absUrl, $headers, $payload) - { - $curl = curl_init(); - $meth = strtolower($meth); - $opts = array(); - - if ($meth == 'get') { - $opts[CURLOPT_HTTPGET] = 1; - } else if ($meth == 'post') { - $opts[CURLOPT_POST] = 1; - if ($payload) - $payload = json_encode($payload); - - $headers[] = 'Content-Type: application/json'; - $headers[] = 'Content-Length: ' . strlen($payload); - $opts[CURLOPT_POSTFIELDS] = $payload; - } else if ($meth == 'put') { - $opts[CURLOPT_CUSTOMREQUEST] = 'PUT'; - if ($payload) - $payload = json_encode($payload); - - $headers[] = 'Content-Type: application/json'; - $headers[] = 'Content-Length: ' . strlen($payload); - $opts[CURLOPT_POSTFIELDS] = $payload; - } else if ($meth == 'delete') { - $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE'; - if (count($params) > 0) { - $encoded = self::encode($params); - $absUrl = "$absUrl?$encoded"; - } - } else { - throw new ShippingEasy_ApiError("Unrecognized method $meth"); + + private function _interpretResponse($rbody, $rcode) + { + try { + $resp = json_decode($rbody, true); + } catch (Exception $e) { + throw new ShippingEasy_ApiError("Invalid response body from API: $rbody (HTTP response code was $rcode)", $rcode, $rbody); + } + if ($rcode < 200 || $rcode >= 300) { + $this->handleApiError($rbody, $rcode, $resp); + } + return $resp; } - $opts[CURLOPT_URL] = $absUrl; - $opts[CURLOPT_RETURNTRANSFER] = true; - $opts[CURLOPT_CONNECTTIMEOUT] = 30; - $opts[CURLOPT_TIMEOUT] = 80; - $opts[CURLOPT_FOLLOWLOCATION] = true; - $opts[CURLOPT_MAXREDIRS] = 4; - $opts[CURLOPT_POSTREDIR] = 1 | 2 | 4; // Maintain method across redirect for all 3XX redirect types + private function _curlRequest($meth, $absUrl, $headers, $payload) + { + $curl = curl_init(); + $meth = strtolower($meth); + $opts = array(); + + if ($meth == 'get') { + $opts[CURLOPT_HTTPGET] = 1; + } elseif ($meth == 'post') { + $opts[CURLOPT_POST] = 1; + if ($payload) { + $payload = json_encode($payload); + } + + $headers[] = 'Content-Type: application/json'; + $headers[] = 'Content-Length: ' . strlen($payload); + $opts[CURLOPT_POSTFIELDS] = $payload; + } elseif ($meth == 'put') { + $opts[CURLOPT_CUSTOMREQUEST] = 'PUT'; + if ($payload) { + $payload = json_encode($payload); + } + + $headers[] = 'Content-Type: application/json'; + $headers[] = 'Content-Length: ' . strlen($payload); + $opts[CURLOPT_POSTFIELDS] = $payload; + } elseif ($meth == 'delete') { + $opts[CURLOPT_CUSTOMREQUEST] = 'DELETE'; + if (count($params) > 0) { + $encoded = self::encode($params); + $absUrl = "$absUrl?$encoded"; + } + } else { + throw new ShippingEasy_ApiError("Unrecognized method $meth"); + } + + $opts[CURLOPT_URL] = $absUrl; + $opts[CURLOPT_RETURNTRANSFER] = true; + $opts[CURLOPT_CONNECTTIMEOUT] = 30; + $opts[CURLOPT_TIMEOUT] = 80; + $opts[CURLOPT_FOLLOWLOCATION] = true; + $opts[CURLOPT_MAXREDIRS] = 4; + $opts[CURLOPT_POSTREDIR] = 1 | 2 | 4; // Maintain method across redirect for all 3XX redirect types $opts[CURLOPT_HTTPHEADER] = $headers; - curl_setopt_array($curl, $opts); - $rbody = curl_exec($curl); - $errno = curl_errno($curl); + curl_setopt_array($curl, $opts); + $rbody = curl_exec($curl); + $errno = curl_errno($curl); - if ($rbody === false) { - $errno = curl_errno($curl); - $message = curl_error($curl); - curl_close($curl); - $this->handleCurlError($errno, $message); - } + if ($rbody === false) { + $errno = curl_errno($curl); + $message = curl_error($curl); + curl_close($curl); + $this->handleCurlError($errno, $message); + } - $rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); - curl_close($curl); - return array($rbody, $rcode); - } + $rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + return array($rbody, $rcode); + } - public function handleCurlError($errno, $message) - { - $apiBase = ShippingEasy::$apiBase; - switch ($errno) { + public function handleCurlError($errno, $message) + { + $apiBase = ShippingEasy::$apiBase; + switch ($errno) { case CURLE_COULDNT_CONNECT: case CURLE_COULDNT_RESOLVE_HOST: case CURLE_OPERATION_TIMEOUTED: @@ -180,7 +187,7 @@ public function handleCurlError($errno, $message) $msg = "Unexpected error communicating with ShippingEasy. If this problem persists, let us know at support@shippingeasy.com."; } - $msg .= "\n\n(Network error [errno $errno]: $message)"; - throw new ShippingEasy_ApiConnectionError($msg); - } + $msg .= "\n\n(Network error [errno $errno]: $message)"; + throw new ShippingEasy_ApiConnectionError($msg); + } } diff --git a/lib/ShippingEasy/Authenticator.php b/lib/ShippingEasy/Authenticator.php index 5ed5c68..a084e7c 100644 --- a/lib/ShippingEasy/Authenticator.php +++ b/lib/ShippingEasy/Authenticator.php @@ -3,8 +3,8 @@ class ShippingEasy_Authenticator { - # Instantiates a new authenticator object. - # + # Instantiates a new authenticator object. + # # http_method - The method of the http request. E.g. "post" or "get". # path - The path of the request's uri. E.g. "/orders/callback" # params - An associative array of the request's query string parameters. E.g. array("api_signature" => "asdsadsad", "api_timestamp" => "1234567899") @@ -13,25 +13,24 @@ class ShippingEasy_Authenticator # public function __construct($http_method=null, $path=null, $params=null, $json_body=null, $api_secret=null) { - $api_secret = isset($api_secret) ? $api_secret : ShippingEasy::$apiSecret; - $this->supplied_signature_string = $params["api_signature"]; - unset($params["api_signature"]); - $this->expected_signature = new ShippingEasy_Signature($api_secret, $http_method, $path, $params, $json_body); + $api_secret = isset($api_secret) ? $api_secret : ShippingEasy::$apiSecret; + $this->supplied_signature_string = $params["api_signature"]; + unset($params["api_signature"]); + $this->expected_signature = new ShippingEasy_Signature($api_secret, $http_method, $path, $params, $json_body); } - public function getExpectedSignature() - { - return $this->expected_signature; - } + public function getExpectedSignature() + { + return $this->expected_signature; + } - public function getSuppliedSignatureString() - { - return $this->supplied_signature_string; - } + public function getSuppliedSignatureString() + { + return $this->supplied_signature_string; + } - public function isAuthenticated() - { - return $this->getExpectedSignature()->equals($this->getSuppliedSignatureString()); - } - -} \ No newline at end of file + public function isAuthenticated() + { + return $this->getExpectedSignature()->equals($this->getSuppliedSignatureString()); + } +} diff --git a/lib/ShippingEasy/Cancellation.php b/lib/ShippingEasy/Cancellation.php index ad412d9..2a610cf 100644 --- a/lib/ShippingEasy/Cancellation.php +++ b/lib/ShippingEasy/Cancellation.php @@ -2,15 +2,14 @@ class ShippingEasy_Cancellation extends ShippingEasy_Object { - - public function __construct($store_api_key, $external_order_identifier) { - $this->store_api_key = $store_api_key; - $this->external_order_identifier = $external_order_identifier; - } - - public function create() - { - return $this->request("post", "/api/stores/$this->store_api_key/orders/$this->external_order_identifier/cancellations"); - } - + public function __construct($store_api_key, $external_order_identifier) + { + $this->store_api_key = $store_api_key; + $this->external_order_identifier = $external_order_identifier; + } + + public function create() + { + return $this->request("post", "/api/stores/$this->store_api_key/orders/$this->external_order_identifier/cancellations"); + } } diff --git a/lib/ShippingEasy/Error.php b/lib/ShippingEasy/Error.php index 9f9760f..05ca19d 100644 --- a/lib/ShippingEasy/Error.php +++ b/lib/ShippingEasy/Error.php @@ -2,26 +2,26 @@ class ShippingEasy_Error extends Exception { - public function __construct($message=null, $http_status=null, $http_body=null, $json_body=null) - { - parent::__construct($message); - $this->http_status = $http_status; - $this->http_body = $http_body; - $this->json_body = $json_body; - } + public function __construct($message=null, $http_status=null, $http_body=null, $json_body=null) + { + parent::__construct($message); + $this->http_status = $http_status; + $this->http_body = $http_body; + $this->json_body = $json_body; + } - public function getHttpStatus() - { - return $this->http_status; - } + public function getHttpStatus() + { + return $this->http_status; + } - public function getHttpBody() - { - return $this->http_body; - } + public function getHttpBody() + { + return $this->http_body; + } - public function getJsonBody() - { - return $this->json_body; - } + public function getJsonBody() + { + return $this->json_body; + } } diff --git a/lib/ShippingEasy/InvalidRequestError.php b/lib/ShippingEasy/InvalidRequestError.php index 1ef2e80..5de1871 100644 --- a/lib/ShippingEasy/InvalidRequestError.php +++ b/lib/ShippingEasy/InvalidRequestError.php @@ -2,8 +2,8 @@ class ShippingEasy_InvalidRequestError extends ShippingEasy_Error { - public function __construct($message, $http_status=null, $http_body=null, $json_body=null) - { - parent::__construct($message, $http_status, $http_body, $json_body); - } + public function __construct($message, $http_status=null, $http_body=null, $json_body=null) + { + parent::__construct($message, $http_status, $http_body, $json_body); + } } diff --git a/lib/ShippingEasy/Object.php b/lib/ShippingEasy/Object.php index ed9f240..7866948 100644 --- a/lib/ShippingEasy/Object.php +++ b/lib/ShippingEasy/Object.php @@ -2,11 +2,9 @@ class ShippingEasy_Object { - public function request($meth, $path, $params=null, $payload = null, $apiKey = null, $apiSecret = null) - { - $requestor = new ShippingEasy_ApiRequestor(); - return $requestor->request($meth, $path, $params, $payload, $apiKey, $apiSecret); - } - + public function request($meth, $path, $params=null, $payload = null, $apiKey = null, $apiSecret = null) + { + $requestor = new ShippingEasy_ApiRequestor(); + return $requestor->request($meth, $path, $params, $payload, $apiKey, $apiSecret); + } } - diff --git a/lib/ShippingEasy/Order.php b/lib/ShippingEasy/Order.php index 9e8d507..989a881 100644 --- a/lib/ShippingEasy/Order.php +++ b/lib/ShippingEasy/Order.php @@ -2,44 +2,45 @@ class ShippingEasy_Order extends ShippingEasy_Object { - public function __construct($store_api_key=null, $values=null) { - $this->store_api_key = $store_api_key; - $this->values = $values; - } - - public function create() - { - return $this->request("post", "/api/stores/$this->store_api_key/orders", null, array("order" => $this->values)); - } - - public function updateRecipient($external_order_id, $recipient_data) - { - return $this->request("put", "/api/stores/$this->store_api_key/orders/$external_order_id/recipient", null, array("recipient" => $recipient_data)); - } - - public function updateStatus($external_order_id, $new_status) - { - return $this->request("put", "/api/stores/$this->store_api_key/orders/$external_order_id/status", null, array("order" => array("order_status" => $new_status))); - } - - public function find($id) - { - return $this->request("get", "/api/orders/$id"); - } - - public function findByStore($external_order_id) - { - return $this->request("get", "/api/stores/$this->store_api_key/orders/$external_order_id"); - } - - public function findAllByStore($params=array()) - { - return $this->request("get", "/api/stores/$this->store_api_key/orders", $params); - } - - public function findAll($params=array()) - { - print_r($params); - return $this->request("get", "/api/orders", $params); - } + public function __construct($store_api_key=null, $values=null) + { + $this->store_api_key = $store_api_key; + $this->values = $values; + } + + public function create() + { + return $this->request("post", "/api/stores/$this->store_api_key/orders", null, array("order" => $this->values)); + } + + public function updateRecipient($external_order_id, $recipient_data) + { + return $this->request("put", "/api/stores/$this->store_api_key/orders/$external_order_id/recipient", null, array("recipient" => $recipient_data)); + } + + public function updateStatus($external_order_id, $new_status) + { + return $this->request("put", "/api/stores/$this->store_api_key/orders/$external_order_id/status", null, array("order" => array("order_status" => $new_status))); + } + + public function find($id) + { + return $this->request("get", "/api/orders/$id"); + } + + public function findByStore($external_order_id) + { + return $this->request("get", "/api/stores/$this->store_api_key/orders/$external_order_id"); + } + + public function findAllByStore($params=array()) + { + return $this->request("get", "/api/stores/$this->store_api_key/orders", $params); + } + + public function findAll($params=array()) + { + print_r($params); + return $this->request("get", "/api/orders", $params); + } } diff --git a/lib/ShippingEasy/PartnerAccount.php b/lib/ShippingEasy/PartnerAccount.php index 9c12e3f..6a7df02 100644 --- a/lib/ShippingEasy/PartnerAccount.php +++ b/lib/ShippingEasy/PartnerAccount.php @@ -2,8 +2,8 @@ class ShippingEasy_PartnerAccount extends ShippingEasy_Object { - public function create($data = array()) - { - return $this->request("post", "/partners/api/accounts", null, array("account" => $data), ShippingEasy::$partnerApiKey, ShippingEasy::$partnerApiSecret); - } + public function create($data = array()) + { + return $this->request("post", "/partners/api/accounts", null, array("account" => $data), ShippingEasy::$partnerApiKey, ShippingEasy::$partnerApiSecret); + } } diff --git a/lib/ShippingEasy/PartnerSession.php b/lib/ShippingEasy/PartnerSession.php index 4b5aeea..1456d96 100644 --- a/lib/ShippingEasy/PartnerSession.php +++ b/lib/ShippingEasy/PartnerSession.php @@ -2,8 +2,8 @@ class ShippingEasy_PartnerSession extends ShippingEasy_Object { - public function create($data = array()) - { - return $this->request("post", "/partners/api/sessions", null, array("session" => $data), ShippingEasy::$partnerApiKey, ShippingEasy::$partnerApiSecret); - } + public function create($data = array()) + { + return $this->request("post", "/partners/api/sessions", null, array("session" => $data), ShippingEasy::$partnerApiKey, ShippingEasy::$partnerApiSecret); + } } diff --git a/lib/ShippingEasy/ShippingEasy.php b/lib/ShippingEasy/ShippingEasy.php index 40e794c..a1e2467 100644 --- a/lib/ShippingEasy/ShippingEasy.php +++ b/lib/ShippingEasy/ShippingEasy.php @@ -2,56 +2,56 @@ abstract class ShippingEasy { - public static $apiKey; - public static $apiSecret; - public static $partnerApiKey; - public static $partnerApiSecret; - public static $apiBase = 'https://api.shippingeasy.com'; - public static $apiVersion = null; - const VERSION = '0.4.3'; - - public static function getApiKey() - { - return self::$apiKey; - } - - public static function setApiKey($apiKey) - { - self::$apiKey = $apiKey; - } - - public static function getPartnerApiKey() - { - return self::$partnerApiKey; - } - - public static function setPartnerApiKey($partnerApiKey) - { - self::$partnerApiKey = $partnerApiKey; - } - - public static function setApiSecret($apiSecret) - { - self::$apiSecret = $apiSecret; - } - - public static function setPartnerApiSecret($partnerApiSecret) - { - self::$partnerApiSecret = $partnerApiSecret; - } - - public static function getApiVersion() - { - return self::$apiVersion; - } - - public static function setApiVersion($apiVersion) - { - self::$apiVersion = $apiVersion; - } - - public static function setApiBase($apiBase) - { - self::$apiBase = $apiBase; - } + public static $apiKey; + public static $apiSecret; + public static $partnerApiKey; + public static $partnerApiSecret; + public static $apiBase = 'https://api.shippingeasy.com'; + public static $apiVersion = null; + const VERSION = '0.4.3'; + + public static function getApiKey() + { + return self::$apiKey; + } + + public static function setApiKey($apiKey) + { + self::$apiKey = $apiKey; + } + + public static function getPartnerApiKey() + { + return self::$partnerApiKey; + } + + public static function setPartnerApiKey($partnerApiKey) + { + self::$partnerApiKey = $partnerApiKey; + } + + public static function setApiSecret($apiSecret) + { + self::$apiSecret = $apiSecret; + } + + public static function setPartnerApiSecret($partnerApiSecret) + { + self::$partnerApiSecret = $partnerApiSecret; + } + + public static function getApiVersion() + { + return self::$apiVersion; + } + + public static function setApiVersion($apiVersion) + { + self::$apiVersion = $apiVersion; + } + + public static function setApiBase($apiBase) + { + self::$apiBase = $apiBase; + } } diff --git a/lib/ShippingEasy/Signature.php b/lib/ShippingEasy/Signature.php index 82335dd..1041b69 100644 --- a/lib/ShippingEasy/Signature.php +++ b/lib/ShippingEasy/Signature.php @@ -2,66 +2,66 @@ class ShippingEasy_Signature { - public function __construct($api_secret=null, $http_method=null, $path=null, $params=null, $json_body=null) - { - $this->api_secret = $api_secret; - $this->http_method = strtoupper($http_method); - $this->path = $path; - ksort($params); - $this->params = $params; + public function __construct($api_secret=null, $http_method=null, $path=null, $params=null, $json_body=null) + { + $this->api_secret = $api_secret; + $this->http_method = strtoupper($http_method); + $this->path = $path; + ksort($params); + $this->params = $params; - if (is_string($json_body)) { - $this->json_body = str_replace("\/","/", $json_body); - } else { - $this->json_body = json_encode($json_body); + if (is_string($json_body)) { + $this->json_body = str_replace("\/", "/", $json_body); + } else { + $this->json_body = json_encode($json_body); + } } - } - public function getApiSecret() - { - return $this->api_secret; - } - - public function getHttpMethod() - { - return $this->http_method; - } + public function getApiSecret() + { + return $this->api_secret; + } - public function getPath() - { - return $this->path; - } + public function getHttpMethod() + { + return $this->http_method; + } - public function getParams() - { - return $this->params; - } + public function getPath() + { + return $this->path; + } - public function getJsonBody() - { - return $this->json_body; - } + public function getParams() + { + return $this->params; + } - public function plaintext() - { - $parts = array($this->getHttpMethod()); - $parts[] = $this->getPath(); - $parts[] = http_build_query($this->getParams()); + public function getJsonBody() + { + return $this->json_body; + } - if ($this->getJsonBody() != "null") - $parts[] = $this->getJsonBody(); + public function plaintext() + { + $parts = array($this->getHttpMethod()); + $parts[] = $this->getPath(); + $parts[] = http_build_query($this->getParams()); - return implode("&", $parts); - } + if ($this->getJsonBody() != "null") { + $parts[] = $this->getJsonBody(); + } - public function encrypted() - { - return hash_hmac('sha256', $this->plaintext(), $this->getApiSecret()); - } + return implode("&", $parts); + } - public function equals($signature) - { - return $this->encrypted() == $signature; - } + public function encrypted() + { + return hash_hmac('sha256', $this->plaintext(), $this->getApiSecret()); + } + public function equals($signature) + { + return $this->encrypted() == $signature; + } } diff --git a/lib/ShippingEasy/SignedUrl.php b/lib/ShippingEasy/SignedUrl.php index 0d52cde..ee7386d 100644 --- a/lib/ShippingEasy/SignedUrl.php +++ b/lib/ShippingEasy/SignedUrl.php @@ -2,34 +2,33 @@ class ShippingEasy_SignedUrl { - public function __construct($http_method=null, $path=null, $params=null, $json_body=null, $api_timestamp=null, $api_key = null, $api_secret = null) - { - $api_secret = isset($api_secret) ? $api_secret : ShippingEasy::$apiSecret; - $params["api_key"] = isset($api_key) ? $api_key : ShippingEasy::$apiKey; - $params["api_timestamp"] = isset($api_timestamp) ? $api_timestamp : time(); - $signature_object = new ShippingEasy_Signature($api_secret, $http_method, $path, $params, $json_body); - $params["api_signature"] = $signature_object->encrypted(); + public function __construct($http_method=null, $path=null, $params=null, $json_body=null, $api_timestamp=null, $api_key = null, $api_secret = null) + { + $api_secret = isset($api_secret) ? $api_secret : ShippingEasy::$apiSecret; + $params["api_key"] = isset($api_key) ? $api_key : ShippingEasy::$apiKey; + $params["api_timestamp"] = isset($api_timestamp) ? $api_timestamp : time(); + $signature_object = new ShippingEasy_Signature($api_secret, $http_method, $path, $params, $json_body); + $params["api_signature"] = $signature_object->encrypted(); - $this->params = $params; - $this->path = $path; - } + $this->params = $params; + $this->path = $path; + } - public function getParams() - { - return $this->params; - } + public function getParams() + { + return $this->params; + } - public function getPath() - { - return $this->path; - } + public function getPath() + { + return $this->path; + } - public function toString() - { - $url = ShippingEasy::$apiBase; - $url .= $this->getPath(); - $url .= "?" . http_build_query($this->getParams()); - return $url; - } - + public function toString() + { + $url = ShippingEasy::$apiBase; + $url .= $this->getPath(); + $url .= "?" . http_build_query($this->getParams()); + return $url; + } }