CopyCurrentConfig.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\AuthorizenetAcceptjs\Setup\Patch\Data;
  8. use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\Encryption\EncryptorInterface;
  11. use Magento\Framework\Setup\ModuleDataSetupInterface;
  12. use Magento\Framework\Setup\Patch\DataPatchInterface;
  13. use Magento\Store\Model\ScopeInterface;
  14. use Magento\Store\Model\StoreManagerInterface;
  15. use Magento\Store\Api\Data\WebsiteInterface;
  16. /**
  17. * Copies the Authorize.net DirectPost configuration values to the new Accept.js module.
  18. */
  19. class CopyCurrentConfig implements DataPatchInterface
  20. {
  21. private const DIRECTPOST_PATH = 'payment/authorizenet_directpost';
  22. private const ACCEPTJS_PATH = 'payment/authorizenet_acceptjs';
  23. private const PAYMENT_PATH_FORMAT = '%s/%s';
  24. /**
  25. * @var ScopeConfigInterface
  26. */
  27. private $scopeConfig;
  28. /**
  29. * @var ConfigInterface
  30. */
  31. private $resourceConfig;
  32. /**
  33. * @var EncryptorInterface
  34. */
  35. private $encryptor;
  36. /**
  37. * @var ModuleDataSetupInterface
  38. */
  39. private $moduleDataSetup;
  40. /**
  41. * @var StoreManagerInterface
  42. */
  43. private $storeManager;
  44. /**
  45. * @var array
  46. */
  47. private $configFieldsToMigrate = [
  48. 'cctypes',
  49. 'debug',
  50. 'email_customer',
  51. 'order_status',
  52. 'payment_action',
  53. 'currency',
  54. 'allow_specific',
  55. 'specificcountry',
  56. 'min_order_total',
  57. 'max_order_total'
  58. ];
  59. /**
  60. * @var array
  61. */
  62. private $encryptedConfigFieldsToMigrate = [
  63. 'login',
  64. 'trans_key',
  65. 'trans_md5'
  66. ];
  67. /**
  68. * @param ModuleDataSetupInterface $moduleDataSetup
  69. * @param ScopeConfigInterface $scopeConfig
  70. * @param ConfigInterface $resourceConfig
  71. * @param EncryptorInterface $encryptor
  72. * @param StoreManagerInterface $storeManager
  73. */
  74. public function __construct(
  75. ModuleDataSetupInterface $moduleDataSetup,
  76. ScopeConfigInterface $scopeConfig,
  77. ConfigInterface $resourceConfig,
  78. EncryptorInterface $encryptor,
  79. StoreManagerInterface $storeManager
  80. ) {
  81. $this->scopeConfig = $scopeConfig;
  82. $this->resourceConfig = $resourceConfig;
  83. $this->encryptor = $encryptor;
  84. $this->moduleDataSetup = $moduleDataSetup;
  85. $this->storeManager = $storeManager;
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. public function apply(): void
  91. {
  92. $this->moduleDataSetup->startSetup();
  93. $this->migrateDefaultValues();
  94. $this->migrateWebsiteValues();
  95. $this->moduleDataSetup->endSetup();
  96. }
  97. /**
  98. * Migrate configuration values from DirectPost to Accept.js on default scope
  99. *
  100. * @return void
  101. */
  102. private function migrateDefaultValues(): void
  103. {
  104. foreach ($this->configFieldsToMigrate as $field) {
  105. $configValue = $this->getOldConfigValue($field);
  106. if (!empty($configValue)) {
  107. $this->saveNewConfigValue($field, $configValue);
  108. }
  109. }
  110. foreach ($this->encryptedConfigFieldsToMigrate as $field) {
  111. $configValue = $this->getOldConfigValue($field);
  112. if (!empty($configValue)) {
  113. $this->saveNewConfigValue(
  114. $field,
  115. $configValue,
  116. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  117. 0,
  118. true
  119. );
  120. }
  121. }
  122. }
  123. /**
  124. * Migrate configuration values from DirectPost to Accept.js on all website scopes
  125. *
  126. * @return void
  127. */
  128. private function migrateWebsiteValues(): void
  129. {
  130. foreach ($this->storeManager->getWebsites() as $website) {
  131. $websiteID = (int) $website->getId();
  132. foreach ($this->configFieldsToMigrate as $field) {
  133. $configValue = $this->getOldConfigValue($field, ScopeInterface::SCOPE_WEBSITES, $websiteID);
  134. if (!empty($configValue)) {
  135. $this->saveNewConfigValue($field, $configValue, ScopeInterface::SCOPE_WEBSITES, $websiteID);
  136. }
  137. }
  138. foreach ($this->encryptedConfigFieldsToMigrate as $field) {
  139. $configValue = $this->getOldConfigValue($field, ScopeInterface::SCOPE_WEBSITES, $websiteID);
  140. if (!empty($configValue)) {
  141. $this->saveNewConfigValue($field, $configValue, ScopeInterface::SCOPE_WEBSITES, $websiteID, true);
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Get old configuration value from the DirectPost module's configuration on the store scope
  148. *
  149. * @param string $field
  150. * @param string $scope
  151. * @param int $scopeID
  152. * @return mixed
  153. */
  154. private function getOldConfigValue(
  155. string $field,
  156. string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  157. int $scopeID = null
  158. ) {
  159. return $this->scopeConfig->getValue(
  160. sprintf(self::PAYMENT_PATH_FORMAT, self::DIRECTPOST_PATH, $field),
  161. $scope,
  162. $scopeID
  163. );
  164. }
  165. /**
  166. * Save configuration value for AcceptJS
  167. *
  168. * @param string $field
  169. * @param mixed $value
  170. * @param string $scope
  171. * @param int $scopeID
  172. * @param bool $isEncrypted
  173. * @return void
  174. */
  175. private function saveNewConfigValue(
  176. string $field,
  177. $value,
  178. string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  179. int $scopeID = 0,
  180. bool $isEncrypted = false
  181. ): void {
  182. $value = $isEncrypted ? $this->encryptor->encrypt($value) : $value;
  183. $this->resourceConfig->saveConfig(
  184. sprintf(self::PAYMENT_PATH_FORMAT, self::ACCEPTJS_PATH, $field),
  185. $value,
  186. $scope,
  187. $scopeID
  188. );
  189. }
  190. /**
  191. * @inheritdoc
  192. */
  193. public static function getDependencies()
  194. {
  195. return [];
  196. }
  197. /**
  198. * @inheritdoc
  199. */
  200. public function getAliases()
  201. {
  202. return [];
  203. }
  204. }