/**
  * 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;
 }
Exemple #3
0
    /**
     * 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;
    }
Exemple #4
0
 /**
  * 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.');
     }
 }
Exemple #6
0
 /**
  * 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;
 }