Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions lib/ShippingEasy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
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');
61 changes: 24 additions & 37 deletions lib/ShippingEasy/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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;
}
Expand All @@ -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:
Expand All @@ -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) {
Expand All @@ -106,31 +100,32 @@ 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);
$opts[CURLOPT_POSTFIELDS] = $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";
}
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions lib/ShippingEasy/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions lib/ShippingEasy/Cancellation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions lib/ShippingEasy/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions lib/ShippingEasy/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

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;
}

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)
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/ShippingEasy/PartnerAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions lib/ShippingEasy/PartnerSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion lib/ShippingEasy/ShippingEasy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
14 changes: 10 additions & 4 deletions lib/ShippingEasy/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

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;

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);
}
}

Expand Down Expand Up @@ -44,7 +50,7 @@ public function getJsonBody()

public function plaintext()
{
$parts = array($this->getHttpMethod());
$parts = [$this->getHttpMethod()];
$parts[] = $this->getPath();

if (!empty($this->getParams()))
Expand All @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions lib/ShippingEasy/SignedUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down