Esempio n. 1
0
 /**
  * @param PayPalHttpConfig $httpConfig
  * @param string                    $request
  * @param mixed                     $options
  * @return mixed|void
  * @throws PayPalConfigurationException
  * @throws PayPalInvalidCredentialException
  * @throws PayPalMissingCredentialException
  */
 public function handle($httpConfig, $request, $options)
 {
     $credential = $this->apiContext->getCredential();
     $config = $this->apiContext->getConfig();
     if ($credential == null) {
         // Try picking credentials from the config file
         $credMgr = PayPalCredentialManager::getInstance($config);
         $credValues = $credMgr->getCredentialObject();
         if (!is_array($credValues)) {
             throw new PayPalMissingCredentialException("Empty or invalid credentials passed");
         }
         $credential = new OAuthTokenCredential($credValues['clientId'], $credValues['clientSecret']);
     }
     if ($credential == null || !$credential instanceof OAuthTokenCredential) {
         throw new PayPalInvalidCredentialException("Invalid credentials passed");
     }
     $httpConfig->setUrl(rtrim(trim($this->_getEndpoint($config)), '/') . (isset($options['path']) ? $options['path'] : ''));
     if (!array_key_exists("User-Agent", $httpConfig->getHeaders())) {
         $httpConfig->addHeader("User-Agent", PayPalUserAgent::getValue(PayPalConstants::SDK_NAME, PayPalConstants::SDK_VERSION));
     }
     if (!is_null($credential) && $credential instanceof OAuthTokenCredential && is_null($httpConfig->getHeader('Authorization'))) {
         $httpConfig->addHeader('Authorization', "Bearer " . $credential->getAccessToken($config), false);
     }
     if ($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') {
         $httpConfig->addHeader('PayPal-Request-Id', $this->apiContext->getRequestId());
     }
     // Add any additional Headers that they may have provided
     $headers = $this->apiContext->getRequestHeaders();
     foreach ($headers as $key => $value) {
         $httpConfig->addHeader($key, $value);
     }
 }
Esempio n. 2
0
 /**
  * @param PayPalHttpConfig $httpConfig
  * @param string                    $request
  * @param mixed                     $options
  * @return mixed|void
  * @throws PayPalConfigurationException
  * @throws PayPalInvalidCredentialException
  * @throws PayPalMissingCredentialException
  */
 public function handle($httpConfig, $request, $options)
 {
     $config = $this->apiContext->getConfig();
     $httpConfig->setUrl(rtrim(trim($this->_getEndpoint($config)), '/') . (isset($options['path']) ? $options['path'] : ''));
     $headers = array("User-Agent" => PayPalUserAgent::getValue(PayPalConstants::SDK_NAME, PayPalConstants::SDK_VERSION), "Authorization" => "Basic " . base64_encode($options['clientId'] . ":" . $options['clientSecret']), "Accept" => "*/*");
     $httpConfig->setHeaders($headers);
     // Add any additional Headers that they may have provided
     $headers = $this->apiContext->getRequestHeaders();
     foreach ($headers as $key => $value) {
         $httpConfig->addHeader($key, $value);
     }
 }
Esempio n. 3
0
 /**
  * @param array  $handlers Array of handlers
  * @param string $path     Resource path relative to base service endpoint
  * @param string $method   HTTP method - one of GET, POST, PUT, DELETE, PATCH etc
  * @param string $data     Request payload
  * @param array  $headers  HTTP headers
  * @return mixed
  * @throws \PayPal\Exception\PayPalConnectionException
  */
 public function execute($handlers = array(), $path, $method, $data = '', $headers = array())
 {
     $config = $this->apiContext->getConfig();
     $httpConfig = new PayPalHttpConfig(null, $method, $config);
     $headers = $headers ? $headers : array();
     $httpConfig->setHeaders($headers + array('Content-Type' => 'application/json'));
     /** @var \Paypal\Handler\IPayPalHandler $handler */
     foreach ($handlers as $handler) {
         if (!is_object($handler)) {
             $fullHandler = "\\" . (string) $handler;
             $handler = new $fullHandler($this->apiContext);
         }
         $handler->handle($httpConfig, $data, array('path' => $path, 'apiContext' => $this->apiContext));
     }
     $connection = new PayPalHttpConnection($httpConfig, $config);
     $response = $connection->execute($data);
     return $response;
 }
