ARCANEDEV /
Stripe
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php namespace Arcanedev\Stripe\Http\Curl; |
||
| 2 | |||
| 3 | use Arcanedev\Stripe\Contracts\Http\Curl\HttpClientInterface; |
||
| 4 | use Arcanedev\Stripe\Exceptions\ApiConnectionException; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Class HttpClient |
||
| 8 | * |
||
| 9 | * @package Arcanedev\Stripe\Http\Curl |
||
| 10 | * @author ARCANEDEV <[email protected]> |
||
| 11 | */ |
||
| 12 | class HttpClient implements HttpClientInterface |
||
| 13 | { |
||
| 14 | /* ------------------------------------------------------------------------------------------------ |
||
| 15 | | Constants |
||
| 16 | | ------------------------------------------------------------------------------------------------ |
||
| 17 | */ |
||
| 18 | const DEFAULT_TIMEOUT = 80; |
||
| 19 | const DEFAULT_CONNECT_TIMEOUT = 30; |
||
| 20 | |||
| 21 | /* ------------------------------------------------------------------------------------------------ |
||
| 22 | | Properties |
||
| 23 | | ------------------------------------------------------------------------------------------------ |
||
| 24 | */ |
||
| 25 | /** |
||
| 26 | * The HTTP Client instance. |
||
| 27 | * |
||
| 28 | * @var \Arcanedev\Stripe\Http\Curl\HttpClient |
||
| 29 | */ |
||
| 30 | private static $instance; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $apiKey; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $apiBaseUrl; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \Arcanedev\Stripe\Http\Curl\HeaderBag |
||
| 44 | */ |
||
| 45 | private $headers; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Arcanedev\Stripe\Http\Curl\CurlOptions |
||
| 49 | */ |
||
| 50 | private $options; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var resource |
||
| 54 | */ |
||
| 55 | private $curl; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | private $timeout = self::DEFAULT_TIMEOUT; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | private $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var mixed |
||
| 69 | */ |
||
| 70 | private $response; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var int |
||
| 74 | */ |
||
| 75 | private $errorCode; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private $errorMessage; |
||
| 81 | |||
| 82 | /* ------------------------------------------------------------------------------------------------ |
||
| 83 | | Constructor & Destructor |
||
| 84 | | ------------------------------------------------------------------------------------------------ |
||
| 85 | */ |
||
| 86 | /** |
||
| 87 | * Create a HttpClient instance. |
||
| 88 | */ |
||
| 89 | 5 | private function __construct() |
|
| 90 | { |
||
| 91 | 5 | $this->headers = new HeaderBag; |
|
| 92 | 5 | $this->options = new CurlOptions; |
|
| 93 | 5 | $this->response = null; |
|
| 94 | 5 | } |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Destroy the instance. |
||
| 98 | */ |
||
| 99 | public function __destruct() |
||
| 100 | { |
||
| 101 | $this->close(); |
||
| 102 | } |
||
| 103 | |||
| 104 | /* ------------------------------------------------------------------------------------------------ |
||
| 105 | | Getters & Setters |
||
| 106 | | ------------------------------------------------------------------------------------------------ |
||
| 107 | */ |
||
| 108 | /** |
||
| 109 | * Set API Key. |
||
| 110 | * |
||
| 111 | * @param string $apiKey |
||
| 112 | * |
||
| 113 | * @return self |
||
| 114 | */ |
||
| 115 | 739 | public function setApiKey($apiKey) |
|
| 116 | { |
||
| 117 | 739 | $this->apiKey = $apiKey; |
|
| 118 | |||
| 119 | 739 | return $this; |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set Base URL. |
||
| 124 | * |
||
| 125 | * @param string $apiBaseUrl |
||
| 126 | * |
||
| 127 | * @return self |
||
| 128 | */ |
||
| 129 | public function setApiBaseUrl($apiBaseUrl) |
||
| 130 | { |
||
| 131 | $this->apiBaseUrl = $apiBaseUrl; |
||
| 132 | |||
| 133 | return $this; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the timeout. |
||
| 138 | * |
||
| 139 | * @return int |
||
| 140 | */ |
||
| 141 | 5 | public function getTimeout() |
|
| 142 | { |
||
| 143 | 5 | return $this->timeout; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set the timeout. |
||
| 148 | * |
||
| 149 | * @param int $seconds |
||
| 150 | * |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | 1419 | public function setTimeout($seconds) |
|
| 154 | { |
||
| 155 | 1419 | $this->timeout = (int) max($seconds, 0); |
|
| 156 | |||
| 157 | 1419 | return $this; |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the connect timeout. |
||
| 162 | * |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | 5 | public function getConnectTimeout() |
|
| 166 | { |
||
| 167 | 5 | return $this->connectTimeout; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set the connect timeout. |
||
| 172 | * |
||
| 173 | * @param int $seconds |
||
| 174 | * |
||
| 175 | * @return self |
||
| 176 | */ |
||
| 177 | 5 | public function setConnectTimeout($seconds) |
|
| 178 | { |
||
| 179 | 5 | $this->connectTimeout = (int) max($seconds, 0); |
|
| 180 | |||
| 181 | 5 | return $this; |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set array options. |
||
| 186 | * |
||
| 187 | * @param array $options |
||
| 188 | * |
||
| 189 | * @return self |
||
| 190 | */ |
||
| 191 | 739 | public function setOptionArray(array $options) |
|
| 192 | { |
||
| 193 | 739 | curl_setopt_array($this->curl, $options); |
|
| 194 | |||
| 195 | 739 | return $this; |
|
| 196 | } |
||
| 197 | |||
| 198 | /* ------------------------------------------------------------------------------------------------ |
||
| 199 | | Curl Functions |
||
| 200 | | ------------------------------------------------------------------------------------------------ |
||
| 201 | */ |
||
| 202 | /** |
||
| 203 | * Init curl. |
||
| 204 | */ |
||
| 205 | 739 | private function init() |
|
| 206 | { |
||
| 207 | 739 | $this->curl = curl_init(); |
|
| 208 | 739 | } |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Execute curl. |
||
| 212 | */ |
||
| 213 | 739 | private function execute() |
|
| 214 | { |
||
| 215 | 739 | $this->response = curl_exec($this->curl); |
|
| 216 | 739 | $this->errorCode = curl_errno($this->curl); |
|
| 217 | 739 | $this->errorMessage = curl_error($this->curl); |
|
| 218 | 739 | } |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Close curl. |
||
| 222 | */ |
||
| 223 | 739 | private function close() |
|
| 224 | { |
||
| 225 | 739 | if (is_resource($this->curl)) { |
|
| 226 | 739 | curl_close($this->curl); |
|
| 227 | 591 | } |
|
| 228 | 739 | } |
|
| 229 | |||
| 230 | /* ------------------------------------------------------------------------------------------------ |
||
| 231 | | Main Functions |
||
| 232 | | ------------------------------------------------------------------------------------------------ |
||
| 233 | */ |
||
| 234 | /** |
||
| 235 | * Get the HTTP. |
||
| 236 | * |
||
| 237 | * @return self |
||
| 238 | */ |
||
| 239 | 1419 | public static function instance() |
|
| 240 | { |
||
| 241 | 1419 | if ( ! self::$instance) { |
|
| 242 | 5 | self::$instance = new self; |
|
| 243 | 4 | } |
|
| 244 | |||
| 245 | 1419 | return self::$instance; |
|
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Curl the request. |
||
| 250 | * |
||
| 251 | * @param string $method |
||
| 252 | * @param string $url |
||
| 253 | * @param array|string $params |
||
| 254 | * @param array $headers |
||
| 255 | * @param bool $hasFile |
||
| 256 | * |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | 739 | public function request($method, $url, $params, $headers, $hasFile) |
|
| 260 | { |
||
| 261 | 739 | if ($method !== 'post') { |
|
| 262 | 520 | $url = str_parse_url($url, $params); |
|
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 263 | 416 | } |
|
| 264 | else { |
||
| 265 | 624 | $params = $hasFile ? $params : self::encode($params); |
|
|
0 ignored issues
–
show
|
|||
| 266 | } |
||
| 267 | |||
| 268 | 739 | $this->headers->prepare($this->apiKey, $headers, $hasFile); |
|
| 269 | 739 | $this->options->make($method, $url, $params, $this->headers->get(), $hasFile); |
|
| 270 | 739 | $this->options->setOptions([ |
|
| 271 | 739 | CURLOPT_CONNECTTIMEOUT => $this->connectTimeout, |
|
| 272 | 739 | CURLOPT_TIMEOUT => $this->timeout, |
|
| 273 | 591 | ]); |
|
| 274 | |||
| 275 | 739 | $this->init(); |
|
| 276 | 739 | $this->setOptionArray($this->options->get()); |
|
| 277 | 739 | $this->execute(); |
|
| 278 | |||
| 279 | 739 | $this->checkCertErrors(); |
|
| 280 | 739 | $this->checkResponse(); |
|
| 281 | |||
| 282 | 739 | $statusCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
| 283 | 739 | $this->close(); |
|
| 284 | |||
| 285 | 739 | return [$this->response, $statusCode]; |
|
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Check Cert Errors. |
||
| 290 | */ |
||
| 291 | 739 | private function checkCertErrors() |
|
| 292 | { |
||
| 293 | 739 | if (SslChecker::hasCertErrors($this->errorCode)) { |
|
| 294 | $this->headers->set( |
||
| 295 | 'X-Stripe-Client-Info', |
||
| 296 | '{"ca":"using Stripe-supplied CA bundle"}' |
||
| 297 | ); |
||
| 298 | |||
| 299 | $this->setOptionArray([ |
||
| 300 | CURLOPT_HTTPHEADER => $this->headers->get(), |
||
| 301 | CURLOPT_CAINFO => SslChecker::caBundle() |
||
| 302 | ]); |
||
| 303 | |||
| 304 | $this->execute(); |
||
| 305 | } |
||
| 306 | 739 | } |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Encode array to query string |
||
| 310 | * |
||
| 311 | * @param array|string $array |
||
| 312 | * @param string|null $prefix |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | 615 | protected static function encode($array, $prefix = null) |
|
| 317 | { |
||
| 318 | // @codeCoverageIgnoreStart |
||
| 319 | if ( ! is_array($array)) return $array; |
||
| 320 | // @codeCoverageIgnoreEnd |
||
| 321 | |||
| 322 | 615 | $result = []; |
|
| 323 | |||
| 324 | 615 | foreach ($array as $key => $value) { |
|
| 325 | 605 | if (is_null($value)) continue; |
|
| 326 | |||
| 327 | 605 | if ($prefix) { |
|
| 328 | 505 | $key = ($key !== null && (! is_int($key) || is_array($value))) |
|
| 329 | 505 | ? $prefix . "[" . $key . "]" |
|
| 330 | 505 | : $prefix . "[]"; |
|
| 331 | 404 | } |
|
| 332 | |||
| 333 | 605 | if ( ! is_array($value)) { |
|
| 334 | 605 | $result[] = urlencode($key) . '=' . urlencode($value); |
|
| 335 | 484 | } |
|
| 336 | 505 | elseif ($enc = self::encode($value, $key)) { |
|
| 337 | 525 | $result[] = $enc; |
|
| 338 | 404 | } |
|
| 339 | 492 | } |
|
| 340 | |||
| 341 | 615 | return implode('&', $result); |
|
| 342 | } |
||
| 343 | |||
| 344 | /* ------------------------------------------------------------------------------------------------ |
||
| 345 | | Other Functions |
||
| 346 | | ------------------------------------------------------------------------------------------------ |
||
| 347 | */ |
||
| 348 | /** |
||
| 349 | * Check Response. |
||
| 350 | * |
||
| 351 | * @throws \Arcanedev\Stripe\Exceptions\ApiConnectionException |
||
| 352 | */ |
||
| 353 | 739 | private function checkResponse() |
|
| 354 | { |
||
| 355 | 739 | if ($this->response !== false) return; |
|
| 356 | |||
| 357 | $this->close(); |
||
| 358 | $this->handleCurlError(); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Handle CURL errors. |
||
| 363 | * |
||
| 364 | * @throws \Arcanedev\Stripe\Exceptions\ApiConnectionException |
||
| 365 | */ |
||
| 366 | private function handleCurlError() |
||
| 367 | { |
||
| 368 | switch ($this->errorCode) { |
||
| 369 | case CURLE_COULDNT_CONNECT: |
||
| 370 | case CURLE_COULDNT_RESOLVE_HOST: |
||
| 371 | case CURLE_OPERATION_TIMEOUTED: |
||
| 372 | $msg = "Could not connect to Stripe ({$this->apiBaseUrl}). Please check your internet connection " |
||
| 373 | . 'and try again. If this problem persists, you should check Stripe\'s service status at ' |
||
| 374 | . 'https://twitter.com/stripestatus, or'; |
||
| 375 | break; |
||
| 376 | |||
| 377 | case CURLE_SSL_CACERT: |
||
| 378 | case CURLE_SSL_PEER_CERTIFICATE: |
||
| 379 | $msg = 'Could not verify Stripe\'s SSL certificate. Please make sure that your network is not ' |
||
| 380 | . "intercepting certificates. (Try going to {$this->apiBaseUrl} in your browser.) " |
||
| 381 | . 'If this problem persists,'; |
||
| 382 | break; |
||
| 383 | |||
| 384 | default: |
||
| 385 | $msg = 'Unexpected error communicating with Stripe. If this problem persists,'; |
||
| 386 | // no break |
||
| 387 | } |
||
| 388 | |||
| 389 | $msg .= ' let us know at [email protected].'; |
||
| 390 | |||
| 391 | $msg .= "\n\n(Network error [errno {$this->errorCode}]: {$this->errorMessage})"; |
||
| 392 | |||
| 393 | throw new ApiConnectionException($msg); |
||
| 394 | } |
||
| 395 | } |
||
| 396 |