-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from aligent/feature/implement-cart-api
Feature/implement cart api
- Loading branch information
Showing
14 changed files
with
416 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace BigCommerce\ApiV3\Api\Carts; | ||
|
||
use BigCommerce\ApiV3\Api\Generic\DeleteResource; | ||
use BigCommerce\ApiV3\Api\Generic\UuidResourceWithUuidParentApi; | ||
use BigCommerce\ApiV3\ResourceModels\Cart\CartItem; | ||
use BigCommerce\ApiV3\ResponseModels\Cart\CartResponse; | ||
use GuzzleHttp\RequestOptions; | ||
|
||
class CartItemsApi extends UuidResourceWithUuidParentApi | ||
{ | ||
use DeleteResource; | ||
|
||
private const CARTS_ENDPOINT = 'carts/%s/items'; | ||
private const CART_ENDPOINT = 'carts/%s/items/%s'; | ||
|
||
/** | ||
* Create a direct link to a Cart. | ||
*/ | ||
public const INCLUDE_REDIRECT_URLS = 'redirect_urls'; | ||
|
||
/** | ||
* The Cart returns an abbreviated result. Use this to return physical items product options. | ||
*/ | ||
public const INCLUDE_PHYSICAL_ITEMS = 'line_items.physical_items.options'; | ||
|
||
/** | ||
* The Cart returns an abbreviated result. Use this to return digital items product options. | ||
*/ | ||
public const INCLUDE_DIGITAL_ITEMS = 'line_items.digital_items.options'; | ||
|
||
public function add(CartItem $cartItem, ?string $include = null): CartResponse | ||
{ | ||
$query = $include ? ['include' => $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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace BigCommerce\ApiV3\Api\Carts; | ||
|
||
use BigCommerce\ApiV3\Api\Generic\UuidResourceWithUuidParentApi; | ||
use BigCommerce\ApiV3\ResponseModels\Cart\CartRedirectUrlsResponse; | ||
|
||
class CartRedirectUrlsApi extends UuidResourceWithUuidParentApi | ||
{ | ||
private const REDIRECT_URL = 'carts/%d/redirect_urls'; | ||
|
||
public function create(): CartRedirectUrlsResponse | ||
{ | ||
$response = $this->getClient()->getRestClient()->put( | ||
sprintf(self::REDIRECT_URL, $this->getParentUuid()) | ||
); | ||
|
||
return new CartRedirectUrlsResponse($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
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'; | ||
private const CART_ENDPOINT = 'carts/%s'; | ||
|
||
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; | ||
} | ||
|
||
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; | ||
} | ||
|
||
public function redirectUrls(): CartRedirectUrlsApi | ||
{ | ||
$redirectsApi = new CartRedirectUrlsApi(); | ||
$redirectsApi->setParentUuid($this->getUuid()); | ||
|
||
return $redirectsApi; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/BigCommerce/Api/Generic/UuidResourceWithUuidParentApi.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace BigCommerce\ApiV3\Api\Generic; | ||
|
||
abstract class UuidResourceWithUuidParentApi extends UuidResourceApi | ||
{ | ||
private string $parentUuid; | ||
|
||
public function getParentUuid(): string | ||
{ | ||
return $this->parentUuid; | ||
} | ||
|
||
public function setParentUuid(string $parentUuid): void | ||
{ | ||
$this->parentUuid = $parentUuid; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace BigCommerce\ApiV3\ResourceModels\Cart; | ||
|
||
use BigCommerce\ApiV3\ResourceModels\ResourceModel; | ||
|
||
class Cart extends ResourceModel | ||
{ | ||
public string $id; | ||
public string $parent_id; | ||
public int $customer_id; | ||
public string $email; | ||
public $currency; | ||
public bool $tax_included; | ||
public float $base_amount; | ||
public float $discount_amount; | ||
public float $cart_amount; | ||
public array $coupons; | ||
public array $discounts; | ||
public $line_items; | ||
public string $created_time; | ||
public string $updated_time; | ||
public int $channel_id; | ||
public string $locale; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace BigCommerce\ApiV3\ResourceModels\Cart; | ||
|
||
use BigCommerce\ApiV3\ResourceModels\ResourceModel; | ||
|
||
class CartItem extends ResourceModel | ||
{ | ||
public array $line_items; | ||
public array $gift_certificates; | ||
public array $custom_items; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace BigCommerce\ApiV3\ResourceModels\Cart; | ||
|
||
use BigCommerce\ApiV3\ResourceModels\ResourceModel; | ||
|
||
class CartRedirectUrls extends ResourceModel | ||
{ | ||
public string $cart_url; | ||
public string $checkout_url; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/BigCommerce/ResponseModels/Cart/CartRedirectUrlsResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace BigCommerce\ApiV3\ResponseModels\Cart; | ||
|
||
use BigCommerce\ApiV3\ResourceModels\Cart\CartRedirectUrls; | ||
use BigCommerce\ApiV3\ResponseModels\SingleResourceResponse; | ||
use stdClass; | ||
|
||
class CartRedirectUrlsResponse extends SingleResourceResponse | ||
{ | ||
private CartRedirectUrls $cartRedirectUrls; | ||
|
||
public function getCartRedirectUrls(): CartRedirectUrls | ||
{ | ||
return $this->cartRedirectUrls; | ||
} | ||
|
||
protected function addData(stdClass $rawData): void | ||
{ | ||
$this->cartRedirectUrls = new CartRedirectUrls($rawData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace BigCommerce\ApiV3\ResponseModels\Cart; | ||
|
||
use BigCommerce\ApiV3\ResourceModels\Cart\Cart; | ||
use BigCommerce\ApiV3\ResponseModels\SingleResourceResponse; | ||
use stdClass; | ||
|
||
class CartResponse extends SingleResourceResponse | ||
{ | ||
private Cart $cart; | ||
|
||
public function getCart(): Cart | ||
{ | ||
return $this->cart; | ||
} | ||
|
||
protected function addData(stdClass $rawData): void | ||
{ | ||
$this->cart = new Cart($rawData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace BigCommerce\Tests\Api\Carts; | ||
|
||
use BigCommerce\ApiV3\Api\Carts\CartItemsApi; | ||
use BigCommerce\ApiV3\ResourceModels\Cart\CartItem; | ||
use BigCommerce\Tests\BigCommerceApiTest; | ||
|
||
class CartItemsApiTest extends BigCommerceApiTest | ||
{ | ||
public function testCanAddCartLineItem() | ||
{ | ||
$this->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()); | ||
} | ||
} |
Oops, something went wrong.