Skip to content

Commit ad13fd1

Browse files
nrostrow-metaNoah Ostrowski
andauthored
Setting up API to remove coupon from dynamic checkout cart (#628)
Co-authored-by: Noah Ostrowski <[email protected]>
1 parent 4f257ad commit ad13fd1

File tree

5 files changed

+169
-0
lines changed

5 files changed

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

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::delete_cart_coupon" title="Delete Cart Coupon" sortOrder="106"/>
1213
<resource id="Meta_Sales::set_cart_shipping_option" title="Set Cart Shipping Option" sortOrder="107"/>
1314
<resource id="Meta_Sales::delete_cart_item" title="Delete Cart Item" sortOrder="108"/>
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\DeleteCartCouponApiInterface" type="Meta\Sales\Model\Api\DeleteCartCouponApi"/>
1011
<preference for="Meta\Sales\Api\SetCartShippingOptionApiInterface" type="Meta\Sales\Model\Api\SetCartShippingOptionApi"/>
1112
<preference for="Meta\Sales\Api\DeleteCartItemApiInterface" type="Meta\Sales\Model\Api\DeleteCartItemApi"/>
1213
<type name="Magento\Sales\Api\OrderRepositoryInterface">

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/deleteCartCoupon" method="DELETE">
29+
<service class="Meta\Sales\Api\DeleteCartCouponApiInterface" method="deleteCartCoupon"/>
30+
<resources>
31+
<resource ref="anonymous"/>
32+
</resources>
33+
</route>
2834
<route url="/V1/meta/:externalBusinessId/:cartId/setCartShippingOption" method="POST">
2935
<service class="Meta\Sales\Api\SetCartShippingOptionApiInterface" method="setCartShippingOption"/>
3036
<resources>

0 commit comments

Comments
 (0)