From 7d9fd8a3d00bf85fe4964f5ad270a937380c9f2b Mon Sep 17 00:00:00 2001 From: Jakub Filounek <37303389+JakubFilounek@users.noreply.github.com> Date: Mon, 12 Feb 2024 15:22:48 +0100 Subject: [PATCH 1/3] Fixed basket category Fixed basket category value ua => ue --- app/code/Ecomail/Ecomail/Model/BasketEventMapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Ecomail/Ecomail/Model/BasketEventMapper.php b/app/code/Ecomail/Ecomail/Model/BasketEventMapper.php index 75abb6e..e96ff9b 100644 --- a/app/code/Ecomail/Ecomail/Model/BasketEventMapper.php +++ b/app/code/Ecomail/Ecomail/Model/BasketEventMapper.php @@ -40,7 +40,7 @@ public function map(Quote $quote): array $data = []; $data['event'] = [ 'email' => $quote->getCustomerEmail(), - 'category' => 'ua', + 'category' => 'ue', 'action' => 'Basket', 'label' => 'Basket', ]; From ae1ef0186df82f58bd7a063b8102f4068d19fb69 Mon Sep 17 00:00:00 2001 From: Jakub Filounek Date: Mon, 20 May 2024 12:07:54 +0200 Subject: [PATCH 2/3] Update to 2.1.1 - Compatibility Magento 2.4.6 - Fixed basket category --- .../System/Config/Form/Field/Webhook.php | 66 +++++++ app/code/Ecomail/Ecomail/CHANGELOG.md | 4 + .../Ecomail/Controller/Webhook/Action.php | 172 ++++++++++++++++++ app/code/Ecomail/Ecomail/Helper/Data.php | 51 ++++++ app/code/Ecomail/Ecomail/Model/Api.php | 2 +- .../Ecomail/Model/SubscriptionManager.php | 5 +- .../Observer/NewsletterSubscription.php | 80 ++++++++ .../Framework/App/Request/CsrfValidator.php | 30 +++ .../Ecomail/Ecomail/etc/adminhtml/system.xml | 5 + app/code/Ecomail/Ecomail/etc/frontend/di.xml | 3 + .../Ecomail/Ecomail/etc/frontend/routes.xml | 8 + app/code/Ecomail/Ecomail/etc/module.xml | 2 +- app/code/Ecomail/Ecomail/i18n/cs_CZ.csv | 2 + app/code/Ecomail/Ecomail/i18n/en_US.csv | 2 + .../system/config/form/field/webhook.phtml | 3 + .../Ecomail/view/frontend/requirejs-config.js | 7 + .../Ecomail/view/frontend/web/js/tracking.js | 51 ++++++ 17 files changed, 488 insertions(+), 5 deletions(-) create mode 100644 app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Webhook.php create mode 100644 app/code/Ecomail/Ecomail/Controller/Webhook/Action.php create mode 100644 app/code/Ecomail/Ecomail/Observer/NewsletterSubscription.php create mode 100644 app/code/Ecomail/Ecomail/Plugin/Framework/App/Request/CsrfValidator.php create mode 100644 app/code/Ecomail/Ecomail/etc/frontend/routes.xml create mode 100644 app/code/Ecomail/Ecomail/view/adminhtml/templates/system/config/form/field/webhook.phtml create mode 100644 app/code/Ecomail/Ecomail/view/frontend/requirejs-config.js create mode 100644 app/code/Ecomail/Ecomail/view/frontend/web/js/tracking.js diff --git a/app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Webhook.php b/app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Webhook.php new file mode 100644 index 0000000..d69d2fd --- /dev/null +++ b/app/code/Ecomail/Ecomail/Block/System/Config/Form/Field/Webhook.php @@ -0,0 +1,66 @@ +helper = $helper; + $this->storeManager = $storeManager; + } + + /** + * @return string + */ + public function getWebhookUrl(): string + { + $baseUrl = $this->storeManager->getDefaultStoreView()->getBaseUrl(); + return rtrim($baseUrl, '/') . '/ecomail/webhook/action/' . $this->helper->getWebhookHash() . '/'; + } + + /** + * Return element html + * + * @param AbstractElement $element + * @return string + * + */ + protected function _getElementHtml(AbstractElement $element): string + { + return $this->_toHtml(); + } +} diff --git a/app/code/Ecomail/Ecomail/CHANGELOG.md b/app/code/Ecomail/Ecomail/CHANGELOG.md index 1c87904..f35dd6d 100644 --- a/app/code/Ecomail/Ecomail/CHANGELOG.md +++ b/app/code/Ecomail/Ecomail/CHANGELOG.md @@ -5,3 +5,7 @@ ## v2.1.0 - 2 Jun, 2023 * added Magento 2.4.6 compatibility + +## v2.2.0 - 2 Jun, 2023 +* added webhook for subscriber status synchronization +* fixed issue with duplicated subscribers \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/Controller/Webhook/Action.php b/app/code/Ecomail/Ecomail/Controller/Webhook/Action.php new file mode 100644 index 0000000..2e19f9f --- /dev/null +++ b/app/code/Ecomail/Ecomail/Controller/Webhook/Action.php @@ -0,0 +1,172 @@ +helper = $helper; + $this->json = $json; + $this->file = $file; + $this->logger = $logger; + $this->httpRequest = $httpRequest; + $this->resultFactory = $resultFactory; + $this->storeManager = $storeManager; + $this->subscriberFactory = $subscriberFactory; + $this->subscriberResource = $subscriberResource; + } + + /** + * @inheritDoc + */ + public function execute() + { + $resultRaw = $this->resultFactory->create(ResultFactory::TYPE_RAW); + + try { + $request = $this->readRequest(); + if ($request['status'] == self::REQUEST_UNSUBSCRIBE) { + $this->unsubscribe($request['email']); + } elseif ($request['status'] == self::REQUEST_SUBSCRIBE) { + $this->subscribe($request['email']); + } + $resultRaw->setContents('SUCCESS'); + } catch (\Exception $e) { + $this->logger->error('Ecomail webhook error: ' . $e->getMessage()); + $resultRaw->setContents('ERROR')->setHttpResponseCode(400); + } + + return $resultRaw; + } + + /** + * @param string $email + * @return void + * @throws AlreadyExistsException + */ + private function unsubscribe(string $email) + { + $subscriber = $this->subscriberFactory->create() + ->loadBySubscriberEmail($email, $this->storeManager->getDefaultStoreView()->getWebsiteId()); + + if (!$subscriber->getId() + || $subscriber->getSubscriberStatus() == Subscriber::STATUS_UNSUBSCRIBED + ) { + return; + } + + $subscriber->setSubscriberStatus(Subscriber::STATUS_UNSUBSCRIBED); + $this->subscriberResource->save($subscriber); + } + + /** + * @param string $email + * @return void + * @throws AlreadyExistsException + */ + private function subscribe(string $email) + { + $subscriber = $this->subscriberFactory->create() + ->loadBySubscriberEmail($email, $this->storeManager->getDefaultStoreView()->getWebsiteId()); + + if (!$subscriber->getId() + || $subscriber->getSubscriberStatus() == Subscriber::STATUS_SUBSCRIBED + ) { + return; + } + + $subscriber->setSubscriberStatus(Subscriber::STATUS_SUBSCRIBED); + $this->subscriberResource->save($subscriber); + } + + /** + * @return array + * @throws LocalizedException + */ + private function readRequest() + { + $params = $this->httpRequest->getParams(); + $keys = array_keys($params); + if (array_shift($keys) != $this->helper->getWebhookHash()) { + throw new NotFoundException(__('Invalid Webhook key.')); + } + + if ($content = $this->file->fileGetContents('php://input')) { + try { + $request = $this->json->unserialize($content); + } catch (\InvalidArgumentException $e) { + throw new LocalizedException(__('Invalid request data, JSON unserialize error.')); + } + + if (isset($request['payload'])) { + return $request['payload']; + } + } + throw new LocalizedException(__('Invalid request data.')); + } +} diff --git a/app/code/Ecomail/Ecomail/Helper/Data.php b/app/code/Ecomail/Ecomail/Helper/Data.php index bf40997..93c7cfa 100644 --- a/app/code/Ecomail/Ecomail/Helper/Data.php +++ b/app/code/Ecomail/Ecomail/Helper/Data.php @@ -4,7 +4,10 @@ use Ecomail\Ecomail\Model\Config\Source\Address; use Magento\Framework\App\Helper\AbstractHelper; +use Magento\Framework\App\Helper\Context; use Magento\Store\Model\ScopeInterface; +use Magento\Framework\Flag\FlagResource; +use Magento\Framework\FlagFactory; class Data extends AbstractHelper { @@ -24,6 +27,21 @@ class Data extends AbstractHelper const XML_PATH_ECOMAIL_TRACKING_ENABLED = 'ecomail/tracking/enabled'; const XML_PATH_ECOMAIL_TRACKING_APP_ID = 'ecomail/tracking/app_id'; + /** + * @param Context $context + * @param FlagFactory $flagFactory + * @param FlagResource $flagResource + */ + public function __construct( + Context $context, + FlagFactory $flagFactory, + FlagResource $flagResource + ) { + parent::__construct($context); + $this->flagFactory = $flagFactory; + $this->flagResource = $flagResource; + } + /** * @param null $store * @return bool @@ -203,4 +221,37 @@ public function getAppId($store = null) $store ); } + + /** + * @return string + */ + public function getWebhookHash() + { + $flag = $this->getFlagObject('ecomail_webhook'); + if ($data = $flag->getFlagData()) { + return $data; + } + + $value = uniqid(); + + $flag->setFlagData($value); + $this->flagResource->save($flag); + return $value; + } + + /** + * @param string $code + * @return \Magento\Framework\Flag + */ + private function getFlagObject($code) + { + $flag = $this->flagFactory->create(['data' => ['flag_code' => $code]]); + $this->flagResource->load( + $flag, + $code, + 'flag_code' + ); + + return $flag; + } } diff --git a/app/code/Ecomail/Ecomail/Model/Api.php b/app/code/Ecomail/Ecomail/Model/Api.php index 80b093a..789fb75 100644 --- a/app/code/Ecomail/Ecomail/Model/Api.php +++ b/app/code/Ecomail/Ecomail/Model/Api.php @@ -182,7 +182,7 @@ private function buildListUrl(string $path): string private function getErrorMessage(array $response): Phrase { if (isset($response['message'])) { - return $response['message']; + return __($response['message']); } if (isset($response['errors'])) { diff --git a/app/code/Ecomail/Ecomail/Model/SubscriptionManager.php b/app/code/Ecomail/Ecomail/Model/SubscriptionManager.php index 7f01cf3..bad5ed3 100644 --- a/app/code/Ecomail/Ecomail/Model/SubscriptionManager.php +++ b/app/code/Ecomail/Ecomail/Model/SubscriptionManager.php @@ -80,9 +80,8 @@ public function subscribeFromOrder(OrderInterface $order) try { if (!$this->subscriberExists($order->getCustomerEmail())) { $this->createLocalSubscriber($order); + $this->api->addSubscriberToList($this->subscriberDataMapper->mapFromOrder($order)); } - - $this->api->addSubscriberToList($this->subscriberDataMapper->mapFromOrder($order)); } catch (Exception $e) { $this->logger->error('Failed to add Ecomail subscription.', [$e]); } @@ -109,7 +108,7 @@ public function subscriberExists(string $customerEmail): bool $subscriber = $this->subscriberFactory->create(); $subscriber->loadByEmail($customerEmail); - return $subscriber->isSubscribed(); + return $subscriber->getId() != null; } /** diff --git a/app/code/Ecomail/Ecomail/Observer/NewsletterSubscription.php b/app/code/Ecomail/Ecomail/Observer/NewsletterSubscription.php new file mode 100644 index 0000000..f3dc683 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Observer/NewsletterSubscription.php @@ -0,0 +1,80 @@ +subscriptionManager = $subscriptionManager; + $this->helper = $helper; + $this->quoteManager = $quoteManager; + $this->eventManager = $eventManager; + } + + /** + * @param Observer $observer + */ + public function execute(Observer $observer) + { + /** @var Subscriber $subscriber */ + $subscriber = $observer->getSubscriber(); + + if ($this->helper->isAvailable() && $subscriber->isStatusChanged()) { + $newSubscriberStatus = $subscriber->getStatus(); + + switch ($newSubscriberStatus) { + case Subscriber::STATUS_SUBSCRIBED: + $this->subscriptionManager->subscribe($subscriber); + $this->quoteManager->setQuoteEmail($subscriber->getEmail()); + $this->eventManager->updateCartContent(); + return; + case Subscriber::STATUS_UNSUBSCRIBED: + $this->subscriptionManager->unsubscribe($subscriber); + return; + default: + return; + } + } + } +} diff --git a/app/code/Ecomail/Ecomail/Plugin/Framework/App/Request/CsrfValidator.php b/app/code/Ecomail/Ecomail/Plugin/Framework/App/Request/CsrfValidator.php new file mode 100644 index 0000000..0153960 --- /dev/null +++ b/app/code/Ecomail/Ecomail/Plugin/Framework/App/Request/CsrfValidator.php @@ -0,0 +1,30 @@ + Magento\Config\Model\Config\Source\Yesno + + + Ecomail\Ecomail\Block\System\Config\Form\Field\Webhook + diff --git a/app/code/Ecomail/Ecomail/etc/frontend/di.xml b/app/code/Ecomail/Ecomail/etc/frontend/di.xml index 05e7385..eabf97b 100644 --- a/app/code/Ecomail/Ecomail/etc/frontend/di.xml +++ b/app/code/Ecomail/Ecomail/etc/frontend/di.xml @@ -37,4 +37,7 @@ + + + diff --git a/app/code/Ecomail/Ecomail/etc/frontend/routes.xml b/app/code/Ecomail/Ecomail/etc/frontend/routes.xml new file mode 100644 index 0000000..e027bb0 --- /dev/null +++ b/app/code/Ecomail/Ecomail/etc/frontend/routes.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/etc/module.xml b/app/code/Ecomail/Ecomail/etc/module.xml index cf00702..041922d 100644 --- a/app/code/Ecomail/Ecomail/etc/module.xml +++ b/app/code/Ecomail/Ecomail/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/app/code/Ecomail/Ecomail/i18n/cs_CZ.csv b/app/code/Ecomail/Ecomail/i18n/cs_CZ.csv index 11db66d..21b5915 100644 --- a/app/code/Ecomail/Ecomail/i18n/cs_CZ.csv +++ b/app/code/Ecomail/Ecomail/i18n/cs_CZ.csv @@ -26,3 +26,5 @@ Enabled,Zapnout "Address type","Typ adresy" "Trigger Autoresponders","Spouštět automatizované odpovědi" "Runs automated campaigns after adding a new subscription.","Spustí automatizované kampaně po přidání nového kontaktu." +"Invalid request data, JSON unserialize error.","Nesprávná data požadavku, chyba parsování JSON." +"Invalid request data.","Nesprávná data požadavku." \ No newline at end of file diff --git a/app/code/Ecomail/Ecomail/i18n/en_US.csv b/app/code/Ecomail/Ecomail/i18n/en_US.csv index cb626f0..68acb87 100644 --- a/app/code/Ecomail/Ecomail/i18n/en_US.csv +++ b/app/code/Ecomail/Ecomail/i18n/en_US.csv @@ -26,3 +26,5 @@ Enabled,Enabled "Address type","Address type" "Trigger Autoresponders","Trigger Autoresponders" "Runs automated campaigns after adding a new subscription.","Runs automated campaigns after adding a new subscription." +"Invalid request data, JSON unserialize error.","Invalid request data, JSON unserialize error." +"Invalid request data.","Invalid request data." diff --git a/app/code/Ecomail/Ecomail/view/adminhtml/templates/system/config/form/field/webhook.phtml b/app/code/Ecomail/Ecomail/view/adminhtml/templates/system/config/form/field/webhook.phtml new file mode 100644 index 0000000..f0c7e72 --- /dev/null +++ b/app/code/Ecomail/Ecomail/view/adminhtml/templates/system/config/form/field/webhook.phtml @@ -0,0 +1,3 @@ + + +escapeHtml($block->getWebhookUrl()); ?> diff --git a/app/code/Ecomail/Ecomail/view/frontend/requirejs-config.js b/app/code/Ecomail/Ecomail/view/frontend/requirejs-config.js new file mode 100644 index 0000000..a0f1459 --- /dev/null +++ b/app/code/Ecomail/Ecomail/view/frontend/requirejs-config.js @@ -0,0 +1,7 @@ +var config = { + map: { + '*': { + 'ecomail': 'Ecomail_Ecomail/js/tracking' + } + } +}; diff --git a/app/code/Ecomail/Ecomail/view/frontend/web/js/tracking.js b/app/code/Ecomail/Ecomail/view/frontend/web/js/tracking.js new file mode 100644 index 0000000..6f0a94b --- /dev/null +++ b/app/code/Ecomail/Ecomail/view/frontend/web/js/tracking.js @@ -0,0 +1,51 @@ +define([ + 'jquery', + 'Magento_Customer/js/customer-data' +], function ($, customerData) { + "use strict"; + + $.widget('ecomail.tracking', { + options: { + appId: null, + src: '//d70shl7vidtft.cloudfront.net/ecmtr-2.4.2.js' + }, + _create: function () { + if (!this.options.appId) { + return; + } + + this.addScript(); + window.ecotrack('newTracker', 'cf', 'd2dpiwfhf3tz0r.cloudfront.net', { + appId: this.options.appId + }); + window.ecotrack('setUserIdFromLocation', 'ecmid'); + window.ecotrack('trackPageView'); + + customerData.get('ecomail').subscribe(this.track); + }, + track: function (data) { + if(data && data.email) { + window.ecotrack('setUserId', data.email); + window.ecotrack('trackPageView'); + + customerData.reload(['ecomail'], true); + } + }, + addScript: function () { + if (!window.ecotrack) { + window.GlobalSnowplowNamespace = window.GlobalSnowplowNamespace || []; + window.GlobalSnowplowNamespace.push('ecotrack'); + window.ecotrack = function () { + (window.ecotrack.q = window.ecotrack.q || []).push(arguments) + }; + window.ecotrack.q = window.ecotrack.q || []; + var script = document.createElement('script'); + var header = document.getElementsByTagName('head')[0]; + script.async = true; + script.src = this.options.src; + header.appendChild(script); + } + } + }); + return $.ecomail.tracking; +}); \ No newline at end of file From 16aeb4a5f9bda2b146c92455ad0d58fbde9f8556 Mon Sep 17 00:00:00 2001 From: Jakub Filounek Date: Mon, 20 May 2024 12:11:19 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 92d664a..949bf0f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Aktuální verzi pluginu lze stáhnout zde: -https://github.com/Ecomailcz/magento-2/releases/download/2.1.0/Ecomail_Ecomail_2.1.0.zip +https://github.com/JakubFilounek/magento-2/releases # Instalace modulu