/** * Returns the url for the account administration iframe. * If the passed account is null, then the url will point to the start page where a new account can be created. * * @param NostoAccountMetaDataIframeInterface $meta the iframe meta data. * @param NostoAccount|null $account the account to return the url for. * @param array $params additional parameters to add to the iframe url. * @return string the iframe url. * @throws NostoException if the url cannot be created. */ public function getUrl(NostoAccountMetaDataIframeInterface $meta, NostoAccountInterface $account = null, array $params = array()) { $defaultParameters = array('lang' => strtolower($meta->getLanguageIsoCode()), 'ps_version' => $meta->getVersionPlatform(), 'nt_version' => $meta->getVersionModule(), 'product_pu' => $meta->getPreviewUrlProduct(), 'category_pu' => $meta->getPreviewUrlCategory(), 'search_pu' => $meta->getPreviewUrlSearch(), 'cart_pu' => $meta->getPreviewUrlCart(), 'front_pu' => $meta->getPreviewUrlFront(), 'shop_lang' => strtolower($meta->getLanguageIsoCodeShop()), 'shop_name' => $meta->getShopName(), 'unique_id' => $meta->getUniqueId(), 'fname' => $meta->getFirstName(), 'lname' => $meta->getLastName(), 'email' => $meta->getEmail()); if ($account instanceof NostoAccountInterface) { $missingScopes = $account->getMissingTokens(); if (!empty($missingScopes)) { $defaultParameters['missing_scopes'] = implode(',', $missingScopes); } } $queryParams = http_build_query(array_merge($defaultParameters, $params)); if ($account !== null && $account->isConnectedToNosto()) { try { $url = $account->ssoLogin($meta) . '?' . $queryParams; } catch (NostoException $e) { // If the SSO fails, we show a "remove account" page to the user in order to // allow to remove Nosto and start over. // The only case when this should happen is when the api token for some // reason is invalid, which is the case when switching between environments. $url = NostoHttpRequest::buildUri($this->getBaseUrl() . self::IFRAME_URI_UNINSTALL . '?' . $queryParams, array('{platform}' => $meta->getPlatform())); } } else { $url = NostoHttpRequest::buildUri($this->getBaseUrl() . self::IFRAME_URI_INSTALL . '?' . $queryParams, array('{platform}' => $meta->getPlatform())); } return $url; }
/** * Sends the re-crawl API request to Nosto. * * @param NostoAccountInterface $account the account to re-crawl the product(s) for. * @param array $payload the request payload as an array that will be json encoded. * @return bool true on success. * @throws NostoException if the request fails or cannot be made. */ protected static function sendRequest(NostoAccountInterface $account, array $payload) { $token = $account->getApiToken('products'); if ($token === null) { throw new NostoException('Failed to send product re-crawl to Nosto. No `products` API token found for account.'); } $request = new NostoApiRequest(); $request->setPath(NostoApiRequest::PATH_PRODUCT_RE_CRAWL); $request->setContentType('application/json'); $request->setAuthBasic('', $token->getValue()); $response = $request->post(json_encode($payload)); if ($response->getCode() !== 200) { Nosto::throwHttpException('Failed to send product re-crawl to Nosto.', $request, $response); } return true; }
/** * Encrypts and returns the data. * * @param NostoAccountInterface $account the account to export the data for. * @param NostoExportCollectionInterface $collection the data collection to export. * @return string the encrypted data. */ public static function export(NostoAccountInterface $account, NostoExportCollectionInterface $collection) { $data = ''; // Use the first 16 chars of the SSO token as secret for encryption. $token = $account->getApiToken('sso'); if (!empty($token)) { $tokenValue = $token->getValue(); $secret = substr($tokenValue, 0, 16); if (!empty($secret)) { $iv = NostoCryptRandom::getRandomString(16); $cipher = new NostoCipher(); $cipher->setSecret($secret); $cipher->setIV($iv); $cipherText = $cipher->encrypt($collection->getJson()); // Prepend the IV to the cipher string so that nosto can parse and use it. // There is no security concern with sending the IV as plain text. $data = $iv . $cipherText; } } return $data; }
/** * Sends the order confirmation to Nosto. * * @param NostoOrderInterface $order the placed order model. * @param NostoAccountInterface $account the Nosto account for the shop where the order was placed. * @param null $customerId the Nosto customer ID of the user who placed the order. * @throws NostoException on failure. * @return true on success. */ public static function send(NostoOrderInterface $order, NostoAccountInterface $account, $customerId = null) { if (!empty($customerId)) { $path = NostoApiRequest::PATH_ORDER_TAGGING; $replaceParams = array('{m}' => $account->getName(), '{cid}' => $customerId); } else { $path = NostoApiRequest::PATH_UNMATCHED_ORDER_TAGGING; $replaceParams = array('{m}' => $account->getName()); } $request = new NostoApiRequest(); $request->setPath($path); $request->setContentType('application/json'); $request->setReplaceParams($replaceParams); $orderData = array('order_number' => $order->getOrderNumber(), 'order_status_code' => $order->getOrderStatus()->getCode(), 'order_status_label' => $order->getOrderStatus()->getLabel(), 'buyer' => array('first_name' => $order->getBuyerInfo()->getFirstName(), 'last_name' => $order->getBuyerInfo()->getLastName(), 'email' => $order->getBuyerInfo()->getEmail()), 'created_at' => Nosto::helper('date')->format($order->getCreatedDate()), 'payment_provider' => $order->getPaymentProvider(), 'external_order_ref' => $order->getExternalOrderRef(), 'purchased_items' => array()); foreach ($order->getPurchasedItems() as $item) { $orderData['purchased_items'][] = array('product_id' => $item->getProductId(), 'quantity' => (int) $item->getQuantity(), 'name' => $item->getName(), 'unit_price' => Nosto::helper('price')->format($item->getUnitPrice()), 'price_currency_code' => strtoupper($item->getCurrencyCode())); } $response = $request->post(json_encode($orderData)); if ($response->getCode() !== 200) { Nosto::throwHttpException('Failed to send order confirmation to Nosto.', $request, $response); } return true; }