From 9d392d84c75ffca8291cc5f7ac2720e5d2cc1a4e Mon Sep 17 00:00:00 2001 From: Pooja-Zinrelo Date: Fri, 22 Nov 2024 11:40:23 +0530 Subject: [PATCH 1/2] add autogeneration of ZIF workflow url minor fix --- Helper/Data.php | 69 +++++++++++++++++++++++++++++++++ Observer/ConfigSaveObserver.php | 46 ++++++++++++++++++++++ composer.json | 2 +- etc/adminhtml/system.xml | 4 +- etc/events.xml | 3 ++ 5 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 Observer/ConfigSaveObserver.php diff --git a/Helper/Data.php b/Helper/Data.php index bf5e3bb..8b9b14b 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -11,6 +11,7 @@ use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Customer\Model\SessionFactory; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\App\Config\Storage\WriterInterface; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Framework\App\RequestInterface; @@ -59,6 +60,10 @@ class Data extends AbstractHelper * @var ScopeConfigInterface $scopeConfig */ protected $scopeConfig; + /** + * @var WriterInterface $writeConfig + */ + protected $writeConfig; /** * @var SessionFactory */ @@ -155,6 +160,7 @@ class Data extends AbstractHelper * @param CategoryRepositoryInterface $categoryRepository * @param StoreManagerInterface $storeManager * @param ScopeConfigInterface $scopeConfig + * @param WriterInterface $writeConfig * @param Json $json */ public function __construct( @@ -178,6 +184,7 @@ public function __construct( CategoryRepositoryInterface $categoryRepository, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig, + WriterInterface $writeConfig, Json $json ) { $this->curl = $curl; @@ -195,6 +202,7 @@ public function __construct( $this->logger = $logger; $this->storeManager = $storeManager; $this->scopeConfig = $scopeConfig; + $this->writeConfig = $writeConfig; $this->json = $json; $this->helperImageFactory = $helperImageFactory; $this->assetRepos = $assetRepos; @@ -911,6 +919,67 @@ public function getWebHookUrl() return $this->scopeConfig->getValue(self::XML_PATH_WEB_HOOK_URL); } + /** + * Save Web Hook Url + * + * @return mixed + */ + public function saveWebHookUrl($webhookUrl) + { + return $this->writeConfig->save(self::XML_PATH_WEB_HOOK_URL, $webhookUrl); + } + + /** + * Create Web Hook Url + * + * @return mixed + */ + public function createWebHookUrl() + { + try{ + $url = 'https://api.zinrelo.com/v2/loyalty/integrations'; + $headers = [ + "content-type" => "application/json", + "accept" => "application/json", + 'api-key' => $this->getApiKey(), + "partner-id" => $this->getPartnerId() + ]; + $body = [ + "integration_type" => "magento_to_zinrelo", + "config" => [ + "secret_key" => $this->getApiKey(), + "events" => [ + "cart_abandonment", + "customer_create", + "customer_update", + "order_create", + "order_complete", + "order_paid", + "full_order_refund", + "partial_order_refund", + "order_cancelled", + "order_shipped", + "review_submitted", + "review_approved" + ] + ], + "status" => "active" + ]; + $jsonBody = json_encode($body); + $curlRequest = $this->curl->create(); + $curlRequest->setHeaders($headers); + $curlRequest->post($url, $jsonBody); + $response = $curlRequest->getBody(); + $data = json_decode($response, true); + return $data['data']['config']['zif_config']['workflow_url']; + } + catch (Exception $e) { + $this->addErrorLog($e->getMessage()); + $error = 'Failed to create a Webhook URL. Please check the details and try again.'; + throw new Exception($error); + } + } + /** * Get Reward Events * diff --git a/Observer/ConfigSaveObserver.php b/Observer/ConfigSaveObserver.php new file mode 100644 index 0000000..15c5595 --- /dev/null +++ b/Observer/ConfigSaveObserver.php @@ -0,0 +1,46 @@ +helper = $helper; + $this->logger = $logger; + } + + public function execute(Observer $observer) + { + $webhookUrl = $this->helper->getWebHookUrl(); + $this->logger->info('Webhook URL: ' . $webhookUrl); + if (empty($webhookUrl)) { + $newWebhookUrl = $this->helper->createWebHookUrl(); + $this->logger->info('New Webhook URL: ' . $newWebhookUrl); + if ($newWebhookUrl) { + $this->helper->saveWebHookUrl($newWebhookUrl); + } + } + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 53d8bb7..007c0e1 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "zinrelo/extension", "description": "Zinrelo Reward Point Integration Extension", "type": "magento2-module", - "version": "2.0.6", + "version": "2.1.0", "require": { "php": "~7.2.0||~7.3.0||~7.4.0||~8.0.0||~8.1.0||~8.2.0", "firebase/php-jwt": "*" diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index b246cfd..fb24c42 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -45,15 +45,13 @@ 1 - + 1 - required-entry validate-url - Magento\Config\Model\Config\Source\Yesno diff --git a/etc/events.xml b/etc/events.xml index a554c26..26ee81f 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -59,4 +59,7 @@ + + + From b7af22aead286d4c45699f31ed75c2c152f9f9c1 Mon Sep 17 00:00:00 2001 From: Pooja-Zinrelo Date: Wed, 4 Dec 2024 13:31:31 +0530 Subject: [PATCH 2/2] modify webhook integration logic --- Helper/Data.php | 64 +++++++++++++++++++++++---------- Observer/ConfigSaveObserver.php | 25 +++++++++---- etc/config.xml | 1 + 3 files changed, 64 insertions(+), 26 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index 8b9b14b..f770eea 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -37,6 +37,8 @@ class Data extends AbstractHelper { public const XML_PATH_LOYALTY_REWARDS_ACTIVE = "zinrelo_loyaltyRewards/settings/loyalty_rewards_active"; public const XML_PATH_WEB_HOOK_URL = "zinrelo_loyaltyRewards/settings/web_hook_url"; + public const XML_PATH_WEBHOOK_INTEGRATION_ID = 'zinrelo_loyaltyRewards/settings/webhook_integration_id'; + public const XML_PATH_WEBHOOK_INTEGRATION_URL = 'zinrelo_loyaltyRewards/settings/webhook_integration_url'; public const XML_PATH_LIVE_WEB_HOOK_URL = "zinrelo_loyaltyRewards/settings/live_web_hook_url"; public const XML_PATH_ABANDONED_CART_TIME = "zinrelo_loyaltyRewards/settings/abandoned_cart_time"; public const XML_PATH_PARTNER_ID = "zinrelo_loyaltyRewards/settings/partner_id"; @@ -926,18 +928,18 @@ public function getWebHookUrl() */ public function saveWebHookUrl($webhookUrl) { - return $this->writeConfig->save(self::XML_PATH_WEB_HOOK_URL, $webhookUrl); + $this->writeConfig->save(self::XML_PATH_WEB_HOOK_URL, $webhookUrl); + return true; } /** - * Create Web Hook Url + * Create ZIF Integration * * @return mixed */ - public function createWebHookUrl() + public function createOrUpdateZIFIntegration($url) { try{ - $url = 'https://api.zinrelo.com/v2/loyalty/integrations'; $headers = [ "content-type" => "application/json", "accept" => "application/json", @@ -948,30 +950,22 @@ public function createWebHookUrl() "integration_type" => "magento_to_zinrelo", "config" => [ "secret_key" => $this->getApiKey(), - "events" => [ - "cart_abandonment", - "customer_create", - "customer_update", - "order_create", - "order_complete", - "order_paid", - "full_order_refund", - "partial_order_refund", - "order_cancelled", - "order_shipped", - "review_submitted", - "review_approved" - ] + "events" => $this->getRewardEvents() ], "status" => "active" ]; + $jsonBody = json_encode($body); $curlRequest = $this->curl->create(); $curlRequest->setHeaders($headers); $curlRequest->post($url, $jsonBody); $response = $curlRequest->getBody(); + if ($this->enableCustomLog()) { + $this->logger->info("Response: " . $response); + $this->logger->info("============================="); + } $data = json_decode($response, true); - return $data['data']['config']['zif_config']['workflow_url']; + return $data; } catch (Exception $e) { $this->addErrorLog($e->getMessage()); @@ -980,6 +974,38 @@ public function createWebHookUrl() } } + + /** + * Get Web Hook Integration ID + * + * @return mixed + */ + public function getWebHookIntegrationID() + { + return $this->scopeConfig->getValue(self::XML_PATH_WEBHOOK_INTEGRATION_ID); + } + + /** + * Save Web Hook Integration ID + * + * @return mixed + */ + public function saveWebHookIntegrationID($webhookIntegrationID) + { + $this->writeConfig->save(self::XML_PATH_WEBHOOK_INTEGRATION_ID, $webhookIntegrationID); + return true; + } + + /** + * Get Web Hook Integration URL + * + * @return mixed + */ + public function getWebHookIntegrationURL() + { + return $this->getConfig(self::XML_PATH_WEBHOOK_INTEGRATION_URL); + } + /** * Get Reward Events * diff --git a/Observer/ConfigSaveObserver.php b/Observer/ConfigSaveObserver.php index 15c5595..ee1a2d9 100644 --- a/Observer/ConfigSaveObserver.php +++ b/Observer/ConfigSaveObserver.php @@ -33,13 +33,24 @@ public function __construct( public function execute(Observer $observer) { - $webhookUrl = $this->helper->getWebHookUrl(); - $this->logger->info('Webhook URL: ' . $webhookUrl); - if (empty($webhookUrl)) { - $newWebhookUrl = $this->helper->createWebHookUrl(); - $this->logger->info('New Webhook URL: ' . $newWebhookUrl); - if ($newWebhookUrl) { - $this->helper->saveWebHookUrl($newWebhookUrl); + $existingWebhookUrl = $this->helper->getWebHookUrl(); + $this->logger->info('Existing Webhook URL: ' . $existingWebhookUrl); + if (empty($existingWebhookUrl)) { + $url = $this->helper->getWebHookIntegrationURL(); + $WebhookData = $this->helper->createOrUpdateZIFIntegration($url); + $WebhookUrl = $WebhookData['data']['config']['zif_config']['workflow_url']; + $WebhookIntegrationID = $WebhookData['data']['id']; + $this->logger->info('New Webhook URL: ' . $WebhookUrl); + if ($WebhookUrl) { + $this->helper->saveWebHookUrl($WebhookUrl); + $this->helper->saveWebHookIntegrationID($WebhookIntegrationID); + } + } + else{ + $webhookIntegrationID = $this->helper->getWebHookIntegrationID(); + if (!empty($webhookIntegrationID)){ + $url = $this->helper->getWebHookIntegrationURL() . "/" . $webhookIntegrationID; + $newWebhookData = $this->helper->createOrUpdateZIFIntegration($url); } } } diff --git a/etc/config.xml b/etc/config.xml index e74b8d4..6b34d2c 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -5,6 +5,7 @@ https://api.zinrelo.com/v2/loyalty/ + https://api.zinrelo.com/v2/loyalty/integrations 30 Earn {{EARN_POINTS}} points by ordering this product. You have {{AVAILABLE_POINTS}} points. Select your reward to redeem.