From dd99971e5042cccddba67b266b3406077eb573a8 Mon Sep 17 00:00:00 2001 From: japatel Date: Sat, 10 Jan 2015 21:04:24 -0600 Subject: [PATCH] Added an ability to provide Curl Options in Configuration - Removed http.Retry, and http.ConnectionTimeout from configuration - Added ability to provide Constant and its value in Configuration File --- lib/PayPal/Auth/OAuthTokenCredential.php | 2 +- lib/PayPal/Core/PayPalConfigManager.php | 2 -- lib/PayPal/Core/PayPalHttpConfig.php | 29 ++++++++++++++++++++++-- lib/PayPal/Transport/PayPalRestCall.php | 2 +- sample/bootstrap.php | 2 +- sample/sdk_config.ini | 6 +++-- 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/PayPal/Auth/OAuthTokenCredential.php b/lib/PayPal/Auth/OAuthTokenCredential.php index 7d3356b..ef720cd 100644 --- a/lib/PayPal/Auth/OAuthTokenCredential.php +++ b/lib/PayPal/Auth/OAuthTokenCredential.php @@ -204,7 +204,7 @@ class OAuthTokenCredential extends PayPalResourceModel */ private function getToken($config, $clientId, $clientSecret, $payload) { - $httpConfig = new PayPalHttpConfig(null, 'POST'); + $httpConfig = new PayPalHttpConfig(null, 'POST', $config); $handlers = array(self::$AUTH_HANDLER); diff --git a/lib/PayPal/Core/PayPalConfigManager.php b/lib/PayPal/Core/PayPalConfigManager.php index 8a84911..f884df4 100644 --- a/lib/PayPal/Core/PayPalConfigManager.php +++ b/lib/PayPal/Core/PayPalConfigManager.php @@ -19,8 +19,6 @@ class PayPalConfigManager * @var array */ private $configs = array( - "http.ConnectionTimeOut" => "30", - "http.Retry" => "5", ); /** diff --git a/lib/PayPal/Core/PayPalHttpConfig.php b/lib/PayPal/Core/PayPalHttpConfig.php index aed127d..3864a96 100644 --- a/lib/PayPal/Core/PayPalHttpConfig.php +++ b/lib/PayPal/Core/PayPalHttpConfig.php @@ -54,12 +54,13 @@ class PayPalHttpConfig * * @param string $url * @param string $method HTTP method (GET, POST etc) defaults to POST + * @param array $configs All Configurations */ - public function __construct($url = null, $method = self::HTTP_POST) + public function __construct($url = null, $method = self::HTTP_POST, $configs = array()) { $this->url = $url; $this->method = $method; - $this->curlOptions = self::$defaultCurlOptions; + $this->curlOptions = $this->getHttpConstantsFromConfigs($configs, 'http.') + self::$defaultCurlOptions; // Update the Cipher List based on OpenSSL or NSS settings $curl = curl_version(); $sslVersion = isset($curl['ssl_version']) ? $curl['ssl_version'] : ''; @@ -274,4 +275,28 @@ class PayPalHttpConfig { $this->curlOptions[CURLOPT_USERAGENT] = $userAgentString; } + + /** + * Retrieves an array of constant key, and value based on Prefix + * + * @param array $configs + * @param $prefix + * @return array + */ + public function getHttpConstantsFromConfigs($configs = array(), $prefix) + { + $arr = array(); + if ($prefix != null && is_array($configs)) { + foreach ($configs as $k => $v) { + // Check if it startsWith + if (substr($k, 0, strlen($prefix)) === $prefix) { + $newKey = ltrim($k, $prefix); + if (defined($newKey)) { + $arr[constant($newKey)] = $v; + } + } + } + } + return $arr; + } } diff --git a/lib/PayPal/Transport/PayPalRestCall.php b/lib/PayPal/Transport/PayPalRestCall.php index 09db90b..bf69f39 100644 --- a/lib/PayPal/Transport/PayPalRestCall.php +++ b/lib/PayPal/Transport/PayPalRestCall.php @@ -54,7 +54,7 @@ class PayPalRestCall { $config = $this->apiContext->getConfig(); - $httpConfig = new PayPalHttpConfig(null, $method); + $httpConfig = new PayPalHttpConfig(null, $method, $config); $headers = $headers ? $headers : array(); $httpConfig->setHeaders($headers + array( diff --git a/sample/bootstrap.php b/sample/bootstrap.php index ad6509b..1c21c80 100644 --- a/sample/bootstrap.php +++ b/sample/bootstrap.php @@ -72,12 +72,12 @@ function getApiContext($clientId, $clientSecret) $apiContext->setConfig( array( 'mode' => 'sandbox', - 'http.ConnectionTimeOut' => 30, 'log.LogEnabled' => true, 'log.FileName' => '../PayPal.log', 'log.LogLevel' => 'FINE', 'validation.level' => 'log', 'cache.enabled' => true, + // 'http.CURLOPT_CONNECTTIMEOUT' => 30 // 'http.headers.PayPal-Partner-Attribution-Id' => '123123123' ) ); diff --git a/sample/sdk_config.ini b/sample/sdk_config.ini index 223786d..adf8c7c 100644 --- a/sample/sdk_config.ini +++ b/sample/sdk_config.ini @@ -8,8 +8,10 @@ acct1.ClientSecret = EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRb ;Connection Information [Http] -http.ConnectionTimeOut = 30 -http.Retry = 1 +; Add Curl Constants to be configured +; The settings provided in configurations would override defaults +; if provided in configurations +http.CURLOPT_CONNECTTIMEOUT = 30 ; Adding HTTP Headers to each request sent to PayPal APIs ;http.headers.PayPal-Partner-Attribution-Id = 123123123