Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: Enhanced Error Handling for License Server API Calls #41

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions src/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Appsero;

use WP_Error;

/**
* Appsero License Checker
*
Expand Down Expand Up @@ -144,7 +146,7 @@ public function deactivate( $license_key ) {
/**
* Send common request
*
* @return array
* @return array|WP_Error
*/
protected function send_request( $license_key, $route ) {
$params = [
Expand All @@ -156,19 +158,13 @@ protected function send_request( $license_key, $route ) {
$response = $this->client->send_request( $params, $route, true );

if ( is_wp_error( $response ) ) {
return [
'success' => false,
'error' => $response->get_error_message(),
];
return $response;
}

$response = json_decode( wp_remote_retrieve_body( $response ), true );

if ( empty( $response ) || isset( $response['exception'] ) ) {
return [
'success' => false,
'error' => $this->client->__trans( 'Unknown error occurred, Please try again.' ),
];
return new WP_Error( 'invalid-json-error', $this->client->__trans( 'Unknown error occurred, Please try again.' ) );
}

if ( isset( $response['errors'] ) && isset( $response['errors']['license_key'] ) ) {
Expand All @@ -187,6 +183,15 @@ protected function send_request( $license_key, $route ) {
public function refresh_license_api() {
$this->check_license_status();

if ( ! empty( $this->error ) ) {
wp_send_json_error(
[
'message' => $this->error,
],
400
);
}

wp_send_json_success(
[
'message' => 'License refreshed successfully.',
Expand Down Expand Up @@ -353,7 +358,9 @@ public function check_license_status() {
if ( isset( $license['key'] ) && ! empty( $license['key'] ) ) {
$response = $this->check( $license['key'] );

if ( isset( $response['success'] ) && $response['success'] ) {
if ( is_wp_error( $response ) ) {
$this->error = $response->get_error_message();
} elseif ( isset( $response['success'] ) && $response['success'] ) {
$license['status'] = 'activate';
$license['remaining'] = $response['remaining'];
$license['activation_limit'] = $response['activation_limit'];
Expand Down Expand Up @@ -633,7 +640,11 @@ private function active_client_license( $license_key ) {

$response = $this->activate( $license_key );

if ( ! $response['success'] ) {
if ( is_wp_error( $response ) ) {
$this->error = $response->get_error_message();

return;
} elseif ( ! $response['success'] ) {
$this->error = $response['error'] ? $response['error'] : $this->client->__trans( 'Unknown error occurred.' );

return;
Expand Down Expand Up @@ -668,6 +679,15 @@ private function deactive_client_license() {
}

$response = $this->deactivate( $license['key'] );
if ( is_wp_error( $response ) ) {
$this->error = $response->get_error_message();

return;
} elseif ( ! $response['success'] ) {
$this->error = $response['error'] ? $response['error'] : $this->client->__trans( 'Unknown error occurred.' );

return;
}

$data = [
'key' => '',
Expand All @@ -676,12 +696,6 @@ private function deactive_client_license() {

update_option( $this->option_key, $data, false );

if ( ! $response['success'] ) {
$this->error = $response['error'] ? $response['error'] : $this->client->__trans( 'Unknown error occurred.' );

return;
}

$this->success = $this->client->__trans( 'License deactivated successfully.' );
}

Expand All @@ -699,7 +713,9 @@ private function refresh_client_license() {

$this->check_license_status();

$this->success = $this->client->__trans( 'License refreshed successfully.' );
if ( empty( $this->error ) ) {
$this->success = $this->client->__trans( 'License refreshed successfully.' );
}
}

/**
Expand Down
Loading