Skip to content

Commit

Permalink
Webhooks added, version changed
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarchenko committed Jul 17, 2023
1 parent 08ca763 commit 354dafc
Show file tree
Hide file tree
Showing 12 changed files with 560 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
106 changes: 106 additions & 0 deletions app/code/Morfdev/Freshdesk/Controller/Webhook/Install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Morfdev\Freshdesk\Controller\Webhook;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Morfdev\Freshdesk\Model\Authorization;
use Psr\Log\LoggerInterface;
use Magento\Framework\Oauth\Helper\Request;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;

class Install extends Action
{
/** @var Authorization */
protected $authorization;

/** @var ConfigInterface */
private $resourceConfig;

/** @var null */
private $postData = null;

/** @var LoggerInterface */
protected $logger;

/**
* Install constructor.
* @param Context $context
* @param Authorization $authorization
* @param LoggerInterface $logger
* @param ConfigInterface $resourceConfig
* @param FormKey $formKey
*/
public function __construct(
Context $context,
Authorization $authorization,
LoggerInterface $logger,
ConfigInterface $resourceConfig,
FormKey $formKey
) {
parent::__construct($context);
$this->_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([]);
}
}
106 changes: 106 additions & 0 deletions app/code/Morfdev/Freshdesk/Controller/Webhook/Uninstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Morfdev\Freshdesk\Controller\Webhook;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Morfdev\Freshdesk\Model\Authorization;
use Psr\Log\LoggerInterface;
use Magento\Framework\Oauth\Helper\Request;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;

class Uninstall extends Action
{
/** @var Authorization */
protected $authorization;

/** @var ConfigInterface */
private $resourceConfig;

/** @var null */
private $postData = null;

/** @var LoggerInterface */
protected $logger;

/**
* Uninstall constructor.
* @param Context $context
* @param Authorization $authorization
* @param LoggerInterface $logger
* @param ConfigInterface $resourceConfig
* @param FormKey $formKey
*/
public function __construct(
Context $context,
Authorization $authorization,
LoggerInterface $logger,
ConfigInterface $resourceConfig,
FormKey $formKey
) {
parent::__construct($context);
$this->_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([]);
}
}
59 changes: 49 additions & 10 deletions app/code/Morfdev/Freshdesk/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
46 changes: 46 additions & 0 deletions app/code/Morfdev/Freshdesk/Model/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Morfdev\Freshdesk\Model;

use Psr\Log\LoggerInterface;
use Morfdev\Freshdesk\Model\Config as SystemConfig;

class Webhook
{

/** @var LoggerInterface */
protected $logger;

/** @var SystemConfig */
protected $systemConfig;

/**
* Authorization constructor.
* @param LoggerInterface $logger
* @param \Morfdev\Freshdesk\Model\Config $systemConfig
*/
public function __construct(
LoggerInterface $logger,
SystemConfig $systemConfig
) {
$this->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);
}
}
}
Loading

0 comments on commit 354dafc

Please sign in to comment.