0

I want to save encrypted value in core_config_data while setup upgrade without using config.xml. I am trying to use data patch file

Vendor\Credentials\Setup\Patch\Data\AddData.php

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Config\Model\Config\Backend\Encrypted;
use Magento\Framework\Encryption\EncryptorInterface;


public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EncryptorInterface $_encryptor,
        WriterInterface $configWriter
    )
    {
        $this->moduleDataSetup=$moduleDataSetup;
        $this->_encryptor = $_encryptor;
        $this->configWriter = $configWriter;
    }



$this->moduleDataSetup->startSetup();
        $privateKey = $encrypted = $this->_encryptor->encrypt($value);
        $publicKey = $encrypted = $this->_encryptor->encrypt($value);
        $this->configWriter->save(self::XML_PATH_PRIVATE_API_KEY, $privateKey, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
        $this->configWriter->save(self::XML_PATH_ENABLED, 1, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
        $this->moduleDataSetup->endSetup();

But entry is not saving. I have checked patch table entry and here patch get register.

3
  • Could you please share the constructor function? I want to check configWriter variable definition. Commented May 20, 2021 at 5:24
  • I have updated the code above Commented May 20, 2021 at 6:43
  • I have checked with your sample code, it's working perfectly. Please check once values of self::XML_PATH_PRIVATE_API_KEY and self::XML_PATH_ENABLED variables. Commented May 20, 2021 at 7:11

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.