PluginProbe
Packeta / 2.1
Packeta v2.1
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
← All changes | src/Packetery/Module/Checkout/CheckoutValidator.php +35 -148 trunk2.1 View file →
@@ -3,10 +3,8 @@
3 3 declare( strict_types=1 );
4 4
5 5 namespace Packetery\Module\Checkout;
6 6
7 -use Packetery\Core\Api\Rest\PickupPointValidateRequest;
8 -use Packetery\Core\Entity;
9 7 use Packetery\Module\Carrier;
10 8 use Packetery\Module\Carrier\CarrierOptionsFactory;
11 9 use Packetery\Module\Carrier\EntityRepository;
12 10 use Packetery\Module\Exception\ProductNotFoundException;
@@ -11,14 +9,9 @@
11 9 use Packetery\Module\Carrier\EntityRepository;
12 10 use Packetery\Module\Exception\ProductNotFoundException;
13 11 use Packetery\Module\Framework\WcAdapter;
14 12 use Packetery\Module\Framework\WpAdapter;
15 -use Packetery\Module\Options\OptionsProvider;
16 13 use Packetery\Module\Order;
17 -use Packetery\Module\Order\PickupPointValidator;
18 -use Packetery\Module\Payment\PaymentHelper;
19 -use WC_REST_Exception;
20 -use WP_Error;
21 14
22 15 class CheckoutValidator {
23 16
24 17 /**
@@ -60,20 +53,8 @@
60 53 * @var EntityRepository
61 54 */
62 55 private $carrierEntityRepository;
63 56
64 - /** @var OptionsProvider */
65 - private $optionsProvider;
66 -
67 - /** @var PickupPointValidator */
68 - private $pickupPointValidator;
69 -
70 - /** @var PaymentHelper */
71 - private $paymentHelper;
72 -
73 - /** @var Carrier\PacketaPickupPointsConfig */
74 - private $pickupPointsConfig;
75 -
76 57 public function __construct(
77 58 WpAdapter $wpAdapter,
78 59 WcAdapter $wcAdapter,
79 60 CheckoutService $checkoutService,
@@ -80,13 +61,9 @@
80 61 CartService $cartService,
81 62 SessionService $sessionService,
82 63 CheckoutStorage $storage,
83 64 CarrierOptionsFactory $carrierOptionsFactory,
84 - EntityRepository $carrierEntityRepository,
85 - OptionsProvider $optionsProvider,
86 - PickupPointValidator $pickupPointValidator,
87 - PaymentHelper $paymentHelper,
88 - Carrier\PacketaPickupPointsConfig $packetaPickupPointsConfig
65 + EntityRepository $carrierEntityRepository
89 66 ) {
90 67 $this->wpAdapter = $wpAdapter;
91 68 $this->wcAdapter = $wcAdapter;
92 69 $this->checkoutService = $checkoutService;
@@ -94,60 +71,31 @@
94 71 $this->sessionService = $sessionService;
95 72 $this->storage = $storage;
96 73 $this->carrierOptionsFactory = $carrierOptionsFactory;
97 74 $this->carrierEntityRepository = $carrierEntityRepository;
98 - $this->optionsProvider = $optionsProvider;
99 - $this->pickupPointValidator = $pickupPointValidator;
100 - $this->paymentHelper = $paymentHelper;
101 - $this->pickupPointsConfig = $packetaPickupPointsConfig;
102 75 }
103 76
104 77 /**
105 - * Using wc_add_notice is not safe because it can be cleared by other plugins.
78 + * Checks if all attributes required for chosen method are set, sets an error otherwise.
106 79 *
107 - * @param array<string, string|int|string[]|bool> $data
108 - * @param WP_Error $wpError
109 - *
110 - * @throws ProductNotFoundException
80 + * @throws ProductNotFoundException Product not found.
111 81 */
112 - public function actionValidateCheckoutData( array $data, WP_Error $wpError ): void {
113 - $error = $this->getFirstError();
114 - if ( $error !== null ) {
115 - $wpError->add( 'packeta_cart_validation_failed', $error );
116 - }
117 - }
118 -
119 - /**
120 - * Using wc_add_notice works even for block checkout, but WC_REST_Exception is recommended.
121 - *
122 - * @throws WC_REST_Exception
123 - * @throws ProductNotFoundException
124 - */
125 - public function actionValidateBlockCheckoutData(): void {
126 - $error = $this->getFirstError();
127 - if ( $error !== null ) {
128 - throw new WC_REST_Exception( 'packeta_cart_validation_failed', $error );
129 - }
130 - }
131 -
132 - /**
133 - * @throws ProductNotFoundException
134 - */
135 - private function getFirstError(): ?string {
82 + public function actionValidateCheckoutData(): void {
136 83 $chosenShippingMethod = $this->checkoutService->resolveChosenMethod();
137 - $this->wcAdapter->sessionSet( PickupPointValidator::VALIDATION_HTTP_ERROR_SESSION_KEY, null );
138 84
139 85 if (
140 86 $chosenShippingMethod === null ||
141 87 $this->checkoutService->isPacketeryShippingMethod( $chosenShippingMethod ) === false
142 88 ) {
143 - return null;
89 + return;
144 90 }
145 91
146 92 $checkoutData = $this->storage->getPostDataIncludingStoredData( $chosenShippingMethod );
147 93
148 94 if ( $this->cartService->isShippingRateRestrictedByProductsCategory( $chosenShippingMethod, $this->wcAdapter->cartGetCartContents() ) ) {
149 - return $this->wpAdapter->__( 'Chosen delivery method is no longer available. Please choose another delivery method.', 'packeta' );
95 + $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Chosen delivery method is no longer available. Please choose another delivery method.', 'packeta' ), 'error' );
96 +
97 + return;
150 98 }
151 99
152 100 $carrierId = $this->checkoutService->getCarrierIdFromPacketeryShippingMethod( $chosenShippingMethod );
153 101 $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrierId );
@@ -153,22 +101,23 @@
153 101 $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrierId );
154 102 $paymentMethod = $this->sessionService->getChosenPaymentMethod();
155 103
156 104 if ( $paymentMethod !== null && $carrierOptions->hasCheckoutPaymentMethodDisallowed( $paymentMethod ) ) {
157 - return $this->wpAdapter->__( 'Chosen delivery method is no longer available. Please choose another delivery method.', 'packeta' );
105 + $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Chosen delivery method is no longer available. Please choose another delivery method.', 'packeta' ), 'error' );
106 +
107 + return;
158 108 }
159 109
160 110 if ( $this->checkoutService->isPickupPointOrder() ) {
161 - return $this->validatePickupPoint(
162 - $checkoutData,
163 - $carrierId,
164 - $paymentMethod,
165 - $this->pickupPointsConfig->getFinalVendorGroups( $carrierOptions->getVendorGroups(), $carrierId )
166 - );
111 + $this->validatePickupPoint( $checkoutData, $carrierId );
112 +
113 + return;
167 114 }
168 115
169 116 if ( $this->checkoutService->isHomeDeliveryOrder() ) {
170 - return $this->validateHomeDelivery( $checkoutData, $carrierId );
117 + $this->validateHomeDelivery( $checkoutData, $carrierId );
118 +
119 + return;
171 120 }
172 121
173 122 if (
174 123 ( ! isset( $checkoutData[ Order\Attribute::CAR_DELIVERY_ID ] ) || $checkoutData[ Order\Attribute::CAR_DELIVERY_ID ] === '' ) &&
@@ -173,29 +122,15 @@
173 122 if (
174 123 ( ! isset( $checkoutData[ Order\Attribute::CAR_DELIVERY_ID ] ) || $checkoutData[ Order\Attribute::CAR_DELIVERY_ID ] === '' ) &&
175 124 $this->checkoutService->isCarDeliveryOrder()
176 125 ) {
177 - return $this->wpAdapter->__( 'Delivery address has not been verified. Verification of delivery address is required by this carrier.', 'packeta' );
126 + $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Delivery address has not been verified. Verification of delivery address is required by this carrier.', 'packeta' ), 'error' );
178 127 }
128 + }
179 129
180 - return null;
181 - }
130 + private function validatePickupPoint( array $checkoutData, ?string $carrierId ): void {
131 + $error = false;
182 132
183 - /**
184 - * @param array $checkoutData
185 - * @param string|null $carrierId
186 - * @param string|null $paymentMethod
187 - * @param string[]|null $vendorGroups
188 - *
189 - * @return string|null
190 - * @throws ProductNotFoundException
191 - */
192 - private function validatePickupPoint(
193 - array $checkoutData,
194 - ?string $carrierId,
195 - ?string $paymentMethod,
196 - ?array $vendorGroups
197 - ): ?string {
198 133 $requiredAttributes = array_filter(
199 134 array_combine(
200 135 array_column( Order\Attribute::$pickupPointAttributes, 'name' ),
201 136 array_column( Order\Attribute::$pickupPointAttributes, 'required' )
@@ -203,82 +138,36 @@
203 138 );
204 139 foreach ( $requiredAttributes as $attr => $required ) {
205 140 $attrValue = $checkoutData[ $attr ] ?? null;
206 141 if ( ! $attrValue ) {
207 - return $this->wpAdapter->__( 'Pickup point is not chosen.', 'packeta' );
142 + $error = true;
208 143 }
209 144 }
145 + if ( $error ) {
146 + $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Pickup point is not chosen.', 'packeta' ), 'error' );
147 + }
210 148
211 149 $customerCountry = $this->checkoutService->getCustomerCountry();
212 - if ( $customerCountry === null ) {
213 - return $this->wpAdapter->__( 'Customer country could not be obtained.', 'packeta' );
150 + if (
151 + ! $error &&
152 + $customerCountry === null
153 + ) {
154 + $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Customer country could not be obtained.', 'packeta' ), 'error' );
155 + $error = true;
214 156 }
215 157
216 158 if (
159 + ! $error &&
217 160 ! $this->carrierEntityRepository->isValidForCountry(
218 161 $carrierId,
219 162 $customerCountry
220 163 )
221 164 ) {
222 - return $this->wpAdapter->__( 'The selected Packeta carrier is not available for the selected delivery country.', 'packeta' );
165 + $this->wcAdapter->addNotice( $this->wpAdapter->__( 'The selected Packeta carrier is not available for the selected delivery country.', 'packeta' ), 'error' );
223 166 }
224 -
225 - if ( $this->optionsProvider->isPickupPointValidationEnabled() ) {
226 - $pickupPointId = $checkoutData[ Order\Attribute::POINT_ID ];
227 - if ( $carrierId === '' ) {
228 - $carrierId = Entity\Carrier::INTERNAL_PICKUP_POINTS_ID;
229 - }
230 - $pickupPointValidationResponse = $this->pickupPointValidator->validate(
231 - $this->createPickupPointValidateRequest(
232 - $pickupPointId,
233 - $carrierId,
234 - ( is_numeric( $carrierId ) ? $pickupPointId : null ),
235 - $paymentMethod,
236 - $vendorGroups
237 - )
238 - );
239 - if ( ! $pickupPointValidationResponse->isValid() ) {
240 - return $this->wpAdapter->__( 'The selected Packeta pickup point could not be validated. Please select another.', 'packeta' );
241 - }
242 - }
243 -
244 - return null;
245 167 }
246 168
247 - /**
248 - * @param string $pickupPointId
249 - * @param string|null $carrierId
250 - * @param string|null $pointCarrierId
251 - * @param string|null $paymentMethod
252 - * @param string[]|null $vendorGroups
253 - *
254 - * @return PickupPointValidateRequest
255 - * @throws ProductNotFoundException
256 - */
257 - private function createPickupPointValidateRequest(
258 - string $pickupPointId,
259 - ?string $carrierId,
260 - ?string $pointCarrierId,
261 - ?string $paymentMethod,
262 - ?array $vendorGroups
263 - ): PickupPointValidateRequest {
264 - return new PickupPointValidateRequest(
265 - $pickupPointId,
266 - $carrierId,
267 - $pointCarrierId,
268 - $this->checkoutService->getCustomerCountry(),
269 - null,
270 - null,
271 - $this->cartService->getCartWeightKg(),
272 - $this->cartService->isAgeVerificationRequired(),
273 - null,
274 - ( $paymentMethod !== null && $this->paymentHelper->isCodPaymentMethod( $paymentMethod ) === true ),
275 - $this->cartService->getBiggestProductSize(),
276 - $vendorGroups
277 - );
278 - }
279 -
280 - private function validateHomeDelivery( array $checkoutData, ?string $carrierId ): ?string {
169 + private function validateHomeDelivery( array $checkoutData, ?string $carrierId ): void {
281 170 $optionId = Carrier\OptionPrefixer::getOptionId( $carrierId );
282 171 $carrierOption = $this->wpAdapter->getOption( $optionId );
283 172
284 173 $addressValidation = 'none';
@@ -292,10 +181,8 @@
292 181 ! isset( $checkoutData[ Order\Attribute::ADDRESS_IS_VALIDATED ] ) ||
293 182 $checkoutData[ Order\Attribute::ADDRESS_IS_VALIDATED ] !== '1'
294 183 )
295 184 ) {
296 - return $this->wpAdapter->__( 'Delivery address has not been verified. Verification of delivery address is required by this carrier.', 'packeta' );
185 + $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Delivery address has not been verified. Verification of delivery address is required by this carrier.', 'packeta' ), 'error' );
297 186 }
298 -
299 - return null;
300 187 }
301 188 }