From 122b03d3236b3ae24bade640b52ab814144ec6cd Mon Sep 17 00:00:00 2001 From: chrishappy <14931423+chrishappy@users.noreply.github.com> Date: Mon, 28 Dec 2020 17:44:38 -0800 Subject: [PATCH 1/5] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 52c15bbb..ec0e0e41 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "bigcommerce/api", + "name": "bigcommerce/api--forked", "type": "library", "description": "Enables PHP applications to communicate with the Bigcommerce API.", "keywords": [ From 5a99c3215e8b3a002dda0ef6d16b59ede623d71c Mon Sep 17 00:00:00 2001 From: Chris-Happy7 Date: Tue, 29 Dec 2020 15:38:28 -0800 Subject: [PATCH 2/5] Introduce $version with default value 'v2' Begin allowing clients to use v3 of the BigCommerce API with fully backwards compatibility --- src/Bigcommerce/Api/Client.php | 40 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 00eb60fa..51ca271d 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -50,7 +50,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 +62,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,9 +256,9 @@ 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); } @@ -270,9 +270,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 +283,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 +301,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 +317,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 +332,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,7 +344,7 @@ 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 ($object == false || is_string($object)) { return $object; @@ -453,9 +453,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; @@ -470,7 +470,7 @@ public static function getTime() * @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'); @@ -1321,9 +1321,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; } From 4d479232fe3d98a8bfeb1ac302c5690e58f8c88f Mon Sep 17 00:00:00 2001 From: Chris-Happy7 Date: Tue, 29 Dec 2020 15:54:26 -0800 Subject: [PATCH 3/5] Allow getProducts to use v3 --- src/Bigcommerce/Api/Client.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 51ca271d..5059da7f 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -260,7 +260,7 @@ public static function getCollection($path, $resource = 'Resource', $version = ' { $response = self::connection()->get(self::$api_path . $version . $path); - return self::mapCollection($resource, $response); + return self::mapCollection($resource, $response, $version); } /** @@ -346,6 +346,10 @@ public static function deleteResource($path, $version = 'v2') */ private static function mapCollection($resource, $object, $version = 'v2') { + if ($version === 'v3') { + $object = $object->data ?? false; + } + if ($object == false || is_string($object)) { return $object; } @@ -473,7 +477,18 @@ public static function getTime($version = 'v2') 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; + } + } /** From 677a0fbe63d352963895fca9ca472f780eea0bd1 Mon Sep 17 00:00:00 2001 From: Chris-Happy7 Date: Tue, 29 Dec 2020 16:57:21 -0800 Subject: [PATCH 4/5] Revert "Update composer.json" This reverts commit 122b03d3236b3ae24bade640b52ab814144ec6cd. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ec0e0e41..52c15bbb 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "bigcommerce/api--forked", + "name": "bigcommerce/api", "type": "library", "description": "Enables PHP applications to communicate with the Bigcommerce API.", "keywords": [ From af61b22a14b8e8801e6f106cccd57450ec98a680 Mon Sep 17 00:00:00 2001 From: Chris-Happy7 Date: Tue, 29 Dec 2020 17:11:12 -0800 Subject: [PATCH 5/5] Add "getCart" and "addCoupon" --- src/Bigcommerce/Api/Client.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 5059da7f..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 { @@ -470,6 +472,8 @@ public static function getTime($version = 'v2') /** * 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 @@ -1477,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. *