Esempio n. 4
0
 /**
  * Executes an HTTP request
  *
  * @param string $data query string OR POST content as a string
  * @return mixed
  * @throws PayPalConnectionException
  */
 public function execute($data)
 {
     //Initialize the logger
     $this->logger->info($this->httpConfig->getMethod() . ' ' . $this->httpConfig->getUrl());
     //Initialize Curl Options
     $ch = curl_init($this->httpConfig->getUrl());
     $options = $this->httpConfig->getCurlOptions();
     if (empty($options[CURLOPT_HTTPHEADER])) {
         unset($options[CURLOPT_HTTPHEADER]);
     }
     curl_setopt_array($ch, $options);
     curl_setopt($ch, CURLOPT_URL, $this->httpConfig->getUrl());
     curl_setopt($ch, CURLOPT_HEADER, true);
     curl_setopt($ch, CURLINFO_HEADER_OUT, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHttpHeaders());
     //Determine Curl Options based on Method
     switch ($this->httpConfig->getMethod()) {
         case 'POST':
             curl_setopt($ch, CURLOPT_POST, true);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
             break;
         case 'PUT':
         case 'PATCH':
         case 'DELETE':
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
             break;
     }
     //Default Option if Method not of given types in switch case
     if ($this->httpConfig->getMethod() != null) {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod());
     }
     //Logging Each Headers for debugging purposes
     foreach ($this->getHttpHeaders() as $header) {
         //TODO: Strip out credentials and other secure info when logging.
         // $this->logger->debug($header);
     }
     //Execute Curl Request
     $result = curl_exec($ch);
     //Retrieve Response Status
     $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     //Retry if Certificate Exception
     if (curl_errno($ch) == 60) {
         $this->logger->info("Invalid or no certificate authority found - Retrying using bundled CA certs file");
         curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
         $result = curl_exec($ch);
         //Retrieve Response Status
         $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     }
     //Retry if Failing
     $retries = 0;
     if (in_array($httpStatus, self::$retryCodes) && $this->httpConfig->getHttpRetryCount() != null) {
         $this->logger->info("Got {$httpStatus} response from server. Retrying");
         do {
             $result = curl_exec($ch);
             //Retrieve Response Status
             $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         } while (in_array($httpStatus, self::$retryCodes) && ++$retries < $this->httpConfig->getHttpRetryCount());
     }
     //Throw Exception if Retries and Certificates doenst work
     if (curl_errno($ch)) {
         $ex = new PayPalConnectionException($this->httpConfig->getUrl(), curl_error($ch), curl_errno($ch));
         curl_close($ch);
         throw $ex;
     }
     // Get Request and Response Headers
     $requestHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
     //Using alternative solution to CURLINFO_HEADER_SIZE as it throws invalid number when called using PROXY.
     $responseHeaderSize = strlen($result) - curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
     $responseHeaders = substr($result, 0, $responseHeaderSize);
     $result = substr($result, $responseHeaderSize);
     $this->logger->debug("Request Headers \t: " . str_replace("\r\n", ", ", $requestHeaders));
     $this->logger->debug(($data && $data != '' ? "Request Data\t\t: " . $data : "No Request Payload") . "\n" . str_repeat('-', 128) . "\n");
     $this->logger->info("Response Status \t: " . $httpStatus);
     $this->logger->debug("Response Headers\t: " . str_replace("\r\n", ", ", $responseHeaders));
     //Close the curl request
     curl_close($ch);
     //More Exceptions based on HttpStatus Code
     if (in_array($httpStatus, self::$retryCodes)) {
         $ex = new PayPalConnectionException($this->httpConfig->getUrl(), "Got Http response code {$httpStatus} when accessing {$this->httpConfig->getUrl()}. " . "Retried {$retries} times.");
         $ex->setData($result);
         $this->logger->error("Got Http response code {$httpStatus} when accessing {$this->httpConfig->getUrl()}. " . "Retried {$retries} times." . $result);
         $this->logger->debug("\n\n" . str_repeat('=', 128) . "\n");
         throw $ex;
     } elseif ($httpStatus < 200 || $httpStatus >= 300) {
         $ex = new PayPalConnectionException($this->httpConfig->getUrl(), "Got Http response code {$httpStatus} when accessing {$this->httpConfig->getUrl()}.", $httpStatus);
         $ex->setData($result);
         $this->logger->error("Got Http response code {$httpStatus} when accessing {$this->httpConfig->getUrl()}. " . $result);
         $this->logger->debug("\n\n" . str_repeat('=', 128) . "\n");
         throw $ex;
     }
     $this->logger->debug(($result && $result != '' ? "Response Data \t: " . $result : "No Response Body") . "\n\n" . str_repeat('=', 128) . "\n");
     //Return result object
     return $result;
 }
