-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from trustpilot/Order_From_Backend
Order_from_backend
- Loading branch information
Showing
11 changed files
with
332 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Trustpilot\Reviews\Helper; | ||
|
||
use Magento\Framework\App\Helper\AbstractHelper; | ||
use \Psr\Log\LoggerInterface; | ||
|
||
class HttpClient extends AbstractHelper | ||
{ | ||
const HTTP_REQUEST_TIMEOUT = 3; | ||
protected $_logger; | ||
|
||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->_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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Trustpilot\Reviews\Helper; | ||
|
||
use Magento\Catalog\Model\Product; | ||
use Magento\Framework\App\Helper\AbstractHelper; | ||
use \Magento\Store\Model\StoreManagerInterface; | ||
|
||
class OrderData extends AbstractHelper | ||
{ | ||
protected $_product; | ||
|
||
public function __construct( | ||
Product $product, | ||
StoreManagerInterface $storeManager | ||
) { | ||
$this->_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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Trustpilot\Reviews\Helper; | ||
|
||
use Magento\Framework\App\Helper\AbstractHelper; | ||
use Trustpilot\Reviews\Helper\HttpClient; | ||
use Trustpilot\Reviews\Helper\Data; | ||
use \Psr\Log\LoggerInterface; | ||
use \Magento\Store\Model\StoreManagerInterface; | ||
|
||
class TrustpilotHttpClient extends AbstractHelper | ||
{ | ||
protected $_logger; | ||
protected $_httpClient; | ||
protected $_dataHelper; | ||
protected $_apiUrl; | ||
protected $_storeManager; | ||
|
||
public function __construct( | ||
LoggerInterface $logger, | ||
HttpClient $httpClient, | ||
StoreManagerInterface $storeManager, | ||
Data $dataHelper) | ||
{ | ||
$this->_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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
namespace Trustpilot\Reviews\Observer; | ||
|
||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\Event\Observer as EventObserver; | ||
use \Psr\Log\LoggerInterface; | ||
use Trustpilot\Reviews\Helper\OrderData; | ||
use Trustpilot\Reviews\Helper\Data; | ||
use Magento\Framework\App\ProductMetadataInterface; | ||
use Trustpilot\Reviews\Helper\TrustpilotHttpClient; | ||
|
||
define('__ACCEPTED__', 202); | ||
|
||
class OrderSaveObserver implements ObserverInterface | ||
{ | ||
|
||
protected $_trustpilotHttpClient; | ||
protected $_logger; | ||
protected $_productMetadata; | ||
protected $_orderDataHelper; | ||
protected $_dataHelper; | ||
protected $_storeManager; | ||
|
||
public function __construct( | ||
LoggerInterface $logger, | ||
TrustpilotHttpClient $trustpilotHttpClient, | ||
OrderData $orderDataHelper, | ||
ProductMetadataInterface $productMetadata, | ||
Data $dataHelper) | ||
{ | ||
$this->_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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.