diff --git a/Helper/Data.php b/Helper/Data.php index bf5e3bb..f770eea 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; @@ -36,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"; @@ -59,6 +62,10 @@ class Data extends AbstractHelper * @var ScopeConfigInterface $scopeConfig */ protected $scopeConfig; + /** + * @var WriterInterface $writeConfig + */ + protected $writeConfig; /** * @var SessionFactory */ @@ -155,6 +162,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 +186,7 @@ public function __construct( CategoryRepositoryInterface $categoryRepository, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig, + WriterInterface $writeConfig, Json $json ) { $this->curl = $curl; @@ -195,6 +204,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 +921,91 @@ public function getWebHookUrl() return $this->scopeConfig->getValue(self::XML_PATH_WEB_HOOK_URL); } + /** + * Save Web Hook Url + * + * @return mixed + */ + public function saveWebHookUrl($webhookUrl) + { + $this->writeConfig->save(self::XML_PATH_WEB_HOOK_URL, $webhookUrl); + return true; + } + + /** + * Create ZIF Integration + * + * @return mixed + */ + public function createOrUpdateZIFIntegration($url) + { + try{ + $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" => $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; + } + 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 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 new file mode 100644 index 0000000..ee1a2d9 --- /dev/null +++ b/Observer/ConfigSaveObserver.php @@ -0,0 +1,57 @@ +helper = $helper; + $this->logger = $logger; + } + + public function execute(Observer $observer) + { + $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); + } + } + } +} \ 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/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. 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 @@ + + +