diff --git a/mailpoet/lib/API/JSON/v1/Services.php b/mailpoet/lib/API/JSON/v1/Services.php index a4736a4067..a42298d5bd 100644 --- a/mailpoet/lib/API/JSON/v1/Services.php +++ b/mailpoet/lib/API/JSON/v1/Services.php @@ -283,6 +283,11 @@ class Services extends APIEndpoint { return $this->checkMSSKey(['key' => $key]); } + public function refreshPremiumKeyStatus() { + $key = $this->settings->get('premium.premium_key'); + return $this->checkPremiumKey(['key' => $key]); + } + private function isItemInArray($item, $array): bool { return in_array($item, $array, true); } diff --git a/mailpoet/lib/API/JSON/v1/Settings.php b/mailpoet/lib/API/JSON/v1/Settings.php index 50776102f0..7a7af1df13 100644 --- a/mailpoet/lib/API/JSON/v1/Settings.php +++ b/mailpoet/lib/API/JSON/v1/Settings.php @@ -512,6 +512,7 @@ class Settings extends APIEndpoint { 'signup_confirmation' => [ 'enabled' => '1', ], + 'premium.premium_key' => $apiKey, ]; return $this->set($new_settings); } diff --git a/mailpoet/lib/WPCOM/DotcomLicenseProvisioner.php b/mailpoet/lib/WPCOM/DotcomLicenseProvisioner.php index dac2230782..3177afe6a3 100644 --- a/mailpoet/lib/WPCOM/DotcomLicenseProvisioner.php +++ b/mailpoet/lib/WPCOM/DotcomLicenseProvisioner.php @@ -103,6 +103,14 @@ class DotcomLicenseProvisioner { return new WP_Error('Provisioning failed activating the data', $this->concatMessages($response)); } + $response = $this->services->refreshPremiumKeyStatus(); + if ($response instanceof ErrorResponse) { + $this->loggerFactory->getLogger(LoggerFactory::TOPIC_PROVISIONING)->error( + 'Refreshing Premium key failed', + ['$response' => $response] + ); + return new WP_Error('Provisioning failed activating the data', $this->concatMessages($response)); + } $this->loggerFactory->getLogger(LoggerFactory::TOPIC_PROVISIONING)->info( 'License was provisioned' diff --git a/mailpoet/tests/integration/WPCOM/DotcomLicenseProvisionerTest.php b/mailpoet/tests/integration/WPCOM/DotcomLicenseProvisionerTest.php index 093c36886d..1fcc031136 100644 --- a/mailpoet/tests/integration/WPCOM/DotcomLicenseProvisionerTest.php +++ b/mailpoet/tests/integration/WPCOM/DotcomLicenseProvisionerTest.php @@ -73,7 +73,7 @@ class DotcomLicenseProvisionerTest extends \MailPoetTest { expect($error->get_error_message())->equals('some-error '); } - public function testItReturnsTrueIfCouldNotRefreshKey() { + public function testItReturnsErrorIfCouldNotRefreshKey() { $result = false; $payload = ['apiKey' => 'some-key']; $eventType = DotcomLicenseProvisioner::EVENT_TYPE_PROVISION_LICENSE; @@ -90,6 +90,27 @@ class DotcomLicenseProvisionerTest extends \MailPoetTest { expect($error->get_error_message())->equals('some-error '); } + public function testItReturnsErrorIfCouldNotVerifyPremiumKey() { + $result = false; + $payload = ['apiKey' => 'some-key']; + $eventType = DotcomLicenseProvisioner::EVENT_TYPE_PROVISION_LICENSE; + $provisioner = $this->construct( + DotcomLicenseProvisioner::class, + [ + $this->diContainer->get(LoggerFactory::class), + $this->make(Settings::class, ['setupMSS' => new SuccessResponse()]), + $this->make(Services::class, + [ + 'refreshMSSKeyStatus' => new SuccessResponse(), + 'refreshPremiumKeyStatus' => new ErrorResponse(['error' => 'some-error']), + ]), + ], + ['isAtomicPlatform' => true]); + $error = $provisioner->provisionLicense($result, $payload, $eventType); + $this->assertInstanceOf(\WP_Error::class, $error); + expect($error->get_error_message())->equals('some-error '); + } + public function testItReturnsTrueIfKeyProvidedMSSActivatedAndRefreshed() { $result = false; $payload = ['apiKey' => 'some-key']; @@ -99,7 +120,11 @@ class DotcomLicenseProvisionerTest extends \MailPoetTest { [ $this->diContainer->get(LoggerFactory::class), $this->make(Settings::class, ['setupMSS' => new SuccessResponse()]), - $this->make(Services::class, ['refreshMSSKeyStatus' => new SuccessResponse()]), + $this->make(Services::class, + [ + 'refreshMSSKeyStatus' => new SuccessResponse(), + 'refreshPremiumKeyStatus' => new SuccessResponse(), + ]), ], ['isAtomicPlatform' => true]); $result = $provisioner->provisionLicense($result, $payload, $eventType);