/**
  * Prepares a correct exception to be thrown in response to a given error code from the services.
  * This method can't actually throw aan Exception because it will be called from inside a catch
  * block and the Exception would be occluded by a run-time error.
  *
  * @param Exception $exc the previous exception thrown
  * @param int $errorCode the error code for the exception
  * @param string $defaultMsg the error message for default error
  * @return PlenigoException the exception that needs to be thrown
  */
 private static function getException($exc, $errorCode, $defaultMsg)
 {
     $clazz = get_class();
     switch ($errorCode) {
         case ErrorCode::INVALID_SECRET_OR_COMPANY_ID:
             PlenigoManager::error($clazz, self::ERR_MSG_SECRET_COMPANY, $exc);
             return new PlenigoException(self::ERR_MSG_SECRET_COMPANY, $errorCode, $exc);
         case ErrorCode::INVALID_PARAMETERS:
             PlenigoManager::error($clazz, self::ERR_MSG_INVALID_PARAMS, $exc);
             return new PlenigoException(self::ERR_MSG_INVALID_PARAMS, $errorCode, $exc);
         default:
             PlenigoManager::error($clazz, $defaultMsg, $exc);
             return new PlenigoException($defaultMsg, $errorCode, $exc);
     }
 }
 /**
  * This method parses the metered view data from the user in the cookie.
  *
  * @return MeteredUserData The metered user data
  */
 private static function getMeteredUserData()
 {
     $cookieText = static::getCookieContents(PlenigoManager::PLENIGO_VIEW_COOKIE_NAME);
     if (is_null($cookieText) || trim($cookieText) == false) {
         $clazz = get_class();
         PlenigoManager::notice($clazz, "Plenigo view cookie not set!!");
         return null;
     }
     $data = EncryptionUtils::decryptWithAES(PlenigoManager::get()->getCompanyId(), $cookieText, self::METERED_INIT_VECTOR);
     if (is_null($data) || strstr($data, '|') === false) {
         $clazz = get_class();
         PlenigoManager::error($clazz, "Cookie data could not be decrypted.");
         return null;
     }
     return static::parseMeteredUserData($data);
 }
 /**
  * <p>Retrieves the product and suscriptions list for the current (logged in) 
  * user, then returns it as an associative array with this sintax</p>
  * <pre>
  * array (
  *   'singleProducts' => array (
  *     0 => array(
  *        'productId' => 'xxxx',
  *        'title' => 'prod title',
  *        'buyDate' => 'YYYY-MM-DD HH:mm:ss +0100',
  *     ),
  *   ),
  *   'subscriptions' => array (
  *     0 => array(
  *        'productId' => 'yyyyyy',
  *        'title' => 'Subscription title',
  *        'buyDate' => 'YYYY-MM-DD HH:mm:ss +0100',
  *        'endDate' => 'YYYY-MM-DD HH:mm:ss +0100',
  *     ),
  *   ),
  * )</pre>
  * 
  * @return array The associative array containing the bought products/subscriptions or an empty array
  * @throws PlenigoException If the compay ID and/or the Secret key is rejected
  */
 public static function getProductsBought()
 {
     $res = array();
     $customer = self::getCustomerInfo();
     $clazz = get_class();
     if (is_null($customer)) {
         PlenigoManager::notice($clazz, self::ERR_MSG_CUSTOMER);
         return $res;
     }
     PlenigoManager::notice($clazz, "customer is good=" . print_r($customer, true));
     $testModeText = PlenigoManager::get()->isTestMode() ? 'true' : 'false';
     $params = array(ApiParams::COMPANY_ID => PlenigoManager::get()->getCompanyId(), ApiParams::SECRET => PlenigoManager::get()->getSecret(), ApiParams::TEST_MODE => $testModeText);
     $url = str_ireplace(ApiParams::URL_USER_ID_TAG, $customer->getCustomerId(), ApiURLs::USER_PRODUCTS);
     $request = static::getRequest($url, false, $params);
     $userDataRequest = new static($request);
     try {
         $response = $userDataRequest->execute();
     } catch (PlenigoException $exc) {
         $errorCode = ErrorCode::getTranslation(ApiURLs::USER_PRODUCTS, $exc->getCode());
         if (empty($errorCode) || is_null($errorCode)) {
             $errorCode = $exc->getCode();
         }
         $clazz = get_class();
         PlenigoManager::error($clazz, self::ERR_MSG_USER_LIST, $exc);
         throw new PlenigoException(self::ERR_MSG_USER_LIST, $exc->getCode(), $exc);
     }
     if (!is_null($response)) {
         PlenigoManager::notice($clazz, "Product list is accessible=" . print_r($response, true));
         $res = get_object_vars($response);
     } else {
         PlenigoManager::notice($clazz, "Product list NOT accesible!");
     }
     return $res;
 }
 /**
  * Prepares the request to get the Access Token.
  *
  * @param string $type        The type of Token Grant Type to use.
  * @param string $code        The Access Code provided by the plenigo API.
  * @param string $redirectUri An optional redirect URI used to get the Access Code.
  * @param string $csrfToken   An optional CSRF Token to pass to the request.
  *
  * @return TokenData The Token Data {@link \plenigo\models\TokenData}.
  *
  * @throws \Exception on request error.
  */
 protected static function getToken($type, $code, $redirectUri = null, $csrfToken = null)
 {
     $map = array('grant_type' => $type, 'client_id' => PlenigoManager::get()->getCompanyId(), 'client_secret' => PlenigoManager::get()->getSecret());
     if ($type == TokenGrantType::REFRESH_TOKEN) {
         $map['refresh_token'] = $code;
     } else {
         $map['code'] = $code;
     }
     if ($redirectUri !== null) {
         $map['redirect_uri'] = $redirectUri;
     }
     if ($csrfToken !== null) {
         $map['state'] = $csrfToken;
     }
     $verify = new Verify($map);
     if ($type == TokenGrantType::REFRESH_TOKEN) {
         $accessUrl = ApiURLs::REFRESH_ACCESS_TOKEN;
     } else {
         $accessUrl = ApiURLs::GET_ACCESS_TOKEN;
     }
     $request = static::postRequest($accessUrl, true, $verify->getMap());
     $accessTokenRequest = new static($request, $csrfToken);
     try {
         $result = $accessTokenRequest->execute();
     } catch (Exception $exc) {
         $errorCode = ErrorCode::getTranslation(ApiURLs::GET_ACCESS_TOKEN, $exc->getCode());
         if (empty($errorCode) || is_null($errorCode)) {
             $errorCode = $exc->getCode();
         }
         $clazz = get_class();
         PlenigoManager::error($clazz, self::ERR_MSG_TOKEN, $exc);
         throw new PlenigoException(self::ERR_MSG_TOKEN, $errorCode, $exc);
     }
     return $result;
 }