-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08ca763
commit 354dafc
Showing
12 changed files
with
560 additions
and
13 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
106 changes: 106 additions & 0 deletions
106
app/code/Morfdev/Freshdesk/Controller/Webhook/Install.php
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,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
106
app/code/Morfdev/Freshdesk/Controller/Webhook/Uninstall.php
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,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([]); | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
Oops, something went wrong.