PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Checkout / CheckoutValidator.php

CheckoutValidator.php in Packeta 2.0.9, at src/Packetery/Module/Checkout/CheckoutValidator.php

246 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Packetery\Module\Checkout;
6
7 use Packetery\Core\Api\Rest\PickupPointValidateRequest;
8 use Packetery\Core\Entity;
9 use Packetery\Module\Carrier;
10 use Packetery\Module\Carrier\CarrierOptionsFactory;
11 use Packetery\Module\Carrier\EntityRepository;
12 use Packetery\Module\Exception\ProductNotFoundException;
13 use Packetery\Module\Framework\WcAdapter;
14 use Packetery\Module\Framework\WpAdapter;
15 use Packetery\Module\Order;
16 use Packetery\Module\Order\PickupPointValidator;
17
18 class CheckoutValidator {
19
20 /**
21 * @var PickupPointValidator
22 */
23 private $pickupPointValidator;
24
25 /**
26 * @var WpAdapter
27 */
28 private $wpAdapter;
29
30 /**
31 * @var WcAdapter
32 */
33 private $wcAdapter;
34
35 /**
36 * @var CheckoutService
37 */
38 private $checkoutService;
39
40 /**
41 * @var CartService
42 */
43 private $cartService;
44
45 /**
46 * @var SessionService
47 */
48 private $sessionService;
49
50 /**
51 * @var CheckoutStorage
52 */
53 private $storage;
54
55 /**
56 * @var CarrierOptionsFactory
57 */
58 private $carrierOptionsFactory;
59
60 /**
61 * @var EntityRepository
62 */
63 private $carrierEntityRepository;
64
65 public function __construct(
66 PickupPointValidator $pickupPointValidator,
67 WpAdapter $wpAdapter,
68 WcAdapter $wcAdapter,
69 CheckoutService $checkoutService,
70 CartService $cartService,
71 SessionService $sessionService,
72 CheckoutStorage $storage,
73 CarrierOptionsFactory $carrierOptionsFactory,
74 EntityRepository $carrierEntityRepository
75 ) {
76 $this->pickupPointValidator = $pickupPointValidator;
77 $this->wpAdapter = $wpAdapter;
78 $this->wcAdapter = $wcAdapter;
79 $this->checkoutService = $checkoutService;
80 $this->cartService = $cartService;
81 $this->sessionService = $sessionService;
82 $this->storage = $storage;
83 $this->carrierOptionsFactory = $carrierOptionsFactory;
84 $this->carrierEntityRepository = $carrierEntityRepository;
85 }
86
87 /**
88 * Checks if all attributes required for chosen method are set, sets an error otherwise.
89 *
90 * @throws ProductNotFoundException Product not found.
91 */
92 public function actionValidateCheckoutData(): void {
93 $chosenShippingMethod = $this->checkoutService->resolveChosenMethod();
94 $this->wcAdapter->sessionSet( PickupPointValidator::VALIDATION_HTTP_ERROR_SESSION_KEY, null );
95
96 if (
97 $chosenShippingMethod === null ||
98 $this->checkoutService->isPacketeryShippingMethod( $chosenShippingMethod ) === false
99 ) {
100 return;
101 }
102
103 $checkoutData = $this->storage->getPostDataIncludingStoredData( $chosenShippingMethod );
104
105 if ( $this->cartService->isShippingRateRestrictedByProductsCategory( $chosenShippingMethod, $this->wcAdapter->cartGetCartContents() ) ) {
106 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Chosen delivery method is no longer available. Please choose another delivery method.', 'packeta' ), 'error' );
107
108 return;
109 }
110
111 $carrierId = $this->checkoutService->getCarrierIdFromPacketeryShippingMethod( $chosenShippingMethod );
112 $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrierId );
113 $paymentMethod = $this->sessionService->getChosenPaymentMethod();
114
115 if ( $paymentMethod !== null && $carrierOptions->hasCheckoutPaymentMethodDisallowed( $paymentMethod ) ) {
116 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Chosen delivery method is no longer available. Please choose another delivery method.', 'packeta' ), 'error' );
117
118 return;
119 }
120
121 if ( $this->checkoutService->isPickupPointOrder() ) {
122 $this->validatePickupPoint( $checkoutData, $carrierId, $chosenShippingMethod );
123
124 return;
125 }
126
127 if ( $this->checkoutService->isHomeDeliveryOrder() ) {
128 $this->validateHomeDelivery( $checkoutData, $carrierId );
129
130 return;
131 }
132
133 if (
134 ( ! isset( $checkoutData[ Order\Attribute::CAR_DELIVERY_ID ] ) || $checkoutData[ Order\Attribute::CAR_DELIVERY_ID ] === '' ) &&
135 $this->checkoutService->isCarDeliveryOrder()
136 ) {
137 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Delivery address has not been verified. Verification of delivery address is required by this carrier.', 'packeta' ), 'error' );
138 }
139 }
140
141 private function validatePickupPoint( array $checkoutData, ?string $carrierId, string $chosenShippingMethod ): void {
142 $error = false;
143
144 $requiredAttributes = array_filter(
145 array_combine(
146 array_column( Order\Attribute::$pickupPointAttributes, 'name' ),
147 array_column( Order\Attribute::$pickupPointAttributes, 'required' )
148 )
149 );
150 foreach ( $requiredAttributes as $attr => $required ) {
151 $attrValue = $checkoutData[ $attr ] ?? null;
152 if ( ! $attrValue ) {
153 $error = true;
154 }
155 }
156 if ( $error ) {
157 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Pickup point is not chosen.', 'packeta' ), 'error' );
158 }
159
160 $customerCountry = $this->checkoutService->getCustomerCountry();
161 if (
162 ! $error &&
163 $customerCountry === null
164 ) {
165 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Customer country could not be obtained.', 'packeta' ), 'error' );
166 $error = true;
167 }
168
169 if (
170 ! $error &&
171 ! $this->carrierEntityRepository->isValidForCountry(
172 $carrierId,
173 $customerCountry
174 )
175 ) {
176 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'The selected Packeta carrier is not available for the selected delivery country.', 'packeta' ), 'error' );
177 $error = true;
178 }
179 // @phpstan-ignore-next-line
180 if ( $error === false && PickupPointValidator::IS_ACTIVE ) {
181 $pickupPointId = $checkoutData[ Order\Attribute::POINT_ID ];
182 $carriersForValidation = $chosenShippingMethod;
183 if ( $carrierId === '' ) {
184 $carrierId = Entity\Carrier::INTERNAL_PICKUP_POINTS_ID;
185 $carriersForValidation = Entity\Carrier::INTERNAL_PICKUP_POINTS_ID;
186 }
187 $pickupPointValidationResponse = $this->pickupPointValidator->validate(
188 $this->createPickupPointValidateRequest(
189 $pickupPointId,
190 $carrierId,
191 ( is_numeric( $carrierId ) ? $pickupPointId : null ),
192 $carriersForValidation
193 )
194 );
195 if ( ! $pickupPointValidationResponse->isValid() ) {
196 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'The selected Packeta pickup point could not be validated. Please select another.', 'packeta' ), 'error' );
197 foreach ( $pickupPointValidationResponse->getErrors() as $validationError ) {
198 $reason = $this->pickupPointValidator->getTranslatedError()[ $validationError['code'] ];
199 // translators: %s: Reason for validation failure.
200 $this->wcAdapter->addNotice( sprintf( $this->wpAdapter->__( 'Reason: %s', 'packeta' ), $reason ), 'error' );
201 }
202 }
203 }
204 }
205
206 private function createPickupPointValidateRequest(
207 string $pickupPointId,
208 ?string $carrierId,
209 ?string $pointCarrierId,
210 string $chosenShippingMethod
211 ): PickupPointValidateRequest {
212 return new PickupPointValidateRequest(
213 $pickupPointId,
214 $carrierId,
215 $pointCarrierId,
216 $this->checkoutService->getCustomerCountry(),
217 $this->checkoutService->getCarrierIdFromPacketeryShippingMethod( $chosenShippingMethod ),
218 false,
219 false,
220 $this->cartService->getCartWeightKg(),
221 $this->cartService->isAgeVerificationRequired(),
222 null
223 );
224 }
225
226 private function validateHomeDelivery( array $checkoutData, ?string $carrierId ): void {
227 $optionId = Carrier\OptionPrefixer::getOptionId( $carrierId );
228 $carrierOption = $this->wpAdapter->getOption( $optionId );
229
230 $addressValidation = 'none';
231 if ( $carrierOption !== false ) {
232 $addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation );
233 }
234
235 if (
236 $addressValidation === 'required' &&
237 (
238 ! isset( $checkoutData[ Order\Attribute::ADDRESS_IS_VALIDATED ] ) ||
239 $checkoutData[ Order\Attribute::ADDRESS_IS_VALIDATED ] !== '1'
240 )
241 ) {
242 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Delivery address has not been verified. Verification of delivery address is required by this carrier.', 'packeta' ), 'error' );
243 }
244 }
245 }
246