Skip to content

Commit 80e8da8

Browse files
nrostrow-metaNoah Ostrowski
andauthored
Dynamic checkout cart shipping options api (#619)
* Setting up wrapper API for fetching cart shipping options * Fixing static test error with how I wrote LocalizedException message --------- Co-authored-by: Noah Ostrowski <[email protected]>
1 parent c99a5e7 commit 80e8da8

File tree

5 files changed

+172
-0
lines changed

5 files changed

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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"/>
1011
</resource>
1112
</resource>
1213
</resources>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<preference for="Meta\Sales\Api\CreateCartApiInterface" type="Meta\Sales\Model\Api\CreateCartApi"/>
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"/>
8+
<preference for="Meta\Sales\Api\CartShippingOptionsApiInterface" type="Meta\Sales\Model\Api\CartShippingOptionsApi"/>
89
<type name="Magento\Sales\Api\OrderRepositoryInterface">
910
<plugin name="facebook_order_extension" type="Meta\Sales\Plugin\OrderGet"/>
1011
</type>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313
<resource ref="anonymous"/>
1414
</resources>
1515
</route>
16+
<route url="/V1/meta/:externalBusinessId/:cartId/cartShippingOptions" method="POST">
17+
<service class="Meta\Sales\Api\CartShippingOptionsApiInterface" method="cartShippingOptions"/>
18+
<resources>
19+
<resource ref="anonymous"/>
20+
</resources>
21+
</route>
1622
</routes>

0 commit comments

Comments
 (0)