/** * Builds the API request and returns it. * * @param NostoAccount $account the Nosto account object. * @param string|null $customerId the Nosto customer ID of the user who placed the order. * @return NostoApiRequest the request object. */ protected function initApiRequest(NostoAccount $account, $customerId) { $request = new NostoApiRequest(); $request->setContentType('application/json'); if (!empty($customerId)) { $request->setPath(NostoApiRequest::PATH_ORDER_TAGGING); $request->setReplaceParams(array('{m}' => $account->getName(), '{cid}' => $customerId)); } else { $request->setPath(NostoApiRequest::PATH_UNMATCHED_ORDER_TAGGING); $request->setReplaceParams(array('{m}' => $account->getName())); } return $request; }
/** * Saves a Nosto account to PS config. * Also handles any attached API tokens. * * @param NostoAccount $account the account to save. * @param null|int $id_lang the ID of the language to set the account name for. * @return bool true if the save was successful, false otherwise. */ public function save(NostoAccount $account, $id_lang) { /** @var NostoTaggingHelperConfig $helper_config */ $helper_config = Nosto::helper('nosto_tagging/config'); $success = $helper_config->saveAccountName($account->getName(), $id_lang); if ($success) { foreach ($account->getTokens() as $token) { $success = $success && $helper_config->saveToken($token->getName(), $token->getValue(), $id_lang); } } return $success; }
/** * Saves the account and the associated api tokens for the store view scope. * * @param NostoAccount $account the account to save. * @param Mage_Core_Model_Store|null $store the store view to save it for. * * @return bool true on success, false otherwise. */ public function save(NostoAccount $account, Mage_Core_Model_Store $store = null) { if ($store === null) { $store = Mage::app()->getStore(); } if ((int) $store->getId() < 1) { return false; } /** @var Mage_Core_Model_Config $config */ $config = Mage::getModel('core/config'); $config->saveConfig(self::XML_PATH_ACCOUNT, $account->getName(), 'stores', $store->getId()); $tokens = array(); foreach ($account->getTokens() as $token) { $tokens[$token->getName()] = $token->getValue(); } $config->saveConfig(self::XML_PATH_TOKENS, json_encode($tokens), 'stores', $store->getId()); Mage::app()->getCacheInstance()->cleanType('config'); return true; }
/** * Saves the account and the associated api tokens for the store view scope. * * @param NostoAccount $account the account to save. * @param Mage_Core_Model_Store|null $store the store view to save it for. * * @return bool true on success, false otherwise. */ public function save(NostoAccount $account, Mage_Core_Model_Store $store = null) { if ($store === null) { $store = Mage::app()->getStore(); } if ((int) $store->getId() < 1) { return false; } /** @var Mage_Core_Model_Config $config */ $config = Mage::getModel('core/config'); $config->saveConfig(self::XML_PATH_ACCOUNT, $account->getName(), 'stores', $store->getId()); $tokens = array(); foreach ($account->getTokens() as $token) { $tokens[$token->getName()] = $token->getValue(); } $config->saveConfig(self::XML_PATH_TOKENS, json_encode($tokens), 'stores', $store->getId()); /** @var Nosto_Tagging_Helper_Cache $helper */ $helper = Mage::helper('nosto_tagging/cache'); $helper->flushCache(); return true; }
/** * Checks if this account is the same as the given account. * They are considered equal if their name property match. The tokens are not relevant in the comparison, * as they are not required by the account upon creation. * * @param NostoAccount $account the account to check. * @return bool true if equals. */ public function equals(NostoAccount $account) { return $account->getName() === $this->getName(); }
/** * Signs the user in to Nosto via SSO. * * Requires that the account has a valid sso token associated with it. * * @param NostoAccount $account the account to sign into. * @param NostoAccountMetaSingleSignOnInterface $meta the SSO meta-data. * @return string a secure login url. * * @throws NostoException on failure. */ public function sso(NostoAccount $account, NostoAccountMetaSingleSignOnInterface $meta) { $token = $account->getApiToken(NostoApiToken::API_SSO); if (is_null($token)) { throw new NostoException(sprintf('No `%s` API token found for account "%s".', NostoApiToken::API_SSO, $account->getName())); } $request = new NostoHttpRequest(); $request->setUrl(NostoHttpRequest::$baseUrl . NostoHttpRequest::PATH_SSO_AUTH); $request->setReplaceParams(array('{platform}' => $meta->getPlatform(), '{email}' => $meta->getEmail())); $request->setContentType('application/x-www-form-urlencoded'); $request->setAuthBasic('', $token->getValue()); $response = $request->post(http_build_query(array('fname' => $meta->getFirstName(), 'lname' => $meta->getLastName()))); if ($response->getCode() !== 200) { throw Nosto::createHttpException('Failed to sign into Nosto using Single Sign On.', $request, $response); } $result = $response->getJsonResult(); if (empty($result->login_url)) { throw new NostoException('No "login_url" returned when logging in employee to Nosto'); } return $result->login_url; }
/** * Sends a currency exchange rate update request to Nosto via the API. * * Checks if multi currency is enabled for the store before attempting to * send the exchange rates. * * @param NostoAccount $account the account for which tp update the rates. * @param Mage_Core_Model_Store $store the store which rates are to be updated. * * @return bool */ public function updateCurrencyExchangeRates(NostoAccount $account, Mage_Core_Model_Store $store) { /** @var Nosto_Tagging_Helper_Data $helper */ $helper = Mage::helper('nosto_tagging'); if (!$helper->isMultiCurrencyMethodExchangeRate($store)) { Mage::log(sprintf('Currency update called without exchange method enabled for account %s', $account->getName()), Zend_Log::DEBUG, Nosto_Tagging_Model_Base::LOG_FILE_NAME); return false; } $currencyCodes = $store->getAvailableCurrencyCodes(true); $baseCurrencyCode = $store->getBaseCurrencyCode(); /** @var Nosto_Tagging_Helper_Currency $helper */ $helper = Mage::helper('nosto_tagging/currency'); try { /** @var Nosto_Tagging_Model_Collection_Rates $collection */ $collection = $helper->getExchangeRateCollection($baseCurrencyCode, $currencyCodes); $service = new NostoOperationExchangeRate($account, $collection); return $service->update(); } catch (NostoException $e) { Mage::log("\n" . $e, Zend_Log::ERR, Nosto_Tagging_Model_Base::LOG_FILE_NAME); } return false; }
/** * Saves the account and the associated api tokens for the store. * * @param \NostoAccount $account the account to save. * @param Store $store the store. * * @return bool true on success, false otherwise. */ public function saveAccount(\NostoAccount $account, Store $store) { if ((int) $store->getId() < 1) { return false; } $tokens = array(); foreach ($account->getTokens() as $token) { $tokens[$token->getName()] = $token->getValue(); } $this->_config->save(self::XML_PATH_ACCOUNT, $account->getName(), ScopeInterface::SCOPE_STORES, $store->getId()); $this->_config->save(self::XML_PATH_TOKENS, json_encode($tokens), ScopeInterface::SCOPE_STORES, $store->getId()); $store->resetConfig(); return true; }