/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $code = $this->option('code');
     $redirectUri = $this->option('redirect-uri');
     $apiConfig = $this->getApiConfig();
     $accountConfig = $this->getAccountConfig($code, $redirectUri);
     $bazaarApi = new BazaarApi();
     $bazaarApi->setApiConfig($apiConfig);
     $bazaarApi->setAccountConfig($accountConfig);
     $this->line(sprintf($this->generateCodeUri, $accountConfig->getRedirectUri(), $accountConfig->getClientId()));
     try {
         $fetchedRefreshToken = $bazaarApi->fetchRefreshToken(new AuthorizationRequest());
         $json = $fetchedRefreshToken->getResponseJson();
         $headers = array_keys($json);
         $rows = [$json];
         $this->info("Refresh Token: " . $fetchedRefreshToken->getRefreshToken());
         $this->comment('Save refresh token in your config file');
         $this->table($headers, $rows);
     } catch (NetworkErrorException $e) {
         $this->error('Error: ' . $e->getClientException()->getRequest()->getUrl());
         $this->error('Response Status: ' . $e->getClientException()->getResponse()->getStatusCode());
         if ($e->getClientException()->getResponse()->getStatusCode() == 401) {
             $this->comment('Your credentials is invalid or you should generate new code from this url: ');
             $this->line(sprintf($this->generateCodeUri, $accountConfig->getRedirectUri(), $accountConfig->getClientId()));
         }
     } catch (\Exception $e) {
         $this->error("Unknown Error: " . $e->getMessage());
     }
 }
 /**
  * cancel a subscription
  *
  * @param CancelSubscriptionRequest|array|string $packageNameOrRequestOrArray
  * @param string|null $subscriptionId
  * @param string|null $purchaseToken
  * @return \Nikapps\BazaarApiPhp\Models\Responses\CancelSubscription
  * @throws ExpiredAccessTokenException
  * @throws \Nikapps\BazaarApiPhp\Exceptions\InvalidPackageNameException
  * @throws \Nikapps\BazaarApiPhp\Exceptions\InvalidTokenException
  * @throws \Nikapps\BazaarApiPhp\Exceptions\NetworkErrorException
  * @throws \Nikapps\BazaarApiPhp\Exceptions\NotFoundException
  * @throws \Nikapps\BazaarApiPhp\Exceptions\InvalidJsonException
  */
 public function cancelSubscription($packageNameOrRequestOrArray, $subscriptionId = null, $purchaseToken = null)
 {
     $cancelSubscriptionRequest = $this->makeCancelSubscriptionRequest($packageNameOrRequestOrArray, $subscriptionId, $purchaseToken);
     try {
         return $this->bazaarApi->cancelSubscription($cancelSubscriptionRequest);
     } catch (ExpiredAccessTokenException $e) {
         if (!$this->config->get('bazaar-api-laravel::api.auto_refresh_token')) {
             //auto refresh token is disabled
             throw new ExpiredAccessTokenException();
         } else {
             //refresh access token
             $this->refreshToken();
             //retry request
             return $this->cancelSubscription($cancelSubscriptionRequest);
         }
     }
 }