/** * Returns a response to a POST RestClient request to a specific * end-point on the plenigo REST API. * * @param string $endPoint The REST end-point to access. * @param bool $oauth TRUE if the needed request is going to the OAuth API. * @param array $params Optional params to pass to the request. * * @return the request result. */ protected static function postRequest($endPoint, $oauth = false, array $params = array()) { if ($oauth) { $clazz = get_class(); PlenigoManager::notice($clazz, "OAUTH POST REQUEST"); $url = PlenigoManager::get()->getUrlOAuth() . $endPoint; } else { $url = PlenigoManager::get()->getUrl() . $endPoint; } return RestClient::post($url, $params); }
/** * This method builds the encoded data from the Checkout Object. * * @param string $dataToEncode the string data to encode. * * @return string The encoded data */ private function buildEncodedData($dataToEncode) { $secret = PlenigoManager::get()->getSecret(); return EncryptionUtils::encryptWithAES($secret, $dataToEncode); }
/** * Create an array with the Company ID, the Secret, a given page size and an optional Last ID for product * or category listings. * * @param int $pageSize The number of items on a single page (min:10, max:100) * @param string $lastID Optional. A string containing the last ID of the current page * @return array A key=>value array to convert to queryString for the URL */ private static function configureListParams($pageSize = 10, $lastID = null) { $size = max(min($pageSize, 100), 10); return array(ApiParams::COMPANY_ID => PlenigoManager::get()->getCompanyId(), ApiParams::SECRET => PlenigoManager::get()->getSecret(), 'size' => $size, 'lastId' => !is_null($lastID) ? $lastID : ''); }
/** * <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; }
/** * 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); }
/** * Return the company-ID * @return string Company-ID */ public static function getCompanyID() { return \plenigo\PlenigoManager::get()->getCompanyId(); }
/** * 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; }