| 1 |
<?php |
| 2 |
|
| 3 |
namespace Monei\Services; |
| 4 |
|
| 5 |
class ApiKeyService { |
| 6 |
private string $test_api_key; |
| 7 |
private string $live_api_key; |
| 8 |
private string $api_key_mode; |
| 9 |
private string $test_account_id; |
| 10 |
private string $live_account_id; |
| 11 |
|
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
// Load the API keys and mode from the database |
| 15 |
$this->test_api_key = get_option( 'monei_test_apikey', '' ); |
| 16 |
$this->live_api_key = get_option( 'monei_live_apikey', '' ); |
| 17 |
$this->api_key_mode = get_option( 'monei_apikey_mode', 'test' ); |
| 18 |
$this->test_account_id = get_option( 'monei_test_accountid', '' ); |
| 19 |
$this->live_account_id = get_option( 'monei_live_accountid', '' ); |
| 20 |
|
| 21 |
// Copy the API keys to the central settings when the plugin is activated or updated |
| 22 |
add_action( 'init', array( $this, 'copyKeysToCentralSettings' ), 0 ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get the appropriate API key based on the selected mode. |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public function get_api_key(): string { |
| 31 |
return ( $this->api_key_mode === 'test' ) ? $this->test_api_key : $this->live_api_key; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get the selected API key mode. |
| 36 |
* |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
public function is_test_mode(): bool { |
| 40 |
return $this->api_key_mode === 'test'; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the account id. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function get_account_id(): string { |
| 49 |
return ( $this->api_key_mode === 'test' ) ? $this->test_account_id : $this->live_account_id; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Update the API keys and mode in the service. |
| 54 |
* This can be called whenever the database is updated. |
| 55 |
*/ |
| 56 |
public function update_keys(): void { |
| 57 |
$this->test_api_key = get_option( 'monei_test_apikey', '' ); |
| 58 |
$this->live_api_key = get_option( 'monei_live_apikey', '' ); |
| 59 |
$this->test_account_id = get_option( 'monei_test_accountid', '' ); |
| 60 |
$this->live_account_id = get_option( 'monei_live_accountid', '' ); |
| 61 |
$this->api_key_mode = get_option( 'monei_apikey_mode', 'test' ); |
| 62 |
} |
| 63 |
|
| 64 |
public function copyKeysToCentralSettings() { |
| 65 |
// Get the current state once |
| 66 |
$keyState = $this->getCurrentKeyState(); |
| 67 |
|
| 68 |
// First, check if we need any migration at all |
| 69 |
if ( $this->needsMigration( $keyState ) ) { |
| 70 |
// Try standalone migration first (has priority) |
| 71 |
$standaloneSuccess = $this->migrateStandaloneKeys( $keyState ); |
| 72 |
|
| 73 |
// Only bother with settings if standalone migration didn't complete everything |
| 74 |
if ( ! $standaloneSuccess ) { |
| 75 |
add_filter( 'option_woocommerce_monei_settings', array( $this, 'processCentralSettings' ), 10, 1 ); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get current state of all keys |
| 82 |
* |
| 83 |
* @return array Current key state |
| 84 |
*/ |
| 85 |
private function getCurrentKeyState() { |
| 86 |
return array( |
| 87 |
'test_api_key' => get_option( 'monei_test_apikey', '' ), |
| 88 |
'live_api_key' => get_option( 'monei_live_apikey', '' ), |
| 89 |
'test_account_id' => get_option( 'monei_test_accountid', '' ), |
| 90 |
'live_account_id' => get_option( 'monei_live_accountid', '' ), |
| 91 |
'current_mode' => get_option( 'monei_apikey_mode', '' ), |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Check if any migration is needed |
| 97 |
* |
| 98 |
* @param array $keyState Current key state |
| 99 |
* @return bool True if migration is needed |
| 100 |
*/ |
| 101 |
private function needsMigration( $keyState ) { |
| 102 |
// Get legacy keys |
| 103 |
$legacyApiKey = get_option( 'monei_apikey', '' ); |
| 104 |
$legacyAccountId = get_option( 'monei_accountid', '' ); |
| 105 |
$existingSettings = get_option( 'woocommerce_monei_settings', array() ); |
| 106 |
$settingsApiKey = $existingSettings['apikey'] ?? ''; |
| 107 |
$settingsAccountId = $existingSettings['accountid'] ?? ''; |
| 108 |
|
| 109 |
// Check if both new key sets are complete |
| 110 |
$testKeysComplete = ! empty( $keyState['test_api_key'] ) && ! empty( $keyState['test_account_id'] ); |
| 111 |
$liveKeysComplete = ! empty( $keyState['live_api_key'] ) && ! empty( $keyState['live_account_id'] ); |
| 112 |
|
| 113 |
// If both are complete, no migration needed |
| 114 |
if ( $testKeysComplete && $liveKeysComplete ) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
// If we have any legacy keys or incomplete new keys, migration is needed |
| 119 |
return ! empty( $legacyApiKey ) || ! empty( $legacyAccountId ) || |
| 120 |
! empty( $settingsApiKey ) || ! empty( $settingsAccountId ) || |
| 121 |
( ! empty( $keyState['test_api_key'] ) && empty( $keyState['test_account_id'] ) ) || |
| 122 |
( ! empty( $keyState['live_api_key'] ) && empty( $keyState['live_account_id'] ) ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Migrate standalone legacy keys (works regardless of settings existence) |
| 127 |
* |
| 128 |
* @param array $keyState Current key state |
| 129 |
* @return bool True if migration was successful and complete, false if settings migration is still needed |
| 130 |
*/ |
| 131 |
private function migrateStandaloneKeys( $keyState ) { |
| 132 |
// Get legacy standalone keys |
| 133 |
$legacyApiKey = get_option( 'monei_apikey', '' ); |
| 134 |
$legacyAccountId = get_option( 'monei_accountid', '' ); |
| 135 |
|
| 136 |
$needsCleanup = false; |
| 137 |
$migratedFromStandalone = false; |
| 138 |
|
| 139 |
// Complete partial new keys using legacy standalone keys |
| 140 |
if ( ! empty( $keyState['test_api_key'] ) && empty( $keyState['test_account_id'] ) && ! empty( $legacyAccountId ) ) { |
| 141 |
update_option( 'monei_test_accountid', $legacyAccountId ); |
| 142 |
$needsCleanup = true; |
| 143 |
$migratedFromStandalone = true; |
| 144 |
} |
| 145 |
|
| 146 |
if ( ! empty( $keyState['live_api_key'] ) && empty( $keyState['live_account_id'] ) && ! empty( $legacyAccountId ) ) { |
| 147 |
update_option( 'monei_live_accountid', $legacyAccountId ); |
| 148 |
$needsCleanup = true; |
| 149 |
$migratedFromStandalone = true; |
| 150 |
} |
| 151 |
|
| 152 |
// Set mode based on existing new keys if mode is not set |
| 153 |
if ( empty( $keyState['current_mode'] ) ) { |
| 154 |
if ( ! empty( $keyState['test_api_key'] ) ) { |
| 155 |
update_option( 'monei_apikey_mode', 'test' ); |
| 156 |
} elseif ( ! empty( $keyState['live_api_key'] ) ) { |
| 157 |
update_option( 'monei_apikey_mode', 'live' ); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// Full migration from legacy standalone keys if no new keys exist |
| 162 |
if ( empty( $keyState['test_api_key'] ) && empty( $keyState['live_api_key'] ) && ! empty( $legacyApiKey ) ) { |
| 163 |
if ( $this->migrateSingleKeySet( $legacyApiKey, $legacyAccountId, $keyState['current_mode'] ) ) { |
| 164 |
$needsCleanup = true; |
| 165 |
$migratedFromStandalone = true; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// Clean up legacy standalone keys if we migrated |
| 170 |
if ( $needsCleanup ) { |
| 171 |
delete_option( 'monei_apikey' ); |
| 172 |
delete_option( 'monei_accountid' ); |
| 173 |
} |
| 174 |
|
| 175 |
// Return true if we migrated anything from standalone (has priority over settings) |
| 176 |
// or if we already had complete key sets |
| 177 |
$initialTestKeysComplete = ! empty( $keyState['test_api_key'] ) && ! empty( $keyState['test_account_id'] ); |
| 178 |
$initialLiveKeysComplete = ! empty( $keyState['live_api_key'] ) && ! empty( $keyState['live_account_id'] ); |
| 179 |
|
| 180 |
return $migratedFromStandalone || ( $initialTestKeysComplete || $initialLiveKeysComplete ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Process and migrate API keys from settings (only called via filter) |
| 185 |
* |
| 186 |
* @param array $default_params The settings array from the filter |
| 187 |
* @return array The processed settings array |
| 188 |
*/ |
| 189 |
public function processCentralSettings( $default_params ) { |
| 190 |
$newTestApiKey = get_option( 'monei_test_apikey', '' ); |
| 191 |
$newLiveApiKey = get_option( 'monei_live_apikey', '' ); |
| 192 |
$newTestAccountId = get_option( 'monei_test_accountid', '' ); |
| 193 |
$newLiveAccountId = get_option( 'monei_live_accountid', '' ); |
| 194 |
$currentMode = get_option( 'monei_apikey_mode', '' ); |
| 195 |
|
| 196 |
// Get keys from settings |
| 197 |
$settingsApiKey = $default_params['apikey'] ?? ''; |
| 198 |
$settingsAccountId = $default_params['accountid'] ?? ''; |
| 199 |
|
| 200 |
$needsCleanup = false; |
| 201 |
$testKeysComplete = ! empty( $newTestApiKey ) && ! empty( $newTestAccountId ); |
| 202 |
$liveKeysComplete = ! empty( $newLiveApiKey ) && ! empty( $newLiveAccountId ); |
| 203 |
|
| 204 |
// If both sets are complete, just clean up settings and return |
| 205 |
if ( $testKeysComplete && $liveKeysComplete ) { |
| 206 |
if ( empty( $currentMode ) ) { |
| 207 |
update_option( 'monei_apikey_mode', 'test' ); |
| 208 |
} |
| 209 |
return $this->cleanup_legacy_keys( $default_params ); |
| 210 |
} |
| 211 |
|
| 212 |
// Complete partial new keys using settings keys |
| 213 |
if ( ! empty( $newTestApiKey ) && empty( $newTestAccountId ) && ! empty( $settingsAccountId ) ) { |
| 214 |
update_option( 'monei_test_accountid', $settingsAccountId ); |
| 215 |
$needsCleanup = true; |
| 216 |
} |
| 217 |
|
| 218 |
if ( ! empty( $newLiveApiKey ) && empty( $newLiveAccountId ) && ! empty( $settingsAccountId ) ) { |
| 219 |
update_option( 'monei_live_accountid', $settingsAccountId ); |
| 220 |
$needsCleanup = true; |
| 221 |
} |
| 222 |
|
| 223 |
// Set mode based on existing new keys if mode is not set |
| 224 |
if ( empty( $currentMode ) ) { |
| 225 |
if ( ! empty( $newTestApiKey ) ) { |
| 226 |
update_option( 'monei_apikey_mode', 'test' ); |
| 227 |
} elseif ( ! empty( $newLiveApiKey ) ) { |
| 228 |
update_option( 'monei_apikey_mode', 'live' ); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
// Full migration from settings keys if no new keys exist |
| 233 |
if ( empty( $newTestApiKey ) && empty( $newLiveApiKey ) && ! empty( $settingsApiKey ) ) { |
| 234 |
if ( $this->migrateSingleKeySet( $settingsApiKey, $settingsAccountId, $currentMode ) ) { |
| 235 |
$needsCleanup = true; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Clean up legacy keys from settings if we did any migration |
| 240 |
if ( $needsCleanup ) { |
| 241 |
$default_params = $this->cleanup_legacy_keys( $default_params ); |
| 242 |
} |
| 243 |
|
| 244 |
return $default_params; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Migrate a single key set based on key prefix |
| 249 |
* |
| 250 |
* @param string $apiKey The API key to migrate |
| 251 |
* @param string $accountId The account ID to migrate |
| 252 |
* @param string $currentMode Current mode setting |
| 253 |
* @return bool True if migration occurred |
| 254 |
*/ |
| 255 |
private function migrateSingleKeySet( $apiKey, $accountId, $currentMode ) { |
| 256 |
if ( strpos( $apiKey, 'pk_test_' ) === 0 ) { |
| 257 |
update_option( 'monei_test_apikey', $apiKey ); |
| 258 |
if ( ! empty( $accountId ) ) { |
| 259 |
update_option( 'monei_test_accountid', $accountId ); |
| 260 |
} |
| 261 |
if ( empty( $currentMode ) ) { |
| 262 |
update_option( 'monei_apikey_mode', 'test' ); |
| 263 |
} |
| 264 |
return true; |
| 265 |
} elseif ( strpos( $apiKey, 'pk_live_' ) === 0 ) { |
| 266 |
update_option( 'monei_live_apikey', $apiKey ); |
| 267 |
if ( ! empty( $accountId ) ) { |
| 268 |
update_option( 'monei_live_accountid', $accountId ); |
| 269 |
} |
| 270 |
if ( empty( $currentMode ) ) { |
| 271 |
update_option( 'monei_apikey_mode', 'live' ); |
| 272 |
} |
| 273 |
return true; |
| 274 |
} |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Clean up legacy keys from settings array |
| 280 |
* |
| 281 |
* @param array $settings_array The settings array |
| 282 |
* @return array The cleaned settings array |
| 283 |
*/ |
| 284 |
private function cleanup_legacy_keys( $settings_array ) { |
| 285 |
if ( isset( $settings_array['apikey'] ) ) { |
| 286 |
unset( $settings_array['apikey'] ); |
| 287 |
} |
| 288 |
if ( isset( $settings_array['accountid'] ) ) { |
| 289 |
unset( $settings_array['accountid'] ); |
| 290 |
} |
| 291 |
|
| 292 |
return $settings_array; |
| 293 |
} |
| 294 |
} |
| 295 |
|