85 lines
2.5 KiB
PHP
85 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* @package Plumrocket_GeoIPLookup
|
|
* @copyright Copyright (c) 2024 Plumrocket Inc. (https://plumrocket.com)
|
|
* @license https://plumrocket.com/license/ End-user License Agreement
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Plumrocket\GeoIPLookup\Controller\Adminhtml\Test;
|
|
|
|
use Magento\Backend\App\Action;
|
|
use Magento\Backend\App\Action\Context;
|
|
use Magento\Framework\HTTP\Client\Curl;
|
|
use Magento\Framework\Serialize\Serializer\Json;
|
|
use Plumrocket\GeoIPLookup\Helper\Config;
|
|
|
|
/**
|
|
* @since 2.0.1
|
|
*/
|
|
class IpapiEndpoint extends Action
|
|
{
|
|
/**
|
|
* @var \Plumrocket\GeoIPLookup\Helper\Config
|
|
*/
|
|
private $configHelper;
|
|
|
|
/**
|
|
* @var \Magento\Framework\HTTP\Client\Curl
|
|
*/
|
|
private $curl;
|
|
|
|
/**
|
|
* @var \Magento\Framework\Serialize\Serializer\Json
|
|
*/
|
|
private $serializer;
|
|
|
|
/**
|
|
* @param \Magento\Backend\App\Action\Context $context
|
|
* @param \Plumrocket\GeoIPLookup\Helper\Config $configHelper
|
|
* @param \Magento\Framework\HTTP\Client\Curl $curl
|
|
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
|
|
*/
|
|
public function __construct(
|
|
Context $context,
|
|
Config $configHelper,
|
|
Curl $curl,
|
|
Json $serializer
|
|
) {
|
|
parent::__construct($context);
|
|
$this->configHelper = $configHelper;
|
|
$this->curl = $curl;
|
|
$this->serializer = $serializer;
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
$params = $this->getRequest()->getParams();
|
|
$protocol = !empty($params['use_ssl']) ? 'https://' : 'http://';
|
|
$baseUrl = $this->configHelper->getIpApiUrl(Config::DEFAULT_IP, false);
|
|
$url = $protocol . $baseUrl . "?access_key={$params['access_key']}";
|
|
|
|
$this->curl->get($url);
|
|
$response = $this->serializer->unserialize($this->curl->getBody());
|
|
|
|
$resultData = ['success' => false];
|
|
if (isset($response['success'])) {
|
|
if (isset($response['error']['info'])) {
|
|
if (isset($response['error']['code']) && $response['error']['code'] !== 101) {
|
|
$resultData['success'] = true;
|
|
} else {
|
|
$resultData['message'] = $response['error']['info'];
|
|
}
|
|
}
|
|
} else if (isset($response['ip']) && $response['ip'] === Config::DEFAULT_IP) {
|
|
$resultData['success'] = true;
|
|
}
|
|
|
|
/** @var \Magento\Framework\Controller\Result\Json $result */
|
|
$result = $this->resultFactory->create('json');
|
|
|
|
return $result->setData($resultData);
|
|
}
|
|
}
|