Skip to content

Commit 0a9c00e

Browse files
nrostrow-metaNoah Ostrowski
andauthored
Setting up custom API to enable setting shipping option for cart (#620)
* Setting up custom API to enable setting shipping option for cart * Fixing static test failures * Fixing static test failures --------- Co-authored-by: Noah Ostrowski <[email protected]>
1 parent ebe7c1b commit 0a9c00e

File tree

7 files changed

+180
-0
lines changed

7 files changed

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

app/code/Meta/Sales/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"magento/module-offline-shipping": "*",
1818
"magento/module-payment": "*",
1919
"magento/module-quote": "*",
20+
"magento/module-checkout": "*",
2021
"magento/module-shipping": "*",
2122
"magento/module-newsletter": "*",
2223
"meta/module-business-extension": "*",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<resource id="Meta_Sales::add_cart_items" title="Add Cart Items" sortOrder="101"/>
1010
<resource id="Meta_Sales::cart_shipping_options" title="Cart Shipping Options" sortOrder="102"/>
1111
<resource id="Meta_Sales::cart_totals" title="Cart Totals" sortOrder="103"/>
12+
<resource id="Meta_Sales::set_cart_shipping_option" title="Set Cart Shipping Option" sortOrder="107"/>
1213
<resource id="Meta_Sales::delete_cart_item" title="Delete Cart Item" sortOrder="108"/>
1314
</resource>
1415
</resource>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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"/>
99
<preference for="Meta\Sales\Api\CartTotalsApiInterface" type="Meta\Sales\Model\Api\CartTotalsApi"/>
10+
<preference for="Meta\Sales\Api\SetCartShippingOptionApiInterface" type="Meta\Sales\Model\Api\SetCartShippingOptionApi"/>
1011
<preference for="Meta\Sales\Api\DeleteCartItemApiInterface" type="Meta\Sales\Model\Api\DeleteCartItemApi"/>
1112
<type name="Magento\Sales\Api\OrderRepositoryInterface">
1213
<plugin name="facebook_order_extension" type="Meta\Sales\Plugin\OrderGet"/>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module name="Magento_Directory"/>
1515
<module name="Magento_Payment"/>
1616
<module name="Magento_Quote"/>
17+
<module name="Magento_Checkout"/>
1718
<module name="Magento_SalesSequence"/>
1819
<module name="Magento_Shipping"/>
1920
<module name="Magento_Webapi"/>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
<resource ref="anonymous"/>
2626
</resources>
2727
</route>
28+
<route url="/V1/meta/:externalBusinessId/:cartId/setCartShippingOption" method="POST">
29+
<service class="Meta\Sales\Api\SetCartShippingOptionApiInterface" method="setCartShippingOption"/>
30+
<resources>
31+
<resource ref="anonymous"/>
32+
</resources>
33+
</route>
2834
<route url="/V1/meta/:externalBusinessId/:cartId/:itemId/deleteCartItem" method="DELETE">
2935
<service class="Meta\Sales\Api\DeleteCartItemApiInterface" method="deleteCartItem"/>
3036
<resources>

0 commit comments

Comments
 (0)