I am making a custom form plugin that integrates ReCaptcha in Magento 2.
To integrate ReCaptcha in the frontend I am trying to read ReCaptcha v3 public key (recaptcha_frontend/type_recaptcha_v3/public_key
) from the system configuration using ScopeConfigInterface
interface in Block template like this.
<?php
namespace Vendor\CustomForm\Block;
use Magento\Store\Model\ScopeInterface;
class Template extends \Magento\Framework\View\Element\Template
{
private const XML_PATH_RECAPTCHA_PUBLIC_KEY = 'recaptcha_frontend/type_recaptcha_v3/public_key';
protected $scopeConfig;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
array $data = []
) {
parent::__construct($context, $data);
$this->scopeConfig = $scopeConfig;
}
public function getReCaptchaPublicKey(): string
{
return $this->scopeConfig->getValue(static::XML_PATH_RECAPTCHA_PUBLIC_KEY, ScopeInterface::SCOPE_WEBSITE);
}
}
And accessing it in the block.phtml
like this.
<?php echo $block->getReCaptchaPublicKey(); ?>
But instead of the public key, I get the following output.
$this->helper('Vendor\Module\Helper\Data')->getConfig('section/group/field');
Any help would be appreciated!
UPDATE: I did try using EncryptorInterface
but no luck!
<?php
namespace Vendor\CustomForm\Block;
use Magento\Store\Model\ScopeInterface;
class Template extends \Magento\Framework\View\Element\Template
{
private const XML_PATH_RECAPTCHA_PUBLIC_KEY = 'recaptcha_frontend/type_recaptcha_v3/public_key';
protected $scopeConfig;
protected $encryptor;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
array $data = []
) {
parent::__construct($context, $data);
$this->scopeConfig = $scopeConfig;
$this->encryptor = $encryptor;
}
public function getReCaptchaPublicKey(): string
{
$key = $this->scopeConfig->getValue(static::XML_PATH_RECAPTCHA_PUBLIC_KEY, ScopeInterface::SCOPE_WEBSITE);
return $this->encryptor->decrypt($key);
}
}
Here is a screenshot of core_config_data
table. I trying to access the value with the path recaptcha_frontend/type_recaptcha_v3/public_key
.