Add handling of subscriber limit error to MailPoet error mapper

When the shop blocked access for an API key due subscriber limit it used
to set the error as "Insufficient privileges," and that was in the plugin
interpreted same as "Subscribers limit reached".
The SHOP-1228 changes the error message will be set to "Subscribers limit reached".
This commit updates the error mapper to be able to process the new error message.
It also keeps the old approach for previously blocked keys.
[MAILPOET-5191]
This commit is contained in:
Rostislav Wolny
2023-04-26 10:41:47 +02:00
committed by Veljko V
parent eb06b6d01e
commit db467d8616
3 changed files with 27 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ class MailerError {
const OPERATION_SEND = 'send'; const OPERATION_SEND = 'send';
const OPERATION_AUTHORIZATION = 'authorization'; const OPERATION_AUTHORIZATION = 'authorization';
const OPERATION_INSUFFICIENT_PRIVILEGES = 'insufficient_privileges'; const OPERATION_INSUFFICIENT_PRIVILEGES = 'insufficient_privileges';
const OPERATION_SUBSCRIBER_LIMIT_REACHED = 'subscriber_limit_reached';
const OPERATION_EMAIL_LIMIT_REACHED = 'email_limit_reached'; const OPERATION_EMAIL_LIMIT_REACHED = 'email_limit_reached';
const OPERATION_PENDING_APPROVAL = 'pending_approval'; const OPERATION_PENDING_APPROVAL = 'pending_approval';

View File

@@ -134,7 +134,7 @@ class MailPoetMapper {
return $message; return $message;
} }
private function getInsufficientPrivilegesMessage(): string { private function getSubscribersLimitReachedMessage(): string {
$message = __('You have reached the subscriber limit of your plan. Please [link1]upgrade your plan[/link1], or [link2]contact our support team[/link2] if you have any questions.', 'mailpoet'); $message = __('You have reached the subscriber limit of your plan. Please [link1]upgrade your plan[/link1], or [link2]contact our support team[/link2] if you have any questions.', 'mailpoet');
$message = Helpers::replaceLinkTags( $message = Helpers::replaceLinkTags(
$message, $message,
@@ -248,9 +248,18 @@ class MailPoetMapper {
return [$operation, $message]; return [$operation, $message];
} }
// Backward compatibility for older blocked keys.
// Exceeded subscribers limit used to use the same error message as insufficient privileges.
// We can change the message to "Insufficient privileges" like wording a couple of months after releasing SHOP-1228
if ($result['error'] === API::ERROR_MESSAGE_INSUFFICIENT_PRIVILEGES) { if ($result['error'] === API::ERROR_MESSAGE_INSUFFICIENT_PRIVILEGES) {
$operation = MailerError::OPERATION_INSUFFICIENT_PRIVILEGES; $operation = MailerError::OPERATION_INSUFFICIENT_PRIVILEGES;
$message = $this->getInsufficientPrivilegesMessage(); $message = $this->getSubscribersLimitReachedMessage();
return [$operation, $message];
}
if ($result['error'] === API::ERROR_MESSAGE_SUBSCRIBERS_LIMIT_REACHED) {
$operation = MailerError::OPERATION_SUBSCRIBER_LIMIT_REACHED;
$message = $this->getSubscribersLimitReachedMessage();
return [$operation, $message]; return [$operation, $message];
} }

View File

@@ -100,6 +100,21 @@ class MailPoetMapperTest extends \MailPoetUnitTest {
expect($error->getMessage())->stringContainsString('You have reached the subscriber limit of your plan.'); expect($error->getMessage())->stringContainsString('You have reached the subscriber limit of your plan.');
} }
public function testGetErrorSubscribersLimits(): void {
$apiResult = [
'code' => API::RESPONSE_CODE_CAN_NOT_SEND,
'status' => API::SENDING_STATUS_SEND_ERROR,
'message' => API::ERROR_MESSAGE_SUBSCRIBERS_LIMIT_REACHED,
'error' => API::ERROR_MESSAGE_SUBSCRIBERS_LIMIT_REACHED,
];
$error = $this->mapper->getErrorForResult($apiResult, $this->subscribers);
expect($error)->isInstanceOf(MailerError::class);
expect($error->getOperation())->equals(MailerError::OPERATION_SUBSCRIBER_LIMIT_REACHED);
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
expect($error->getMessage())->stringContainsString('You have reached the subscriber limit of your plan.');
}
public function testGetErrorUnauthorizedEmail() { public function testGetErrorUnauthorizedEmail() {
$apiResult = [ $apiResult = [
'code' => API::RESPONSE_CODE_CAN_NOT_SEND, 'code' => API::RESPONSE_CODE_CAN_NOT_SEND,