diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 00eb60fa..66aaa41f 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -7,6 +7,8 @@ /** * Bigcommerce API Client. + * + * Note: Unless specified, the functions only support the API version 2 */ class Client { @@ -50,7 +52,7 @@ class Client * * @var string */ - private static $path_prefix = '/api/v2'; + private static $path_prefix = '/api/'; /** * Full URL path to the configured store API. @@ -62,7 +64,7 @@ class Client private static $store_hash; private static $auth_token; private static $client_secret; - private static $stores_prefix = '/stores/%s/v2'; + private static $stores_prefix = '/stores/%s/'; private static $api_url = 'https://api.bigcommerce.com'; private static $login_url = 'https://login.bigcommerce.com'; @@ -256,11 +258,11 @@ public static function setConnection(Connection $connection = null) * @param string $resource resource class to map individual items * @return mixed array|string mapped collection or XML string if useXml is true */ - public static function getCollection($path, $resource = 'Resource') + public static function getCollection($path, $resource = 'Resource', $version = 'v2') { - $response = self::connection()->get(self::$api_path . $path); + $response = self::connection()->get(self::$api_path . $version . $path); - return self::mapCollection($resource, $response); + return self::mapCollection($resource, $response, $version); } /** @@ -270,9 +272,9 @@ public static function getCollection($path, $resource = 'Resource') * @param string $resource resource class to map individual items * @return mixed Resource|string resource object or XML string if useXml is true */ - public static function getResource($path, $resource = 'Resource') + public static function getResource($path, $resource = 'Resource', $version = 'v2') { - $response = self::connection()->get(self::$api_path . $path); + $response = self::connection()->get(self::$api_path . $version . $path); return self::mapResource($resource, $response); } @@ -283,9 +285,9 @@ public static function getResource($path, $resource = 'Resource') * @param string $path api endpoint * @return mixed int|string count value or XML string if useXml is true */ - public static function getCount($path) + public static function getCount($path, $version = 'v2') { - $response = self::connection()->get(self::$api_path . $path); + $response = self::connection()->get(self::$api_path . $version . $path); if ($response == false || is_string($response)) { return $response; @@ -301,13 +303,13 @@ public static function getCount($path) * @param mixed $object object or XML string to create * @return mixed */ - public static function createResource($path, $object) + public static function createResource($path, $object, $version = 'v2') { if (is_array($object)) { $object = (object)$object; } - return self::connection()->post(self::$api_path . $path, $object); + return self::connection()->post(self::$api_path . $version . $path, $object); } /** @@ -317,13 +319,13 @@ public static function createResource($path, $object) * @param mixed $object object or XML string to update * @return mixed */ - public static function updateResource($path, $object) + public static function updateResource($path, $object, $version = 'v2') { if (is_array($object)) { $object = (object)$object; } - return self::connection()->put(self::$api_path . $path, $object); + return self::connection()->put(self::$api_path . $version . $path, $object); } /** @@ -332,9 +334,9 @@ public static function updateResource($path, $object) * @param string $path api endpoint * @return mixed */ - public static function deleteResource($path) + public static function deleteResource($path, $version = 'v2') { - return self::connection()->delete(self::$api_path . $path); + return self::connection()->delete(self::$api_path . $version . $path); } /** @@ -344,8 +346,12 @@ public static function deleteResource($path) * @param array $object object collection * @return array */ - private static function mapCollection($resource, $object) + private static function mapCollection($resource, $object, $version = 'v2') { + if ($version === 'v3') { + $object = $object->data ?? false; + } + if ($object == false || is_string($object)) { return $object; } @@ -453,9 +459,9 @@ public static function getCustomerLoginToken($id, $redirectUrl = '', $requestIp * * @return \DateTime */ - public static function getTime() + public static function getTime($version = 'v2') { - $response = self::connection()->get(self::$api_path . '/time'); + $response = self::connection()->get(self::$api_path . $version . '/time'); if ($response == false || is_string($response)) { return $response; @@ -466,14 +472,27 @@ public static function getTime() /** * Returns the default collection of products. + * + * Supports v2 & v3 * * @param array $filter * @return mixed array|string list of products or XML string if useXml is true */ - public static function getProducts($filter = array()) + public static function getProducts($filter = array(), $version = 'v2') { $filter = Filter::create($filter); - return self::getCollection('/products' . $filter->toQuery(), 'Product'); + + switch ($version) { + case 'v3': + return self::getCollection('/catalog/products' . $filter->toQuery(), 'Product', $version); + break; + + case 'v2': + default: + return self::getCollection('/products' . $filter->toQuery(), 'Product'); + break; + } + } /** @@ -1321,9 +1340,9 @@ public static function getRequestLogs() return self::getCollection('/requestlogs', 'RequestLog'); } - public static function getStore() + public static function getStore($version = 'v2') { - $response = self::connection()->get(self::$api_path . '/store'); + $response = self::connection()->get(self::$api_path . $version . '/store'); return $response; } @@ -1462,6 +1481,36 @@ public static function getOrderShippingAddresses($orderID, $filter = array()) return self::getCollection('/orders/' . $orderID . '/shipping_addresses' . $filter->toQuery(), 'Address'); } + /** + * Get Cart + * + * Only supports v3 + * + * @param $cartID + * @return mixed + */ + public static function getCart($cartID) { + return self::getResource('/carts/' . $cartID, 'Resource', 'v3'); + } + + /** + * Apply Coupon to Checkout + * + * Only supports v3 + * + * @param $checkoutID + * @param mixed $object the coupon code to apply + * @return mixed + */ + public static function addCoupon($checkoutID, $object) + { + if (is_array($object)) { + $object = (object)$object; + } + + return self::createResource('/checkouts/' . $checkoutID . '/coupons', $object, 'v3'); + } + /** * Create a new currency. *