| @@ -21,9 +21,12 @@ | ||
| 21 | 21 | * @return void |
| 22 | 22 | */ |
| 23 | 23 | public function authorize() { |
| 24 | 24 | |
| 25 | - if ( ! empty( \Depicter::cache('base')->get( 'is_client_registered' ) ) ) { | |
| 25 | + if ( | |
| 26 | + \Depicter::cache('base')->get( 'is_client_registered' ) && | |
| 27 | + \Depicter::auth()->getClientKey() | |
| 28 | + ) { | |
| 26 | 29 | return; |
| 27 | 30 | } |
| 28 | 31 | |
| 29 | 32 | $params = [ |
| @@ -34,30 +37,23 @@ | ||
| 34 | 37 | ] |
| 35 | 38 | ]; |
| 36 | 39 | |
| 37 | 40 | try{ |
| 38 | - $endpoint = \Depicter::remote()->endpoint() . 'v1/client/register'; | |
| 39 | - $response = \Depicter::remote()->post( $endpoint, $params ); | |
| 41 | + $response = \Depicter::remote()->post( 'v1/client/register', $params ); | |
| 40 | 42 | |
| 41 | - if ( $response->getStatusCode() == 200 || $response->getStatusCode() == 204 ) { | |
| 42 | - $payload = JSON::decode( $response->getBody(), true ); | |
| 43 | + $payload = JSON::decode( $response->getBody(), true ); | |
| 43 | 44 | |
| 44 | - if ( ! empty( $payload['client_key'] ) ) { | |
| 45 | - \Depicter::options()->set( 'client_key', $payload['client_key'] ); | |
| 46 | - } | |
| 45 | + if ( ! empty( $payload['client_key'] ) ) { | |
| 46 | + \Depicter::options()->set( 'client_key', $payload['client_key'] ); | |
| 47 | + } elseif ( ! empty( $payload['errors'] ) ) { | |
| 48 | + \Depicter::options()->set('register_error_message', $payload['errors'] ); | |
| 47 | 49 | } |
| 48 | 50 | |
| 49 | - } catch ( GuzzleException $e ) { | |
| 50 | - try{ | |
| 51 | - $this->reportError([ | |
| 52 | - 'issueType' => 'depicter-wp-api', | |
| 53 | - 'issueRelatesTo' => 'client-registration', | |
| 54 | - 'userDescription' => $e->getMessage(). ' | ' . $params['headers']['user-agent'] | |
| 55 | - ]); | |
| 56 | - } catch( GuzzleException $e ){} | |
| 51 | + \Depicter::cache('base')->set( 'is_client_registered', true, DAY_IN_SECONDS ); | |
| 52 | + | |
| 53 | + } catch ( GuzzleException|\Exception $e ) { | |
| 54 | + \Depicter::options()->set('register_error_message', $e->getMessage() ); | |
| 57 | 55 | } |
| 58 | - | |
| 59 | - \Depicter::cache('base')->set( 'is_client_registered', true, DAY_IN_SECONDS ); | |
| 60 | 56 | } |
| 61 | 57 | |
| 62 | 58 | /** |
| 63 | 59 | * Send user feedback |
| @@ -100,7 +96,44 @@ | ||
| 100 | 96 | $response = \Depicter::remote()->post( 'v1/subscriber/store', [ |
| 101 | 97 | 'form_params' => $bodyParams |
| 102 | 98 | ]); |
| 103 | 99 | return $response->getStatusCode() == 200; |
| 100 | + } | |
| 101 | + | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * Validate user activation status | |
| 105 | + */ | |
| 106 | + public function validateActivation() { | |
| 107 | + try { | |
| 108 | + $response = \Depicter::remote()->post( 'v1/client/validate/activation' ); | |
| 109 | + $info = JSON::decode( $response->getBody(), true); | |
| 110 | + | |
| 111 | + if( is_null( $info['success'] ) ){ | |
| 112 | + \Depicter::options()->set('activation_error_message', '' ); | |
| 113 | + | |
| 114 | + } elseif ( ! empty( $info['data'] ) ) { | |
| 115 | + \Depicter::options()->set('subscription_status', $info['data']['status'] ); | |
| 116 | + \Depicter::options()->set('subscription_expires_at', $info['data']['expires_at'] ); | |
| 117 | + \Depicter::options()->set('user_tier', $info['data']['user_tier'] ); | |
| 118 | + \Depicter::options()->set('activation_error_message', '' ); | |
| 119 | + | |
| 120 | + if ( $info['data']['status'] == 'active' ) { | |
| 121 | + return true; | |
| 122 | + } | |
| 123 | + } elseif( $info['success'] == 1 ){ | |
| 124 | + return true; | |
| 125 | + } | |
| 126 | + | |
| 127 | + if( ! empty( $info['log'] ) ) { | |
| 128 | + \Depicter::options()->set('activation_log_message', $info['log'] ); | |
| 129 | + } | |
| 130 | + | |
| 131 | + } catch ( GuzzleException $exception ) { | |
| 132 | + \Depicter::options()->set('activation_error_message' , $exception->getMessage() ); | |
| 133 | + \Depicter::options()->set('connection_error_message' , $exception->getMessage() ); | |
| 134 | + } | |
| 135 | + | |
| 136 | + return false; | |
| 104 | 137 | } |
| 105 | 138 | |
| 106 | 139 | } |