shopware /
SwagConnect
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * (c) shopware AG <[email protected]> |
||
| 4 | * For the full copyright and license information, please view the LICENSE |
||
| 5 | * file that was distributed with this source code. |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace ShopwarePlugins\Connect\Subscribers; |
||
| 9 | |||
| 10 | use Enlight\Event\SubscriberInterface; |
||
| 11 | use Enlight_Event_EventManager; |
||
| 12 | use Shopware\Components\Model\ModelManager; |
||
| 13 | use Shopware\Connect\SDK; |
||
| 14 | use Shopware\Connect\Struct\Address; |
||
| 15 | use Shopware\Connect\Struct\CheckResult; |
||
| 16 | use Shopware\Connect\Struct\Message; |
||
| 17 | use Shopware\Connect\Struct\OrderItem; |
||
| 18 | use Shopware\Connect\Struct\Product; |
||
| 19 | use Shopware\Models\Order\Status; |
||
| 20 | use ShopwarePlugins\Connect\Components\BasketHelper; |
||
| 21 | use ShopwarePlugins\Connect\Components\ConnectFactory; |
||
| 22 | use ShopwarePlugins\Connect\Components\Exceptions\CheckoutException; |
||
| 23 | use ShopwarePlugins\Connect\Components\Helper; |
||
| 24 | use ShopwarePlugins\Connect\Components\Logger; |
||
| 25 | use ShopwarePlugins\Connect\Components\Utils\ConnectOrderUtil; |
||
| 26 | use ShopwarePlugins\Connect\Components\Utils\OrderPaymentMapper; |
||
| 27 | use Shopware\Models\Order\Order; |
||
| 28 | use Shopware\Models\Payment\Payment; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Handles the whole checkout manipulation, which is required for the connect checkout |
||
| 32 | */ |
||
| 33 | class Checkout implements SubscriberInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var Logger |
||
| 37 | */ |
||
| 38 | protected $logger; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $newSessionId; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var ConnectFactory |
||
| 47 | */ |
||
| 48 | protected $factory; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var ModelManager |
||
| 52 | */ |
||
| 53 | protected $manager; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var Enlight_Event_EventManager |
||
| 57 | */ |
||
| 58 | protected $eventManager; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var SDK |
||
| 62 | */ |
||
| 63 | private $sdk; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var BasketHelper |
||
| 67 | */ |
||
| 68 | private $basketHelper; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var Helper |
||
| 72 | */ |
||
| 73 | private $helper; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param ModelManager $manager |
||
| 77 | * @param Enlight_Event_EventManager $eventManager |
||
| 78 | * @param SDK $sdk |
||
| 79 | * @param BasketHelper $basketHelper |
||
| 80 | * @param Helper $helper |
||
| 81 | */ |
||
| 82 | public function __construct( |
||
| 83 | ModelManager $manager, |
||
| 84 | Enlight_Event_EventManager $eventManager, |
||
| 85 | SDK $sdk, |
||
| 86 | BasketHelper $basketHelper, |
||
| 87 | Helper $helper |
||
| 88 | ) { |
||
| 89 | $this->manager = $manager; |
||
| 90 | $this->eventManager = $eventManager; |
||
| 91 | $this->logger = new Logger(Shopware()->Db()); |
||
| 92 | $this->factory = new ConnectFactory(); |
||
| 93 | $this->sdk = $sdk; |
||
| 94 | $this->basketHelper = $basketHelper; |
||
| 95 | $this->helper = $helper; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public static function getSubscribedEvents() |
||
| 102 | { |
||
| 103 | return [ |
||
| 104 | 'Enlight_Controller_Action_PostDispatch_Frontend_Checkout' => [ 'fixBasketForConnect', '-1' ], |
||
| 105 | 'Enlight_Controller_Action_PreDispatch_Frontend_Checkout' => 'reserveConnectProductsOnCheckoutFinish', |
||
| 106 | 'Shopware_Modules_Admin_Regenerate_Session_Id' => 'updateSessionId', |
||
| 107 | ]; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param \Enlight_Event_EventArgs $args |
||
| 112 | */ |
||
| 113 | public function updateSessionId(\Enlight_Event_EventArgs $args) |
||
| 114 | { |
||
| 115 | $this->newSessionId = $args->get('newSessionId'); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | protected function getCountryCode() |
||
| 122 | { |
||
| 123 | $countryCodeUtil = $this->factory->getCountryCodeResolver(); |
||
| 124 | |||
| 125 | return $countryCodeUtil->getIso3CountryCode(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Event listener method for the checkout confirm- and cartAction. |
||
| 130 | * |
||
| 131 | * @param \Enlight_Event_EventArgs $args |
||
| 132 | * @throws CheckoutException |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function fixBasketForConnect(\Enlight_Event_EventArgs $args) |
||
| 136 | { |
||
| 137 | /** @var $action \Enlight_Controller_Action */ |
||
| 138 | $action = $args->getSubject(); |
||
| 139 | $view = $action->View(); |
||
| 140 | $request = $action->Request(); |
||
| 141 | $actionName = $request->getActionName(); |
||
| 142 | $sessionId = Shopware()->SessionID(); |
||
| 143 | |||
| 144 | $userId = Shopware()->Session()->sUserId; |
||
| 145 | $hasConnectProduct = $this->helper->hasBasketConnectProducts($sessionId, $userId); |
||
| 146 | |||
| 147 | if ($hasConnectProduct === false && $this->newSessionId) { |
||
| 148 | $hasConnectProduct = $this->helper->hasBasketConnectProducts($this->newSessionId); |
||
| 149 | } |
||
| 150 | |||
| 151 | $view->hasConnectProduct = $hasConnectProduct; |
||
| 152 | |||
| 153 | if ($actionName === 'ajax_add_article') { |
||
| 154 | $view->extendsTemplate('frontend/connect/ajax_add_article.tpl'); |
||
| 155 | } |
||
| 156 | |||
| 157 | // send order to connect |
||
| 158 | // this method must be called after external payments (Sofort, Billsafe) |
||
| 159 | if ($actionName === 'finish' && !empty($view->sOrderNumber)) { |
||
| 160 | try { |
||
| 161 | $this->checkoutReservedProducts($view->sOrderNumber); |
||
| 162 | } catch (CheckoutException $e) { |
||
| 163 | $this->setOrderStatusError($view->sOrderNumber); |
||
| 164 | throw $e; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | // clear connect reserved products |
||
| 169 | // sometimes with external payment methods |
||
| 170 | // $hasConnectProduct will be false, because order is already finished |
||
| 171 | // and information about connect products is not available. |
||
| 172 | if (!$hasConnectProduct) { |
||
| 173 | $this->helper->clearConnectReservation(); |
||
| 174 | |||
| 175 | return; |
||
| 176 | } |
||
| 177 | |||
| 178 | if (!in_array($actionName, ['confirm', 'shippingPayment', 'cart', 'finish'])) { |
||
| 179 | return; |
||
| 180 | } |
||
| 181 | |||
| 182 | if (empty($view->sBasket) || !$request->isDispatched()) { |
||
| 183 | return; |
||
| 184 | } |
||
| 185 | |||
| 186 | if (!empty($view->sOrderNumber)) { |
||
| 187 | return; |
||
| 188 | } |
||
| 189 | |||
| 190 | if (Shopware()->Config()->get('requirePhoneField')) { |
||
| 191 | $this->enforcePhoneNumber($view); |
||
| 192 | } |
||
| 193 | |||
| 194 | // Wrap the basket array in order to make it some more readable |
||
| 195 | $this->basketHelper->setBasket($view->sBasket); |
||
| 196 | |||
| 197 | // If no messages are shown, yet, check products from remote shop and build message array |
||
| 198 | if (($connectMessages = Shopware()->Session()->connectMessages) === null) { |
||
| 199 | $connectMessages = []; |
||
| 200 | |||
| 201 | $session = Shopware()->Session(); |
||
| 202 | $userData = $session['sOrderVariables']['sUserData']; |
||
| 203 | // prepare an order to check products |
||
| 204 | $order = new \Shopware\Connect\Struct\Order(); |
||
| 205 | $order->orderItems = []; |
||
| 206 | $order->billingAddress = $order->deliveryAddress = $this->getDeliveryAddress($userData); |
||
| 207 | |||
| 208 | $allProducts = []; |
||
| 209 | |||
| 210 | foreach ($this->basketHelper->getConnectProducts() as $shopId => $products) { |
||
| 211 | $products = $this->helper->prepareConnectUnit($products); |
||
| 212 | foreach ($products as $product) { |
||
| 213 | $allProducts[] = $product; |
||
| 214 | $order->orderItems[] = new OrderItem([ |
||
| 215 | 'product' => $product, |
||
| 216 | 'count' => $this->basketHelper->getQuantityForProduct($product), |
||
| 217 | ]); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | $this->eventManager->notify( |
||
| 222 | 'Connect_Merchant_Create_Order_Before', |
||
| 223 | [ |
||
| 224 | //we use clone to not be able to modify the connect order |
||
| 225 | 'order' => clone $order, |
||
| 226 | 'basket' => $view->sBasket, |
||
| 227 | ] |
||
| 228 | ); |
||
| 229 | |||
| 230 | try { |
||
| 231 | /** @var $checkResult \Shopware\Connect\Struct\CheckResult */ |
||
| 232 | $checkResult = $this->sdk->checkProducts($order); |
||
| 233 | $this->basketHelper->setCheckResult($checkResult); |
||
| 234 | |||
| 235 | if ($checkResult->hasErrors()) { |
||
| 236 | $connectMessages = $checkResult->errors; |
||
| 237 | } |
||
| 238 | } catch (\Exception $e) { |
||
| 239 | $this->logger->write(true, 'Error during checkout', $e, 'checkout'); |
||
| 240 | // If the checkout results in an exception because the remote shop is not available |
||
| 241 | // don't show the exception to the user but tell him to remove the products from that shop |
||
| 242 | $connectMessages = $this->getNotAvailableMessageForProducts($allProducts); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | if ($connectMessages) { |
||
| 247 | $connectMessages = $this->translateConnectMessages($connectMessages); |
||
| 248 | } |
||
| 249 | |||
| 250 | Shopware()->Session()->connectMessages = null; |
||
| 251 | |||
| 252 | // If no products are bought from the local shop, move the first connect shop into |
||
| 253 | // the content section. Also set that shop's id in the template |
||
| 254 | $shopId = $this->basketHelper->fixBasket(); |
||
| 255 | if ($shopId) { |
||
| 256 | $view->shopId = $shopId; |
||
| 257 | } |
||
| 258 | // Increase amount and shipping costs by the amount of connect shipping costs |
||
| 259 | $this->basketHelper->recalculate($this->basketHelper->getCheckResult()); |
||
| 260 | |||
| 261 | $connectMessages = $this->getNotShippableMessages($this->basketHelper->getCheckResult(), $connectMessages); |
||
| 262 | |||
| 263 | $view->assign($this->basketHelper->getDefaultTemplateVariables()); |
||
| 264 | |||
| 265 | // Set the sOrderVariables for the session based on the original content subarray of the basket array |
||
| 266 | // @HL - docs? |
||
| 267 | if ($actionName === 'confirm') { |
||
| 268 | $session = Shopware()->Session(); |
||
| 269 | /** @var $variables \ArrayObject */ |
||
| 270 | $variables = $session->offsetGet('sOrderVariables'); |
||
| 271 | |||
| 272 | $session->offsetSet('sOrderVariables', $this->basketHelper->getOrderVariablesForSession($variables)); |
||
| 273 | } |
||
| 274 | |||
| 275 | $view->assign($this->basketHelper->getConnectTemplateVariables($connectMessages)); |
||
| 276 | $view->assign('showShippingCostsSeparately', $this->factory->getConfigComponent()->getConfig('showShippingCostsSeparately', false)); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Helper to translate connect messages from the SDK. Will use the normalized message itself as namespace key |
||
| 281 | * |
||
| 282 | * @param $connectMessages |
||
| 283 | * @return mixed |
||
| 284 | */ |
||
| 285 | private function translateConnectMessages($connectMessages) |
||
| 286 | { |
||
| 287 | $namespace = Shopware()->Snippets()->getNamespace('frontend/checkout/connect'); |
||
| 288 | |||
| 289 | foreach ($connectMessages as &$connectMessage) { |
||
| 290 | $message = trim($connectMessage->message); |
||
| 291 | $normalized = strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $connectMessage->message)); |
||
| 292 | if (empty($normalized) || empty($message)) { |
||
| 293 | $normalized = 'unknown-connect-error'; |
||
| 294 | $message = 'Unknown error'; |
||
| 295 | } |
||
| 296 | $translation = $namespace->get( |
||
| 297 | $normalized, |
||
| 298 | $message, |
||
| 299 | true |
||
| 300 | ); |
||
| 301 | |||
| 302 | $connectMessage->message = $translation; |
||
| 303 | } |
||
| 304 | |||
| 305 | return $connectMessages; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Event listener method for the checkout->finishAction. Will reserve products and redirect to |
||
| 310 | * the confirm page if a product cannot be reserved |
||
| 311 | * |
||
| 312 | * @event Enlight_Controller_Action_PreDispatch_Frontend_Checkout |
||
| 313 | * @param \Enlight_Event_EventArgs $args |
||
| 314 | */ |
||
| 315 | public function reserveConnectProductsOnCheckoutFinish(\Enlight_Event_EventArgs $args) |
||
| 316 | { |
||
| 317 | /** @var $controller \Enlight_Controller_Action */ |
||
| 318 | $controller = $args->getSubject(); |
||
| 319 | $request = $controller->Request(); |
||
| 320 | $view = $controller->View(); |
||
| 321 | $session = Shopware()->Session(); |
||
| 322 | $userData = $session['sOrderVariables']['sUserData']; |
||
| 323 | $paymentName = $userData['additional']['payment']['name']; |
||
| 324 | |||
| 325 | if (($request->getActionName() !== 'finish' && $request->getActionName() !== 'payment')) { |
||
| 326 | if (($request->getActionName() === 'confirm' && $paymentName === 'klarna_checkout')) { |
||
| 327 | // BEP-1010 Fix for Klarna checkout |
||
| 328 | } else { |
||
| 329 | return; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | if (empty($session['sOrderVariables'])) { |
||
| 334 | return; |
||
| 335 | } |
||
| 336 | |||
| 337 | if (!$this->helper->hasBasketConnectProducts(Shopware()->SessionID())) { |
||
| 338 | return; |
||
| 339 | } |
||
| 340 | |||
| 341 | $userData = $session['sOrderVariables']['sUserData']; |
||
| 342 | $paymentId = $userData['additional']['payment']['id']; |
||
| 343 | |||
| 344 | if ($this->isPaymentAllowed($paymentId) === false) { |
||
| 345 | $connectMessage = new \stdClass(); |
||
| 346 | $connectMessage->message = 'frontend_checkout_cart_connect_payment_not_allowed'; |
||
| 347 | |||
| 348 | $connectMessages = [ |
||
| 349 | 0 => [ |
||
| 350 | 'connectmessage' => $connectMessage |
||
| 351 | ] |
||
| 352 | ]; |
||
| 353 | |||
| 354 | Shopware()->Session()->connectMessages = $this->translateConnectMessages($connectMessages); |
||
| 355 | $controller->forward('confirm'); |
||
| 356 | } |
||
| 357 | |||
| 358 | if (Shopware()->Config()->get('requirePhoneField')) { |
||
| 359 | $this->enforcePhoneNumber($view); |
||
| 360 | } |
||
| 361 | |||
| 362 | $order = new \Shopware\Connect\Struct\Order(); |
||
| 363 | $order->orderItems = []; |
||
| 364 | $order->deliveryAddress = $this->getDeliveryAddress($userData); |
||
| 365 | |||
| 366 | $basket = $session['sOrderVariables']['sBasket']; |
||
| 367 | |||
| 368 | /** @var \ShopwarePlugins\Connect\Components\Utils\OrderPaymentMapper $orderPaymentMapper */ |
||
| 369 | $orderPaymentMapper = new OrderPaymentMapper(); |
||
| 370 | $orderPaymentName = $userData['additional']['payment']['name']; |
||
| 371 | $order->paymentType = $orderPaymentMapper->mapShopwareOrderPaymentToConnect($orderPaymentName); |
||
| 372 | |||
| 373 | foreach ($basket['content'] as $row) { |
||
| 374 | if (!empty($row['mode'])) { |
||
| 375 | continue; |
||
| 376 | } |
||
| 377 | |||
| 378 | $articleDetailId = $row['additional_details']['articleDetailsID']; |
||
| 379 | if ($this->helper->isRemoteArticleDetailDBAL($articleDetailId) === false) { |
||
| 380 | continue; |
||
| 381 | } |
||
| 382 | $shopProductId = $this->helper->getShopProductId($articleDetailId); |
||
| 383 | |||
| 384 | $products = $this->helper->getRemoteProducts([$shopProductId->sourceId], $shopProductId->shopId); |
||
| 385 | $products = $this->helper->prepareConnectUnit($products); |
||
| 386 | |||
| 387 | if (empty($products)) { |
||
| 388 | continue; |
||
| 389 | } |
||
| 390 | $product = $products[0]; |
||
| 391 | |||
| 392 | |||
| 393 | if ($product === null || $product->shopId === null) { |
||
| 394 | continue; |
||
| 395 | } |
||
| 396 | |||
| 397 | $orderItem = new \Shopware\Connect\Struct\OrderItem(); |
||
| 398 | $orderItem->product = $product; |
||
| 399 | $orderItem->count = (int) $row['quantity']; |
||
| 400 | $order->orderItems[] = $orderItem; |
||
| 401 | } |
||
| 402 | |||
| 403 | if (empty($order->orderItems)) { |
||
| 404 | return; |
||
| 405 | } |
||
| 406 | |||
| 407 | try { |
||
| 408 | $order = $this->eventManager->filter( |
||
| 409 | 'Connect_Subscriber_OrderReservation_OrderFilter', |
||
| 410 | $order |
||
| 411 | ); |
||
| 412 | |||
| 413 | /** @var $reservation \Shopware\Connect\Struct\Reservation */ |
||
| 414 | $reservation = $this->sdk->reserveProducts($order); |
||
| 415 | |||
| 416 | if (!$reservation || !$reservation->success) { |
||
| 417 | throw new \Exception('Error during reservation'); |
||
| 418 | } |
||
| 419 | |||
| 420 | if (!empty($reservation->messages)) { |
||
| 421 | $messages = $reservation->messages; |
||
| 422 | } |
||
| 423 | } catch (\Exception $e) { |
||
| 424 | $this->logger->write(true, 'Error during reservation', $e, 'reservation'); |
||
| 425 | $messages = $this->getNotAvailableMessageForProducts(array_map( |
||
| 426 | function ($orderItem) { |
||
| 427 | return $orderItem->product; |
||
| 428 | }, |
||
| 429 | $order->orderItems |
||
| 430 | )); |
||
| 431 | } |
||
| 432 | |||
| 433 | if (!empty($messages)) { |
||
| 434 | Shopware()->Session()->connectMessages = $messages; |
||
| 435 | $controller->forward('confirm'); |
||
| 436 | } else { |
||
| 437 | Shopware()->Session()->connectReservation = serialize($reservation); |
||
|
0 ignored issues
–
show
|
|||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Helper method to create an address struct from shopware session info |
||
| 443 | * |
||
| 444 | * @param $userData |
||
| 445 | * @return Address |
||
| 446 | */ |
||
| 447 | private function getDeliveryAddress($userData) |
||
| 448 | { |
||
| 449 | if (!$userData) { |
||
| 450 | return $this->createDummyAddress('DEU'); |
||
| 451 | } |
||
| 452 | |||
| 453 | $shippingData = $userData['shippingaddress']; |
||
| 454 | $address = new Address(); |
||
| 455 | $address->zip = $shippingData['zipcode']; |
||
| 456 | $address->city = $shippingData['city']; |
||
| 457 | $address->country = $userData['additional']['countryShipping']['iso3']; //when the user is not logged in |
||
| 458 | $address->phone = $userData['billingaddress']['phone']; |
||
| 459 | $address->email = $userData['additional']['user']['email']; |
||
| 460 | if (!empty($userData['additional']['stateShipping']['shortcode'])) { |
||
| 461 | $address->state = $userData['additional']['stateShipping']['shortcode']; |
||
| 462 | } |
||
| 463 | if (!empty($shippingData['department'])) { |
||
| 464 | $address->department = $shippingData['department']; |
||
| 465 | } |
||
| 466 | if (!empty($shippingData['additionalAddressLine1'])) { |
||
| 467 | $address->additionalAddressLine1 = $shippingData['additionalAddressLine1']; |
||
| 468 | } |
||
| 469 | if (!empty($shippingData['additionalAddressLine2'])) { |
||
| 470 | $address->additionalAddressLine2 = $shippingData['additionalAddressLine2']; |
||
| 471 | } |
||
| 472 | $address->firstName = $shippingData['firstname']; |
||
| 473 | $address->surName = $shippingData['lastname']; |
||
| 474 | if (!empty($shippingData['company'])) { |
||
| 475 | $address->company = $shippingData['company']; |
||
| 476 | } |
||
| 477 | $address->street = $shippingData['street']; |
||
| 478 | $address->streetNumber = (string) $shippingData['streetnumber']; |
||
| 479 | |||
| 480 | return $address; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param string $country |
||
| 485 | * @return Address |
||
| 486 | */ |
||
| 487 | private function createDummyAddress($country = 'DEU') |
||
| 488 | { |
||
| 489 | return new Address([ |
||
| 490 | 'country' => $country, |
||
| 491 | 'firstName' => 'Shopware', |
||
| 492 | 'surName' => 'AG', |
||
| 493 | 'street' => 'Eggeroder Str. 6', |
||
| 494 | 'zip' => '48624', |
||
| 495 | 'city' => 'Schöppingen', |
||
| 496 | 'phone' => '+49 (0) 2555 92885-0', |
||
| 497 | 'email' => '[email protected]' |
||
| 498 | ]); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Hooks the sSaveOrder frontend method and reserves the connect products |
||
| 503 | * |
||
| 504 | * @param $orderNumber |
||
| 505 | * @throws \ShopwarePlugins\Connect\Components\Exceptions\CheckoutException |
||
| 506 | */ |
||
| 507 | public function checkoutReservedProducts($orderNumber) |
||
| 508 | { |
||
| 509 | if (empty($orderNumber)) { |
||
| 510 | return; |
||
| 511 | } |
||
| 512 | |||
| 513 | $reservation = unserialize(Shopware()->Session()->connectReservation); |
||
| 514 | |||
| 515 | if ($reservation !== null && $reservation !== false) { |
||
| 516 | $result = $this->sdk->checkout($reservation, $orderNumber); |
||
| 517 | foreach ($result as $shopId => $success) { |
||
| 518 | if (!$success) { |
||
| 519 | $e = new CheckoutException("Could not checkout from warehouse {$shopId}"); |
||
| 520 | $this->logger->write(true, 'Error during checkout with this reservation: ' . json_encode($reservation, JSON_PRETTY_PRINT), $e, 'checkout'); |
||
| 521 | throw $e; |
||
| 522 | } |
||
| 523 | } |
||
| 524 | $this->helper->clearConnectReservation(); |
||
| 525 | } |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Asks the user to leave is phone number if connect products are in the basket and the |
||
| 530 | * phone number was not configured, yet. |
||
| 531 | * |
||
| 532 | * @param \Enlight_View_Default $view |
||
| 533 | * @return null |
||
| 534 | */ |
||
| 535 | public function enforcePhoneNumber($view) |
||
| 536 | { |
||
| 537 | if (Shopware()->Session()->sUserId && $this->helper->hasBasketConnectProducts(Shopware()->SessionID())) { |
||
| 538 | $id = Shopware()->Session()->sUserId; |
||
| 539 | |||
| 540 | $sql = 'SELECT phone FROM s_user_billingaddress WHERE userID = :id'; |
||
| 541 | $result = Shopware()->Db()->fetchOne($sql, ['id' => $id]); |
||
| 542 | if (!$result) { |
||
| 543 | $view->assign('phoneMissing', true); |
||
| 544 | } |
||
| 545 | } |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @param Product[] $products |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | protected function getNotAvailableMessageForProducts($products) |
||
| 553 | { |
||
| 554 | $messages = []; |
||
| 555 | foreach ($products as $product) { |
||
| 556 | $messages[] = new Message([ |
||
| 557 | 'message' => 'Due to technical reasons, product %product is not available.', |
||
| 558 | 'values' => [ |
||
| 559 | 'product' => $product->title, |
||
| 560 | ] |
||
| 561 | ]); |
||
| 562 | } |
||
| 563 | |||
| 564 | return $messages; |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @param \Shopware\Connect\Struct\CheckResult $checkResult |
||
| 569 | * @param $connectMessages |
||
| 570 | * @return mixed |
||
| 571 | */ |
||
| 572 | protected function getNotShippableMessages($checkResult, $connectMessages) |
||
| 573 | { |
||
| 574 | if (!$checkResult instanceof CheckResult) { |
||
| 575 | return $connectMessages; |
||
| 576 | } |
||
| 577 | |||
| 578 | $namespace = Shopware()->Snippets()->getNamespace('frontend/checkout/connect'); |
||
| 579 | |||
| 580 | foreach ($checkResult->shippingCosts as $shipping) { |
||
| 581 | if ($shipping->isShippable === false) { |
||
| 582 | if (empty($shipping->messages)) { |
||
| 583 | |||
| 584 | $shop = $this->sdk->getShop($shipping->shopId); |
||
| 585 | |||
| 586 | $connectMessages[] = new Message([ |
||
| 587 | 'message' => $namespace->get( |
||
| 588 | 'frontend_checkout_cart_connect_not_shippable_detailed', |
||
| 589 | 'Ihre Bestellung kann nicht geliefert werden. Der Händler %supplierName liefert nicht in Ihr Land.', |
||
| 590 | false |
||
| 591 | ), |
||
| 592 | 'values' => [ |
||
| 593 | 'supplierName' => $shop->name |
||
| 594 | ] |
||
| 595 | ]); |
||
| 596 | |||
| 597 | continue; |
||
| 598 | } |
||
| 599 | |||
| 600 | foreach ($shipping->messages as $message) { |
||
| 601 | $connectMessages[] = new Message([ |
||
| 602 | 'message' => $namespace->get( |
||
| 603 | $message->message, |
||
| 604 | $message->message |
||
| 605 | ), |
||
| 606 | 'values' => $message->values |
||
| 607 | ]); |
||
| 608 | } |
||
| 609 | } |
||
| 610 | } |
||
| 611 | |||
| 612 | return $connectMessages; |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @param $orderNumber |
||
| 617 | * @return void |
||
| 618 | */ |
||
| 619 | private function setOrderStatusError($orderNumber) |
||
| 620 | { |
||
| 621 | $repo = $this->manager->getRepository(Order::class); |
||
| 622 | |||
| 623 | /** @var Order $order */ |
||
| 624 | $order = $repo->findOneBy(['number' => $orderNumber]); |
||
| 625 | |||
| 626 | $repoStatus = $this->manager->getRepository(Status::class); |
||
| 627 | $status = $repoStatus->findOneBy(['name' => ConnectOrderUtil::ORDER_STATUS_ERROR, 'group' => Status::GROUP_STATE ]); |
||
| 628 | |||
| 629 | $order->setOrderStatus($status); |
||
| 630 | $this->manager->persist($order); |
||
| 631 | $this->manager->flush(); |
||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Check is allowed payment method with connect products |
||
| 636 | * @param int $paymentId |
||
| 637 | * @return bool |
||
| 638 | */ |
||
| 639 | private function isPaymentAllowed($paymentId) |
||
| 640 | { |
||
| 641 | if ($paymentId < 1) { |
||
| 642 | return false; |
||
| 643 | } |
||
| 644 | |||
| 645 | $paymentRepository = $this->manager->getRepository(Payment::class); |
||
| 646 | /** @var Payment $payment */ |
||
| 647 | $payment = $paymentRepository->find($paymentId); |
||
| 648 | |||
| 649 | if (!$payment) { |
||
| 650 | return false; |
||
| 651 | } |
||
| 652 | |||
| 653 | if ($payment->getAttribute()->getConnectIsAllowed() == 0) { |
||
| 654 | return false; |
||
| 655 | } |
||
| 656 | |||
| 657 | return true; |
||
| 658 | } |
||
| 659 | } |
||
| 660 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: