public function process_authorization()
 {
     $reconnect = $_REQUEST['action'] == 'ecwid_oauth_reconnect';
     if (isset($_REQUEST['error']) || !isset($_REQUEST['code'])) {
         if ($reconnect) {
             $this->update_state(array('mode' => self::MODE_RECONNECT, 'error' => 'cancelled'));
         } else {
             $this->update_state(array('mode' => self::MODE_CONNECT, 'error' => 'cancelled'));
         }
         wp_redirect('admin.php?page=ecwid&connection_error' . ($reconnect ? '&reconnect' : ''));
         exit;
     }
     $base_admin_url = 'admin-post.php?action=ecwid_oauth' . ($reconnect ? '_reconnect' : '');
     $params['code'] = $_REQUEST['code'];
     $params['client_id'] = self::OAUTH_CLIENT_ID;
     $params['client_secret'] = self::OAUTH_CLIENT_SECRET;
     $params['redirect_uri'] = admin_url($base_admin_url);
     $params['grant_type'] = 'authorization_code';
     $return = EcwidPlatform::http_post_request('https://my.ecwid.com/api/oauth/token', $params);
     if (is_array($return) && isset($return['body'])) {
         $result = json_decode($return['body']);
     }
     if (!is_array($return) || !isset($result->store_id) || !isset($result->scope) || !isset($result->access_token) || $result->token_type != 'Bearer') {
         ecwid_log_error(var_export($return, true));
         return $this->trigger_auth_error($reconnect ? 'reconnect' : 'default');
     }
     Ecwid_Kissmetrics::record($reconnect ? 'accountReconnected' : 'accountConnected');
     update_option('ecwid_store_id', $result->store_id);
     update_option('ecwid_oauth_scope', $result->scope);
     $this->_init_crypt();
     $this->_save_token($result->access_token);
     // Reset "Create store cookie" set previously to display the landing page
     //in "Connect" mode rather than "Create" mode
     setcookie('ecwid_create_store_clicked', null, strtotime('-1 day'), ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
     if (isset($this->state->return_url) && !empty($this->state->return_url)) {
         wp_redirect(admin_url($this->state->return_url));
     } else {
         $url = '';
         if ($reconnect) {
             $url = 'admin.php?page=ecwid&setting-updated=true';
         } else {
             $url = 'admin.php?page=ecwid';
         }
         wp_redirect($url);
     }
     exit;
 }
 public function process_authorization()
 {
     if (isset($_REQUEST['error']) || !isset($_REQUEST['code'])) {
         return $this->trigger_auth_error();
     }
     $params['code'] = $_REQUEST['code'];
     $params['client_id'] = get_option('ecwid_oauth_client_id');
     $params['client_secret'] = get_option('ecwid_oauth_client_secret');
     $params['redirect_uri'] = admin_url('admin-post.php?action=ecwid_oauth');
     $params['grant_type'] = 'authorization_code';
     $return = wp_remote_post('https://my.ecwid.com/api/oauth/token', array('body' => $params));
     if (is_array($return) && isset($return['body'])) {
         $result = json_decode($return['body']);
     }
     if (!is_array($return) || !isset($result->store_id) || !isset($result->scope) || !isset($result->access_token) || $result->token_type != 'Bearer') {
         ecwid_log_error(var_export($return, true));
         return $this->trigger_auth_error();
     }
     update_option('ecwid_store_id', $result->store_id);
     update_option('ecwid_oauth_token', $result->access_token);
     setcookie('ecwid_create_store_clicked', null, strtotime('-1 day'), ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
     wp_redirect('admin.php?page=ecwid&settings-updated=true');
 }