Skip to content

Commit 3f760d2

Browse files
nrostrow-metaNoah Ostrowski
andauthored
Dynamic checkout cart totals api (#617)
* Initial version of cartTotals API * Fixing spacing * Making the error we return for NoSuchEntityException more helpful * Fixing static test error with how I was setting LocalizedException message --------- Co-authored-by: Noah Ostrowski <[email protected]>
1 parent fe5c646 commit 3f760d2

File tree

5 files changed

+170
-1
lines changed

5 files changed

+170
-1
lines changed
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+
use Magento\Quote\Api\Data\TotalsInterface;
24+
25+
/**
26+
* Get Magento cart totals
27+
*/
28+
interface CartTotalsApiInterface
29+
{
30+
/**
31+
* Get Magento cart totals
32+
*
33+
* @param string $externalBusinessId
34+
* @param string $cartId
35+
* @return \Magento\Quote\Api\Data\TotalsInterface
36+
* @throws UnauthorizedTokenException
37+
* @throws \Magento\Framework\Exception\NoSuchEntityException
38+
* @throws \Magento\Framework\Exception\LocalizedException
39+
*/
40+
public function getCartTotals(string $externalBusinessId, string $cartId): TotalsInterface;
41+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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\Framework\Exception\LocalizedException;
24+
use Magento\Framework\Exception\NoSuchEntityException;
25+
use Magento\Quote\Api\Data\TotalsInterface;
26+
use Magento\Quote\Model\GuestCart\GuestCartTotalRepository;
27+
use Meta\BusinessExtension\Helper\FBEHelper;
28+
use Meta\BusinessExtension\Model\Api\CustomApiKey\Authenticator;
29+
use Meta\Sales\Api\CartTotalsApiInterface;
30+
use Meta\Sales\Helper\OrderHelper;
31+
32+
class CartTotalsApi implements CartTotalsApiInterface
33+
{
34+
/**
35+
* @var Authenticator
36+
*/
37+
private Authenticator $authenticator;
38+
39+
/**
40+
* @var OrderHelper
41+
*/
42+
private OrderHelper $orderHelper;
43+
44+
/**
45+
* @var GuestCartTotalRepository
46+
*/
47+
private GuestCartTotalRepository $guestCartTotalRepository;
48+
49+
/**
50+
* @var FBEHelper
51+
*/
52+
private FBEHelper $fbeHelper;
53+
54+
/**
55+
* @param Authenticator $authenticator
56+
* @param OrderHelper $orderHelper
57+
* @param GuestCartTotalRepository $guestCartTotalRepository
58+
* @param FBEHelper $fbeHelper
59+
*/
60+
public function __construct(
61+
Authenticator $authenticator,
62+
OrderHelper $orderHelper,
63+
GuestCartTotalRepository $guestCartTotalRepository,
64+
FBEHelper $fbeHelper
65+
) {
66+
$this->authenticator = $authenticator;
67+
$this->orderHelper = $orderHelper;
68+
$this->guestCartTotalRepository = $guestCartTotalRepository;
69+
$this->fbeHelper = $fbeHelper;
70+
}
71+
72+
/**
73+
* Get Magento cart totals
74+
*
75+
* @param string $externalBusinessId
76+
* @param string $cartId
77+
* @return \Magento\Quote\Api\Data\TotalsInterface
78+
* @throws UnauthorizedTokenException
79+
* @throws \Magento\Framework\Exception\NoSuchEntityException
80+
* @throws \Magento\Framework\Exception\LocalizedException
81+
*/
82+
public function getCartTotals(string $externalBusinessId, string $cartId): TotalsInterface
83+
{
84+
$this->authenticator->authenticateRequest();
85+
$storeId = $this->orderHelper->getStoreIdByExternalBusinessId($externalBusinessId);
86+
try {
87+
return $this->guestCartTotalRepository->get($cartId);
88+
} catch (NoSuchEntityException $e) {
89+
$le = new LocalizedException(__(
90+
"No such entity with cartId = %1",
91+
$cartId
92+
));
93+
$this->fbeHelper->logExceptionImmediatelyToMeta(
94+
$le,
95+
[
96+
'store_id' => $storeId,
97+
'event' => 'cart_totals_api',
98+
'event_type' => 'no_such_entity_exception',
99+
'extra_data' => [
100+
'cart_id' => $cartId
101+
]
102+
]
103+
);
104+
throw $le;
105+
} catch (\Throwable $e) {
106+
$this->fbeHelper->logExceptionImmediatelyToMeta(
107+
$e,
108+
[
109+
'store_id' => $storeId,
110+
'event' => 'cart_totals_api',
111+
'event_type' => 'error_getting_cart_totals',
112+
'extra_data' => [
113+
'cart_id' => $cartId
114+
]
115+
]
116+
);
117+
throw $e;
118+
}
119+
}
120+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
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"/>
99
<resource id="Meta_Sales::add_cart_items" title="Add Cart Items" sortOrder="101"/>
10-
<resource id="Meta_Sales::cart_shipping_opttions" title="Cart Shipping Options" sortOrder="102"/>
10+
<resource id="Meta_Sales::cart_shipping_options" title="Cart Shipping Options" sortOrder="102"/>
11+
<resource id="Meta_Sales::cart_totals" title="Cart Totals" sortOrder="103"/>
1112
</resource>
1213
</resource>
1314
</resources>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<preference for="Meta\Sales\Api\AddCartItemsApiInterface" type="Meta\Sales\Model\Api\AddCartItemsApi"/>
77
<preference for="Meta\Sales\Api\AddCartItemsApiResponseInterface" type="Meta\Sales\Model\Api\AddCartItemsApiResponse"/>
88
<preference for="Meta\Sales\Api\CartShippingOptionsApiInterface" type="Meta\Sales\Model\Api\CartShippingOptionsApi"/>
9+
<preference for="Meta\Sales\Api\CartTotalsApiInterface" type="Meta\Sales\Model\Api\CartTotalsApi"/>
910
<type name="Magento\Sales\Api\OrderRepositoryInterface">
1011
<plugin name="facebook_order_extension" type="Meta\Sales\Plugin\OrderGet"/>
1112
</type>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@
1919
<resource ref="anonymous"/>
2020
</resources>
2121
</route>
22+
<route url="/V1/meta/:externalBusinessId/:cartId/cartTotals" method="GET">
23+
<service class="Meta\Sales\Api\CartTotalsApiInterface" method="getCartTotals"/>
24+
<resources>
25+
<resource ref="anonymous"/>
26+
</resources>
27+
</route>
2228
</routes>

0 commit comments

Comments
 (0)