|
| 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 | +} |
0 commit comments