namespace Softweb\ModifyCheckoutFields\Helper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\HTTP\Client\Curl;
use Magento\Store\Model\StoreManagerInterface;
class LicenseValidator extends \Magento\Framework\App\Helper\AbstractHelper {
protected $storeManager;
protected $curl;
protected $cache;
public function __construct(Context $context, StoreManagerInterface $storeManager, Curl $curl, CacheInterface $cache) {
parent::__construct($context);
$this->storeManager = $storeManager;
$this->curl = $curl;
$this->cache = $cache;
}
public function isValid() {
$module = "Softweb_ModifyCheckoutFields";
$domain = parse_url($this->storeManager->getStore()->getBaseUrl(), PHP_URL_HOST);
$cacheKey = "sw_license_" . md5($module . $domain);
$cached = $this->cache->load($cacheKey);
if ($cached !== false) {
return $cached === "1";
}
$url = "https://your-license-server.com/api/check?module={
$module
}
&domain={
$domain
}
";
try {
$this->curl->get($url);
$response = json_decode($this->curl->getBody(), true);
$valid = isset($response["valid"]) && $response["valid"] === true;
$this->cache->save($valid ? "1" : "0", $cacheKey, array(), 86400);
return $valid;
}
catch (\Exception $e) {
return false;
}
}
}
© 2023 Quttera Ltd. All rights reserved.