From 7d8de91ea517e410a7389674520fb3320c8e49cd Mon Sep 17 00:00:00 2001 From: Vilmantas Grockis Date: Wed, 6 Jun 2018 15:54:11 +0300 Subject: [PATCH 1/3] push Order from backend --- Block/Success.php | 2 +- Helper/Data.php | 5 +-- Helper/HttpClient.php | 72 ++++++++++++++++++++++++++++++ Helper/OrderData.php | 79 +++++++++++++++++++++++++++++++++ Helper/TrustpilotHttpClient.php | 46 +++++++++++++++++++ Observer/ConfigObserver.php | 61 ++++++++++++++++++------- Observer/OrderSaveObserver.php | 76 +++++++++++++++++++++++++++++++ composer.json | 2 +- etc/adminhtml/events.xml | 3 ++ etc/events.xml | 6 +++ 10 files changed, 331 insertions(+), 21 deletions(-) mode change 100755 => 100644 Block/Success.php mode change 100644 => 100755 Helper/Data.php create mode 100755 Helper/HttpClient.php create mode 100755 Helper/OrderData.php create mode 100755 Helper/TrustpilotHttpClient.php mode change 100644 => 100755 Observer/ConfigObserver.php create mode 100755 Observer/OrderSaveObserver.php mode change 100755 => 100644 composer.json create mode 100755 etc/events.xml diff --git a/Block/Success.php b/Block/Success.php old mode 100755 new mode 100644 index 4cf26e4..1735bd5 --- a/Block/Success.php +++ b/Block/Success.php @@ -48,7 +48,7 @@ public function __construct( $this->_helper = $helper; $this->_notifications = $notifications; $this->_customer = $customer; - $this->_version = '1.0.82'; + $this->_version = '#{Octopus.Release.Number}'; parent::__construct($context, $data); } diff --git a/Helper/Data.php b/Helper/Data.php old mode 100644 new mode 100755 index 5887174..b7b6f0a --- a/Helper/Data.php +++ b/Helper/Data.php @@ -11,12 +11,11 @@ class Data extends AbstractHelper public function getGeneralConfigValue($value) { - return $this->scopeConfig->getValue(self::XML_PATH_TRUSTPILOT_GENERAL - . $value, \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE); + return $this->scopeConfig->getValue(self::XML_PATH_TRUSTPILOT_GENERAL . $value, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); } public function getTrustBoxConfigValue($value) { - return $this->scopeConfig->getValue(self::XML_PATH_TRUSTPILOT_TRUSTBOX . $value, \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE); + return $this->scopeConfig->getValue(self::XML_PATH_TRUSTPILOT_TRUSTBOX . $value, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); } } diff --git a/Helper/HttpClient.php b/Helper/HttpClient.php new file mode 100755 index 0000000..9616b80 --- /dev/null +++ b/Helper/HttpClient.php @@ -0,0 +1,72 @@ +_logger = $logger; + } + + public function request($url, $httpRequest, $origin = null, $data = null, $params = array(), $timeout = self::HTTP_REQUEST_TIMEOUT) + { + try{ + $ch = curl_init(); + $this->setCurlOptions($ch, $httpRequest, $data, $origin, $timeout); + $url = $this->buildParams($url, $params); + curl_setopt($ch, CURLOPT_URL, $url); + $content = curl_exec($ch); + $responseData = json_decode($content); + $responseInfo = curl_getinfo($ch); + $responseCode = $responseInfo['http_code']; + curl_close($ch); + $response = array(); + $response['code'] = $responseCode; + if ($responseData) { + $response['data'] = $responseData; + } + return $response; + } catch (Exception $e){ + //intentionally empty + } + } + + private function jsonEncoder($data) + { + if (function_exists('json_encode')) + return json_encode($data); + elseif (method_exists('Tools', 'jsonEncode')) + return Tools::jsonEncode($data); + } + + private function setCurlOptions($ch, $httpRequest, $data, $origin, $timeout) + { + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); + if ($httpRequest == 'POST') { + $encoded_data = $this->jsonEncoder($data); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: application/json', 'Content-Length: ' . strlen($encoded_data), 'Origin: ' . $origin)); + curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_data); + return; + } elseif ($httpRequest == 'GET') { + curl_setopt($ch, CURLOPT_POST, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + return; + } + return; + } + + private function buildParams($url, $params = array()){ + if (!empty($params) && is_array($params)) { + $url .= '?'.http_build_query($params); + } + return $url; + } +} diff --git a/Helper/OrderData.php b/Helper/OrderData.php new file mode 100755 index 0000000..f769b79 --- /dev/null +++ b/Helper/OrderData.php @@ -0,0 +1,79 @@ +_storeManager = $storeManager; + $this->_product = $product; + } + + public function getEmail($order) + { + if ($this->is_empty($order)) + return ''; + + if (!($this->is_empty($order->getCustomerEmail()))) + return $order->getCustomerEmail(); + + else if (!($this->is_empty($order->getShippingAddress()->getEmail()))) + return $order->getShippingAddress()->getEmail(); + + else if (!($this->is_empty($order->getBillingAddress()->getEmail()))) + return $order->getBillingAddress()->getEmail(); + + else if (!($this->is_empty($order->getCustomerId()))) + return $this->_customer->load($order->getCustomerId())->getEmail(); + + return ''; + } + + public function getSkus($products) + { + $skus = []; + foreach ($products as $product) { + array_push($skus, $product['sku']); + } + return $skus; + } + + public function is_empty($var) + { + return empty($var); + } + + public function getProducts($order){ + $products = []; + try { + $items = $order->getAllItems(); + foreach ($items as $i) { + $product = $this->_product->load($i->getProductId()); + $brand = $product->getAttributeText('manufacturer'); + array_push( + $products, + [ + 'productUrl' => $product->getProductUrl(), + 'name' => $product->getName(), + 'brand' => $brand ? $brand : '', + 'sku' => $product->getSku(), + 'imageUrl' => $this->_storeManager->getStore($order->getStoreId())->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) + . 'catalog/product' . $product->getImage() + ] + ); + } + } catch (Exception $e) { + // Just skipping products data if we are not able to collect it + } + return $products; + } +} diff --git a/Helper/TrustpilotHttpClient.php b/Helper/TrustpilotHttpClient.php new file mode 100755 index 0000000..21df562 --- /dev/null +++ b/Helper/TrustpilotHttpClient.php @@ -0,0 +1,46 @@ +_logger = $logger; + $this->_httpClient = $httpClient; + $this->_dataHelper = $dataHelper; + $this->_storeManager=$storeManager; + + } + + public function postInvitation($integrationKey, $storeId, $data = array()) + { + $this->_apiUrl = $this->_dataHelper->getGeneralConfigValue('ApiUrl'); + $url = $this->_apiUrl . $integrationKey . '/invitation'; + $httpRequest = "POST"; + $origin = $this->_storeManager->getStore($storeId)->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); + $response = $this->_httpClient->request( + $url, + $httpRequest, + $origin, + $data + ); + return $response; + } +} diff --git a/Observer/ConfigObserver.php b/Observer/ConfigObserver.php old mode 100644 new mode 100755 index 964f74a..56be388 --- a/Observer/ConfigObserver.php +++ b/Observer/ConfigObserver.php @@ -20,7 +20,7 @@ public function __construct(LoggerInterface $logger, Data $helper) public function execute(EventObserver $observer) { $settings = self::getSettings(); - $service_url = $this->_helper->getGeneralConfigValue('ApiUrl') . $settings->general->key . '/settings' ; + $service_url = $this->_helper->getGeneralConfigValue('ApiUrl') . $this->_helper->getGeneralConfigValue('key') . '/settings' ; $curl = curl_init($service_url); $headers = [ 'Content-Type: application/json; charset=utf-8' @@ -43,22 +43,51 @@ public function execute(EventObserver $observer) public function getSettings() { - $general = new \stdClass(); - $general->key = trim($this->_helper->getGeneralConfigValue('key'));; + $globalSettings = new \stdClass(); + $globalSettings->source = 'Magento2'; + $globalSettings->pluginVersion = $this->_helper->getGeneralConfigValue('ReleaseNumber'); + $globalSettings->magentoVersion = 'Magento-'.$this->getVersion(); + $id = 0; + + $stores = $this->getStores(); + foreach ($stores as $store) { + $general = new \stdClass(); + $general->key = trim($this->_helper->getGeneralConfigValue('key')); + $general->storeId = $store->getId(); + $general->storeCode = $store->getCode(); + $general->storeName = $store->getName(); + $general->storeTitle = $store->getTitle(); + $general->storeActive = $store->isActive(); + $general->storeHomeUrl = $store->getCurrentUrl(); + $general->websiteId = $store["website_id"]; + + $trustbox = new \stdClass(); + $trustbox->enabled = trim($this->_helper->getTrustBoxConfigValue('trustbox_enable')); + $trustbox->locale = trim($this->_helper->getTrustBoxConfigValue('trustbox_locale')); + $trustbox->template = trim($this->_helper->getTrustBoxConfigValue('trustbox_template')); + $trustbox->position = trim($this->_helper->getTrustBoxConfigValue('trustbox_position')); + $trustbox->paddingx = trim($this->_helper->getTrustBoxConfigValue('trustbox_paddingx')); + $trustbox->paddingy = trim($this->_helper->getTrustBoxConfigValue('trustbox_paddingy')); + + $settings = new \stdClass(); + $settings->general = $general; + $settings->trustbox = $trustbox; + + $globalSettings->$id = $settings; + $id = $id + 1; + } + return $globalSettings; + } - $trustbox = new \stdClass(); - $trustbox->enabled = trim($this->_helper->getTrustBoxConfigValue('trustbox_enable')); - $trustbox->locale = trim($this->_helper->getTrustBoxConfigValue('trustbox_locale')); - $trustbox->template = trim($this->_helper->getTrustBoxConfigValue('trustbox_template')); - $trustbox->position = trim($this->_helper->getTrustBoxConfigValue('trustbox_position')); - $trustbox->paddingx = trim($this->_helper->getTrustBoxConfigValue('trustbox_paddingx')); - $trustbox->paddingy = trim($this->_helper->getTrustBoxConfigValue('trustbox_paddingy')); + private function getVersion() { + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + $productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface'); + return $productMetadata->getVersion(); + } - $settings = new \stdClass(); - $settings->source = 'Magento2'; - $settings->general = $general; - $settings->trustbox = $trustbox; - - return $settings; + private function getStores() { + $om = \Magento\Framework\App\ObjectManager::getInstance(); + $storeManager = $om->get('Magento\Store\Model\StoreManagerInterface'); + return $storeManager->getStores($withDefault = false); } } diff --git a/Observer/OrderSaveObserver.php b/Observer/OrderSaveObserver.php new file mode 100755 index 0000000..31476b3 --- /dev/null +++ b/Observer/OrderSaveObserver.php @@ -0,0 +1,76 @@ +_dataHelper = $dataHelper; + $this->_trustpilotHttpClient = $trustpilotHttpClient; + $this->_logger = $logger; + $this->_orderDataHelper = $orderDataHelper; + $this->_productMetadata = $productMetadata; + $this->_version = $this->_dataHelper->getGeneralConfigValue('ReleaseNumber'); + } + + public function execute(EventObserver $observer) + { + $order = $observer->getEvent()->getOrder(); + $storeId = $order->getStoreId(); + $orderStatusId = $order->getState(); + try { + $key = trim($this->_dataHelper->getGeneralConfigValue('key')); + $data = [ + 'referenceId' => $order->getRealOrderId(), + 'source' => 'Magento-' . $this->_productMetadata->getVersion(), + 'pluginVersion' => $this->_version, + 'orderStatusId' => $orderStatusId , + 'orderStatusName' => $order->getStatusLabel(), + 'hook' => 'sales_order_save_after' + ]; + if ($orderStatusId == \Magento\Sales\Model\Order::STATE_NEW) { + $data['recipientEmail'] = trim($this->_orderDataHelper->getEmail($order)); + $data['recipientName'] = $order->getCustomerName(); + $response = $this->_trustpilotHttpClient->postInvitation($key, $storeId, $data); + if ($response['code'] == __ACCEPTED__) { + $products = $this->_orderDataHelper->getProducts($order); + $data['products'] = $products; + $data['productSkus'] = $this->_orderDataHelper->getSkus($products); + $this->_trustpilotHttpClient->postInvitation($key, $storeId, $data); + } + } else { + $data['payloadType'] = 'OrderStatusUpdate'; + $this->_trustpilotHttpClient->postInvitation($key, $storeId, $data); + } + return; + } catch (Exception $e) { + $error = ['message' => $e->getMessage()]; + $data = ['error' => $error]; + $this->_trustpilotHttpClient->postInvitation($key, $storeId, $data); + return; + } + } +} \ No newline at end of file diff --git a/composer.json b/composer.json old mode 100755 new mode 100644 index 5f686f2..86705ee --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "trustpilot/module-reviews", "description": "The Trustpilot Review extension makes it simple and easy for merchants to collect reviews from their customers to power their marketing efforts, increase sales conversion, build their online reputation and draw business insights.", "type": "magento2-module", - "version": "1.0.82", + "version": "#{Octopus.Release.Number}", "license": [ "OSL-3.0" ], diff --git a/etc/adminhtml/events.xml b/etc/adminhtml/events.xml index ad6cfff..e519e98 100644 --- a/etc/adminhtml/events.xml +++ b/etc/adminhtml/events.xml @@ -3,4 +3,7 @@ + + + diff --git a/etc/events.xml b/etc/events.xml new file mode 100755 index 0000000..205a0c7 --- /dev/null +++ b/etc/events.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From b7cca8e598c9d147ba0c5564850dafd3a802144f Mon Sep 17 00:00:00 2001 From: Vilmantas Grockis Date: Wed, 6 Jun 2018 15:56:49 +0300 Subject: [PATCH 2/3] Release number added --- Block/Success.php | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Block/Success.php b/Block/Success.php index 1735bd5..6df7693 100644 --- a/Block/Success.php +++ b/Block/Success.php @@ -48,7 +48,7 @@ public function __construct( $this->_helper = $helper; $this->_notifications = $notifications; $this->_customer = $customer; - $this->_version = '#{Octopus.Release.Number}'; + $this->_version = '1.0.128'; parent::__construct($context, $data); } diff --git a/composer.json b/composer.json index 86705ee..0277556 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "trustpilot/module-reviews", "description": "The Trustpilot Review extension makes it simple and easy for merchants to collect reviews from their customers to power their marketing efforts, increase sales conversion, build their online reputation and draw business insights.", "type": "magento2-module", - "version": "#{Octopus.Release.Number}", + "version": "1.0.128", "license": [ "OSL-3.0" ], From 00d243d97b18014524e22ebd5fc5d11a2747e485 Mon Sep 17 00:00:00 2001 From: Vilmantas Grockis Date: Wed, 6 Jun 2018 16:53:29 +0300 Subject: [PATCH 3/3] Updated WidgetUrl --- etc/config.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/config.xml b/etc/config.xml index 40b40b4..d2a96c1 100755 --- a/etc/config.xml +++ b/etc/config.xml @@ -5,7 +5,7 @@ https://invitejs.trustpilot.com/api/ https://invitejs.trustpilot.com/tp.min.js - http://widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js + //widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js