1010use Magento \Framework \App \Config \ConfigResource \ConfigInterface ;
1111use Magento \Framework \App \Config \ScopeConfigInterface ;
1212use Magento \Framework \Encryption \EncryptorInterface ;
13- use Magento \Framework \Setup \InstallDataInterface ;
14- use Magento \Framework \Setup \ModuleContextInterface ;
1513use Magento \Framework \Setup \ModuleDataSetupInterface ;
1614use Magento \Framework \Setup \Patch \DataPatchInterface ;
17- use Magento \Framework \Setup \Patch \PatchInterface ;
1815use Magento \Store \Model \ScopeInterface ;
16+ use Magento \Store \Model \StoreManagerInterface ;
17+ use Magento \Store \Api \Data \WebsiteInterface ;
1918
2019/**
2120 * Copies the Authorize.net DirectPost configuration values to the new Accept.js module.
@@ -46,6 +45,11 @@ class CopyCurrentConfig implements DataPatchInterface
4645 */
4746 private $ moduleDataSetup ;
4847
48+ /**
49+ * @var StoreManagerInterface
50+ */
51+ private $ storeManager ;
52+
4953 /**
5054 * @var array
5155 */
@@ -76,26 +80,40 @@ class CopyCurrentConfig implements DataPatchInterface
7680 * @param ScopeConfigInterface $scopeConfig
7781 * @param ConfigInterface $resourceConfig
7882 * @param EncryptorInterface $encryptor
83+ * @param StoreManagerInterface $storeManager
7984 */
8085 public function __construct (
8186 ModuleDataSetupInterface $ moduleDataSetup ,
8287 ScopeConfigInterface $ scopeConfig ,
8388 ConfigInterface $ resourceConfig ,
84- EncryptorInterface $ encryptor
89+ EncryptorInterface $ encryptor ,
90+ StoreManagerInterface $ storeManager
8591 ) {
8692 $ this ->scopeConfig = $ scopeConfig ;
8793 $ this ->resourceConfig = $ resourceConfig ;
8894 $ this ->encryptor = $ encryptor ;
8995 $ this ->moduleDataSetup = $ moduleDataSetup ;
96+ $ this ->storeManager = $ storeManager ;
9097 }
9198
9299 /**
93100 * @inheritdoc
94101 */
95- public function apply ()
102+ public function apply (): void
96103 {
97104 $ this ->moduleDataSetup ->startSetup ();
105+ $ this ->migrateDefaultValues ();
106+ $ this ->migrateWebsiteValues ();
107+ $ this ->moduleDataSetup ->endSetup ();
108+ }
98109
110+ /**
111+ * Migrate configuration values from DirectPost to Accept.js on default scope
112+ *
113+ * @return void
114+ */
115+ private function migrateDefaultValues (): void
116+ {
99117 foreach ($ this ->configFieldsToMigrate as $ field ) {
100118 $ configValue = $ this ->getOldConfigValue ($ field );
101119
@@ -108,24 +126,62 @@ public function apply()
108126 $ configValue = $ this ->getOldConfigValue ($ field );
109127
110128 if (!empty ($ configValue )) {
111- $ this ->saveNewConfigValue ($ field , $ configValue , true );
129+ $ this ->saveNewConfigValue (
130+ $ field ,
131+ $ configValue ,
132+ ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
133+ 0 ,
134+ true
135+ );
112136 }
113137 }
138+ }
114139
115- $ this ->moduleDataSetup ->endSetup ();
140+ /**
141+ * Migrate configuration values from DirectPost to Accept.js on all website scopes
142+ *
143+ * @return void
144+ */
145+ private function migrateWebsiteValues (): void
146+ {
147+ foreach ($ this ->storeManager ->getWebsites () as $ website ) {
148+ $ websiteID = (int ) $ website ->getId ();
149+
150+ foreach ($ this ->configFieldsToMigrate as $ field ) {
151+ $ configValue = $ this ->getOldConfigValue ($ field , ScopeInterface::SCOPE_WEBSITES , $ websiteID );
152+
153+ if (!empty ($ configValue )) {
154+ $ this ->saveNewConfigValue ($ field , $ configValue , ScopeInterface::SCOPE_WEBSITES , $ websiteID );
155+ }
156+ }
157+
158+ foreach ($ this ->encryptedConfigFieldsToMigrate as $ field ) {
159+ $ configValue = $ this ->getOldConfigValue ($ field , ScopeInterface::SCOPE_WEBSITES , $ websiteID );
160+
161+ if (!empty ($ configValue )) {
162+ $ this ->saveNewConfigValue ($ field , $ configValue , ScopeInterface::SCOPE_WEBSITES , $ websiteID , true );
163+ }
164+ }
165+ }
116166 }
117167
118168 /**
119169 * Get old configuration value from the DirectPost module's configuration on the store scope
120170 *
121171 * @param string $field
172+ * @param string $scope
173+ * @param int $scopeID
122174 * @return mixed
123175 */
124- private function getOldConfigValue (string $ field )
125- {
176+ private function getOldConfigValue (
177+ string $ field ,
178+ string $ scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
179+ int $ scopeID = null
180+ ) {
126181 return $ this ->scopeConfig ->getValue (
127182 sprintf (self ::PAYMENT_PATH_FORMAT , self ::DIRECTPOST_PATH , $ field ),
128- ScopeInterface::SCOPE_STORE
183+ $ scope ,
184+ $ scopeID
129185 );
130186 }
131187
@@ -134,15 +190,25 @@ private function getOldConfigValue(string $field)
134190 *
135191 * @param string $field
136192 * @param mixed $value
193+ * @param string $scope
194+ * @param int $scopeID
137195 * @param bool $isEncrypted
196+ * @return void
138197 */
139- private function saveNewConfigValue (string $ field , $ value , $ isEncrypted = false ): void
140- {
198+ private function saveNewConfigValue (
199+ string $ field ,
200+ $ value ,
201+ string $ scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
202+ int $ scopeID = 0 ,
203+ bool $ isEncrypted = false
204+ ): void {
141205 $ value = $ isEncrypted ? $ this ->encryptor ->encrypt ($ value ) : $ value ;
206+
142207 $ this ->resourceConfig ->saveConfig (
143208 sprintf (self ::PAYMENT_PATH_FORMAT , self ::ACCEPTJS_PATH , $ field ),
144209 $ value ,
145- ScopeInterface::SCOPE_STORE
210+ $ scope ,
211+ $ scopeID
146212 );
147213 }
148214
0 commit comments