Skip to content

Commit b9b2b30

Browse files
nrostrow-metaNoah Ostrowski
andauthored
Dynamic checkout add cart coupon api (#627)
* API to enable adding coupon to cart * Adding config value check to addCartCoupon * Modifying API return interface to include coupon rule in response * Getting status correctly instead of hard-coding for testing * Refactoring API logic a little bit * Adding missing semicolon * Allowing rule to be set as null * Setting config value back to 0 * Fixing static test failures --------- Co-authored-by: Noah Ostrowski <[email protected]>
1 parent ad13fd1 commit b9b2b30

File tree

9 files changed

+374
-0
lines changed

9 files changed

+374
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\AddCartCouponApiResponseInterface;
24+
25+
/**
26+
* Add coupon to Magento cart
27+
*/
28+
interface AddCartCouponApiInterface
29+
{
30+
/**
31+
* Add coupon to Magento cart
32+
*
33+
* @param string $externalBusinessId
34+
* @param string $cartId
35+
* @param string $couponCode
36+
* @return \Meta\Sales\Api\AddCartCouponApiResponseInterface
37+
* @throws \Magento\Framework\Exception\UnauthorizedTokenException
38+
* @throws \Magento\Framework\Exception\NoSuchEntityException
39+
* @throws \Magento\Framework\Exception\CouldNotSaveException
40+
* @throws \Magento\Framework\Exception\LocalizedException
41+
*/
42+
public function addCartCoupon(
43+
string $externalBusinessId,
44+
string $cartId,
45+
string $couponCode
46+
): AddCartCouponApiResponseInterface;
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\SalesRule\Api\Data\RuleInterface;
24+
25+
/**
26+
* AddCartCouponApi response
27+
*/
28+
interface AddCartCouponApiResponseInterface
29+
{
30+
/**
31+
* Get status of trying to add coupon to Magento cart
32+
*
33+
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
34+
* @return bool
35+
*/
36+
public function getStatus(): bool;
37+
38+
/**
39+
* Get rule of coupon trying to be added to Magento cart
40+
*
41+
* @return \Magento\SalesRule\Api\Data\RuleInterface|null
42+
*/
43+
public function getRule(): ?RuleInterface;
44+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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 Magento\SalesRule\Api\RuleRepositoryInterface;
27+
use Magento\SalesRule\Api\Data\RuleInterface;
28+
use Magento\SalesRule\Model\CouponFactory;
29+
use Meta\BusinessExtension\Helper\FBEHelper;
30+
use Meta\BusinessExtension\Model\Api\CustomApiKey\Authenticator;
31+
use Meta\Sales\Api\AddCartCouponApiInterface;
32+
use Meta\Sales\Api\AddCartCouponApiResponseInterface;
33+
use Meta\Sales\Helper\OrderHelper;
34+
use Meta\Sales\Model\Api\AddCartCouponApiResponse;
35+
36+
/**
37+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38+
*/
39+
class AddCartCouponApi implements AddCartCouponApiInterface
40+
{
41+
/**
42+
* @var Authenticator
43+
*/
44+
private Authenticator $authenticator;
45+
46+
/**
47+
* @var OrderHelper
48+
*/
49+
private OrderHelper $orderHelper;
50+
51+
/**
52+
* @var GuestCouponManagement
53+
*/
54+
private GuestCouponManagement $guestCouponManagement;
55+
56+
/**
57+
* @var CouponFactory
58+
*/
59+
private CouponFactory $couponFactory;
60+
61+
/**
62+
* @var RuleRepositoryInterface
63+
*/
64+
private RuleRepositoryInterface $ruleRepository;
65+
66+
/**
67+
* @var FBEHelper
68+
*/
69+
private FBEHelper $fbeHelper;
70+
71+
/**
72+
* @param Authenticator $authenticator
73+
* @param OrderHelper $orderHelper
74+
* @param GuestCouponManagement $guestCouponManagement
75+
* @param CouponFactory $couponFactory
76+
* @param RuleRepositoryInterface $ruleRepository
77+
* @param FBEHelper $fbeHelper
78+
*/
79+
public function __construct(
80+
Authenticator $authenticator,
81+
OrderHelper $orderHelper,
82+
GuestCouponManagement $guestCouponManagement,
83+
CouponFactory $couponFactory,
84+
RuleRepositoryInterface $ruleRepository,
85+
FBEHelper $fbeHelper
86+
) {
87+
$this->authenticator = $authenticator;
88+
$this->orderHelper = $orderHelper;
89+
$this->guestCouponManagement = $guestCouponManagement;
90+
$this->couponFactory = $couponFactory;
91+
$this->ruleRepository = $ruleRepository;
92+
$this->fbeHelper = $fbeHelper;
93+
}
94+
95+
/**
96+
* Add coupon to Magento cart
97+
*
98+
* @param string $externalBusinessId
99+
* @param string $cartId
100+
* @param string $couponCode
101+
* @return \Meta\Sales\Api\AddCartCouponApiResponseInterface
102+
* @throws \Magento\Framework\Exception\UnauthorizedTokenException
103+
* @throws \Magento\Framework\Exception\NoSuchEntityException
104+
* @throws \Magento\Framework\Exception\CouldNotSaveException
105+
* @throws \Magento\Framework\Exception\LocalizedException
106+
*/
107+
public function addCartCoupon(
108+
string $externalBusinessId,
109+
string $cartId,
110+
string $couponCode
111+
): AddCartCouponApiResponseInterface {
112+
$this->orderHelper->checkDynamicCheckoutConfig();
113+
$this->authenticator->authenticateRequest();
114+
$storeId = $this->orderHelper->getStoreIdByExternalBusinessId($externalBusinessId);
115+
try {
116+
$response = new AddCartCouponApiResponse();
117+
$status = $this->guestCouponManagement->set($cartId, $couponCode);
118+
$response->setStatus($status);
119+
$rule = $this->getCouponRule($storeId, $cartId, $couponCode);
120+
$response->setRule($rule);
121+
return $response;
122+
} catch (NoSuchEntityException $e) {
123+
if (strpos($e->getMessage(), 'cartId') !== false) {
124+
$le = new LocalizedException(__(
125+
"No such entity with cartId = %1",
126+
$cartId
127+
));
128+
} else {
129+
$le = $e;
130+
}
131+
$this->fbeHelper->logExceptionImmediatelyToMeta(
132+
$le,
133+
[
134+
'store_id' => $storeId,
135+
'event' => 'add_cart_coupon_api',
136+
'event_type' => 'no_such_entity_exception',
137+
'extra_data' => [
138+
'cart_id' => $cartId,
139+
'coupon_code' => $couponCode
140+
]
141+
]
142+
);
143+
throw $le;
144+
} catch (\Throwable $e) {
145+
$this->fbeHelper->logExceptionImmediatelyToMeta(
146+
$e,
147+
[
148+
'store_id' => $storeId,
149+
'event' => 'add_cart_coupon_api',
150+
'event_type' => 'error_adding_cart_coupon',
151+
'extra_data' => [
152+
'cart_id' => $cartId,
153+
'coupon_code' => $couponCode
154+
]
155+
]
156+
);
157+
throw $e;
158+
}
159+
}
160+
161+
/**
162+
* Get Rule for particular couponCode
163+
*
164+
* @param string $storeId
165+
* @param string $cartId
166+
* @param string $couponCode
167+
* @return \Magento\SalesRule\Api\Data\RuleInterface|null
168+
*/
169+
public function getCouponRule(string $storeId, string $cartId, string $couponCode): ?RuleInterface
170+
{
171+
try {
172+
$coupon = $this->couponFactory->create();
173+
$coupon->load($couponCode, 'code');
174+
$rule = $this->ruleRepository->getById($coupon->getRuleId());
175+
return $rule;
176+
} catch (\Throwable $e) {
177+
$this->fbeHelper->logExceptionImmediatelyToMeta(
178+
$e,
179+
[
180+
'store_id' => $storeId,
181+
'event' => 'add_cart_coupon_api',
182+
'event_type' => 'error_getting_coupon_rule_from_code',
183+
'extra_data' => [
184+
'cart_id' => $cartId,
185+
'coupon_code' => $couponCode
186+
]
187+
]
188+
);
189+
return null;
190+
}
191+
}
192+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\SalesRule\Api\Data\RuleInterface;
24+
use Meta\Sales\Api\AddCartCouponApiResponseInterface;
25+
26+
class AddCartCouponApiResponse implements AddCartCouponApiResponseInterface
27+
{
28+
/**
29+
* @var bool
30+
*/
31+
private $status = false;
32+
33+
/**
34+
* @var \Magento\SalesRule\Api\Data\RuleInterface|null
35+
*/
36+
private $rule = null;
37+
38+
/**
39+
* Getter
40+
*
41+
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
42+
* @return bool
43+
*/
44+
public function getStatus(): bool
45+
{
46+
return $this->status;
47+
}
48+
49+
/**
50+
* Setter
51+
*
52+
* @param bool $status
53+
* @return void
54+
*/
55+
public function setStatus(bool $status): void
56+
{
57+
$this->status = $status;
58+
}
59+
60+
/**
61+
* Getter
62+
*
63+
* @return \Magento\SalesRule\Api\Data\RuleInterface|null
64+
*/
65+
public function getRule(): ?RuleInterface
66+
{
67+
return $this->rule;
68+
}
69+
70+
/**
71+
* Setter
72+
*
73+
* @param \Magento\SalesRule\Api\Data\RuleInterface|null $rule
74+
* @return void
75+
*/
76+
public function setRule(?RuleInterface $rule): void
77+
{
78+
$this->rule = $rule;
79+
}
80+
}

app/code/Meta/Sales/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"magento/module-payment": "*",
1919
"magento/module-quote": "*",
2020
"magento/module-checkout": "*",
21+
"magento/module-sales-rule": "*",
2122
"magento/module-shipping": "*",
2223
"magento/module-newsletter": "*",
2324
"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::add_cart_coupon" title="Add Cart Coupon" sortOrder="105"/>
1213
<resource id="Meta_Sales::delete_cart_coupon" title="Delete Cart Coupon" sortOrder="106"/>
1314
<resource id="Meta_Sales::set_cart_shipping_option" title="Set Cart Shipping Option" sortOrder="107"/>
1415
<resource id="Meta_Sales::delete_cart_item" title="Delete Cart Item" sortOrder="108"/>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
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\AddCartCouponApiInterface" type="Meta\Sales\Model\Api\AddCartCouponApi"/>
11+
<preference for="Meta\Sales\Api\AddCartCouponApiResponseInterface" type="Meta\Sales\Model\Api\AddCartCouponApiResponse"/>
1012
<preference for="Meta\Sales\Api\DeleteCartCouponApiInterface" type="Meta\Sales\Model\Api\DeleteCartCouponApi"/>
1113
<preference for="Meta\Sales\Api\SetCartShippingOptionApiInterface" type="Meta\Sales\Model\Api\SetCartShippingOptionApi"/>
1214
<preference for="Meta\Sales\Api\DeleteCartItemApiInterface" type="Meta\Sales\Model\Api\DeleteCartItemApi"/>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<module name="Magento_Payment"/>
1616
<module name="Magento_Quote"/>
1717
<module name="Magento_Checkout"/>
18+
<module name="Magento_SalesRule"/>
1819
<module name="Magento_SalesSequence"/>
1920
<module name="Magento_Shipping"/>
2021
<module name="Magento_Webapi"/>

0 commit comments

Comments
 (0)