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


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->_encryptor = $_encryptor;
        $this->configWriter = $configWriter;

        $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);

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

  • Could you please share the constructor function? I want to check configWriter variable definition. May 20, 2021 at 5:24
  • I have updated the code above 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. May 20, 2021 at 7:11


Your Answer

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

Browse other questions tagged or ask your own question.