/** * @dataProvider checkUpdateDataProvider * @param bool $callInbox * @param string $curlRequest */ public function testCheckUpdate($callInbox, $curlRequest) { $mockName = 'Test Product Name'; $mockVersion = '0.0.0'; $mockEdition = 'Test Edition'; $mockUrl = 'http://test-url'; $this->productMetadata->expects($this->once())->method('getName')->willReturn($mockName); $this->productMetadata->expects($this->once())->method('getVersion')->willReturn($mockVersion); $this->productMetadata->expects($this->once())->method('getEdition')->willReturn($mockEdition); $this->urlBuilder->expects($this->once())->method('getUrl')->with('*/*/*')->willReturn($mockUrl); $configValues = ['timeout' => 2, 'useragent' => $mockName . '/' . $mockVersion . ' (' . $mockEdition . ')', 'referer' => $mockUrl]; $lastUpdate = 0; $this->cacheManager->expects($this->once())->method('load')->will($this->returnValue($lastUpdate)); $this->curlFactory->expects($this->at(0))->method('create')->will($this->returnValue($this->curl)); $this->curl->expects($this->once())->method('setConfig')->with($configValues)->willReturnSelf(); $this->curl->expects($this->once())->method('read')->will($this->returnValue($curlRequest)); $this->backendConfig->expects($this->at(0))->method('getValue')->will($this->returnValue('1')); $this->backendConfig->expects($this->once())->method('isSetFlag')->will($this->returnValue(false)); $this->backendConfig->expects($this->at(1))->method('getValue')->will($this->returnValue('http://feed.magento.com')); $this->deploymentConfig->expects($this->once())->method('get')->with(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE)->will($this->returnValue('Sat, 6 Sep 2014 16:46:11 UTC')); if ($callInbox) { $this->inboxFactory->expects($this->once())->method('create')->will($this->returnValue($this->inboxModel)); $this->inboxModel->expects($this->once())->method('parse')->will($this->returnSelf()); } else { $this->inboxFactory->expects($this->never())->method('create'); $this->inboxModel->expects($this->never())->method('parse'); } $this->feed->checkUpdate(); }
/** * Post back to PayPal to check whether this request is a valid one * * @return void * @throws RemoteServiceUnavailableException * @throws \Exception */ protected function _postBack() { $httpAdapter = $this->_curlFactory->create(); $postbackQuery = http_build_query($this->getRequestData()) . '&cmd=_notify-validate'; $postbackUrl = $this->_config->getPayPalIpnUrl(); $this->_addDebugData('postback_to', $postbackUrl); $httpAdapter->setConfig(['verifypeer' => $this->_config->getValue('verifyPeer')]); $httpAdapter->write(\Zend_Http_Client::POST, $postbackUrl, '1.1', ['Connection: close'], $postbackQuery); try { $postbackResult = $httpAdapter->read(); } catch (\Exception $e) { $this->_addDebugData('http_error', ['error' => $e->getMessage(), 'code' => $e->getCode()]); throw $e; } /* * Handle errors on PayPal side. */ $responseCode = \Zend_Http_Response::extractCode($postbackResult); if (empty($postbackResult) || in_array($responseCode, ['500', '502', '503'])) { if (empty($postbackResult)) { $reason = 'Empty response.'; } else { $reason = 'Response code: ' . $responseCode . '.'; } $this->_debugData['exception'] = 'PayPal IPN postback failure. ' . $reason; throw new RemoteServiceUnavailableException(__($reason)); } $response = preg_split('/^\\r?$/m', $postbackResult, 2); $response = trim($response[1]); if ($response != 'VERIFIED') { $this->_addDebugData('postback', $postbackQuery); $this->_addDebugData('postback_result', $postbackResult); throw new \Exception('PayPal IPN postback failure. See system.log for details.'); } }
/** * If file is accessible return true or false * * @return bool */ private function _isFileAccessible() { $unsecureBaseURL = $this->_config->getValue(Store::XML_PATH_UNSECURE_BASE_URL, 'default'); /** @var $http \Magento\Framework\HTTP\Adapter\Curl */ $http = $this->_curlFactory->create(); $http->setConfig(['timeout' => $this->_verificationTimeOut]); $http->write(\Zend_Http_Client::POST, $unsecureBaseURL . $this->_filePath); $responseBody = $http->read(); $responseCode = \Zend_Http_Response::extractCode($responseBody); $http->close(); return $responseCode == 200; }
/** * Retrieve feed data as XML element * * @return \SimpleXMLElement */ public function getFeedData() { $curl = $this->curlFactory->create(); $curl->setConfig( [ 'timeout' => 2, 'useragent' => $this->productMetadata->getName() . '/' . $this->productMetadata->getVersion() . ' (' . $this->productMetadata->getEdition() . ')', 'referer' => $this->urlBuilder->getUrl('*/*/*') ] ); $curl->write(\Zend_Http_Client::GET, $this->getFeedUrl(), '1.0'); $data = $curl->read(); if ($data === false) { return false; } $data = preg_split('/^\r?$/m', $data, 2); $data = trim($data[1]); $curl->close(); try { $xml = new \SimpleXMLElement($data); } catch (\Exception $e) { return false; } return $xml; }
/** * @dataProvider checkUpdateDataProvider * @param bool $callInbox * @param string $curlRequest */ public function testCheckUpdate($callInbox, $curlRequest) { $lastUpdate = 1410121748; $this->curlFactory->expects($this->at(0))->method('create')->will($this->returnValue($this->curl)); $this->curl->expects($this->any())->method('read')->will($this->returnValue($curlRequest)); $this->backendConfig->expects($this->at(0))->method('getValue')->will($this->returnValue('1')); $this->backendConfig->expects($this->once())->method('isSetFlag')->will($this->returnValue(false)); $this->backendConfig->expects($this->at(1))->method('getValue')->will($this->returnValue('http://feed.magento.com')); $this->cacheManager->expects($this->once())->method('load')->will($this->returnValue($lastUpdate)); $this->deploymentConfig->expects($this->once())->method('get')->with('install/date')->will($this->returnValue('Sat, 6 Sep 2014 16:46:11 UTC')); if ($callInbox) { $this->inboxFactory->expects($this->once())->method('create')->will($this->returnValue($this->inboxModel)); $this->inboxModel->expects($this->once())->method('parse')->will($this->returnSelf()); } else { $this->inboxFactory->expects($this->never())->method('create'); $this->inboxModel->expects($this->never())->method('parse'); } $this->feed->checkUpdate(); }
/** * Send http request * * @param string $endpoint * @param array $params * @return array */ public function sendRequest($endpoint, $params) { $curl = $this->curlFactory->create(); $config = ['timeout' => 10]; $curl->setConfig($config); $curl->write(\Zend_Http_Client::POST, $endpoint, '1.1', [], http_build_query($params, '', '&')); $response = $curl->read(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); $response = @unserialize($response); if (is_array($response)) { return $response; } return []; }
/** * Post back to PayPal to check whether this request is a valid one * * @return void * @throws \Exception */ protected function _postBack() { $httpAdapter = $this->_curlFactory->create(); $postbackQuery = http_build_query($this->getRequestData()) . '&cmd=_notify-validate'; $postbackUrl = $this->_config->getPaypalUrl(); $this->_addDebugData('postback_to', $postbackUrl); $httpAdapter->setConfig(array('verifypeer' => $this->_config->getConfigValue('verifyPeer'))); $httpAdapter->write(\Zend_Http_Client::POST, $postbackUrl, '1.1', array('Connection: close'), $postbackQuery); try { $postbackResult = $httpAdapter->read(); } catch (\Exception $e) { $this->_addDebugData('http_error', array('error' => $e->getMessage(), 'code' => $e->getCode())); throw $e; } $response = preg_split('/^\\r?$/m', $postbackResult, 2); $response = trim($response[1]); if ($response != 'VERIFIED') { $this->_addDebugData('postback', $postbackQuery); $this->_addDebugData('postback_result', $postbackResult); throw new \Exception('PayPal IPN postback failure. See ' . self::DEFAULT_LOG_FILE . ' for details.'); } }
/** * Do the API call * * @param string $methodName * @param array $request * @return array * @throws \Magento\Framework\Exception\LocalizedException|\Exception * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function call($methodName, array $request) { $request = $this->_addMethodToRequest($methodName, $request); $eachCallRequest = $this->_prepareEachCallRequest($methodName); if ($this->getUseCertAuthentication()) { $key = array_search('SIGNATURE', $eachCallRequest); if ($key) { unset($eachCallRequest[$key]); } } $request = $this->_exportToRequest($eachCallRequest, $request); $debugData = ['url' => $this->getApiEndpoint(), $methodName => $request]; try { $http = $this->_curlFactory->create(); $config = ['timeout' => 60, 'verifypeer' => $this->_config->getValue('verifyPeer')]; if ($this->getUseProxy()) { $config['proxy'] = $this->getProxyHost() . ':' . $this->getProxyPort(); } if ($this->getUseCertAuthentication()) { $config['ssl_cert'] = $this->getApiCertificate(); } $http->setConfig($config); $http->write(\Zend_Http_Client::POST, $this->getApiEndpoint(), '1.1', $this->_headers, $this->_buildQuery($request)); $response = $http->read(); } catch (\Exception $e) { $debugData['http_error'] = ['error' => $e->getMessage(), 'code' => $e->getCode()]; $this->_debug($debugData); throw $e; } $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); $response = $this->_deformatNVP($response); $debugData['response'] = $response; $this->_debug($debugData); $response = $this->_postProcessResponse($response); // handle transport error if ($http->getErrno()) { $this->_logger->critical(new \Exception(sprintf('PayPal NVP CURL connection error #%s: %s', $http->getErrno(), $http->getError()))); $http->close(); throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t contact the PayPal gateway.')); } // cUrl resource must be closed after checking it for errors $http->close(); if (!$this->_validateResponse($methodName, $response)) { $this->_logger->critical(new \Exception(__("PayPal response hasn't required fields."))); throw new \Magento\Framework\Exception\LocalizedException(__('Something went wrong while processing your order.')); } $this->_callErrors = []; if ($this->_isCallSuccessful($response)) { if ($this->_rawResponseNeeded) { $this->setRawSuccessResponseData($response); } return $response; } $this->_handleCallErrors($response); return $response; }