-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/thehcginstitute-com/m1/issues/590
- Loading branch information
1 parent
40fe6df
commit 7e8bc11
Showing
1 changed file
with
94 additions
and
104 deletions.
There are no files selected for viewing
198 changes: 94 additions & 104 deletions
198
app/code/community/Ebizmarts/MailChimp/controllers/WebhookController.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 |
---|---|---|
@@ -1,125 +1,115 @@ | ||
<?php | ||
|
||
/** | ||
* MailChimp For Magento | ||
* | ||
* @category Ebizmarts_MailChimp | ||
* @author Ebizmarts Team <[email protected]> | ||
* @copyright Ebizmarts (http://ebizmarts.com) | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
* @date: 5/19/16 3:55 PM | ||
* @file: WebhookController.php | ||
*/ | ||
use Ebizmarts_MailChimp_Model_ProcessWebhook as Process; | ||
class Ebizmarts_MailChimp_WebhookController extends Mage_Core_Controller_Front_Action | ||
{ | ||
protected $_mailchimpHelper = null; | ||
protected $_mailchimpWebhookHelper = null; | ||
protected $_mailchimpHelper = null; | ||
protected $_mailchimpWebhookHelper = null; | ||
|
||
/** | ||
* @return Ebizmarts_MailChimp_Helper_Data|Mage_Core_Helper_Abstract | ||
*/ | ||
protected function getHelper($type='mailchimp') | ||
{ | ||
if (!$this->_mailchimpHelper) { | ||
$this->_mailchimpHelper = Mage::helper($type); | ||
} | ||
/** | ||
* @return Ebizmarts_MailChimp_Helper_Data|Mage_Core_Helper_Abstract | ||
*/ | ||
protected function getHelper($type='mailchimp') | ||
{ | ||
if (!$this->_mailchimpHelper) { | ||
$this->_mailchimpHelper = Mage::helper($type); | ||
} | ||
|
||
return $this->_mailchimpHelper; | ||
} | ||
return $this->_mailchimpHelper; | ||
} | ||
|
||
/** | ||
* @return Ebizmarts_MailChimp_Helper_Webhook | ||
*/ | ||
protected function getWebhookHelper() | ||
{ | ||
if (!$this->_mailchimpWebhookHelper) { | ||
$this->_mailchimpWebhookHelper = Mage::helper('mailchimp/webhook'); | ||
} | ||
/** | ||
* @return Ebizmarts_MailChimp_Helper_Webhook | ||
*/ | ||
protected function getWebhookHelper() | ||
{ | ||
if (!$this->_mailchimpWebhookHelper) { | ||
$this->_mailchimpWebhookHelper = Mage::helper('mailchimp/webhook'); | ||
} | ||
|
||
return $this->_mailchimpWebhookHelper; | ||
} | ||
return $this->_mailchimpWebhookHelper; | ||
} | ||
|
||
/** | ||
* Entry point for all webhook operations | ||
*/ | ||
function indexAction() | ||
{ | ||
$request = $this->getRequest(); | ||
$requestKey = $request->getParam('wkey'); | ||
$moduleName = $request->getModuleName(); | ||
$data = $request->getPost(); | ||
$helper = $this->getHelper(); | ||
$webhookHelper = $this->getWebhookHelper(); | ||
/** | ||
* Entry point for all webhook operations | ||
*/ | ||
function indexAction() | ||
{ | ||
$request = $this->getRequest(); | ||
$requestKey = $request->getParam('wkey'); | ||
$moduleName = $request->getModuleName(); | ||
$data = $request->getPost(); | ||
$helper = $this->getHelper(); | ||
$webhookHelper = $this->getWebhookHelper(); | ||
|
||
if ($moduleName == 'monkey') { | ||
if (isset($data['data']['list_id'])) { | ||
$listId = $data['data']['list_id']; | ||
$storeIds = $helper->getMagentoStoreIdsByListId($listId); | ||
if ($moduleName == 'monkey') { | ||
if (isset($data['data']['list_id'])) { | ||
$listId = $data['data']['list_id']; | ||
$storeIds = $helper->getMagentoStoreIdsByListId($listId); | ||
|
||
if (!empty($storeIds)) { | ||
$storeId = $storeIds[0]; | ||
if (!empty($storeIds)) { | ||
$storeId = $storeIds[0]; | ||
|
||
if ($helper->isSubscriptionEnabled($storeId)) { | ||
$this->_deleteWebhook($storeId, $listId); | ||
} | ||
} | ||
} | ||
} else { | ||
//Checking if "wkey" para is present on request, we cannot check for !isPost() | ||
//because Mailchimp pings the URL (GET request) to validate webhook | ||
if (!$requestKey) { | ||
$this->getResponse() | ||
->setHeader('HTTP/1.1', '403 Forbidden') | ||
->sendResponse(); | ||
return $this; | ||
} | ||
if ($helper->isSubscriptionEnabled($storeId)) { | ||
$this->_deleteWebhook($storeId, $listId); | ||
} | ||
} | ||
} | ||
} else { | ||
//Checking if "wkey" para is present on request, we cannot check for !isPost() | ||
//because Mailchimp pings the URL (GET request) to validate webhook | ||
if (!$requestKey) { | ||
$this->getResponse() | ||
->setHeader('HTTP/1.1', '403 Forbidden') | ||
->sendResponse(); | ||
return $this; | ||
} | ||
|
||
$myKey = $webhookHelper->getWebhooksKey(); | ||
$myKey = $webhookHelper->getWebhooksKey(); | ||
|
||
//Validate "wkey" GET parameter | ||
if ($myKey == $requestKey) { | ||
if ($request->getPost('type')) { | ||
Mage::getModel('mailchimp/processWebhook')->saveWebhookRequest($data); | ||
} | ||
//Validate "wkey" GET parameter | ||
if ($myKey == $requestKey) { | ||
if ($request->getPost('type')) { | ||
Mage::getModel('mailchimp/processWebhook')->saveWebhookRequest($data); | ||
} | ||
# 2024-03-16 Dmitrii Fediuk https://upwork.com/fl/mage2pro | ||
# "«Webhook successfully created» should not be logged as an error my MailChimp": | ||
# https://github.com/thehcginstitute-com/m1/issues/480 | ||
} | ||
} | ||
else { | ||
$helper->logError($this->__('Webhook Key invalid! Key Request: %s - My Key: %s', $requestKey, $myKey)); | ||
$helper->logError($this->__('Webhook call ended')); | ||
} | ||
} | ||
} | ||
$helper->logError($this->__('Webhook Key invalid! Key Request: %s - My Key: %s', $requestKey, $myKey)); | ||
$helper->logError($this->__('Webhook call ended')); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param $storeId | ||
* @param $listId | ||
* @throws Exception | ||
*/ | ||
protected function _deleteWebhook($storeId, $listId) | ||
{ | ||
$helper = $this->getHelper(); | ||
$webhookHelper = $this->getWebhookHelper(); | ||
/** | ||
* @param $storeId | ||
* @param $listId | ||
* @throws Exception | ||
*/ | ||
protected function _deleteWebhook($storeId, $listId) | ||
{ | ||
$helper = $this->getHelper(); | ||
$webhookHelper = $this->getWebhookHelper(); | ||
|
||
try { | ||
$api = $helper->getApi($storeId); | ||
} catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { | ||
$helper->logError($e->getMessage()); | ||
$api = null; | ||
} | ||
try { | ||
$api = $helper->getApi($storeId); | ||
} catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { | ||
$helper->logError($e->getMessage()); | ||
$api = null; | ||
} | ||
|
||
if (!$api) { | ||
try { | ||
$webhooks = $api->getLists()->getWebhooks()->getAll($listId); | ||
foreach ($webhooks['webhooks'] as $webhook) { | ||
if (strpos($webhook['url'], 'monkey/webhook') !== false) { | ||
$webhookHelper->deleteWebhookFromList($api->getLists()->getWebhooks(), $listId, $webhook['id']); | ||
} | ||
} | ||
} catch (MailChimp_Error $e) { | ||
$helper->logError($e->getFriendlyMessage()); | ||
} | ||
} | ||
} | ||
if (!$api) { | ||
try { | ||
$webhooks = $api->getLists()->getWebhooks()->getAll($listId); | ||
foreach ($webhooks['webhooks'] as $webhook) { | ||
if (strpos($webhook['url'], 'monkey/webhook') !== false) { | ||
$webhookHelper->deleteWebhookFromList($api->getLists()->getWebhooks(), $listId, $webhook['id']); | ||
} | ||
} | ||
} catch (MailChimp_Error $e) { | ||
$helper->logError($e->getFriendlyMessage()); | ||
} | ||
} | ||
} | ||
} |