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/Checkout.php +24 -149 trunk2.1 View file →
@@ -6,9 +6,8 @@
6 6
7 7 use Packetery\Core\Entity;
8 8 use Packetery\Module\Carrier;
9 9 use Packetery\Module\Carrier\CarrierOptionsFactory;
10 -use Packetery\Module\DiagnosticsLogger\DiagnosticsLogger;
11 10 use Packetery\Module\Exception\ProductNotFoundException;
12 11 use Packetery\Module\Framework\WcAdapter;
13 12 use Packetery\Module\Framework\WpAdapter;
14 13 use Packetery\Module\Options\OptionsProvider;
@@ -13,9 +12,8 @@
13 12 use Packetery\Module\Framework\WpAdapter;
14 13 use Packetery\Module\Options\OptionsProvider;
15 14 use Packetery\Module\Order;
16 15 use Packetery\Module\Payment\PaymentHelper;
17 -use Packetery\Module\WcLogger;
18 16 use WC_Cart;
19 17 use WC_Payment_Gateway;
20 18
21 19 class Checkout {
@@ -94,13 +92,8 @@
94 92 * @var OrderUpdater
95 93 */
96 94 private $orderUpdater;
97 95
98 - /**
99 - * @var DiagnosticsLogger
100 - */
101 - private $diagnosticsLogger;
102 -
103 96 public function __construct(
104 97 WpAdapter $wpAdapter,
105 98 WcAdapter $wcAdapter,
106 99 CarrierOptionsFactory $carrierOptionsFactory,
@@ -114,10 +107,9 @@
114 107 CheckoutRenderer $renderer,
115 108 CartService $cartService,
116 109 SessionService $sessionService,
117 110 CheckoutValidator $validator,
118 - OrderUpdater $orderUpdater,
119 - DiagnosticsLogger $diagnosticsLogger
111 + OrderUpdater $orderUpdater
120 112 ) {
121 113 $this->wpAdapter = $wpAdapter;
122 114 $this->wcAdapter = $wcAdapter;
123 115 $this->carrierOptionsFactory = $carrierOptionsFactory;
@@ -132,9 +124,8 @@
132 124 $this->cartService = $cartService;
133 125 $this->sessionService = $sessionService;
134 126 $this->validator = $validator;
135 127 $this->orderUpdater = $orderUpdater;
136 - $this->diagnosticsLogger = $diagnosticsLogger;
137 128 }
138 129
139 130 public function registerHooks(): void {
140 131 // This action works for both classic and Divi templates.
@@ -145,15 +136,9 @@
145 136 'actionRenderHiddenInputFields',
146 137 ]
147 138 );
148 139
149 - $this->wpAdapter->addAction( 'woocommerce_after_checkout_validation', [ $this->validator, 'actionValidateCheckoutData' ], 10, 2 );
150 - // Provides following parameters: \Automattic\WooCommerce\Admin\Overrides\Order, WP_REST_Request
151 - $this->wpAdapter->addAction(
152 - 'woocommerce_store_api_checkout_update_order_from_request',
153 - [ $this->validator, 'actionValidateBlockCheckoutData' ]
154 - );
155 -
140 + $this->wpAdapter->addAction( 'woocommerce_checkout_process', [ $this->validator, 'actionValidateCheckoutData' ] );
156 141 $this->wpAdapter->addAction( 'woocommerce_checkout_update_order_meta', [ $this->orderUpdater, 'actionUpdateOrderById' ] );
157 142 $this->wpAdapter->addAction(
158 143 'woocommerce_store_api_checkout_order_processed',
159 144 [
@@ -223,94 +208,47 @@
223 208 * @throws ProductNotFoundException Product not found.
224 209 */
225 210 public function actionCalculateFees( WC_Cart $cart ): void {
226 211 $chosenShippingMethodOptionId = $this->checkoutService->calculateShippingAndGetOptionId();
227 - $isPacketeryShippingMethod = $this->checkoutService->isPacketeryShippingMethod( (string) $chosenShippingMethodOptionId );
228 212
229 213 if (
230 214 $chosenShippingMethodOptionId === null ||
231 - $isPacketeryShippingMethod === false
215 + $this->checkoutService->isPacketeryShippingMethod( $chosenShippingMethodOptionId ) === false
232 216 ) {
233 - $this->diagnosticsLogger->log(
234 - 'No packetery shipping method chosen',
235 - [
236 - 'chosenShippingMethodOptionId' => $chosenShippingMethodOptionId,
237 - 'isPacketeryShippingMethod' => $isPacketeryShippingMethod,
238 - ]
239 - );
240 -
241 217 return;
242 218 }
243 219
244 - $carrierOptions = $this->carrierOptionsFactory->createByOptionId( $chosenShippingMethodOptionId );
245 - $chosenCarrier = $this->carrierEntityRepository->getAnyById(
220 + $carrierOptions = $this->carrierOptionsFactory->createByOptionId( $chosenShippingMethodOptionId );
221 + $chosenCarrier = $this->carrierEntityRepository->getAnyById(
246 222 $this->checkoutService->getCarrierIdFromPacketeryShippingMethod( $chosenShippingMethodOptionId )
247 223 );
248 - $maxTaxClass = $this->cartService->getTaxClassWithMaxRate();
249 - $isTaxable = $maxTaxClass !== null;
250 - $hasCouponFreeShippingForFeesAllowed = $carrierOptions->hasCouponFreeShippingForFeesAllowed();
251 - $wcAdapterCart = $this->wcAdapter->cart();
252 - $isFreeShippingCouponApplied = $this->rateCalculator->isFreeShippingCouponApplied( $wcAdapterCart );
224 + $maxTaxClass = $this->cartService->getTaxClassWithMaxRate();
225 + $isTaxable = $maxTaxClass !== null;
253 226
254 - $this->diagnosticsLogger->log(
255 - 'Coupon free shipping for fees parameters',
256 - [
257 - 'hasCouponFreeShippingForFeesAllowed' => $hasCouponFreeShippingForFeesAllowed,
258 - 'isFreeShippingCouponApplied' => $isFreeShippingCouponApplied,
259 - 'wcAdapterCart' => $wcAdapterCart,
260 - ]
261 - );
262 - if ( $hasCouponFreeShippingForFeesAllowed === true && $isFreeShippingCouponApplied === true ) {
227 + if (
228 + $carrierOptions->hasCouponFreeShippingForFeesAllowed() &&
229 + $this->rateCalculator->isFreeShippingCouponApplied( $this->wcAdapter->cart() )
230 + ) {
263 231 return;
264 232 }
265 233
266 - $this->diagnosticsLogger->log(
267 - 'Coupon free shipping for fees is not allowed',
268 - [
269 - 'chosenShippingMethodOptionId' => $chosenShippingMethodOptionId,
270 - 'hasCouponFreeShippingForFeesAllowed' => $hasCouponFreeShippingForFeesAllowed,
271 - 'isFreeShippingCouponApplied' => $isFreeShippingCouponApplied,
272 - 'chosenCarrier' => $chosenCarrier,
273 - 'carrierOptions' => $carrierOptions,
274 - 'maxTaxClass' => $maxTaxClass,
275 - 'isTaxable' => $isTaxable,
276 - 'cart' => $cart,
277 - ]
278 - );
279 -
280 234 $this->addAgeVerificationFee( $cart, $chosenCarrier, $carrierOptions, $isTaxable, $maxTaxClass );
281 235 $this->addCodSurchargeFee( $cart, $carrierOptions, $isTaxable, $maxTaxClass );
282 236 }
283 237
284 238 private function addAgeVerificationFee( WC_Cart $cart, ?Entity\Carrier $chosenCarrier, Carrier\Options $carrierOptions, bool $isTaxable, ?string $maxTaxClass ): void {
285 - $isAgeVerificationRequired = $this->cartService->isAgeVerificationRequired();
286 - $ageVerificationFee = $carrierOptions->getAgeVerificationFee();
287 - $this->diagnosticsLogger->log(
288 - 'Age verification parameters',
289 - [
290 - 'chosenCarrier' => $chosenCarrier,
291 - 'ageVerificationFee' => $ageVerificationFee,
292 - 'isAgeVerificationRequired' => $isAgeVerificationRequired,
293 - 'isTaxable' => $isTaxable,
294 - 'maxTaxClass' => $maxTaxClass,
295 - ]
296 - );
297 239 if (
298 240 $chosenCarrier === null ||
299 241 ! $chosenCarrier->supportsAgeVerification() ||
300 - $ageVerificationFee === null ||
301 - $isAgeVerificationRequired === false
242 + $carrierOptions->getAgeVerificationFee() === null ||
243 + ! $this->cartService->isAgeVerificationRequired()
302 244 ) {
303 - $this->diagnosticsLogger->log( 'Age verification fee is not added', [] );
304 -
305 245 return;
306 246 }
307 - $feeAmount = $this->currencySwitcherService->getConvertedPrice( $ageVerificationFee );
308 - $this->diagnosticsLogger->log( 'Age verification converted price is added', [ 'feeAmount' => $feeAmount ] );
247 + $feeAmount = $this->currencySwitcherService->getConvertedPrice( $carrierOptions->getAgeVerificationFee() );
309 248
310 249 if ( $isTaxable && $feeAmount > 0 && $this->optionsProvider->arePricesTaxInclusive() ) {
311 250 $feeAmount = $this->calcTaxExclusiveFeeAmount( $feeAmount, $maxTaxClass );
312 - $this->diagnosticsLogger->log( 'Age verification tax exclusive fee amount is added', [ 'feeAmount' => $feeAmount ] );
313 251 }
314 252 $cart->add_fee( $this->wpAdapter->__( 'Age verification fee', 'packeta' ), $feeAmount, $isTaxable, $maxTaxClass );
315 253 }
316 254
@@ -320,17 +258,9 @@
320 258 } else {
321 259 $paymentMethod = $this->sessionService->getChosenPaymentMethod();
322 260 }
323 261
324 - $isCodPaymentMethod = $this->paymentHelper->isCodPaymentMethod( (string) $paymentMethod );
325 - $this->diagnosticsLogger->log(
326 - 'COD surcharge parameters',
327 - [
328 - 'paymentMethod' => $paymentMethod,
329 - 'isCodPaymentMethod' => $isCodPaymentMethod,
330 - ]
331 - );
332 - if ( $paymentMethod === null || $isCodPaymentMethod === false ) {
262 + if ( $paymentMethod === null || $this->paymentHelper->isCodPaymentMethod( $paymentMethod ) === false ) {
333 263 return;
334 264 }
335 265
336 266 $applicableSurcharge = $this->rateCalculator->getCODSurcharge(
@@ -336,11 +266,9 @@
336 266 $applicableSurcharge = $this->rateCalculator->getCODSurcharge(
337 267 $carrierOptions->toArray(),
338 268 $this->wcAdapter->cartGetSubtotal()
339 269 );
340 - $this->diagnosticsLogger->log( 'Get COD surcharge', [ 'applicableSurcharge' => $applicableSurcharge ] );
341 270 $applicableSurcharge = $this->currencySwitcherService->getConvertedPrice( $applicableSurcharge );
342 - $this->diagnosticsLogger->log( 'Get COD surcharge converted', [ 'applicableSurcharge' => $applicableSurcharge ] );
343 271 if ( $applicableSurcharge <= 0 ) {
344 272 return;
345 273 }
346 274
@@ -345,9 +273,8 @@
345 273 }
346 274
347 275 if ( $isTaxable && $this->optionsProvider->arePricesTaxInclusive() ) {
348 276 $applicableSurcharge = $this->calcTaxExclusiveFeeAmount( $applicableSurcharge, $maxTaxClass );
349 - $this->diagnosticsLogger->log( 'Get COD surcharge tax exclusive', [ 'applicableSurcharge' => $applicableSurcharge ] );
350 277 }
351 278
352 279 $cart->add_fee( $this->wpAdapter->__( 'COD surcharge', 'packeta' ), $applicableSurcharge, $isTaxable, $maxTaxClass );
353 280 }
@@ -372,36 +299,17 @@
372 299
373 300 /**
374 301 * Filters out payment methods, that can not be used.
375 302 *
376 - * @param WC_Payment_Gateway[]|mixed $availableGateways Available gateways.
303 + * @param WC_Payment_Gateway[] $availableGateways Available gateways.
377 304 *
378 - * @return WC_Payment_Gateway[]|mixed
305 + * @return WC_Payment_Gateway[]
379 306 */
380 - public function filterPaymentGateways( $availableGateways ) {
381 - $this->diagnosticsLogger->log(
382 - 'Payment gateways for filtering',
383 - [
384 - 'availableGateways' => $availableGateways,
385 - ]
386 - );
387 - if ( ! is_array( $availableGateways ) ) {
388 - WcLogger::logArgumentTypeError( __METHOD__, 'availableGateways', 'array', $availableGateways );
389 -
390 - return $availableGateways;
391 - }
392 -
307 + public function filterPaymentGateways( array $availableGateways ): array {
393 308 $order = null;
394 309 $wpOrderPay = $this->checkoutService->getOrderPayParameter();
395 310 if ( is_numeric( $wpOrderPay ) ) {
396 311 $order = $this->orderRepository->getByIdWithValidCarrier( (int) $wpOrderPay );
397 - $this->diagnosticsLogger->log(
398 - 'Order found by order pay parameter',
399 - [
400 - 'wpOrderPay' => $wpOrderPay,
401 - 'order' => $order,
402 - ]
403 - );
404 312 }
405 313
406 314 if ( $order instanceof Entity\Order ) {
407 315 $chosenMethod = Carrier\OptionPrefixer::getOptionId( $order->getCarrier()->getId() );
@@ -408,29 +316,14 @@
408 316 } else {
409 317 $chosenMethod = $this->sessionService->getChosenMethodFromSession();
410 318 }
411 319
412 - $isPacketeryShippingMethod = $this->checkoutService->isPacketeryShippingMethod( $chosenMethod );
413 - $this->diagnosticsLogger->log(
414 - 'Chosen method',
415 - [
416 - 'chosenMethod' => $chosenMethod,
417 - 'isPacketeryShippingMethod' => $isPacketeryShippingMethod,
418 - ]
419 - );
420 - if ( $isPacketeryShippingMethod === false ) {
320 + if ( ! $this->checkoutService->isPacketeryShippingMethod( $chosenMethod ) ) {
421 321 return $availableGateways;
422 322 }
423 323
424 324 $carrierId = $this->checkoutService->getCarrierIdFromPacketeryShippingMethod( $chosenMethod );
425 325 $carrier = $this->carrierEntityRepository->getAnyById( $carrierId );
426 - $this->diagnosticsLogger->log(
427 - 'Carrier for filtering gateways',
428 - [
429 - 'carrierId' => $carrierId,
430 - 'carrier' => $carrier,
431 - ]
432 - );
433 326 if ( $carrier === null ) {
434 327 return $availableGateways;
435 328 }
436 329
@@ -435,37 +328,19 @@
435 328 }
436 329
437 330 $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrierId );
438 331 foreach ( $availableGateways as $key => $availableGateway ) {
439 - if ( ! $availableGateway instanceof WC_Payment_Gateway ) {
440 - WcLogger::logArgumentTypeError( __METHOD__, 'availableGateway', 'WC_Payment_Gateway', $availableGateway );
441 -
442 - continue;
443 - }
444 -
445 - $isCodPaymentMethod = $this->paymentHelper->isCodPaymentMethod( $availableGateway->id );
446 - $supportsCod = $carrier->supportsCod();
447 - $hasCheckoutPaymentMethodDisallowed = $carrierOptions->hasCheckoutPaymentMethodDisallowed( $availableGateway->id );
448 - $this->diagnosticsLogger->log(
449 - 'Payment method filtering parameters',
450 - [
451 - 'availableGateway' => $availableGateway,
452 - 'isCodPaymentMethod' => $isCodPaymentMethod,
453 - 'supportsCod' => $supportsCod,
454 - 'hasCheckoutPaymentMethodDisallowed' => $hasCheckoutPaymentMethodDisallowed,
455 - ]
456 - );
457 -
458 - if ( $isCodPaymentMethod === true && $supportsCod === false ) {
332 + if (
333 + $this->paymentHelper->isCodPaymentMethod( $availableGateway->id ) &&
334 + ! $carrier->supportsCod()
335 + ) {
459 336 unset( $availableGateways[ $key ] );
460 337 }
461 338
462 - if ( $hasCheckoutPaymentMethodDisallowed === true ) {
339 + if ( $carrierOptions->hasCheckoutPaymentMethodDisallowed( $availableGateway->id ) ) {
463 340 unset( $availableGateways[ $key ] );
464 341 }
465 342 }
466 -
467 - $this->diagnosticsLogger->log( 'Filtered payment methods', [ 'availableGateways' => $availableGateways ] );
468 343
469 344 return $availableGateways;
470 345 }
471 346 }