Esempio n. 5
0
 /**
  * Executes an HTTP request
  *
  * @param string $data query string OR POST content as a string
  * @return mixed
  * @throws PayPalConnectionException
  */
 public function execute($data)
 {
     //Initialize the logger
     $this->logger->info($this->httpConfig->getMethod() . ' ' . $this->httpConfig->getUrl());
     //Initialize Curl Options
     $ch = curl_init($this->httpConfig->getUrl());
     curl_setopt_array($ch, $this->httpConfig->getCurlOptions());
     curl_setopt($ch, CURLOPT_URL, $this->httpConfig->getUrl());
     curl_setopt($ch, CURLOPT_HEADER, false);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHttpHeaders());
     //Determine Curl Options based on Method
     switch ($this->httpConfig->getMethod()) {
         case 'POST':
             curl_setopt($ch, CURLOPT_POST, true);
         case 'PUT':
         case 'PATCH':
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
             break;
     }
     //Default Option if Method not of given types in switch case
     if ($this->httpConfig->getMethod() != NULL) {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod());
     }
     //Logging Each Headers for debugging purposes
     foreach ($this->getHttpHeaders() as $header) {
         //TODO: Strip out credentials and other secure info when logging.
         $this->logger->fine($header);
     }
     $this->logger->fine(($data && $data != '' ? "Payload : " . $data : "No Request Payload") . "\n");
     //Execute Curl Request
     $result = curl_exec($ch);
     //Retry if Certificate Exception
     if (curl_errno($ch) == 60) {
         $this->logger->info("Invalid or no certificate authority found - Retrying using bundled CA certs file");
         curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
         $result = curl_exec($ch);
     }
     //Retrieve Response Status
     $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     //Retry if Failing
     $retries = 0;
     if (in_array($httpStatus, self::$retryCodes) && $this->httpConfig->getHttpRetryCount() != null) {
         $this->logger->info("Got {$httpStatus} response from server. Retrying");
         do {
             $result = curl_exec($ch);
             $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         } while (in_array($httpStatus, self::$retryCodes) && ++$retries < $this->httpConfig->getHttpRetryCount());
     }
     //Throw Exception if Retries and Certificates doenst work
     if (curl_errno($ch)) {
         $ex = new PayPalConnectionException($this->httpConfig->getUrl(), curl_error($ch), curl_errno($ch));
         curl_close($ch);
         throw $ex;
     }
     //Close the curl request
     curl_close($ch);
     $this->logger->fine(($result && $result != '' ? "Response : " . $result : "No Response Body") . "\n\n");
     //More Exceptions based on HttpStatus Code
     if (in_array($httpStatus, self::$retryCodes)) {
         $ex = new PayPalConnectionException($this->httpConfig->getUrl(), "Got Http response code {$httpStatus} when accessing {$this->httpConfig->getUrl()}. " . "Retried {$retries} times.");
         $ex->setData($result);
         throw $ex;
     } else {
         if ($httpStatus < 200 || $httpStatus >= 300) {
             $ex = new PayPalConnectionException($this->httpConfig->getUrl(), "Got Http response code {$httpStatus} when accessing {$this->httpConfig->getUrl()}.", $httpStatus);
             $ex->setData($result);
             throw $ex;
         }
     }
     //Return result object
     return $result;
 }
Esempio n. 6
0
 /**
  * @test
  */
 public function testProxyOpts()
 {
     $proxy = 'http://*****:*****@hostname:8081';
     $o = new PayPalHttpConfig();
     $o->setHttpProxy($proxy);
     $curlOpts = $o->getCurlOptions();
     $this->assertEquals('hostname:8081', $curlOpts[CURLOPT_PROXY]);
     $this->assertEquals('me:secret', $curlOpts[CURLOPT_PROXYUSERPWD]);
     $this->setExpectedException('PayPal\\Exception\\PayPalConfigurationException');
     $o->setHttpProxy('invalid string');
 }