Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add webhook controller for contact unsubscribe
- Loading branch information
Andreas Penz
committed
Jul 18, 2019
1 parent
47b153f
commit f845d1a
Showing
5 changed files
with
223 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
*/ | ||
|
||
namespace CommerceLeague\ActiveCampaign\Controller; | ||
|
||
use CommerceLeague\ActiveCampaign\Helper\Config as ConfigHelper; | ||
use Magento\Framework\App\Action\Action; | ||
use Magento\Framework\App\Action\Context; | ||
use Magento\Framework\App\Action\HttpPostActionInterface; | ||
use Magento\Framework\App\CsrfAwareActionInterface; | ||
use Magento\Framework\App\Request\InvalidRequestException; | ||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\Controller\Result\RawFactory as RawResultFactory; | ||
use Magento\Framework\Controller\Result\Raw as RawResult; | ||
use Magento\Framework\Phrase; | ||
|
||
/** | ||
* Class AbstractWebhook | ||
*/ | ||
abstract class AbstractWebhook extends Action implements HttpPostActionInterface, CsrfAwareActionInterface | ||
{ | ||
private const PARAM_TOKEN = 'token'; | ||
|
||
/** | ||
* @var ConfigHelper | ||
*/ | ||
private $configHelper; | ||
|
||
/** | ||
* @var RawResultFactory | ||
*/ | ||
private $rawResultFactory; | ||
|
||
/** | ||
* @param Context $context | ||
* @param ConfigHelper $configHelper | ||
* @param RawResultFactory $rawResultFactory | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
ConfigHelper $configHelper, | ||
RawResultFactory $rawResultFactory | ||
) { | ||
parent::__construct($context); | ||
$this->configHelper = $configHelper; | ||
$this->rawResultFactory = $rawResultFactory; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException | ||
{ | ||
/** @var RawResult $response */ | ||
$response = $this->rawResultFactory->create(); | ||
$response->setHttpResponseCode(401); | ||
$response->setContents(''); | ||
|
||
return new InvalidRequestException($response, [new Phrase('Invalid Token.')]); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function validateForCsrf(RequestInterface $request): ?bool | ||
{ | ||
$token = $request->getParam(self::PARAM_TOKEN); | ||
|
||
if (!$token || $this->configHelper->getWebhookToken() !== $token) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,81 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
*/ | ||
|
||
namespace CommerceLeague\ActiveCampaign\Controller\Webhook\Contact; | ||
|
||
use CommerceLeague\ActiveCampaign\Controller\AbstractWebhook; | ||
use CommerceLeague\ActiveCampaign\Helper\Config as ConfigHelper; | ||
use CommerceLeague\ActiveCampaign\Logger\Logger; | ||
use Magento\Framework\App\Action\Context; | ||
use Magento\Framework\Controller\Result\RawFactory as RawResultFactory; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Newsletter\Model\SubscriberFactory; | ||
use Magento\Newsletter\Model\Subscriber; | ||
|
||
/** | ||
* Class Unsubscribe | ||
*/ | ||
class Unsubscribe extends AbstractWebhook | ||
{ | ||
/** | ||
* @var SubscriberFactory | ||
*/ | ||
private $subscriberFactory; | ||
|
||
/** | ||
* @var Logger | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param Context $context | ||
* @param ConfigHelper $configHelper | ||
* @param RawResultFactory $rawResultFactory | ||
* @param SubscriberFactory $subscriberFactory | ||
* @param Logger $logger | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
ConfigHelper $configHelper, | ||
RawResultFactory $rawResultFactory, | ||
SubscriberFactory $subscriberFactory, | ||
Logger $logger | ||
) { | ||
parent::__construct($context, $configHelper, $rawResultFactory); | ||
$this->subscriberFactory = $subscriberFactory; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute() | ||
{ | ||
$params = $this->getRequest()->getParams(); | ||
|
||
if (!isset($params['contact']) || !(isset($params['contact']['email']))) { | ||
$this->logger->error(__('Invalid webhook params received')); | ||
return; | ||
} | ||
|
||
$email = $params['contact']['email']; | ||
|
||
/** @var Subscriber $subscriber */ | ||
$subscriber = $this->subscriberFactory->create(); | ||
$subscriber->loadByEmail($email); | ||
|
||
if (!$subscriber->getId()) { | ||
$this->logger->error(__('Unable to find subscriber with email "%s"', $email)); | ||
return; | ||
} | ||
|
||
try { | ||
$subscriber->unsubscribe(); | ||
} catch (LocalizedException $e) { | ||
$this->logger->error($e->getMessage()); | ||
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
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