/** * @param string $currencyFrom * @param string $currencyTo * @param int $retry * @return float|null */ protected function _convert($currencyFrom, $currencyTo, $retry = 0) { $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL); $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url); try { sleep($this->_scopeConfig->getValue('currency/google/delay', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)); $response = $this->_httpClient->setUri($url)->setConfig(['timeout' => $this->_scopeConfig->getValue('currency/google/timeout', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)])->request('GET')->getBody(); $data = explode('bld>', $response); if (empty($data[1])) { $this->_messages[] = __('We can\'t retrieve a rate from %1.', $url); return null; } $data = explode($currencyTo, $data[1]); $rate = null; if (empty($data[0])) { $this->_messages[] = __('We can\'t retrieve a rate from %1.', $url); return null; } else { $rate = $data[0]; } return (double) $rate; } catch (\Exception $e) { if ($retry == 0) { $this->_convert($currencyFrom, $currencyTo, 1); } else { $this->_messages[] = __('We can\'t retrieve a rate from %1.', $url); } } }
/** * Returns HTTP request to events url * * @return \Magento\Framework\HTTP\ZendClient */ protected function getRequest() { if (!isset($this->request)) { $this->request = $this->clientFactory->create(); $this->request->setUri($this->getEventsUrl()); $insertKey = $this->config->getInsightsInsertKey(); $this->request->setMethod(ZendClient::POST); $this->request->setHeaders(['X-Insert-Key' => $insertKey, 'Content-Type' => 'application/json']); } return $this->request; }
/** * {@inheritdoc} */ public function postToConsumer($consumerId, $endpointUrl) { try { $consumer = $this->_consumerFactory->create()->load($consumerId); if (!$consumer->getId()) { throw new \Magento\Framework\Oauth\Exception(__('A consumer with ID %1 does not exist', $consumerId), OauthInterface::ERR_PARAMETER_REJECTED); } $consumerData = $consumer->getData(); $verifier = $this->_tokenFactory->create()->createVerifierToken($consumerId); $storeBaseUrl = $this->_storeManager->getStore()->getBaseUrl(); $this->_httpClient->setUri($endpointUrl); $this->_httpClient->setParameterPost(array('oauth_consumer_key' => $consumerData['key'], 'oauth_consumer_secret' => $consumerData['secret'], 'store_base_url' => $storeBaseUrl, 'oauth_verifier' => $verifier->getVerifier())); $maxredirects = $this->_dataHelper->getConsumerPostMaxRedirects(); $timeout = $this->_dataHelper->getConsumerPostTimeout(); $this->_httpClient->setConfig(array('maxredirects' => $maxredirects, 'timeout' => $timeout)); $this->_httpClient->request(\Magento\Framework\HTTP\ZendClient::POST); return $verifier->getVerifier(); } catch (\Magento\Framework\Model\Exception $exception) { throw $exception; } catch (\Magento\Framework\Oauth\Exception $exception) { throw $exception; } catch (\Exception $exception) { $this->_logger->logException($exception); throw new \Magento\Framework\Oauth\Exception('Unable to post data to consumer due to an unexpected error'); } }
/** * @param string $currencyFrom * @param string $currencyTo * @param int $retry * @return float|null */ protected function _convert($currencyFrom, $currencyTo, $retry = 0) { $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, $this->_url); $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url); try { $response = $this->_httpClient->setUri($url)->setConfig(array('timeout' => $this->_scopeConfig->getValue('currency/webservicex/timeout', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)))->request('GET')->getBody(); $xml = simplexml_load_string($response, null, LIBXML_NOERROR); if (!$xml) { $this->_messages[] = __('We can\'t retrieve a rate from %1.', $url); return null; } return (double) $xml; } catch (\Exception $e) { if ($retry == 0) { $this->_convert($currencyFrom, $currencyTo, 1); } else { $this->_messages[] = __('We can\'t retrieve a rate from %1.', $url); } } }