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