@@ -6,14 +6,19 @@ class ApiKeyService {
66 private string $ test_api_key ;
77 private string $ live_api_key ;
88 private string $ api_key_mode ;
9- private string $ account_id ;
9+ private string $ test_account_id ;
10+ private string $ live_account_id ;
1011
11- public function __construct () {
12+
13+ public function __construct () {
1214 // Load the API keys and mode from the database
1315 $ this ->test_api_key = get_option ( 'monei_test_apikey ' , '' );
1416 $ this ->live_api_key = get_option ( 'monei_live_apikey ' , '' );
1517 $ this ->api_key_mode = get_option ( 'monei_apikey_mode ' , 'test ' );
16- $ this ->account_id = get_option ( 'monei_accountid ' );
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
1722 add_action ( 'init ' , array ( $ this , 'copyKeysToCentralSettings ' ), 0 );
1823 }
1924
@@ -41,7 +46,7 @@ public function is_test_mode(): bool {
4146 * @return string
4247 */
4348 public function get_account_id (): string {
44- return $ this ->account_id ;
49+ return ( $ this ->api_key_mode === ' test ' ) ? $ this -> test_account_id : $ this -> live_account_id ;
4550 }
4651
4752 /**
@@ -51,6 +56,8 @@ public function get_account_id(): string {
5156 public function update_keys (): void {
5257 $ this ->test_api_key = get_option ( 'monei_test_apikey ' , '' );
5358 $ 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 ' );
5461 $ this ->api_key_mode = get_option ( 'monei_apikey_mode ' , 'test ' );
5562 }
5663
@@ -66,22 +73,22 @@ function ( $default_params ) {
6673 if ( empty ( $ centralApiKey ) && empty ( $ ccApiKey ) ) {
6774 return $ default_params ;
6875 }
69-
7076 $ keyToUse = ! empty ( $ centralApiKey ) ? $ centralApiKey : $ ccApiKey ;
77+ $ accountId =! empty ( $ centralAccountId )? $ centralAccountId : $ ccAccountId ;
7178
7279 if ( strpos ( $ keyToUse , 'pk_test_ ' ) === 0 ) {
7380 update_option ( 'monei_test_apikey ' , $ keyToUse );
7481 update_option ( 'monei_apikey_mode ' , 'test ' );
75- } else {
82+ update_option ( 'monei_test_accountid ' , $ accountId );
83+ delete_option ( 'monei_apikey ' );
84+ delete_option ( 'monei_accountid ' );
85+ } else if (strpos ( $ keyToUse , 'pk_live_ ' ) === 0 ) {
7686 update_option ( 'monei_live_apikey ' , $ keyToUse );
7787 update_option ( 'monei_apikey_mode ' , 'live ' );
78- }
79-
80- delete_option ( 'monei_apikey ' );
81-
82- if ( empty ( $ centralAccountId ) && ! empty ( $ ccAccountId ) ) {
83- update_option ( 'monei_accountid ' , $ ccAccountId );
84- }
88+ update_option ( 'monei_live_accountid ' , $ accountId );
89+ delete_option ( 'monei_apikey ' );
90+ delete_option ( 'monei_accountid ' );
91+ }
8592
8693 return $ default_params ;
8794 },
0 commit comments