This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
PayPalHttpConfig.php
302 lines (273 loc) · 7 KB
/
PayPalHttpConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
namespace PayPal\Core;
use PayPal\Exception\PayPalConfigurationException;
/**
* Class PayPalHttpConfig
* Http Configuration Class
*
* @package PayPal\Core
*/
class PayPalHttpConfig
{
/**
* Some default options for curl
* These are typically overridden by PayPalConnectionManager
*
* @var array
*/
public static $defaultCurlOptions = array(
CURLOPT_SSLVERSION => 6,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60, // maximum number of seconds to allow cURL functions to execute
CURLOPT_USERAGENT => 'PayPal-PHP-SDK',
CURLOPT_HTTPHEADER => array(),
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_SSL_CIPHER_LIST => 'TLSv1:TLSv1.2'
//Allowing TLSv1 cipher list.
//Adding it like this for backward compatibility with older versions of curl
);
const HEADER_SEPARATOR = ';';
const HTTP_GET = 'GET';
const HTTP_POST = 'POST';
private $headers = array();
private $curlOptions;
private $url;
private $method;
/***
* Number of times to retry a failed HTTP call
*/
private $retryCount = 0;
/**
* Default Constructor
*
* @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, $configs = array())
{
$this->url = $url;
$this->method = $method;
$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'] : '';
if($sslVersion && substr_compare($sslVersion, "NSS/", 0, strlen("NSS/")) === 0) {
//Remove the Cipher List for NSS
$this->removeCurlOption(CURLOPT_SSL_CIPHER_LIST);
}
}
/**
* Gets Url
*
* @return null|string
*/
public function getUrl()
{
return $this->url;
}
/**
* Gets Method
*
* @return string
*/
public function getMethod()
{
return $this->method;
}
/**
* Gets all Headers
*
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Get Header by Name
*
* @param $name
* @return string|null
*/
public function getHeader($name)
{
if (array_key_exists($name, $this->headers)) {
return $this->headers[$name];
}
return null;
}
/**
* Sets Url
*
* @param $url
*/
public function setUrl($url)
{
$this->url = $url;
}
/**
* Set Headers
*
* @param array $headers
*/
public function setHeaders(array $headers = array())
{
$this->headers = $headers;
}
/**
* Adds a Header
*
* @param $name
* @param $value
* @param bool $overWrite allows you to override header value
*/
public function addHeader($name, $value, $overWrite = true)
{
if (!array_key_exists($name, $this->headers) || $overWrite) {
$this->headers[$name] = $value;
} else {
$this->headers[$name] = $this->headers[$name] . self::HEADER_SEPARATOR . $value;
}
}
/**
* Removes a Header
*
* @param $name
*/
public function removeHeader($name)
{
unset($this->headers[$name]);
}
/**
* Gets all curl options
*
* @return array
*/
public function getCurlOptions()
{
return $this->curlOptions;
}
/**
* Add Curl Option
*
* @param string $name
* @param mixed $value
*/
public function addCurlOption($name, $value)
{
$this->curlOptions[$name] = $value;
}
/**
* Removes a curl option from the list
*
* @param $name
*/
public function removeCurlOption($name)
{
unset($this->curlOptions[$name]);
}
/**
* Set Curl Options. Overrides all curl options
*
* @param $options
*/
public function setCurlOptions($options)
{
$this->curlOptions = $options;
}
/**
* Set ssl parameters for certificate based client authentication
*
* @param $certPath
* @param null $passPhrase
*/
public function setSSLCert($certPath, $passPhrase = null)
{
$this->curlOptions[CURLOPT_SSLCERT] = realpath($certPath);
if (isset($passPhrase) && trim($passPhrase) != "") {
$this->curlOptions[CURLOPT_SSLCERTPASSWD] = $passPhrase;
}
}
/**
* Set connection timeout in seconds
*
* @param integer $timeout
*/
public function setHttpTimeout($timeout)
{
$this->curlOptions[CURLOPT_CONNECTTIMEOUT] = $timeout;
}
/**
* Set HTTP proxy information
*
* @param string $proxy
* @throws PayPalConfigurationException
*/
public function setHttpProxy($proxy)
{
$urlParts = parse_url($proxy);
if ($urlParts == false || !array_key_exists("host", $urlParts)) {
throw new PayPalConfigurationException("Invalid proxy configuration " . $proxy);
}
$this->curlOptions[CURLOPT_PROXY] = $urlParts["host"];
if (isset($urlParts["port"])) {
$this->curlOptions[CURLOPT_PROXY] .= ":" . $urlParts["port"];
}
if (isset($urlParts["user"])) {
$this->curlOptions[CURLOPT_PROXYUSERPWD] = $urlParts["user"] . ":" . $urlParts["pass"];
}
}
/**
* Set Http Retry Counts
*
* @param int $retryCount
*/
public function setHttpRetryCount($retryCount)
{
$this->retryCount = $retryCount;
}
/**
* Get Http Retry Counts
*
* @return int
*/
public function getHttpRetryCount()
{
return $this->retryCount;
}
/**
* Sets the User-Agent string on the HTTP request
*
* @param string $userAgentString
*/
public function setUserAgent($userAgentString)
{
$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;
}
}