diff --git a/lib/ShippingEasy.php b/lib/ShippingEasy.php index bee24dc..84b7ccc 100644 --- a/lib/ShippingEasy.php +++ b/lib/ShippingEasy.php @@ -13,21 +13,21 @@ throw new Exception('ShippingEasy needs the Multibyte String PHP extension.'); } -require(dirname(__FILE__) . '/ShippingEasy/ShippingEasy.php'); +require(__DIR__ . '/ShippingEasy/ShippingEasy.php'); // Errors -require(dirname(__FILE__) . '/ShippingEasy/Error.php'); -require(dirname(__FILE__) . '/ShippingEasy/ApiError.php'); -require(dirname(__FILE__) . '/ShippingEasy/ApiConnectionError.php'); -require(dirname(__FILE__) . '/ShippingEasy/AuthenticationError.php'); -require(dirname(__FILE__) . '/ShippingEasy/InvalidRequestError.php'); +require(__DIR__ . '/ShippingEasy/Error.php'); +require(__DIR__ . '/ShippingEasy/ApiError.php'); +require(__DIR__ . '/ShippingEasy/ApiConnectionError.php'); +require(__DIR__ . '/ShippingEasy/AuthenticationError.php'); +require(__DIR__ . '/ShippingEasy/InvalidRequestError.php'); -require(dirname(__FILE__) . '/ShippingEasy/ApiRequestor.php'); -require(dirname(__FILE__) . '/ShippingEasy/Authenticator.php'); -require(dirname(__FILE__) . '/ShippingEasy/Object.php'); -require(dirname(__FILE__) . '/ShippingEasy/Order.php'); -require(dirname(__FILE__) . '/ShippingEasy/PartnerSession.php'); -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(__DIR__ . '/ShippingEasy/ApiRequestor.php'); +require(__DIR__ . '/ShippingEasy/Authenticator.php'); +require(__DIR__ . '/ShippingEasy/Object.php'); +require(__DIR__ . '/ShippingEasy/Order.php'); +require(__DIR__ . '/ShippingEasy/PartnerSession.php'); +require(__DIR__ . '/ShippingEasy/PartnerAccount.php'); +require(__DIR__ . '/ShippingEasy/Signature.php'); +require(__DIR__ . '/ShippingEasy/SignedUrl.php'); +require(__DIR__ . '/ShippingEasy/Cancellation.php'); \ No newline at end of file diff --git a/lib/ShippingEasy/ApiRequestor.php b/lib/ShippingEasy/ApiRequestor.php index 34480a8..e6ed8f3 100644 --- a/lib/ShippingEasy/ApiRequestor.php +++ b/lib/ShippingEasy/ApiRequestor.php @@ -18,7 +18,7 @@ public static function encode($arr, $prefix=null) if (!is_array($arr)) return $arr; - $r = array(); + $r = []; foreach ($arr as $k => $v) { if (is_null($v)) continue; @@ -29,9 +29,9 @@ public static function encode($arr, $prefix=null) $k = $prefix."[]"; if (is_array($v)) { - $r[] = self::encode($v, $k, true); + $r[] = self::encode($v, $k); } else { - $r[] = urlencode($k)."=".urlencode($v); + $r[] = urlencode($k)."=".urlencode((string) $v); } } @@ -40,7 +40,7 @@ public static function encode($arr, $prefix=null) public function request($meth, $path, $params=null, $payload = null, $apiKey = null, $apiSecret = null) { - list($rbody, $rcode) = $this->_requestRaw($meth, $path, $params, $payload, $apiKey, $apiSecret); + [$rbody, $rcode] = $this->_requestRaw($meth, $path, $params, $payload, $apiKey, $apiSecret); $resp = $this->_interpretResponse($rbody, $rcode); return $resp; } @@ -52,11 +52,11 @@ public function handleApiError($rbody, $rcode, $resp) 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; + $message = $error[0]['message'] ?? null; switch ($rcode) { case 400: - throw new ShippingEasy_InvalidRequestError(json_encode($error), $rcode, $rbody, $resp); + throw new ShippingEasy_InvalidRequestError(json_encode($error, JSON_THROW_ON_ERROR), $rcode, $rbody, $resp); case 404: throw new ShippingEasy_InvalidRequestError($message, $rcode, $rbody, $resp); case 401: @@ -74,28 +74,22 @@ private function _requestRaw($http_method, $path, $params, $payload, $apiKey, $a $langVersion = phpversion(); $uname = php_uname(); - $ua = array('bindings_version' => ShippingEasy::VERSION, - 'lang' => 'php', - 'lang_version' => $langVersion, - 'publisher' => 'ShippingEasy', - 'uname' => $uname); + $ua = ['bindings_version' => ShippingEasy::VERSION, 'lang' => 'php', 'lang_version' => $langVersion, 'publisher' => 'ShippingEasy', 'uname' => $uname]; - $headers = array('X-ShippingEasy-Client-User-Agent: ' . json_encode($ua), - 'User-Agent: ShippingEasy/v1 PhpBindings/' . ShippingEasy::VERSION, - 'Authorization: Bearer ' . $apiKey); + $headers = ['X-ShippingEasy-Client-User-Agent: ' . json_encode($ua, JSON_THROW_ON_ERROR), '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); + [$rbody, $rcode] = $this->_curlRequest($http_method, $absUrl, $headers, $payload); + return [$rbody, $rcode]; } private function _interpretResponse($rbody, $rcode) { try { - $resp = json_decode($rbody, true); - } catch (Exception $e) { + $resp = json_decode((string) $rbody, true, 512, JSON_THROW_ON_ERROR); + } catch (Exception) { throw new ShippingEasy_ApiError("Invalid response body from API: $rbody (HTTP response code was $rcode)", $rcode, $rbody); } if ($rcode < 200 || $rcode >= 300) { @@ -106,16 +100,17 @@ private function _interpretResponse($rbody, $rcode) private function _curlRequest($meth, $absUrl, $headers, $payload) { + $params = null; $curl = curl_init(); - $meth = strtolower($meth); - $opts = array(); + $meth = strtolower((string) $meth); + $opts = []; if ($meth == 'get') { $opts[CURLOPT_HTTPGET] = 1; } else if ($meth == 'post') { $opts[CURLOPT_POST] = 1; if ($payload) - $payload = json_encode($payload); + $payload = json_encode($payload, JSON_THROW_ON_ERROR); $headers[] = 'Content-Type: application/json'; $headers[] = 'Content-Length: ' . strlen($payload); @@ -123,14 +118,14 @@ private function _curlRequest($meth, $absUrl, $headers, $payload) } else if ($meth == 'put') { $opts[CURLOPT_CUSTOMREQUEST] = 'PUT'; if ($payload) - $payload = json_encode($payload); + $payload = json_encode($payload, JSON_THROW_ON_ERROR); $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) { + if (($params === null ? 0 : count($params)) > 0) { $encoded = self::encode($params); $absUrl = "$absUrl?$encoded"; } @@ -160,25 +155,17 @@ private function _curlRequest($meth, $absUrl, $headers, $payload) $rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); - return array($rbody, $rcode); + return [$rbody, $rcode]; } public function handleCurlError($errno, $message) { $apiBase = ShippingEasy::$apiBase; - switch ($errno) { - case CURLE_COULDNT_CONNECT: - case CURLE_COULDNT_RESOLVE_HOST: - case CURLE_OPERATION_TIMEOUTED: - $msg = "Could not connect to ShippingEasy ($apiBase). Please check your internet connection and try again. If this problem persists, let us know at support@shippingeasy.com."; - break; - case CURLE_SSL_CACERT: - case CURLE_SSL_PEER_CERTIFICATE: - $msg = "Could not verify ShippingEasy's SSL certificate. Please make sure that your network is not intercepting certificates. (Try going to $apiBase in your browser.) If this problem persists, let us know at support@shippingeasy.com."; - break; - default: - $msg = "Unexpected error communicating with ShippingEasy. If this problem persists, let us know at support@shippingeasy.com."; - } + $msg = match ($errno) { + CURLE_COULDNT_CONNECT, CURLE_COULDNT_RESOLVE_HOST, CURLE_OPERATION_TIMEOUTED => "Could not connect to ShippingEasy ($apiBase). Please check your internet connection and try again. If this problem persists, let us know at support@shippingeasy.com.", + CURLE_SSL_CACERT, CURLE_SSL_PEER_CERTIFICATE => "Could not verify ShippingEasy's SSL certificate. Please make sure that your network is not intercepting certificates. (Try going to $apiBase in your browser.) If this problem persists, let us know at support@shippingeasy.com.", + default => "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); diff --git a/lib/ShippingEasy/Authenticator.php b/lib/ShippingEasy/Authenticator.php index 5ed5c68..fe5bdc9 100644 --- a/lib/ShippingEasy/Authenticator.php +++ b/lib/ShippingEasy/Authenticator.php @@ -2,7 +2,9 @@ class ShippingEasy_Authenticator { - + public $supplied_signature_string; + public $expected_signature; + # Instantiates a new authenticator object. # # http_method - The method of the http request. E.g. "post" or "get". @@ -13,7 +15,7 @@ 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; + $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); diff --git a/lib/ShippingEasy/Cancellation.php b/lib/ShippingEasy/Cancellation.php index ad412d9..4969f43 100644 --- a/lib/ShippingEasy/Cancellation.php +++ b/lib/ShippingEasy/Cancellation.php @@ -2,6 +2,8 @@ class ShippingEasy_Cancellation extends ShippingEasy_Object { + public $store_api_key; + public $external_order_identifier; public function __construct($store_api_key, $external_order_identifier) { $this->store_api_key = $store_api_key; diff --git a/lib/ShippingEasy/Error.php b/lib/ShippingEasy/Error.php index 9f9760f..28b25ee 100644 --- a/lib/ShippingEasy/Error.php +++ b/lib/ShippingEasy/Error.php @@ -2,6 +2,10 @@ class ShippingEasy_Error extends Exception { + public $http_status; + public $http_body; + public $json_body; + public function __construct($message=null, $http_status=null, $http_body=null, $json_body=null) { parent::__construct($message); diff --git a/lib/ShippingEasy/Order.php b/lib/ShippingEasy/Order.php index f03aa70..d8f9bc9 100644 --- a/lib/ShippingEasy/Order.php +++ b/lib/ShippingEasy/Order.php @@ -2,6 +2,9 @@ class ShippingEasy_Order extends ShippingEasy_Object { + public $store_api_key; + public $values; + public function __construct($store_api_key=null, $values=null) { $this->store_api_key = $store_api_key; $this->values = $values; @@ -9,17 +12,17 @@ public function __construct($store_api_key=null, $values=null) { public function create() { - return $this->request("post", "/api/stores/$this->store_api_key/orders", null, array("order" => $this->values)); + return $this->request("post", "/api/stores/$this->store_api_key/orders", null, ["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)); + return $this->request("put", "/api/stores/$this->store_api_key/orders/$external_order_id/recipient", null, ["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))); + return $this->request("put", "/api/stores/$this->store_api_key/orders/$external_order_id/status", null, ["order" => ["order_status" => $new_status]]); } public function find($id) @@ -32,12 +35,12 @@ 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()) + public function findAllByStore($params=[]) { return $this->request("get", "/api/stores/$this->store_api_key/orders", $params); } - public function findAll($params=array()) + public function findAll($params=[]) { return $this->request("get", "/api/orders", $params); } diff --git a/lib/ShippingEasy/PartnerAccount.php b/lib/ShippingEasy/PartnerAccount.php index 9c12e3f..73b5709 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()) + public function create($data = []) { - return $this->request("post", "/partners/api/accounts", null, array("account" => $data), ShippingEasy::$partnerApiKey, ShippingEasy::$partnerApiSecret); + return $this->request("post", "/partners/api/accounts", null, ["account" => $data], ShippingEasy::$partnerApiKey, ShippingEasy::$partnerApiSecret); } } diff --git a/lib/ShippingEasy/PartnerSession.php b/lib/ShippingEasy/PartnerSession.php index 4b5aeea..e984642 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()) + public function create($data = []) { - return $this->request("post", "/partners/api/sessions", null, array("session" => $data), ShippingEasy::$partnerApiKey, ShippingEasy::$partnerApiSecret); + return $this->request("post", "/partners/api/sessions", null, ["session" => $data], ShippingEasy::$partnerApiKey, ShippingEasy::$partnerApiSecret); } } diff --git a/lib/ShippingEasy/ShippingEasy.php b/lib/ShippingEasy/ShippingEasy.php index 40e794c..3975696 100644 --- a/lib/ShippingEasy/ShippingEasy.php +++ b/lib/ShippingEasy/ShippingEasy.php @@ -8,7 +8,7 @@ abstract class ShippingEasy public static $partnerApiSecret; public static $apiBase = 'https://api.shippingeasy.com'; public static $apiVersion = null; - const VERSION = '0.4.3'; + final const VERSION = '0.4.3'; public static function getApiKey() { diff --git a/lib/ShippingEasy/Signature.php b/lib/ShippingEasy/Signature.php index 5fdbe61..9c9a7d7 100644 --- a/lib/ShippingEasy/Signature.php +++ b/lib/ShippingEasy/Signature.php @@ -2,10 +2,16 @@ class ShippingEasy_Signature { + public $api_secret; + public $http_method; + public $path; + public $params; + public $json_body; + 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->http_method = strtoupper((string) $http_method); $this->path = $path; ksort($params); $this->params = $params; @@ -13,7 +19,7 @@ public function __construct($api_secret=null, $http_method=null, $path=null, $pa if (is_string($json_body)) { $this->json_body = str_replace("\/","/", $json_body); } else { - $this->json_body = json_encode($json_body); + $this->json_body = json_encode($json_body, JSON_THROW_ON_ERROR); } } @@ -44,7 +50,7 @@ public function getJsonBody() public function plaintext() { - $parts = array($this->getHttpMethod()); + $parts = [$this->getHttpMethod()]; $parts[] = $this->getPath(); if (!empty($this->getParams())) @@ -58,7 +64,7 @@ public function plaintext() public function encrypted() { - return hash_hmac('sha256', $this->plaintext(), $this->getApiSecret()); + return hash_hmac('sha256', (string) $this->plaintext(), (string) $this->getApiSecret()); } public function equals($signature) diff --git a/lib/ShippingEasy/SignedUrl.php b/lib/ShippingEasy/SignedUrl.php index 0d52cde..d80983b 100644 --- a/lib/ShippingEasy/SignedUrl.php +++ b/lib/ShippingEasy/SignedUrl.php @@ -2,11 +2,14 @@ class ShippingEasy_SignedUrl { + public $params; + public $path; + 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(); + $api_secret ??= ShippingEasy::$apiSecret; + $params["api_key"] = $api_key ?? ShippingEasy::$apiKey; + $params["api_timestamp"] = $api_timestamp ?? time(); $signature_object = new ShippingEasy_Signature($api_secret, $http_method, $path, $params, $json_body); $params["api_signature"] = $signature_object->encrypted();