diff --git a/app/code/Morfdev/Freshdesk/Controller/Adminhtml/System/Generate.php b/app/code/Morfdev/Freshdesk/Controller/Adminhtml/System/Generate.php
index 1b373f3..fc76c04 100644
--- a/app/code/Morfdev/Freshdesk/Controller/Adminhtml/System/Generate.php
+++ b/app/code/Morfdev/Freshdesk/Controller/Adminhtml/System/Generate.php
@@ -36,7 +36,7 @@ public function execute()
'section' => $section,
'website' => $website,
'store' => $store,
- 'groups' => ['general' => ['fields' => ['token' => ['value' => md5(time())]]]],
+ 'groups' => ['general' => ['fields' => ['token' => ['value' => hash('sha256',time())]]]],
];
/** @var \Magento\Config\Model\Config $configModel */
$configModel = $this->configFactory->create(['data' => $configData]);
diff --git a/app/code/Morfdev/Freshdesk/Controller/Webhook/Install.php b/app/code/Morfdev/Freshdesk/Controller/Webhook/Install.php
new file mode 100644
index 0000000..b1aa167
--- /dev/null
+++ b/app/code/Morfdev/Freshdesk/Controller/Webhook/Install.php
@@ -0,0 +1,106 @@
+_request->setParam('form_key', $formKey->getFormKey());
+ $this->authorization = $authorization;
+ $this->logger = $logger;
+ $this->resourceConfig = $resourceConfig;
+ }
+
+ /**
+ * @return mixed|null
+ */
+ private function getPostData()
+ {
+ if (null !== $this->postData) {
+ return $this->postData;
+ }
+ $this->postData = file_get_contents('php://input');
+ if (false === $this->postData) {
+ $this->logger->error(__('Invalid POST data'));
+ return $this->postData = null;
+ }
+ $this->postData = json_decode($this->postData, true);
+ if (null === $this->postData) {
+ $this->logger->error(__('Invalid JSON'));
+ }
+ return $this->postData;
+ }
+
+ /**
+ * Check authorization with Freshdesk account
+ * @return bool
+ */
+ private function authorise()
+ {
+ return $this->authorization->isAuth($this->getPostData());
+ }
+
+ /**
+ * @return \Magento\Framework\Controller\Result\Json
+ */
+ public function execute()
+ {
+ /** @var \Magento\Framework\Controller\Result\Json $resultJson */
+ $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
+ $scope = $this->authorise();
+ if (null === $scope) {
+ $resultJson->setHttpResponseCode(Request::HTTP_UNAUTHORIZED);
+ return $resultJson->setData($scope);
+ }
+ try {
+ $postData = $this->getPostData();
+ if (null === $postData || !isset($postData['delivery_url']) || !isset($postData['type'])) {
+ throw new \Exception("Error on install webhook");
+ }
+ $this->resourceConfig->saveConfig('morfdev_freshdesk/general/'. $postData['type'] . '_destination_url', $postData['delivery_url'], 'default', 0);
+ } catch (\Exception $e) {
+ $resultJson->setHttpResponseCode(500);
+ return $resultJson->setData([
+ 'message' => $e->getMessage(),
+ 'trace' => $e->getTraceAsString()
+ ]);
+ }
+ return $resultJson->setData([]);
+ }
+}
\ No newline at end of file
diff --git a/app/code/Morfdev/Freshdesk/Controller/Webhook/Uninstall.php b/app/code/Morfdev/Freshdesk/Controller/Webhook/Uninstall.php
new file mode 100644
index 0000000..4991285
--- /dev/null
+++ b/app/code/Morfdev/Freshdesk/Controller/Webhook/Uninstall.php
@@ -0,0 +1,106 @@
+_request->setParam('form_key', $formKey->getFormKey());
+ $this->authorization = $authorization;
+ $this->logger = $logger;
+ $this->resourceConfig = $resourceConfig;
+ }
+
+ /**
+ * @return mixed|null
+ */
+ private function getPostData()
+ {
+ if (null !== $this->postData) {
+ return $this->postData;
+ }
+ $this->postData = file_get_contents('php://input');
+ if (false === $this->postData) {
+ $this->logger->error(__('Invalid POST data'));
+ return $this->postData = null;
+ }
+ $this->postData = json_decode($this->postData, true);
+ if (null === $this->postData) {
+ $this->logger->error(__('Invalid JSON'));
+ }
+ return $this->postData;
+ }
+
+ /**
+ * Check authorization with Freshdesk account
+ * @return bool
+ */
+ private function authorise()
+ {
+ return $this->authorization->isAuth($this->getPostData());
+ }
+
+ /**
+ * @return \Magento\Framework\Controller\Result\Json
+ */
+ public function execute()
+ {
+ /** @var \Magento\Framework\Controller\Result\Json $resultJson */
+ $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
+ $scope = $this->authorise();
+ if (null === $scope) {
+ $resultJson->setHttpResponseCode(Request::HTTP_UNAUTHORIZED);
+ return $resultJson->setData($scope);
+ }
+ try {
+ $postData = $this->getPostData();
+ if (null === $postData || !isset($postData['type'])) {
+ throw new \Exception("Error on install webhook");
+ }
+ $this->resourceConfig->saveConfig('morfdev_freshdesk/general/'. $postData['type'] .'destination_url', '', 'default', 0);
+ } catch (\Exception $e) {
+ $resultJson->setHttpResponseCode(500);
+ return $resultJson->setData([
+ 'message' => $e->getMessage(),
+ 'trace' => $e->getTraceAsString()
+ ]);
+ }
+ return $resultJson->setData([]);
+ }
+}
\ No newline at end of file
diff --git a/app/code/Morfdev/Freshdesk/Model/Config.php b/app/code/Morfdev/Freshdesk/Model/Config.php
index 0db3349..2c88d66 100644
--- a/app/code/Morfdev/Freshdesk/Model/Config.php
+++ b/app/code/Morfdev/Freshdesk/Model/Config.php
@@ -5,7 +5,9 @@
class Config
{
- const FRESHDESK_CONFIG_API_TOKEN_PATH = 'morfdev_freshdesk/general/token';
+ const FRESHWORKS_CONFIG_API_TOKEN_PATH = 'morfdev_freshdesk/general/token';
+ const FRESHWORKS_CONFIG_FRESHDESK_DESTINATION_URL_PATH = 'morfdev_freshdesk/general/freshdesk_destination_url';
+ const FRESHWORKS_CONFIG_FRESHSALES_DESTINATION_URL_PATH = 'morfdev_freshdesk/general/freshsales_destination_url';
/** @var ScopeConfigInterface */
protected $scopeConfig;
@@ -19,13 +21,50 @@ public function __construct(
$this->scopeConfig = $scopeConfig;
}
- /**
- * @return string
- */
- public function getApiTokenForDefault()
- {
- return $this->scopeConfig->getValue(
- self::FRESHDESK_CONFIG_API_TOKEN_PATH
- );
- }
+ /**
+ * @return string
+ */
+ public function getApiTokenForDefault()
+ {
+ return $this->scopeConfig->getValue(
+ self::FRESHWORKS_CONFIG_API_TOKEN_PATH
+ );
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getFreshdeskDestinationUrl()
+ {
+ return $this->scopeConfig->getValue(
+ self::FRESHWORKS_CONFIG_FRESHDESK_DESTINATION_URL_PATH
+ );
+ }
+
+ /**
+ * @return string|null
+ */
+ public function getFreshsalesDestinationUrl()
+ {
+ return $this->scopeConfig->getValue(
+ self::FRESHWORKS_CONFIG_FRESHSALES_DESTINATION_URL_PATH
+ );
+ }
+
+ /**
+ * @return array
+ */
+ public function getDestinationUrlList()
+ {
+ $list = [];
+ $freshdeskUrl = $this->getFreshdeskDestinationUrl();
+ if ($freshdeskUrl) {
+ $list[] = $freshdeskUrl;
+ }
+ $freshsalesUrl = $this->getFreshsalesDestinationUrl();
+ if ($freshsalesUrl) {
+ $list[] = $freshsalesUrl;
+ }
+ return $list;
+ }
}
diff --git a/app/code/Morfdev/Freshdesk/Model/Webhook.php b/app/code/Morfdev/Freshdesk/Model/Webhook.php
new file mode 100644
index 0000000..86ddf73
--- /dev/null
+++ b/app/code/Morfdev/Freshdesk/Model/Webhook.php
@@ -0,0 +1,46 @@
+logger = $logger;
+ $this->systemConfig = $systemConfig;
+ }
+
+ /**
+ * @param array $data
+ * @return void
+ */
+ public function sendData($data)
+ {
+ $destinationUrlList = $this->systemConfig->getDestinationUrlList();
+ foreach ($destinationUrlList as $destinationUrl) {
+ $ch = curl_init($destinationUrl);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
+ curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
+ curl_exec($ch);
+ curl_close($ch);
+ }
+ }
+}
diff --git a/app/code/Morfdev/Freshdesk/Observer/CustomerCreatedObserver.php b/app/code/Morfdev/Freshdesk/Observer/CustomerCreatedObserver.php
new file mode 100644
index 0000000..b699378
--- /dev/null
+++ b/app/code/Morfdev/Freshdesk/Observer/CustomerCreatedObserver.php
@@ -0,0 +1,91 @@
+addressRepository = $addressRepository;
+ $this->addressRenderer = $addressRenderer;
+ $this->addressMapper = $mapper;
+ $this->webhook = $webhook;
+ $this->countryFactory = $countryFactory;
+ }
+
+ /**
+ * @param \Magento\Framework\Event\Observer $observer
+ * @return void
+ */
+ public function execute(\Magento\Framework\Event\Observer $observer)
+ {
+ $customer = $observer->getEvent()->getCustomer();
+ $addressRenderer = $this->addressRenderer->getFormatByCode('html')->getRenderer();
+ try {
+ $billingAddress = $this->addressRepository->getById($customer->getDefaultBilling());
+ $billingAddressFormatted = $addressRenderer->renderArray($this->addressMapper->toFlatArray($billingAddress));
+ $address = [
+ 'address_1' => $billingAddress->getStreet()[0],
+ 'address_2' => $billingAddress->getStreet()[1],
+ 'city' => $billingAddress->getCity(),
+ 'state' => $billingAddress->getRegion()->getRegion(),
+ 'country' => $this->countryFactory->create()->loadByCode($billingAddress->getCountryId())->getName(),
+ 'postcode' => $billingAddress->getPostcode()
+
+ ];
+ $phone = $billingAddress->getTelephone();
+ $company = $billingAddress->getCompany();
+ } catch (\Exception $e) {
+ $billingAddressFormatted = null;
+ $phone = null;
+ $company = null;
+ $address = [];
+ }
+
+ $data = [
+ 'scope' => "customer.created",
+ 'email' => $customer->getEmail(),
+ 'first_name' => $customer->getFirstname(),
+ 'last_name' => $customer->getLastname(),
+ 'phone' => $phone,
+ 'addressFormatted' => $billingAddressFormatted,
+ 'address' => $address,
+ 'company' => $company
+ ];
+ $this->webhook->sendData($data);
+ }
+}
diff --git a/app/code/Morfdev/Freshdesk/Observer/CustomerUpdatedObserver.php b/app/code/Morfdev/Freshdesk/Observer/CustomerUpdatedObserver.php
new file mode 100644
index 0000000..ad99225
--- /dev/null
+++ b/app/code/Morfdev/Freshdesk/Observer/CustomerUpdatedObserver.php
@@ -0,0 +1,99 @@
+customerRepository = $customerRepository;
+ $this->addressRepository = $addressRepository;
+ $this->addressRenderer = $addressRenderer;
+ $this->addressMapper = $mapper;
+ $this->webhook = $webhook;
+ }
+
+ /**
+ * @param \Magento\Framework\Event\Observer $observer
+ * @return void
+ */
+ public function execute(\Magento\Framework\Event\Observer $observer)
+ {
+ $customerAddress = $observer->getCustomerAddress();
+ try {
+ $customer = $this->customerRepository->getById($customerAddress->getCustomerId());
+ } catch (\Exception $e) {
+ $customer = null;
+ }
+ if (!$customer) {
+ return;
+ }
+
+ $addressRenderer = $this->addressRenderer->getFormatByCode('html')->getRenderer();
+ try {
+ $billingAddress = $this->addressRepository->getById($customer->getDefaultBilling());
+ $billingAddressFormatted = $addressRenderer->renderArray($this->addressMapper->toFlatArray($billingAddress));
+ $address = [
+ 'address_1' => $customerAddress->getStreetLine(1),
+ 'address_2' => $customerAddress->getStreetLine(2),
+ 'city' => $customerAddress->getCity(),
+ 'state' => $customerAddress->getRegion(),
+ 'country' => $customerAddress->getCountryModel()->getName(),
+ 'postcode' => $customerAddress->getPostcode()
+
+ ];
+ $phone = $billingAddress->getTelephone();
+ $company = $billingAddress->getCompany();
+ } catch (\Exception $e) {
+ $billingAddressFormatted = null;
+ $phone = null;
+ $company = null;
+ $address = [];
+ }
+
+ $data = [
+ 'scope' => "customer.updated",
+ 'email' => $customer->getEmail(),
+ 'first_name' => $customer->getFirstname(),
+ 'last_name' => $customer->getLastname(),
+ 'phone' => $phone,
+ 'addressFormatted' => $billingAddressFormatted,
+ 'address' => $address,
+ 'company' => $company
+ ];
+ $this->webhook->sendData($data);
+ }
+}
diff --git a/app/code/Morfdev/Freshdesk/Observer/OrderCreatedObserver.php b/app/code/Morfdev/Freshdesk/Observer/OrderCreatedObserver.php
new file mode 100644
index 0000000..cde24bf
--- /dev/null
+++ b/app/code/Morfdev/Freshdesk/Observer/OrderCreatedObserver.php
@@ -0,0 +1,37 @@
+webhook = $webhook;
+ }
+
+ /**
+ * @param \Magento\Framework\Event\Observer $observer
+ * @return void
+ */
+ public function execute(\Magento\Framework\Event\Observer $observer)
+ {
+ $order = $observer->getEvent()->getOrder();
+ $data = [
+ 'scope' => "order.created",
+ 'email' => $order->getCustomerEmail(),
+ 'number' => $order->getIncrementId(),
+ 'amount' => $order->getBaseGrandTotal()
+ ];
+ $this->webhook->sendData($data);
+ }
+}
diff --git a/app/code/Morfdev/Freshdesk/composer.json b/app/code/Morfdev/Freshdesk/composer.json
index 21a1bce..673d2e3 100644
--- a/app/code/Morfdev/Freshdesk/composer.json
+++ b/app/code/Morfdev/Freshdesk/composer.json
@@ -2,7 +2,7 @@
"name": "morfdev/module-freshdesk",
"description": "N/A",
"type": "magento2-module",
- "version": "1.0.0",
+ "version": "1.0.1",
"license": [
"proprietary"
],
diff --git a/app/code/Morfdev/Freshdesk/etc/adminhtml/system.xml b/app/code/Morfdev/Freshdesk/etc/adminhtml/system.xml
index 9e074b5..8754f2c 100644
--- a/app/code/Morfdev/Freshdesk/etc/adminhtml/system.xml
+++ b/app/code/Morfdev/Freshdesk/etc/adminhtml/system.xml
@@ -21,6 +21,17 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file
showInWebsite="0" showInStore="0">
Morfdev\Freshdesk\Block\Adminhtml\System\Config\Field\GenerateButton
+
+
+
+ Generated on Magento 2 Connector app settings on install. Need for webhook features. Webhooks don't work if empty
+
+
+
+ Generated on Magento 2 Connector app settings on install. Need for webhook features. Webhooks don't work if empty
+
diff --git a/app/code/Morfdev/Freshdesk/etc/events.xml b/app/code/Morfdev/Freshdesk/etc/events.xml
new file mode 100644
index 0000000..8cddfac
--- /dev/null
+++ b/app/code/Morfdev/Freshdesk/etc/events.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/code/Morfdev/Freshdesk/etc/module.xml b/app/code/Morfdev/Freshdesk/etc/module.xml
index b69fdd0..2e4a5e8 100644
--- a/app/code/Morfdev/Freshdesk/etc/module.xml
+++ b/app/code/Morfdev/Freshdesk/etc/module.xml
@@ -1,4 +1,4 @@
-
+
\ No newline at end of file