Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 69 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 Down Expand Up @@ -59,6 +60,10 @@ class Data extends AbstractHelper
* @var ScopeConfigInterface $scopeConfig
*/
protected $scopeConfig;
/**
* @var WriterInterface $writeConfig
*/
protected $writeConfig;
/**
* @var SessionFactory
*/
Expand Down Expand Up @@ -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(
Expand All @@ -178,6 +184,7 @@ public function __construct(
CategoryRepositoryInterface $categoryRepository,
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
WriterInterface $writeConfig,
Json $json
) {
$this->curl = $curl;
Expand All @@ -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;
Expand Down Expand Up @@ -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" => [
Comment thread
Pooja-Zinrelo marked this conversation as resolved.
Outdated
"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
*
Expand Down
46 changes: 46 additions & 0 deletions Observer/ConfigSaveObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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)
{
$webhookUrl = $this->helper->getWebHookUrl();
$this->logger->info('Webhook URL: ' . $webhookUrl);
if (empty($webhookUrl)) {
$newWebhookUrl = $this->helper->createWebHookUrl();
$this->logger->info('New Webhook URL: ' . $newWebhookUrl);
Comment thread
Pooja-Zinrelo marked this conversation as resolved.
Outdated
if ($newWebhookUrl) {
$this->helper->saveWebHookUrl($newWebhookUrl);
}
}
}
}
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",
Copy link
Copy Markdown
Contributor Author

@Pooja-Zinrelo Pooja-Zinrelo Nov 22, 2024

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
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>