Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -59,6 +62,10 @@ class Data extends AbstractHelper
* @var ScopeConfigInterface $scopeConfig
*/
protected $scopeConfig;
/**
* @var WriterInterface $writeConfig
*/
protected $writeConfig;
/**
* @var SessionFactory
*/
Expand Down Expand Up @@ -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(
Expand All @@ -178,6 +186,7 @@ public function __construct(
CategoryRepositoryInterface $categoryRepository,
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
WriterInterface $writeConfig,
Json $json
) {
$this->curl = $curl;
Expand All @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
57 changes: 57 additions & 0 deletions Observer/ConfigSaveObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Zinrelo\LoyaltyRewards\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Zinrelo\LoyaltyRewards\Helper\Data;
use Zinrelo\LoyaltyRewards\Logger\Logger as ZinreloLogger;

class ConfigSaveObserver implements ObserverInterface
{
/**
* @var Data
*/
private $helper;
/**
* @var ZinreloLogger
*/
private $logger;
/**
* Data constructor.
*
* @param Data $helper
* @param ZinreloLogger $logger
*/
public function __construct(
Data $helper,
ZinreloLogger $logger,
) {
$this->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);
}
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "zinrelo/extension",
"description": "Zinrelo Reward Point Integration Extension",
"type": "magento2-module",
"version": "2.0.6",
"version": "2.1.0",

@Pooja-Zinrelo Pooja-Zinrelo Nov 22, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check latest version and ask agency to update code in github

"require": {
"php": "~7.2.0||~7.3.0||~7.4.0||~8.0.0||~8.1.0||~8.2.0",
"firebase/php-jwt": "*"
Expand Down
4 changes: 1 addition & 3 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@
<field id="loyalty_rewards_active">1</field>
</depends>
</field>

<field id="web_hook_url" showInDefault="1" showInStore="0" showInWebsite="1" sortOrder="35" translate="label" type="text">
<label>Webhook URL</label>
<comment><![CDATA[This field is optional.]]></comment>
<depends>
<field id="loyalty_rewards_active">1</field>
</depends>
<validate>required-entry validate-url</validate>
</field>

<field id="product_page_rewards_point_enable" showInDefault="1" showInStore="0" showInWebsite="1" sortOrder="40" translate="label" type="select">
<label>Enable reward points text on product pages</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<zinrelo_loyaltyRewards>
<settings>
<live_web_hook_url>https://api.zinrelo.com/v2/loyalty/</live_web_hook_url>
<webhook_integration_url>https://api.zinrelo.com/v2/loyalty/integrations</webhook_integration_url>
<abandoned_cart_time>30</abandoned_cart_time>
<product_page_reward_label>Earn {{EARN_POINTS}} points by ordering this product.</product_page_reward_label>
<cart_page_reward_dropdown_label>You have {{AVAILABLE_POINTS}} points. Select your reward to redeem.</cart_page_reward_dropdown_label>
Expand Down
3 changes: 3 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@
<event name="sales_order_creditmemo_save_commit_after">
<observer name="orderRefundZinrelo" instance="Zinrelo\LoyaltyRewards\Observer\OrderRefund"/>
</event>
<event name="admin_system_config_changed_section_zinrelo_loyaltyRewards">
<observer name="configSaveObserver" instance="Zinrelo\LoyaltyRewards\Observer\ConfigSaveObserver"/>
</event>
</config>