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
packeta / src / Packetery / Module / Checkout / CheckoutValidator.php

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

189 lines 5.5 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\Module\Carrier;
8 use Packetery\Module\Carrier\CarrierOptionsFactory;
9 use Packetery\Module\Carrier\EntityRepository;
10 use Packetery\Module\Exception\ProductNotFoundException;
11 use Packetery\Module\Framework\WcAdapter;
12 use Packetery\Module\Framework\WpAdapter;
13 use Packetery\Module\Order;
14
15 class CheckoutValidator {
16
17 /**
18 * @var WpAdapter
19 */
20 private $wpAdapter;
21
22 /**
23 * @var WcAdapter
24 */
25 private $wcAdapter;
26
27 /**
28 * @var CheckoutService
29 */
30 private $checkoutService;
31
32 /**
33 * @var CartService
34 */
35 private $cartService;
36
37 /**
38 * @var SessionService
39 */
40 private $sessionService;
41
42 /**
43 * @var CheckoutStorage
44 */
45 private $storage;
46
47 /**
48 * @var CarrierOptionsFactory
49 */
50 private $carrierOptionsFactory;
51
52 /**
53 * @var EntityRepository
54 */
55 private $carrierEntityRepository;
56
57 public function __construct(
58 WpAdapter $wpAdapter,
59 WcAdapter $wcAdapter,
60 CheckoutService $checkoutService,
61 CartService $cartService,
62 SessionService $sessionService,
63 CheckoutStorage $storage,
64 CarrierOptionsFactory $carrierOptionsFactory,
65 EntityRepository $carrierEntityRepository
66 ) {
67 $this->wpAdapter = $wpAdapter;
68 $this->wcAdapter = $wcAdapter;
69 $this->checkoutService = $checkoutService;
70 $this->cartService = $cartService;
71 $this->sessionService = $sessionService;
72 $this->storage = $storage;
73 $this->carrierOptionsFactory = $carrierOptionsFactory;
74 $this->carrierEntityRepository = $carrierEntityRepository;
75 }
76
77 /**
78 * Checks if all attributes required for chosen method are set, sets an error otherwise.
79 *
80 * @throws ProductNotFoundException Product not found.
81 */
82 public function actionValidateCheckoutData(): void {
83 $chosenShippingMethod = $this->checkoutService->resolveChosenMethod();
84
85 if (
86 $chosenShippingMethod === null ||
87 $this->checkoutService->isPacketeryShippingMethod( $chosenShippingMethod ) === false
88 ) {
89 return;
90 }
91
92 $checkoutData = $this->storage->getPostDataIncludingStoredData( $chosenShippingMethod );
93
94 if ( $this->cartService->isShippingRateRestrictedByProductsCategory( $chosenShippingMethod, $this->wcAdapter->cartGetCartContents() ) ) {
95 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Chosen delivery method is no longer available. Please choose another delivery method.', 'packeta' ), 'error' );
96
97 return;
98 }
99
100 $carrierId = $this->checkoutService->getCarrierIdFromPacketeryShippingMethod( $chosenShippingMethod );
101 $carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrierId );
102 $paymentMethod = $this->sessionService->getChosenPaymentMethod();
103
104 if ( $paymentMethod !== null && $carrierOptions->hasCheckoutPaymentMethodDisallowed( $paymentMethod ) ) {
105 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Chosen delivery method is no longer available. Please choose another delivery method.', 'packeta' ), 'error' );
106
107 return;
108 }
109
110 if ( $this->checkoutService->isPickupPointOrder() ) {
111 $this->validatePickupPoint( $checkoutData, $carrierId );
112
113 return;
114 }
115
116 if ( $this->checkoutService->isHomeDeliveryOrder() ) {
117 $this->validateHomeDelivery( $checkoutData, $carrierId );
118
119 return;
120 }
121
122 if (
123 ( ! isset( $checkoutData[ Order\Attribute::CAR_DELIVERY_ID ] ) || $checkoutData[ Order\Attribute::CAR_DELIVERY_ID ] === '' ) &&
124 $this->checkoutService->isCarDeliveryOrder()
125 ) {
126 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Delivery address has not been verified. Verification of delivery address is required by this carrier.', 'packeta' ), 'error' );
127 }
128 }
129
130 private function validatePickupPoint( array $checkoutData, ?string $carrierId ): void {
131 $error = false;
132
133 $requiredAttributes = array_filter(
134 array_combine(
135 array_column( Order\Attribute::$pickupPointAttributes, 'name' ),
136 array_column( Order\Attribute::$pickupPointAttributes, 'required' )
137 )
138 );
139 foreach ( $requiredAttributes as $attr => $required ) {
140 $attrValue = $checkoutData[ $attr ] ?? null;
141 if ( ! $attrValue ) {
142 $error = true;
143 }
144 }
145 if ( $error ) {
146 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Pickup point is not chosen.', 'packeta' ), 'error' );
147 }
148
149 $customerCountry = $this->checkoutService->getCustomerCountry();
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;
156 }
157
158 if (
159 ! $error &&
160 ! $this->carrierEntityRepository->isValidForCountry(
161 $carrierId,
162 $customerCountry
163 )
164 ) {
165 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'The selected Packeta carrier is not available for the selected delivery country.', 'packeta' ), 'error' );
166 }
167 }
168
169 private function validateHomeDelivery( array $checkoutData, ?string $carrierId ): void {
170 $optionId = Carrier\OptionPrefixer::getOptionId( $carrierId );
171 $carrierOption = $this->wpAdapter->getOption( $optionId );
172
173 $addressValidation = 'none';
174 if ( $carrierOption !== false ) {
175 $addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation );
176 }
177
178 if (
179 $addressValidation === 'required' &&
180 (
181 ! isset( $checkoutData[ Order\Attribute::ADDRESS_IS_VALIDATED ] ) ||
182 $checkoutData[ Order\Attribute::ADDRESS_IS_VALIDATED ] !== '1'
183 )
184 ) {
185 $this->wcAdapter->addNotice( $this->wpAdapter->__( 'Delivery address has not been verified. Verification of delivery address is required by this carrier.', 'packeta' ), 'error' );
186 }
187 }
188 }
189