0

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.

Screenshot of core_config_data table

14
  • This may not directly answer the question but it does answer your problem. If you are echoing the key in a phtml template, there is no need to encrypt it. Commented Jan 20, 2021 at 4:59
  • Also, post the field configuration from your system.xml Did you try to echo the value without decrypting? I suspect that your value is either not decrypted or you have a different crypt key in your env.php than what was used to encrypt it Commented Jan 20, 2021 at 5:00
  • Thanks btw! I did try echoing without decrypting but it didn't work. Also, my plugin doesn't have a system.xml. Commented Jan 20, 2021 at 5:09
  • Also, no issue with crypt value in env.php, since these are freshly saved values in system configuration. Commented Jan 20, 2021 at 5:14
  • So you're storing the value in config.xml but no field is configured in system.xml? Try adding backend_model to your field in config.xml <recpatcha_token backend_model="Magento\Config\Model\Config\Backend\Encrypted"/> Commented Jan 20, 2021 at 5:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.