Skip to content

Commit 2014271

Browse files
nrostrow-metaNoah Ostrowski
andauthored
Dynamic checkout add cart items api (#616)
* Initial basic version of addCartItems dynamic checkout API * Set up new response interface for API * Fixing comment * Updating logging and changing variables from protected to private * Fixing static test failures --------- Co-authored-by: Noah Ostrowski <[email protected]>
1 parent 059fefd commit 2014271

File tree

7 files changed

+281
-0
lines changed

7 files changed

+281
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Sales\Api;
22+
23+
use Meta\Sales\Api\AddCartItemsApiResponseInterface;
24+
25+
/**
26+
* Add items to Magento cart
27+
*/
28+
interface AddCartItemsApiInterface
29+
{
30+
/**
31+
* Add items to Magento cart
32+
*
33+
* @param string $externalBusinessId
34+
* @param \Magento\Quote\Api\Data\CartItemInterface[] $items
35+
* @return \Meta\Sales\Api\AddCartItemsApiResponseInterface
36+
* @throws UnauthorizedTokenException
37+
* @throws LocalizedException
38+
*/
39+
public function addCartItems(string $externalBusinessId, array $items): AddCartItemsApiResponseInterface;
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Sales\Api;
22+
23+
/**
24+
* AddCartItemsApi response
25+
*/
26+
interface AddCartItemsApiResponseInterface
27+
{
28+
/**
29+
* Get array of CartItemInterface for items added successfully
30+
*
31+
* @return \Magento\Quote\Api\Data\CartItemInterface[]
32+
*/
33+
public function getItemsAdded(): array;
34+
35+
/**
36+
* Get array of SKU/exception message for items that failed to be added
37+
*
38+
* @return array
39+
*/
40+
public function getExceptions(): array;
41+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Sales\Model\Api;
22+
23+
use Magento\Quote\Model\GuestCart\GuestCartItemRepository;
24+
use Meta\BusinessExtension\Helper\FBEHelper;
25+
use Meta\BusinessExtension\Model\Api\CustomApiKey\Authenticator;
26+
use Meta\Sales\Api\AddCartItemsApiInterface;
27+
use Meta\Sales\Api\AddCartItemsApiResponseInterface;
28+
use Meta\Sales\Helper\OrderHelper;
29+
use Meta\Sales\Model\Api\AddCartItemsApiResponse;
30+
31+
class AddCartItemsApi implements AddCartItemsApiInterface
32+
{
33+
/**
34+
* @var Authenticator
35+
*/
36+
private Authenticator $authenticator;
37+
38+
/**
39+
* @var OrderHelper
40+
*/
41+
private OrderHelper $orderHelper;
42+
43+
/**
44+
* @var GuestCartItemRepository
45+
*/
46+
private GuestCartItemRepository $guestCartItemRepository;
47+
48+
/**
49+
* @var FBEHelper
50+
*/
51+
private FBEHelper $fbeHelper;
52+
53+
/**
54+
* @param Authenticator $authenticator
55+
* @param OrderHelper $orderHelper
56+
* @param GuestCartItemRepository $guestCartItemRepository
57+
* @param FBEHelper $fbeHelper
58+
*/
59+
public function __construct(
60+
Authenticator $authenticator,
61+
OrderHelper $orderHelper,
62+
GuestCartitemRepository $guestCartItemRepository,
63+
FBEHelper $fbeHelper
64+
) {
65+
$this->authenticator = $authenticator;
66+
$this->orderHelper = $orderHelper;
67+
$this->guestCartItemRepository = $guestCartItemRepository;
68+
$this->fbeHelper = $fbeHelper;
69+
}
70+
71+
/**
72+
* Add items to Magento cart
73+
*
74+
* @param string $externalBusinessId
75+
* @param \Magento\Quote\Api\Data\CartItemInterface[] $items
76+
* @return \Meta\Sales\Api\AddCartItemsApiResponseInterface
77+
* @throws UnauthorizedTokenException
78+
* @throws LocalizedException
79+
*/
80+
public function addCartItems(string $externalBusinessId, array $items): AddCartItemsApiResponseInterface
81+
{
82+
$this->authenticator->authenticateRequest();
83+
$storeId = $this->orderHelper->getStoreIdByExternalBusinessId($externalBusinessId);
84+
$itemsAdded = [];
85+
$exceptions = [];
86+
foreach ($items as $item) {
87+
try {
88+
$itemsAdded[] = $this->guestCartItemRepository->save($item);
89+
} catch (\Exception $e) {
90+
$exceptions[] = [
91+
'sku' => $item->getSku(),
92+
'exception_message' => $e->getMessage()
93+
];
94+
$this->fbeHelper->logExceptionImmediatelyToMeta(
95+
$e,
96+
[
97+
'store_id' => $storeId,
98+
'event' => 'add_cart_items_api',
99+
'event_type' => 'error_adding_item',
100+
'extra_data' => [
101+
'cart_id' => $item->getQuoteId(),
102+
'sku' => $item->getSku()
103+
]
104+
]
105+
);
106+
}
107+
}
108+
$response = new AddCartItemsApiResponse();
109+
$response->setItemsAdded($itemsAdded);
110+
$response->setExceptions($exceptions);
111+
return $response;
112+
}
113+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Sales\Model\Api;
22+
23+
use Meta\Sales\Api\AddCartItemsApiResponseInterface;
24+
25+
class AddCartItemsApiResponse implements AddCartitemsApiResponseInterface
26+
{
27+
/**
28+
* @var \Magento\Quote\Api\Data\CartItemInterfaceCartItemInterface[]
29+
*/
30+
private $itemsAdded = [];
31+
32+
/**
33+
* @var array
34+
*/
35+
private $exceptions = [];
36+
37+
/**
38+
* Getter
39+
*
40+
* @return \Magento\Quote\Api\Data\CartItemInterfaceCartItemInterface[]
41+
*/
42+
public function getItemsAdded(): array
43+
{
44+
return $this->itemsAdded;
45+
}
46+
47+
/**
48+
* Setter
49+
*
50+
* @param \Magento\Quote\Api\Data\CartItemInterface[] $items
51+
* @return void
52+
*/
53+
public function setItemsAdded(array $items): void
54+
{
55+
$this->itemsAdded = $items;
56+
}
57+
58+
/**
59+
* Getter
60+
*
61+
* @return array
62+
*/
63+
public function getExceptions(): array
64+
{
65+
return $this->exceptions;
66+
}
67+
68+
/**
69+
* Setter
70+
*
71+
* @param array $exceptions
72+
* @return void
73+
*/
74+
public function setExceptions(array $exceptions): void
75+
{
76+
$this->exceptions = $exceptions;
77+
}
78+
}

app/code/Meta/Sales/etc/acl.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<resource id="Magento_Backend::admin">
77
<resource id="Meta_Sales::rest_api" title="Meta - Orders API" sortOrder="52">
88
<resource id="Meta_Sales::create_cart" title="Create Cart" sortOrder="100"/>
9+
<resource id="Meta_Sales::add_cart_items" title="Add Cart Items" sortOrder="101"/>
910
</resource>
1011
</resource>
1112
</resources>

app/code/Meta/Sales/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
44
<preference for="Meta\Sales\Api\Data\FacebookOrderInterface" type="Meta\Sales\Model\FacebookOrder"/>
55
<preference for="Meta\Sales\Api\CreateCartApiInterface" type="Meta\Sales\Model\Api\CreateCartApi"/>
6+
<preference for="Meta\Sales\Api\AddCartItemsApiInterface" type="Meta\Sales\Model\Api\AddCartItemsApi"/>
7+
<preference for="Meta\Sales\Api\AddCartItemsApiResponseInterface" type="Meta\Sales\Model\Api\AddCartItemsApiResponse"/>
68
<type name="Magento\Sales\Api\OrderRepositoryInterface">
79
<plugin name="facebook_order_extension" type="Meta\Sales\Plugin\OrderGet"/>
810
</type>

app/code/Meta/Sales/etc/webapi.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@
77
<resource ref="anonymous"/>
88
</resources>
99
</route>
10+
<route url="/V1/meta/:externalBusinessId/addCartItems" method="POST">
11+
<service class="Meta\Sales\Api\AddCartItemsApiInterface" method="addCartItems"/>
12+
<resources>
13+
<resource ref="anonymous"/>
14+
</resources>
15+
</route>
1016
</routes>

0 commit comments

Comments
 (0)