From 970e819493036036a656c8bf08a27d1b0246ea08 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Thu, 1 Apr 2021 10:10:09 +1030 Subject: [PATCH 1/6] Add Cart API without update customer or tests, #43 --- src/BigCommerce/Api/Carts/CartsApi.php | 27 +++++++++++++++++++ src/BigCommerce/Client.php | 13 +++++++++ src/BigCommerce/ResourceModels/Cart/Cart.php | 24 +++++++++++++++++ .../ResponseModels/Cart/CartResponse.php | 21 +++++++++++++++ tests/BigCommerce/Api/Carts/CartsApiTest.php | 22 +++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 src/BigCommerce/Api/Carts/CartsApi.php create mode 100644 src/BigCommerce/ResourceModels/Cart/Cart.php create mode 100644 src/BigCommerce/ResponseModels/Cart/CartResponse.php create mode 100644 tests/BigCommerce/Api/Carts/CartsApiTest.php diff --git a/src/BigCommerce/Api/Carts/CartsApi.php b/src/BigCommerce/Api/Carts/CartsApi.php new file mode 100644 index 00000000..aa349a46 --- /dev/null +++ b/src/BigCommerce/Api/Carts/CartsApi.php @@ -0,0 +1,27 @@ +getResource()); + } + + public function singleResourceUrl(): string + { + return sprintf(self::CART_ENDPOINT, $this->getUuid()); + } +} diff --git a/src/BigCommerce/Client.php b/src/BigCommerce/Client.php index 4e3d4659..9ba8bd66 100644 --- a/src/BigCommerce/Client.php +++ b/src/BigCommerce/Client.php @@ -2,6 +2,7 @@ namespace BigCommerce\ApiV3; +use BigCommerce\ApiV3\Api\Carts\CartsApi; use BigCommerce\ApiV3\Api\Catalog\CatalogApi; use BigCommerce\ApiV3\Api\Orders\OrdersApi; use BigCommerce\ApiV3\Api\Customers\CustomersApi; @@ -129,6 +130,18 @@ public function theme(string $uuid): ThemesApi return $api; } + public function carts(): CartsApi + { + return new CartsApi($this); + } + + public function cart(string $uuid): CartsApi + { + $api = $this->carts(); + $api->setUuid($uuid); + return $api; + } + public function order(int $orderId): OrdersApi { return new OrdersApi($this, $orderId); diff --git a/src/BigCommerce/ResourceModels/Cart/Cart.php b/src/BigCommerce/ResourceModels/Cart/Cart.php new file mode 100644 index 00000000..9a513cf2 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Cart/Cart.php @@ -0,0 +1,24 @@ +cart; + } + + protected function addData(stdClass $rawData): void + { + $this->cart = new Cart($rawData); + } +} diff --git a/tests/BigCommerce/Api/Carts/CartsApiTest.php b/tests/BigCommerce/Api/Carts/CartsApiTest.php new file mode 100644 index 00000000..1288ea96 --- /dev/null +++ b/tests/BigCommerce/Api/Carts/CartsApiTest.php @@ -0,0 +1,22 @@ +markTestIncomplete(); + } + + public function testCanCreateACart() + { + $this->markTestIncomplete(); + } + + public function testCanUpdateCustomerIdForCart() + { + $this->markTestIncomplete(); + } +} From 65847f25295487eeace61c67354ba83eac91f26c Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Thu, 1 Apr 2021 13:50:25 +1030 Subject: [PATCH 2/6] Implement tests for Carts Api --- src/BigCommerce/Api/Carts/CartsApi.php | 26 +++++++++++++ tests/BigCommerce/Api/Carts/CartsApiTest.php | 21 +++++++++- tests/BigCommerce/responses/carts_get.json | 41 ++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 tests/BigCommerce/responses/carts_get.json diff --git a/src/BigCommerce/Api/Carts/CartsApi.php b/src/BigCommerce/Api/Carts/CartsApi.php index aa349a46..5da1b35d 100644 --- a/src/BigCommerce/Api/Carts/CartsApi.php +++ b/src/BigCommerce/Api/Carts/CartsApi.php @@ -2,14 +2,18 @@ namespace BigCommerce\ApiV3\Api\Carts; +use BigCommerce\ApiV3\Api\Generic\CreateResource; use BigCommerce\ApiV3\Api\Generic\DeleteResource; use BigCommerce\ApiV3\Api\Generic\GetResource; use BigCommerce\ApiV3\Api\Generic\UuidResourceApi; +use BigCommerce\ApiV3\ResourceModels\Cart\Cart; use BigCommerce\ApiV3\ResponseModels\Cart\CartResponse; +use GuzzleHttp\RequestOptions; class CartsApi extends UuidResourceApi { use GetResource; + use CreateResource; use DeleteResource; private const CARTS_ENDPOINT = 'carts'; @@ -20,8 +24,30 @@ public function get(): CartResponse return new CartResponse($this->getResource()); } + public function create(Cart $cart): CartResponse + { + return new CartResponse($this->createResource($cart)); + } + + public function updateCustomerId(int $customerId): CartResponse + { + $response = $this->getClient()->getRestClient()->put( + $this->singleResourceUrl(), + [ + RequestOptions::JSON => ['customer_id' => $customerId], + ] + ); + + return new CartResponse($response); + } + public function singleResourceUrl(): string { return sprintf(self::CART_ENDPOINT, $this->getUuid()); } + + public function multipleResourceUrl(): string + { + return self::CARTS_ENDPOINT; + } } diff --git a/tests/BigCommerce/Api/Carts/CartsApiTest.php b/tests/BigCommerce/Api/Carts/CartsApiTest.php index 1288ea96..739f2a03 100644 --- a/tests/BigCommerce/Api/Carts/CartsApiTest.php +++ b/tests/BigCommerce/Api/Carts/CartsApiTest.php @@ -1,22 +1,39 @@ markTestIncomplete(); + $this->setReturnData('carts_get.json'); + + $id = 'aae435b7-e8a4-48f2-abcd-ad0675dc3123'; + $cart = $this->getApi()->cart($id)->get()->getCart(); + $this->assertEquals(1815, $cart->cart_amount); + $this->assertEquals("carts/$id", $this->getLastRequest()->getUri()->getPath()); } public function testCanCreateACart() { - $this->markTestIncomplete(); + $this->setReturnData('carts_get.json', 201); + $cart = new Cart(); + + $this->getApi()->carts()->create($cart); + $this->assertEquals("carts", $this->getLastRequest()->getUri()->getPath()); } public function testCanUpdateCustomerIdForCart() { + $this->setReturnData('carts_get.json', 201); + $id = 'aae435b7-e8a4-48f2-abcd-ad0675dc3123'; + + $this->getApi()->cart($id)->updateCustomerId(3); + + $this->assertEquals("carts/$id", $this->getLastRequest()->getUri()->getPath()); + $this->assertEquals(json_encode(['customer_id' => 3]), $this->getLastRequest()->getBody()); $this->markTestIncomplete(); } } diff --git a/tests/BigCommerce/responses/carts_get.json b/tests/BigCommerce/responses/carts_get.json new file mode 100644 index 00000000..c7fc6512 --- /dev/null +++ b/tests/BigCommerce/responses/carts_get.json @@ -0,0 +1,41 @@ +{ + "data": { + "id": "aae435b7-e8a4-48f2-abcd-ad0675dc3123", + "customer_id": 0, + "channel_id": 1, + "email": "", + "currency": { + "code": "AUD" + }, + "tax_included": true, + "base_amount": 1815, + "discount_amount": 0, + "cart_amount": 1815, + "coupons": [], + "line_items": { + "physical_items": [], + "digital_items": [], + "gift_certificates": [], + "custom_items": [ + { + "id": "7c0afc28-abcd-42ad-afe6-7a87d6923bc5", + "sku": "made-up", + "name": "My product", + "quantity": 33, + "list_price": 55, + "extended_list_price": 1815, + "image_url": "https://cdn11.bigcommerce.com/r-3b68d717e18793df0b51c71e16bc24664ad18b7c/themes/ClassicNext/images/ProductDefault.gif" + } + ] + }, + "created_time": "2021-04-01T03:12:25+00:00", + "updated_time": "2021-04-01T03:12:25+00:00", + "locale": "en", + "redirect_urls": { + "cart_url": "https://aligent.mybigcommerce.com/cart.php?action=load&id=aae435b7-e8a4-48f2-82ca-ad0675dc3123&token=mytoken", + "checkout_url": "https://aligent.mybigcommerce.com/cart.php?action=loadInCheckout&id=aae435b7-e8a4-48f2-abcd-ad0675dc3123&token=mytoken", + "embedded_checkout_url": "https://aligent.mybigcommerce.com/cart.php?embedded=1&action=loadInCheckout&id=aae435b7-e8a4-48f2-abcd-ad0675dc3123&token=mytoken" + } + }, + "meta": {} +} \ No newline at end of file From fda99d776b3b04e4b5a17b08b619a9be6c463900 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Thu, 1 Apr 2021 13:50:58 +1030 Subject: [PATCH 3/6] Update release notes --- RELEASE_NOTES.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 4229f1eb..a56d77aa 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,3 +1,3 @@ -### Bug Fix +### New Features -Fix issue with Metafield APIs missing create and update endpoints. +- Implement the Cart API. From bd2d69eeb2063d2bbf655d97ab35d58a5cb4371e Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Thu, 1 Apr 2021 14:37:54 +1030 Subject: [PATCH 4/6] Implement CartItems API --- RELEASE_NOTES.md | 3 +- src/BigCommerce/Api/Carts/CartItemsApi.php | 72 +++++++++++++++++++ src/BigCommerce/Api/Carts/CartsApi.php | 16 +++++ .../Generic/UuidResourceWithUuidParentApi.php | 20 ++++++ .../ResourceModels/Cart/CartItem.php | 14 ++++ .../Api/Carts/CartItemsApiTest.php | 42 +++++++++++ 6 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 src/BigCommerce/Api/Carts/CartItemsApi.php create mode 100644 src/BigCommerce/Api/Generic/UuidResourceWithUuidParentApi.php create mode 100644 src/BigCommerce/ResourceModels/Cart/CartItem.php create mode 100644 tests/BigCommerce/Api/Carts/CartItemsApiTest.php diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index a56d77aa..8ab3954a 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,3 +1,4 @@ ### New Features -- Implement the Cart API. +- Implement the [Cart API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart/createacart). +- Implement the [Cart Items API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart-items/addcartlineitem) \ No newline at end of file diff --git a/src/BigCommerce/Api/Carts/CartItemsApi.php b/src/BigCommerce/Api/Carts/CartItemsApi.php new file mode 100644 index 00000000..784605f2 --- /dev/null +++ b/src/BigCommerce/Api/Carts/CartItemsApi.php @@ -0,0 +1,72 @@ + $include] : []; + $response = $this->getClient()->getRestClient()->post( + $this->multipleResourceUrl(), + [ + RequestOptions::JSON => $cartItem, + RequestOptions::QUERY => $query, + ] + ); + + return new CartResponse($response); + } + + public function update(CartItem $cartItem, ?string $include = null): CartResponse + { + $query = $include ? ['include' => $include] : []; + $response = $this->getClient()->getRestClient()->put( + $this->singleResourceUrl(), + [ + RequestOptions::JSON => $cartItem, + RequestOptions::QUERY => $query, + ] + ); + + return new CartResponse($response); + } + + public function multipleResourceUrl(): string + { + return sprintf(self::CARTS_ENDPOINT, $this->getParentUuid()); + } + + public function singleResourceUrl(): string + { + return sprintf(self::CART_ENDPOINT, $this->getParentUuid(), $this->getUuid()); + } +} diff --git a/src/BigCommerce/Api/Carts/CartsApi.php b/src/BigCommerce/Api/Carts/CartsApi.php index 5da1b35d..25c5d575 100644 --- a/src/BigCommerce/Api/Carts/CartsApi.php +++ b/src/BigCommerce/Api/Carts/CartsApi.php @@ -50,4 +50,20 @@ public function multipleResourceUrl(): string { return self::CARTS_ENDPOINT; } + + public function item(string $id): CartItemsApi + { + $itemsApi = $this->items(); + $itemsApi->setUuid($id); + + return $itemsApi; + } + + public function items(): CartItemsApi + { + $itemsApi = new CartItemsApi($this->getClient()); + $itemsApi->setParentUuid($this->getUuid()); + + return $itemsApi; + } } diff --git a/src/BigCommerce/Api/Generic/UuidResourceWithUuidParentApi.php b/src/BigCommerce/Api/Generic/UuidResourceWithUuidParentApi.php new file mode 100644 index 00000000..00e1240a --- /dev/null +++ b/src/BigCommerce/Api/Generic/UuidResourceWithUuidParentApi.php @@ -0,0 +1,20 @@ +parentUuid; + } + + public function setParentUuid(string $parentUuid): void + { + $this->parentUuid = $parentUuid; + } +} diff --git a/src/BigCommerce/ResourceModels/Cart/CartItem.php b/src/BigCommerce/ResourceModels/Cart/CartItem.php new file mode 100644 index 00000000..ce7581b2 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Cart/CartItem.php @@ -0,0 +1,14 @@ +setReturnData('carts_get.json', 201); + $id = 'aae435b7-e8a4-48f2-abcd-ad0675dc3123'; + + $lineItem = new CartItem(); + $lineItem->line_items[] = [ + "sku" => "made-up", + "name" => "My product", + "quantity" => 33, + "list_price" => 55 + ]; + + $this->getApi()->cart($id)->items()->add($lineItem, CartItemsApi::INCLUDE_REDIRECT_URLS); + + $this->assertEquals("carts/$id/items", $this->getLastRequest()->getUri()->getPath()); + } + + public function testCanDeleteCartLineItem() + { + $this->setReturnData('blank.json', 204); + $id = 'aae435b7-e8a4-48f2-abcd-ad0675dc3123'; + $lineItemId = '7c0afc28-abcd-42ad-afe6-7a87d6923bc5'; + + $this->getApi()->cart($id)->item($lineItemId)->delete(); + $lastRequest = $this->getLastRequest(); + $this->assertEquals("carts/$id/items/$lineItemId", $lastRequest->getUri()->getPath()); + $this->assertEquals('DELETE', $lastRequest->getMethod()); + } +} From 6c1f0cc17530dfd200a984b10c38dcec1570e0ef Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Thu, 1 Apr 2021 15:18:34 +1030 Subject: [PATCH 5/6] Implement the Cart Redirect URLs API --- RELEASE_NOTES.md | 3 ++- .../Api/Carts/CartRedirectUrlsApi.php | 22 +++++++++++++++++ src/BigCommerce/Api/Carts/CartsApi.php | 8 +++++++ .../ResourceModels/Cart/CartRedirectUrls.php | 13 ++++++++++ .../Cart/CartRedirectUrlsResponse.php | 24 +++++++++++++++++++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/BigCommerce/Api/Carts/CartRedirectUrlsApi.php create mode 100644 src/BigCommerce/ResourceModels/Cart/CartRedirectUrls.php create mode 100644 src/BigCommerce/ResponseModels/Cart/CartRedirectUrlsResponse.php diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 8ab3954a..0bdd4708 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,4 +1,5 @@ ### New Features - Implement the [Cart API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart/createacart). -- Implement the [Cart Items API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart-items/addcartlineitem) \ No newline at end of file +- Implement the [Cart Items API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart-items/addcartlineitem) +- Implement the [Cart Redirect URLS API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart-redirect-urls/createcartredirecturl) diff --git a/src/BigCommerce/Api/Carts/CartRedirectUrlsApi.php b/src/BigCommerce/Api/Carts/CartRedirectUrlsApi.php new file mode 100644 index 00000000..70adec0c --- /dev/null +++ b/src/BigCommerce/Api/Carts/CartRedirectUrlsApi.php @@ -0,0 +1,22 @@ +getClient()->getRestClient()->put( + sprintf(self::REDIRECT_URL, $this->getParentUuid()) + ); + + return new CartRedirectUrlsResponse($response); + } +} diff --git a/src/BigCommerce/Api/Carts/CartsApi.php b/src/BigCommerce/Api/Carts/CartsApi.php index 25c5d575..66b4b7fc 100644 --- a/src/BigCommerce/Api/Carts/CartsApi.php +++ b/src/BigCommerce/Api/Carts/CartsApi.php @@ -66,4 +66,12 @@ public function items(): CartItemsApi return $itemsApi; } + + public function redirectUrls(): CartRedirectUrlsApi + { + $redirectsApi = new CartRedirectUrlsApi(); + $redirectsApi->setParentUuid($this->getUuid()); + + return $redirectsApi; + } } diff --git a/src/BigCommerce/ResourceModels/Cart/CartRedirectUrls.php b/src/BigCommerce/ResourceModels/Cart/CartRedirectUrls.php new file mode 100644 index 00000000..b679c885 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Cart/CartRedirectUrls.php @@ -0,0 +1,13 @@ +cartRedirectUrls; + } + + protected function addData(stdClass $rawData): void + { + $this->cartRedirectUrls = new CartRedirectUrls($rawData); + } +} From a1d079483d26065a3a2605dea7951aec9e82bc47 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Sun, 4 Apr 2021 19:46:36 +0930 Subject: [PATCH 6/6] Fix code style issues --- src/BigCommerce/Api/Carts/CartItemsApi.php | 2 -- src/BigCommerce/Api/Carts/CartRedirectUrlsApi.php | 2 -- src/BigCommerce/Api/Generic/UuidResourceWithUuidParentApi.php | 2 -- src/BigCommerce/ResourceModels/Cart/Cart.php | 1 + src/BigCommerce/ResourceModels/Cart/CartItem.php | 2 -- src/BigCommerce/ResourceModels/Cart/CartRedirectUrls.php | 2 -- .../ResponseModels/Cart/CartRedirectUrlsResponse.php | 2 -- src/BigCommerce/ResponseModels/Cart/CartResponse.php | 1 + tests/BigCommerce/Api/Carts/CartItemsApiTest.php | 2 -- tests/BigCommerce/Api/Carts/CartsApiTest.php | 1 + 10 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/BigCommerce/Api/Carts/CartItemsApi.php b/src/BigCommerce/Api/Carts/CartItemsApi.php index 784605f2..b36f8516 100644 --- a/src/BigCommerce/Api/Carts/CartItemsApi.php +++ b/src/BigCommerce/Api/Carts/CartItemsApi.php @@ -1,9 +1,7 @@