From 47b450986b319cf736a2fea289ee6ef656dd812a Mon Sep 17 00:00:00 2001 From: Marvin-Magmodules Date: Thu, 2 Jan 2020 15:57:41 +0100 Subject: [PATCH 1/6] Combined 1.2.0 edits --- Api/Data/Quote/QuoteReferenceInterface.php | 7 + .../Shipping/Method/Paazl/Widget/Config.php | 75 ++++ .../Shipping/Method/Paazl/Widget/Modal.php | 124 +++++ Model/Admin/Order/Create/LanguageProvider.php | 52 +++ .../Order/Create/PaazlConfigProvider.php | 80 ++++ .../Order/Create/WidgetConfigProvider.php | 424 ++++++++++++++++++ Model/Carrier/Paazlshipping.php | 50 ++- Model/Checkout/PaazlConfigProvider.php | 2 + Model/Checkout/WidgetConfigProvider.php | 2 +- Model/CompositeConfigProvider.php | 44 ++ Model/Config.php | 8 + Model/Quote/QuoteReference.php | 31 ++ Model/TokenRetriever.php | 15 +- Observer/Admin/Order/Create/ProcessData.php | 77 ++++ composer.json | 2 +- etc/adminhtml/di.xml | 16 + etc/adminhtml/events.xml | 11 + etc/adminhtml/system.xml | 5 + etc/module.xml | 2 +- .../layout/sales_order_create_index.xml | 26 ++ ...rder_create_load_block_shipping_method.xml | 14 + view/adminhtml/requirejs-config.js | 5 + .../templates/order/create/paazl-js.phtml | 43 ++ .../order/create/shipping/method/form.phtml | 107 +++++ .../order/create/shipping/method/paazl.phtml | 14 + .../shipping/method/paazl/widget/config.phtml | 19 + .../shipping/method/paazl/widget/modal.phtml | 16 + view/adminhtml/web/css/styles.css | 13 + .../shipping/method/paazl/widget/config.js | 109 +++++ .../shipping/method/paazl/widget/modal.js | 77 ++++ .../shipping/method/paazl/widget/config.html | 3 + view/base/requirejs-config.js | 25 ++ .../templates/checkout/widget.phtml | 2 +- view/frontend/requirejs-config.js | 17 - .../web/js/checkout/view/widget-config.js | 86 ++-- 35 files changed, 1504 insertions(+), 99 deletions(-) create mode 100755 Block/Adminhtml/Order/Create/Shipping/Method/Paazl/Widget/Config.php create mode 100755 Block/Adminhtml/Order/Create/Shipping/Method/Paazl/Widget/Modal.php create mode 100755 Model/Admin/Order/Create/LanguageProvider.php create mode 100755 Model/Admin/Order/Create/PaazlConfigProvider.php create mode 100755 Model/Admin/Order/Create/WidgetConfigProvider.php create mode 100755 Model/CompositeConfigProvider.php create mode 100755 Observer/Admin/Order/Create/ProcessData.php create mode 100755 etc/adminhtml/di.xml create mode 100755 etc/adminhtml/events.xml create mode 100755 view/adminhtml/layout/sales_order_create_index.xml create mode 100755 view/adminhtml/layout/sales_order_create_load_block_shipping_method.xml create mode 100755 view/adminhtml/templates/order/create/paazl-js.phtml create mode 100755 view/adminhtml/templates/order/create/shipping/method/form.phtml create mode 100755 view/adminhtml/templates/order/create/shipping/method/paazl.phtml create mode 100755 view/adminhtml/templates/order/create/shipping/method/paazl/widget/config.phtml create mode 100755 view/adminhtml/templates/order/create/shipping/method/paazl/widget/modal.phtml create mode 100755 view/adminhtml/web/js/admin/order/create/shipping/method/paazl/widget/config.js create mode 100755 view/adminhtml/web/js/admin/order/create/shipping/method/paazl/widget/modal.js create mode 100755 view/adminhtml/web/template/admin/order/create/shipping/method/paazl/widget/config.html create mode 100755 view/base/requirejs-config.js rename view/{frontend => base}/templates/checkout/widget.phtml (88%) mode change 100644 => 100755 diff --git a/Api/Data/Quote/QuoteReferenceInterface.php b/Api/Data/Quote/QuoteReferenceInterface.php index 7ed39a1..de1ab3f 100644 --- a/Api/Data/Quote/QuoteReferenceInterface.php +++ b/Api/Data/Quote/QuoteReferenceInterface.php @@ -73,4 +73,11 @@ public function setTokenExpiresAt($value); * @return string */ public function getTokenExpiresAt(); + + /** + * @param \DateTime|null $now + * + * @return bool + */ + public function isTokenExpired(\DateTime $now = null); } diff --git a/Block/Adminhtml/Order/Create/Shipping/Method/Paazl/Widget/Config.php b/Block/Adminhtml/Order/Create/Shipping/Method/Paazl/Widget/Config.php new file mode 100755 index 0000000..be2d760 --- /dev/null +++ b/Block/Adminhtml/Order/Create/Shipping/Method/Paazl/Widget/Config.php @@ -0,0 +1,75 @@ +configProvider = $configProvider; + $this->serializer = $serializer; + parent::__construct($context, $data); + } + + /** + * Retrieves order configuration + * + * @return array + */ + public function getWidgetConfig() + { + return $this->configProvider->getConfig(); + } + + /** + * Retrieves serialized order config. + * + * @return bool|string + */ + public function getSerializedWidgetConfig() + { + return $this->serializer->serialize($this->getWidgetConfig()); + } +} diff --git a/Block/Adminhtml/Order/Create/Shipping/Method/Paazl/Widget/Modal.php b/Block/Adminhtml/Order/Create/Shipping/Method/Paazl/Widget/Modal.php new file mode 100755 index 0000000..d79fac4 --- /dev/null +++ b/Block/Adminhtml/Order/Create/Shipping/Method/Paazl/Widget/Modal.php @@ -0,0 +1,124 @@ +sessionQuote = $sessionQuote; + $this->serializer = $serializer; + parent::__construct($context, $data); + } + + /** + * Retrieve quote session object + * + * @return SessionQuote + */ + protected function getSession() + { + return $this->sessionQuote; + } + + /** + * Retrieve quote model object + * + * @return Quote + */ + public function getQuote() + { + return $this->getSession()->getQuote(); + } + + /** + * Retrieve customer identifier + * + * @return int + */ + public function getCustomerId() + { + return $this->getSession()->getCustomerId(); + } + + /** + * Retrieve store model object + * + * @return Store + */ + public function getStore() + { + return $this->getSession()->getStore(); + } + + /** + * Retrieve store identifier + * + * @return int + */ + public function getStoreId() + { + return $this->getSession()->getStoreId(); + } + + /** + * Initialize paazl modal widget + * + * @return string + */ + public function getPaazlWidgetModalJson() + { + return $this->serializer->serialize( + [ + 'paazlWidgetModal' => [ + 'modalClass' => 'ui-dialog-active ui-popup paazl-widget-modal' + ], + ] + ); + } +} diff --git a/Model/Admin/Order/Create/LanguageProvider.php b/Model/Admin/Order/Create/LanguageProvider.php new file mode 100755 index 0000000..6834efe --- /dev/null +++ b/Model/Admin/Order/Create/LanguageProvider.php @@ -0,0 +1,52 @@ +resolver = $resolver; + } + + /** + * Retrieve assoc array of checkout configuration + * + * @return array + */ + public function getConfig() + { + $languageCode = 'en'; + if ($this->resolver->getLocale()) { + $locale = $this->resolver->getLocale(); + $languageCode = explode('_', $locale)[0]; + } + + return [ + 'language' => $languageCode, + ]; + } +} diff --git a/Model/Admin/Order/Create/PaazlConfigProvider.php b/Model/Admin/Order/Create/PaazlConfigProvider.php new file mode 100755 index 0000000..cdfab0d --- /dev/null +++ b/Model/Admin/Order/Create/PaazlConfigProvider.php @@ -0,0 +1,80 @@ +config = $config; + $this->urlProvider = $urlProvider; + $this->widgetConfigProvider = $widgetConfigProvider; + } + + /** + * Retrieve assoc array of checkout configuration + * + * @return array + */ + public function getConfig() + { + $config = []; + if (!$this->config->isEnabled()) { + return $config; + } + + $config['hideOtherMethods'] = $this->config->isHideOtherShippingMethods(); + $config['carrierCode'] = Paazlshipping::CODE; + $config['methodCode'] = Paazlshipping::CODE; + $config['baseApiUrl'] = $this->urlProvider->getBaseUrl(); + $config['googleMapKey'] = $this->config->getGoogleMapKey(); + $config['widgetConfig'] = $this->widgetConfigProvider->getConfig(); + $config['mode'] = $this->config->isProductionApiMode() ? 'live' : 'test'; + + if (empty($config['widgetConfig']['token'])) { + // We were unable to obtain a token - enabling other methods if they're available + $config['hideOtherMethods'] = false; + } + + return [ + 'paazlshipping' => $config + ]; + } +} diff --git a/Model/Admin/Order/Create/WidgetConfigProvider.php b/Model/Admin/Order/Create/WidgetConfigProvider.php new file mode 100755 index 0000000..cbcd7c3 --- /dev/null +++ b/Model/Admin/Order/Create/WidgetConfigProvider.php @@ -0,0 +1,424 @@ +scopeConfig = $scopeConfig; + $this->sessionQuote = $sessionQuote; + $this->order = $order; + $this->paazlApi = $paazlApi; + $this->generalHelper = $generalHelper; + $this->tokenRetriever = $tokenRetriever; + $this->urlProvider = $urlProvider; + $this->languageProvider = $languageProvider; + $this->productRepository = $productRepository; + } + + /** + * {@inheritDoc} + */ + public function getConfig() + { + $countryId = $this->getDefaultCountry(); + $postcode = $this->getDefaultPostcode(); + + $shippingAddress = $this->getQuote()->getShippingAddress(); + if ($shippingAddress->getCountryId() && + $shippingAddress->getPostcode()) { + $countryId = $shippingAddress->getCountryId(); + $postcode = $shippingAddress->getPostcode(); + } + + $numberOfProcessingDays = self::DEFAULT_NUMBER_OF_PROCESSING_DAYS; + $goods = []; + foreach ($this->getQuote()->getAllVisibleItems() as $item) { + $goodsItem = [ + "quantity" => (int)$item->getQty(), + "weight" => doubleval($item->getWeight()), + "price" => $this->formatPrice($item->getPrice()) + ]; + + if (($itemNumberOfProcessingDays = $this->getProductNumberOfProcessingDays($item)) + && $itemNumberOfProcessingDays > $numberOfProcessingDays) { + $numberOfProcessingDays = (int)$itemNumberOfProcessingDays; + } + + if ($deliveryMatrixCode = $this->getProductDeliveryMatrix($item)) { + $goodsItem["startMatrix"] = $deliveryMatrixCode; + } + $goods[] = $goodsItem; + } + + $config = [ + "mountElementId" => "widget_paazlshipping_paazlshipping", + "apiKey" => $this->getApiKey(), + "token" => $this->getApiToken(), + "loadPaazlBasedData" => true, + "loadCarrierBasedData" => true, + "availableTabs" => $this->getAvailableTabs(), + "defaultTab" => $this->getDefaultTab(), + "style" => $this->getWidgetTheme(), + "nominatedDateEnabled" => $this->getNominatedDateEnabled(), + "consigneeCountryCode" => $countryId, + "consigneePostalCode" => $postcode, + "numberOfProcessingDays" => $numberOfProcessingDays, + "deliveryDateOptions" => [ + "startDate" => date("Y-m-d"), + "numberOfDays" => 10 + ], + "currency" => "EUR", + "deliveryOptionDateFormat" => "ddd DD MMM", + "deliveryEstimateDateFormat" => "dddd DD MMMM", + "pickupOptionDateFormat" => "ddd DD MMM", + "pickupEstimateDateFormat" => "dddd DD MMMM", + "sortingModel" => [ + "orderBy" => "PRICE", + "sortOrder" => "ASC" + ], + "shipmentParameters" => [ + "totalWeight" => $this->getTotalWeight(), + "totalPrice" => $this->formatPrice($this->getQuote()->getSubtotal()), + "numberOfGoods" => $this->getProductsCount(), + "goods" => $goods + ], + "shippingOptionsLimit" => $this->getShippingOptionsLimit(), + "pickupLocationsPageLimit" => $this->getPickupLocationsPageLimit(), + "pickupLocationsLimit" => $this->getPickupLocationsLimit(), + "initialPickupLocations" => $this->getInitialPickupLocations() + ]; + + $config = array_merge($config, $this->languageProvider->getConfig()); + + $this->generalHelper->addTolog('config', $config); + + return $config; + } + + /** + * @return Quote + */ + public function getQuote() + { + return $this->sessionQuote->getQuote(); + } + + /** + * @return mixed + */ + public function getDefaultCountry() + { + return $this->scopeConfig->getDefaultCountry(); + } + + /** + * @return mixed + */ + public function getDefaultPostcode() + { + return $this->scopeConfig->getDefaultPostcode(); + } + + /** + * @param $price + * + * @return double + */ + public function formatPrice($price) + { + return number_format($price, 2, '.', ''); + } + + /** + * @return mixed + */ + public function getApiKey() + { + return $this->scopeConfig->getApiKey(); + } + + /** + * @return mixed + */ + public function getApiToken() + { + try { + return $this->tokenRetriever->retrieveByQuote($this->getQuote()); + } catch (LocalizedException $e) { + $this->generalHelper->addTolog('exception', $e->getMessage()); + } + + return null; + } + + /** + * @return array + */ + public function getAvailableTabs() + { + return $this->scopeConfig->getAvailableTabs(); + } + + /** + * @return mixed + */ + public function getDefaultTab() + { + return $this->scopeConfig->getDefaultTab(); + } + + /** + * @return boolean + */ + public function getWidgetTheme() + { + $widgetTheme = $this->scopeConfig->getWidgetTheme(); + return $widgetTheme == 'CUSTOM' ? 'DEFAULT' : $widgetTheme; + } + + /** + * @return string + */ + public function getNominatedDateEnabled() + { + return $this->scopeConfig->getNominatedDateEnabled(); + } + + /** + * @return float + */ + public function getTotalWeight() + { + $weight = 0; + $quote = $this->getQuote(); + foreach ($quote->getAllVisibleItems() as $_item) { + $weight += $_item->getWeight(); + } + return $weight; + } + + /** + * @return float + */ + public function getProductsCount() + { + $count = 0; + $quote = $this->getQuote(); + foreach ($quote->getAllVisibleItems() as $_item) { + $count += $_item->getQty(); + } + return $count; + } + + /** + * Gets number of processing days from product + * + * @param AbstractItem $item + * @return int|mixed|null + * @throws NoSuchEntityException + */ + public function getProductNumberOfProcessingDays(AbstractItem $item) + { + $product = $this->productRepository->get($item->getSku()); + + if ($attribute = $this->scopeConfig->getProductAttributeNumberOfProcessingDays()) { + if (($numberOfProcessingDays = $product->getData($attribute)) !== null) { + if (is_numeric($numberOfProcessingDays) + && $numberOfProcessingDays >= Config::MIN_NUMBER_OF_PROCESSING_DAYS + && $numberOfProcessingDays <= Config::MAX_NUMBER_OF_PROCESSING_DAYS + ) { + return $numberOfProcessingDays; + } + } + } + + return null; + } + + /** + * @return int + */ + public function getShippingOptionsLimit() + { + return $this->scopeConfig->getShippingOptionsLimit(); + } + + /** + * @return int + */ + public function getPickupLocationsPageLimit() + { + return $this->scopeConfig->getPickupLocationsPageLimit(); + } + + /** + * @return int + */ + public function getPickupLocationsLimit() + { + return $this->scopeConfig->getPickupLocationsLimit(); + } + + /** + * @return int + */ + public function getInitialPickupLocations() + { + return $this->scopeConfig->getInitialPickupLocations(); + } + + /** + * @return mixed + */ + public function getCustomCss() + { + return $this->scopeConfig->getCustomCss(); + } + + /** + * @return boolean + */ + public function isEnabled() + { + return $this->scopeConfig->isEnabled(); + } + + /** + * @return string + */ + public function getApiBaseUrl() + { + return $this->urlProvider->getBaseUrl(); + } + + /** + * Gets delivery matrix from product + * + * @param AbstractItem $item + * @return int|mixed|null + * @throws NoSuchEntityException + */ + public function getProductDeliveryMatrix(AbstractItem $item) + { + $product = $this->productRepository->get($item->getSku()); + + if ($attribute = $this->scopeConfig->getProductAttributeDeliveryMatrix()) { + if (($deliveryMatrixCode = $product->getData($attribute)) !== null + && $this->validateDeliveryMatrixCode($deliveryMatrixCode) + ) { + return $deliveryMatrixCode; + } + } + + return null; + } + + /** + * Validates matrix code according to Paazl instructions + * + * @param string $value + * @return bool + */ + protected function validateDeliveryMatrixCode(string $value) + { + preg_match('/^[A-Z]{1,2}$/', $value, $matches); + + return count($matches) === 1; + } +} diff --git a/Model/Carrier/Paazlshipping.php b/Model/Carrier/Paazlshipping.php index 692a26b..fa94368 100755 --- a/Model/Carrier/Paazlshipping.php +++ b/Model/Carrier/Paazlshipping.php @@ -8,6 +8,7 @@ use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\DataObject; +use Magento\Quote\Model\Quote\Item; use Magento\Shipping\Model\Carrier\AbstractCarrier; use Magento\Shipping\Model\Carrier\CarrierInterface; use Magento\Shipping\Model\Rate\ResultFactory; @@ -15,7 +16,6 @@ use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory; use Magento\Quote\Model\Quote\Address\RateRequest; use Paazl\CheckoutWidget\Model\ExtInfoHandler; -use Magento\Checkout\Helper\Data as CheckoutHelper; use Paazl\CheckoutWidget\Model\Config; use Magento\Framework\App\State as AppState; use Magento\Framework\App\Area; @@ -57,11 +57,6 @@ class Paazlshipping extends AbstractCarrier implements CarrierInterface */ private $rateMethodFactory; - /** - * @var CheckoutHelper - */ - private $checkoutHelper; - /** * @var LoggerInterface */ @@ -91,7 +86,6 @@ class Paazlshipping extends AbstractCarrier implements CarrierInterface * @param AppState $appState * @param ResultFactory $rateResultFactory * @param MethodFactory $rateMethodFactory - * @param CheckoutHelper $checkoutHelper * @param Config $config * @param ExtInfoHandler $extInfoHandler * @param array $data @@ -103,14 +97,12 @@ public function __construct( AppState $appState, ResultFactory $rateResultFactory, MethodFactory $rateMethodFactory, - CheckoutHelper $checkoutHelper, Config $config, ExtInfoHandler $extInfoHandler, array $data = [] ) { $this->rateResultFactory = $rateResultFactory; $this->rateMethodFactory = $rateMethodFactory; - $this->checkoutHelper = $checkoutHelper; $this->logger = $logger; parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data); @@ -132,12 +124,6 @@ public function getCarrierCode() */ public function isActive() { - $areaCode = $this->appState->getAreaCode(); - if ($areaCode === Area::AREA_ADMIN || - $areaCode === Area::AREA_ADMINHTML) { - return false; - } - return parent::isActive() && $this->config->isEnabled(); } @@ -160,7 +146,7 @@ public function getAllowedMethods() */ public function collectRates(RateRequest $request) { - if (!$this->isActive()) { + if (!$this->getConfigFlag('active')) { return false; } @@ -176,12 +162,15 @@ public function collectRates(RateRequest $request) $method->setMethodTitle($this->getConfigData('name')); // Recalculate shipping price - $info = $this->extInfoHandler->getInfoFromQuote($quote = $this->checkoutHelper->getQuote()); - - if ($info && $info->getType()) { - $shippingPrice = $info->getPrice(); - if ($info->getOptionTitle()) { - $method->setMethodTitle($info->getOptionTitle()); + $quote = $this->extractQuote($request); + if ($quote) { + $info = $this->extInfoHandler->getInfoFromQuote($quote); + + if ($info && $info->getType()) { + $shippingPrice = $info->getPrice(); + if ($info->getOptionTitle()) { + $method->setMethodTitle($info->getOptionTitle()); + } } } @@ -192,4 +181,21 @@ public function collectRates(RateRequest $request) $result->append($method); return $result; } + + /** + * @param RateRequest $request + * + * @return \Magento\Quote\Model\Quote|null + */ + private function extractQuote(RateRequest $request) + { + $quote = null; + $items = $request->getAllItems(); + $current = current($items); + if ($current instanceof Item) { + $quote = $current->getQuote(); + } + + return $quote; + } } diff --git a/Model/Checkout/PaazlConfigProvider.php b/Model/Checkout/PaazlConfigProvider.php index d730ce0..1b8bc7c 100755 --- a/Model/Checkout/PaazlConfigProvider.php +++ b/Model/Checkout/PaazlConfigProvider.php @@ -66,6 +66,8 @@ public function getConfig() $config['carrierCode'] = Paazlshipping::CODE; $config['methodCode'] = Paazlshipping::CODE; $config['baseApiUrl'] = $this->urlProvider->getBaseUrl(); + $config['checkoutApiUrl'] = $this->urlProvider->getCheckoutUrl(); + $config['saveShippingInfoInstantly'] = $this->config->saveShippingInformationInstantly(); $config['googleMapKey'] = $this->config->getGoogleMapKey(); $config['widgetConfig'] = $this->widgetConfigProvider->getConfig(); $config['mode'] = $this->config->isProductionApiMode() ? 'live' : 'test'; diff --git a/Model/Checkout/WidgetConfigProvider.php b/Model/Checkout/WidgetConfigProvider.php index f28dcd7..bf6af81 100644 --- a/Model/Checkout/WidgetConfigProvider.php +++ b/Model/Checkout/WidgetConfigProvider.php @@ -234,7 +234,7 @@ public function getDefaultPostcode() */ public function formatPrice($price) { - return number_format($price, 2); + return number_format($price, 2, '.', ''); } /** diff --git a/Model/CompositeConfigProvider.php b/Model/CompositeConfigProvider.php new file mode 100755 index 0000000..42bc60b --- /dev/null +++ b/Model/CompositeConfigProvider.php @@ -0,0 +1,44 @@ +configProviders = $configProviders; + } + + /** + * {@inheritdoc} + */ + public function getConfig() + { + $config = []; + foreach ($this->configProviders as $configProvider) { + $config = array_merge_recursive($config, $configProvider->getConfig()); + } + return $config; + } +} diff --git a/Model/Config.php b/Model/Config.php index 23fb80a..146f31c 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -346,4 +346,12 @@ public function getCarrierTitle() { return $this->getValue(self::API_CONFIG_PATH . '/title'); } + + /** + * @return bool + */ + public function saveShippingInformationInstantly() + { + return !!$this->getValue(self::API_CONFIG_PATH . '/onestep_checkout_used'); + } } diff --git a/Model/Quote/QuoteReference.php b/Model/Quote/QuoteReference.php index abf013b..125fd1e 100644 --- a/Model/Quote/QuoteReference.php +++ b/Model/Quote/QuoteReference.php @@ -7,6 +7,7 @@ namespace Paazl\CheckoutWidget\Model\Quote; use Magento\Framework\Model\AbstractModel; +use Magento\Framework\Stdlib\DateTime; use Paazl\CheckoutWidget\Api\Data\Quote\QuoteReferenceInterface; use Paazl\CheckoutWidget\Model\ResourceModel\Quote\QuoteReference as QuoteReferenceResource; @@ -98,4 +99,34 @@ public function getTokenExpiresAt() { return $this->getData(self::TOKEN_EXPIRES_AT); } + + /** + * @inheritDoc + */ + public function isTokenExpired(\DateTime $now = null) + { + $result = true; + $expiresAt = $this->getTokenExpiresAt(); + if (empty($expiresAt)) { + return $result; + } + + $expires = \DateTime::createFromFormat( + DateTime::DATETIME_PHP_FORMAT, + $expiresAt, + new \DateTimeZone('UTC') + ); + + try { + $now = $now ?: new \DateTime('now', new \DateTimeZone('UTC')); + // phpcs:ignore + } catch (\Exception $e) { + } + + if ($now && $expires && ($now < $expires)) { + $result = false; + } + + return $result; + } } diff --git a/Model/TokenRetriever.php b/Model/TokenRetriever.php index babfce4..1e83de7 100644 --- a/Model/TokenRetriever.php +++ b/Model/TokenRetriever.php @@ -116,21 +116,8 @@ public function retrieveByQuote(Quote $quote) if (!$this->token) { $reference = $this->getQuoteReference($quote); - if ($reference->getToken()) { - try { - $api = $this->apiFactory->create(); - // Check if token is expired - if (($token = $api->getApiToken($this->referenceBuilder->getQuoteReference($quote))) - && $token->getToken() !== $reference->getToken() - ) { - // ... and refresh it in QuoteReference - $this->refreshQuoteReferenceApiToken($reference, $token->getToken()); - } - } catch (\Exception $exception) { - throw new LocalizedException(__($exception->getMessage()), $exception); - } + if ($reference->getToken() && !$reference->isTokenExpired()) { $this->token = $reference->getToken(); - return $this->token; } diff --git a/Observer/Admin/Order/Create/ProcessData.php b/Observer/Admin/Order/Create/ProcessData.php new file mode 100755 index 0000000..142c640 --- /dev/null +++ b/Observer/Admin/Order/Create/ProcessData.php @@ -0,0 +1,77 @@ +messageManager = $messageManager; + $this->checkoutInfoToQuote = $checkoutInfoToQuote; + $this->api = $api; + $this->referenceBuilder = $referenceBuilder; + } + + /** + * {@inheritdoc} + */ + public function execute(Observer $observer) + { + /** @var OrderCreateModel $orderCreateModel */ + $orderCreateModel = $observer->getOrderCreateModel(); + $shippingMethod = $orderCreateModel->getShippingAddress()->getShippingMethod(); + if ($shippingMethod === Paazlshipping::CODE . '_' . Paazlshipping::CODE) { + /** @var Quote $quote */ + $quote = $orderCreateModel->getQuote(); + $this->checkoutInfoToQuote->process($quote); + $quote->getShippingAddress()->setCollectShippingRates(true); + } + } +} diff --git a/composer.json b/composer.json index 1de9f6b..4715657 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "paazl/magento2-checkout-widget", "description": "Paazl checkoutWidget for Magento 2", "type": "magento2-module", - "version": "1.1.4", + "version": "1.2.0", "keywords": [ "Paazl", "Magento 2", diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml new file mode 100755 index 0000000..95fb61b --- /dev/null +++ b/etc/adminhtml/di.xml @@ -0,0 +1,16 @@ + + + + + + + Paazl\CheckoutWidget\Model\Admin\Order\Create\PaazlConfigProvider + + + + diff --git a/etc/adminhtml/events.xml b/etc/adminhtml/events.xml new file mode 100755 index 0000000..043c8dd --- /dev/null +++ b/etc/adminhtml/events.xml @@ -0,0 +1,11 @@ + + + + + + + diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 0b73d26..f44576c 100755 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -227,6 +227,11 @@ + + + Magento\Config\Model\Config\Source\Yesno + OneStepCheckout, Custom Checkout etc. + diff --git a/etc/module.xml b/etc/module.xml index 8d2c38a..6cedb6c 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -5,7 +5,7 @@ --> - + diff --git a/view/adminhtml/layout/sales_order_create_index.xml b/view/adminhtml/layout/sales_order_create_index.xml new file mode 100755 index 0000000..f52bed4 --- /dev/null +++ b/view/adminhtml/layout/sales_order_create_index.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + diff --git a/view/adminhtml/layout/sales_order_create_load_block_shipping_method.xml b/view/adminhtml/layout/sales_order_create_load_block_shipping_method.xml new file mode 100755 index 0000000..a0c8555 --- /dev/null +++ b/view/adminhtml/layout/sales_order_create_load_block_shipping_method.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/view/adminhtml/requirejs-config.js b/view/adminhtml/requirejs-config.js index 3726877..93d5309 100755 --- a/view/adminhtml/requirejs-config.js +++ b/view/adminhtml/requirejs-config.js @@ -4,6 +4,11 @@ */ var config = { + map: { + '*': { + paazlWidgetModal: 'Paazl_CheckoutWidget/js/admin/order/create/shipping/method/paazl/widget/modal' + } + }, config: { mixins: { 'mage/validation': { diff --git a/view/adminhtml/templates/order/create/paazl-js.phtml b/view/adminhtml/templates/order/create/paazl-js.phtml new file mode 100755 index 0000000..2dfa511 --- /dev/null +++ b/view/adminhtml/templates/order/create/paazl-js.phtml @@ -0,0 +1,43 @@ + + + diff --git a/view/adminhtml/templates/order/create/shipping/method/form.phtml b/view/adminhtml/templates/order/create/shipping/method/form.phtml new file mode 100755 index 0000000..6f5934a --- /dev/null +++ b/view/adminhtml/templates/order/create/shipping/method/form.phtml @@ -0,0 +1,107 @@ + + +getShippingRates(); ?> + + + getActiveMethodRate()): ?> +
+
+
+ escapeHtml($block->getCarrierName($_rate->getCarrier())) ?> +
+
+ escapeHtml($_rate->getMethodTitle() ? $_rate->getMethodTitle() : $_rate->getMethodDescription()) ?> - + + getShippingPrice($_rate->getPrice(), $this->helper('Magento\Tax\Helper\Data')->displayShippingPriceIncludingTax()); ?> + getShippingPrice($_rate->getPrice(), true); ?> + + + helper('Magento\Tax\Helper\Data')->displayShippingBothPrices() && $_incl != $_excl): ?> + ( ) + + +
+
+ + + +
+ + + +getIsRateRequest()): ?> +
+ +
+ +
+ + + + +
+ + + diff --git a/view/adminhtml/templates/order/create/shipping/method/paazl.phtml b/view/adminhtml/templates/order/create/shipping/method/paazl.phtml new file mode 100755 index 0000000..c30c6ae --- /dev/null +++ b/view/adminhtml/templates/order/create/shipping/method/paazl.phtml @@ -0,0 +1,14 @@ + +
+ getChildHtml('', false) ?> +
diff --git a/view/adminhtml/templates/order/create/shipping/method/paazl/widget/config.phtml b/view/adminhtml/templates/order/create/shipping/method/paazl/widget/config.phtml new file mode 100755 index 0000000..177afaf --- /dev/null +++ b/view/adminhtml/templates/order/create/shipping/method/paazl/widget/config.phtml @@ -0,0 +1,19 @@ + + diff --git a/view/adminhtml/templates/order/create/shipping/method/paazl/widget/modal.phtml b/view/adminhtml/templates/order/create/shipping/method/paazl/widget/modal.phtml new file mode 100755 index 0000000..489c35e --- /dev/null +++ b/view/adminhtml/templates/order/create/shipping/method/paazl/widget/modal.phtml @@ -0,0 +1,16 @@ + + +
+
+
diff --git a/view/adminhtml/web/css/styles.css b/view/adminhtml/web/css/styles.css index 18ae739..32c0acb 100644 --- a/view/adminhtml/web/css/styles.css +++ b/view/adminhtml/web/css/styles.css @@ -168,3 +168,16 @@ aside.paazl_order_data_form_modal .page-actions.floating-header { .paazl-delivery-date .value { margin-left: 1rem; } + +aside.paazl-widget-modal .modal-inner-wrap { + width: calc(720px + 6rem); +} + +section.paazl.method-select { + padding-bottom: 0; +} + +section.paazl.method-select section.paazl__body.body form.options { + max-height: 400px; + overflow-y: auto; +} \ No newline at end of file diff --git a/view/adminhtml/web/js/admin/order/create/shipping/method/paazl/widget/config.js b/view/adminhtml/web/js/admin/order/create/shipping/method/paazl/widget/config.js new file mode 100755 index 0000000..27459e4 --- /dev/null +++ b/view/adminhtml/web/js/admin/order/create/shipping/method/paazl/widget/config.js @@ -0,0 +1,109 @@ +/** + * Copyright © 2019 Paazl. All rights reserved. + * See COPYING.txt for license details. + */ + +define([ + 'ko', + 'uiComponent', + 'jquery', + 'domReady' +], function (ko, Component, $, domReady) { + 'use strict'; + + var shippingConfig, + widgetConfig; + + domReady(function () { + /** + * Preventing propagation of click event in order to avoid send of Shipping form + */ + $(document).on('click', "#widget_paazlshipping_paazlshipping button", function (event) { + event.preventDefault(); + event.stopPropagation(); + return false; + }); + }); + + return Component.extend({ + configJson: ko.observable(), + customerAddressId: null, + state: { + postcode: null, + country: null, + token: null + }, + + initialize: function () { + this._super(); + this.initConfig(); + }, + + initConfig: function () { + shippingConfig = order.paazlshipping || {}; + widgetConfig = shippingConfig.widgetConfig || {}; + + this.setConfigJson(widgetConfig); + }, + + setConfigJson: function (data) { + this.configJson(data); + }, + + isHideOtherMethods: function () { + return shippingConfig.hideOtherMethods; + }, + + getCarrierCode: function () { + return shippingConfig.carrierCode; + }, + + getMethodCode: function () { + return shippingConfig.methodCode; + }, + + /** + * + * @return {string} + */ + getJsName: function () { + var name = 'checkoutjs'; + if (shippingConfig.mode === 'test') { + name += '_test'; + } else { + name += '_live'; + } + + return name; + }, + + loadWidget: function (postcode, country) { + this.initialize(); + + var data = this.configJson(), + self = this; + + if (!data) { + return; + } + var container = $('#' + data.mountElementId); + + if (!container.length) { + return; + } + + container.html(''); + + var infoUpdate = function (paazlCheckout) { + data.consigneePostalCode = postcode || data.consigneePostalCode; + data.consigneeCountryCode = country || data.consigneeCountryCode; + paazlCheckout.init(data); + self.state.postcode = data.consigneePostalCode; + self.state.country = data.consigneeCountryCode; + self.state.token = data.token; + }; + + require([this.getJsName()], infoUpdate); + } + }); +}); diff --git a/view/adminhtml/web/js/admin/order/create/shipping/method/paazl/widget/modal.js b/view/adminhtml/web/js/admin/order/create/shipping/method/paazl/widget/modal.js new file mode 100755 index 0000000..451f6f5 --- /dev/null +++ b/view/adminhtml/web/js/admin/order/create/shipping/method/paazl/widget/modal.js @@ -0,0 +1,77 @@ +/** + * Copyright © 2019 Paazl. All rights reserved. + * See COPYING.txt for license details. + */ + +define([ + 'jquery', + 'Paazl_CheckoutWidget/js/admin/order/create/shipping/method/paazl/widget/config', + 'jquery/ui', + 'Magento_Ui/js/modal/modal' +], function($, widgetConfig) { + "use strict"; + + $.widget('mage.paazlWidgetModal', $.mage.modal, { + options: { + modalForm: '#paazl-modal', + modalButton: '.paazl-modal-open' + }, + _create: function () { + Object.assign(this.options, this._prepareModal()); + this._super(); + }, + _prepareModal: function () { + return { + responsive: true, + clickableOverlay: false, + title: $.mage.__('Choice shipping method'), + modalClass: 'popup paazl-widget-modal', + buttons: [{ + text: $.mage.__('Continue'), + class: '', + click: function () { + this.saveShippingMethod(); + } + }] + }; + }, + openModal: function () { + this._super(); + + let addressContainer = order.shippingAsBilling ? + 'billingAddressContainer' : + 'shippingAddressContainer', + addressIndex = order.shippingAsBilling ? + 'billing_address' : + 'shipping_address', + data = order.serializeData(order[addressContainer]).toObject(), + postcode = data['order[' + addressIndex + '][postcode]'], + countryId = data['order[' + addressIndex + '][country_id]']; + + widgetConfig.prototype.loadWidget(postcode, countryId); + }, + closeModal: function (force = false) { + this._super(); + + if (force) { + this._super(); + return; + } + + if (!order.oldShippingMethod) { + order.resetShippingMethod(); + return; + } + + order.showModalFlag = false; + order.setShippingMethod(order.oldShippingMethod); + }, + saveShippingMethod: function () { + order.showModalFlag = false; + order.setShippingMethod('paazlshipping_paazlshipping'); + this.closeModal(true); + } + }); + + return $.mage.paazlWidgetModal; +}); \ No newline at end of file diff --git a/view/adminhtml/web/template/admin/order/create/shipping/method/paazl/widget/config.html b/view/adminhtml/web/template/admin/order/create/shipping/method/paazl/widget/config.html new file mode 100755 index 0000000..64dcaab --- /dev/null +++ b/view/adminhtml/web/template/admin/order/create/shipping/method/paazl/widget/config.html @@ -0,0 +1,3 @@ + + + diff --git a/view/base/requirejs-config.js b/view/base/requirejs-config.js new file mode 100755 index 0000000..3fe691b --- /dev/null +++ b/view/base/requirejs-config.js @@ -0,0 +1,25 @@ +/** + * Copyright © 2019 Paazl. All rights reserved. + * See COPYING.txt for license details. + */ + +var config = { + map: { + '*': { + paazlCheckout: 'Paazl_CheckoutWidget/js/checkout', + widgetConfig: 'Paazl_CheckoutWidget/js/checkout/view/widget-config', + paazlShippingModal: 'Paazl_CheckoutWidget/js/admin/order/create/paazl-shipping/modal', + customJs: 'Paazl_CheckoutWidget/js/custom', + checkoutjs_test: "https://widget-acc.paazl.com/v1/checkout.js", + checkoutjs_live: "https://widget.paazl.com/v1/checkout.js" + } + }, + paths: { + paazlCheckout: 'Paazl_CheckoutWidget/js/checkout' + }, + shim: { + 'Paazl_CheckoutWidget/js/checkout': { + deps: ['jquery'] + } + }, +}; diff --git a/view/frontend/templates/checkout/widget.phtml b/view/base/templates/checkout/widget.phtml old mode 100644 new mode 100755 similarity index 88% rename from view/frontend/templates/checkout/widget.phtml rename to view/base/templates/checkout/widget.phtml index a992ed6..2d987e7 --- a/view/frontend/templates/checkout/widget.phtml +++ b/view/base/templates/checkout/widget.phtml @@ -6,7 +6,7 @@ // @codingStandardsIgnoreFile -/** @var \Paazl\CheckoutWidget\Block\Checkout\Widget $block */ +/** @var Paazl\CheckoutWidget\Block\Checkout\Widget $block */ ?> getApiBaseUrl()) : ?>