config = $configFactory->create(); $this->config->setMethod(Config::METHOD_EXPRESS); $this->payment = $payment; $this->session = $session; $this->serializer = $serializer; $this->smartButtonConfig = $smartButtonConfig; $this->urlBuilder = $urlBuilder; $this->quoteIdMask = $quoteIdToMaskedQuoteId; } /** * Check `in_context` config value * * @return bool */ private function isInContext(): bool { return (bool)(int) $this->config->getValue('in_context'); } /** * Check `visible_on_cart` config value * * @return bool */ private function isVisibleOnCart(): bool { return (bool)(int) $this->config->getValue('visible_on_cart'); } /** * Check is Paypal In-Context Express Checkout button should render in cart/mini-cart * * @return bool */ private function shouldRender(): bool { return $this->payment->isAvailable($this->session->getQuote()) && $this->isInContext() && $this->isVisibleOnCart() && $this->getQuoteId() && !$this->getIsInCatalogProduct(); } /** * @inheritdoc */ protected function _toHtml() { if (!$this->shouldRender()) { return ''; } return parent::_toHtml(); } /** * Get shortcut alias * * @return string */ public function getAlias() { return $this->getData(self::ALIAS_ELEMENT_INDEX); } /** * Returns string to initialize js component * * @return string */ public function getJsInitParams(): string { $config = []; $quoteId = $this->getQuoteId(); if (!empty($quoteId)) { $clientConfig = [ 'quoteId' => $quoteId, 'customerId' => $this->session->getQuote()->getCustomerId(), 'button' => 1, 'getTokenUrl' => $this->urlBuilder->getUrl( 'paypal/express/getTokenData', ['_secure' => $this->getRequest()->isSecure()] ), 'onAuthorizeUrl' => $this->urlBuilder->getUrl( 'paypal/express/onAuthorization', ['_secure' => $this->getRequest()->isSecure()] ), 'onCancelUrl' => $this->urlBuilder->getUrl( 'paypal/express/cancel', ['_secure' => $this->getRequest()->isSecure()] ) ]; $smartButtonsConfig = $this->getIsShoppingCart() ? $this->smartButtonConfig->getConfig('cart') : $this->smartButtonConfig->getConfig('mini_cart'); $clientConfig = array_replace_recursive($clientConfig, $smartButtonsConfig); $config = [ 'Magento_Paypal/js/in-context/button' => [ 'clientConfig' => $clientConfig ] ]; } $json = $this->serializer->serialize($config); return $json; } /** * Returns container id * * @return string */ public function getContainerId(): string { return $this->getData('button_id'); } /** * Get quote id from session * * @return string */ private function getQuoteId(): string { $quoteId = (int)$this->session->getQuoteId(); if (!$this->session->getQuote()->getCustomerId()) { try { $quoteId = $this->quoteIdMask->execute($quoteId); } catch (NoSuchEntityException $e) { $quoteId = ""; } } return (string)$quoteId; } }