/wp-content/plugins/woocommerce-gateway-paypal-powered-by-braintree/braintree_sdk/lib/Braintree/Http.php

https://gitlab.com/hunt9310/ras · PHP · 182 lines · 151 code · 24 blank · 7 comment · 36 complexity · 6f52db2eeb08fd1f4ebf60bc328f27e0 MD5 · raw file

  1. <?php
  2. namespace Braintree;
  3. /**
  4. * Braintree HTTP Client
  5. * processes Http requests using curl
  6. *
  7. * @copyright 2015 Braintree, a division of PayPal, Inc.
  8. */
  9. class Http
  10. {
  11. protected $_config;
  12. private $_useClientCredentials = false;
  13. public function __construct($config)
  14. {
  15. $this->_config = $config;
  16. }
  17. public function delete($path)
  18. {
  19. $response = $this->_doRequest('DELETE', $path);
  20. if($response['status'] === 200) {
  21. return true;
  22. } else {
  23. Util::throwStatusCodeException($response['status']);
  24. }
  25. }
  26. public function get($path)
  27. {
  28. $response = $this->_doRequest('GET', $path);
  29. if ($response['status'] === 200) {
  30. return Xml::buildArrayFromXml($response['body']);
  31. } else {
  32. Util::throwStatusCodeException($response['status']);
  33. }
  34. }
  35. public function post($path, $params = null)
  36. {
  37. $response = $this->_doRequest('POST', $path, $this->_buildXml($params));
  38. $responseCode = $response['status'];
  39. if($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode == 400) {
  40. return Xml::buildArrayFromXml($response['body']);
  41. } else {
  42. Util::throwStatusCodeException($responseCode);
  43. }
  44. }
  45. public function put($path, $params = null)
  46. {
  47. $response = $this->_doRequest('PUT', $path, $this->_buildXml($params));
  48. $responseCode = $response['status'];
  49. if($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode == 400) {
  50. return Xml::buildArrayFromXml($response['body']);
  51. } else {
  52. Util::throwStatusCodeException($responseCode);
  53. }
  54. }
  55. private function _buildXml($params)
  56. {
  57. return empty($params) ? null : Xml::buildXmlFromArray($params);
  58. }
  59. private function _getHeaders()
  60. {
  61. return [
  62. 'Accept: application/xml',
  63. 'Content-Type: application/xml',
  64. ];
  65. }
  66. private function _getAuthorization()
  67. {
  68. if ($this->_useClientCredentials) {
  69. return [
  70. 'user' => $this->_config->getClientId(),
  71. 'password' => $this->_config->getClientSecret(),
  72. ];
  73. } else if ($this->_config->isAccessToken()) {
  74. return [
  75. 'token' => $this->_config->getAccessToken(),
  76. ];
  77. } else {
  78. return [
  79. 'user' => $this->_config->getPublicKey(),
  80. 'password' => $this->_config->getPrivateKey(),
  81. ];
  82. }
  83. }
  84. public function useClientCredentials()
  85. {
  86. $this->_useClientCredentials = true;
  87. }
  88. private function _doRequest($httpVerb, $path, $requestBody = null)
  89. {
  90. return $this->_doUrlRequest($httpVerb, $this->_config->baseUrl() . $path, $requestBody);
  91. }
  92. public function _doUrlRequest($httpVerb, $url, $requestBody = null)
  93. {
  94. $curl = curl_init();
  95. curl_setopt($curl, CURLOPT_TIMEOUT, 60);
  96. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $httpVerb);
  97. curl_setopt($curl, CURLOPT_URL, $url);
  98. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  99. $headers = $this->_getHeaders($curl);
  100. $headers[] = 'User-Agent: Braintree PHP Library ' . Version::get();
  101. $headers[] = 'X-ApiVersion: ' . Configuration::API_VERSION;
  102. $authorization = $this->_getAuthorization();
  103. if (isset($authorization['user'])) {
  104. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  105. curl_setopt($curl, CURLOPT_USERPWD, $authorization['user'] . ':' . $authorization['password']);
  106. } else if (isset($authorization['token'])) {
  107. $headers[] = 'Authorization: Bearer ' . $authorization['token'];
  108. }
  109. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  110. // curl_setopt($curl, CURLOPT_VERBOSE, true);
  111. if ($this->_config->sslOn()) {
  112. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  113. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
  114. curl_setopt($curl, CURLOPT_CAINFO, $this->getCaFile());
  115. }
  116. if(!empty($requestBody)) {
  117. curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
  118. }
  119. if($this->_config->isUsingProxy()) {
  120. $proxyHost = $this->_config->getProxyHost();
  121. $proxyPort = $this->_config->getProxyPort();
  122. $proxyType = $this->_config->getProxyType();
  123. curl_setopt($curl, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort);
  124. if(!empty($proxyType)) {
  125. curl_setopt($curl, CURLOPT_PROXYTYPE, $proxyType);
  126. }
  127. }
  128. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  129. $response = curl_exec($curl);
  130. $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  131. curl_close($curl);
  132. if ($this->_config->sslOn()) {
  133. if ($httpStatus == 0) {
  134. throw new Exception\SSLCertificate();
  135. }
  136. }
  137. return ['status' => $httpStatus, 'body' => $response];
  138. }
  139. private function getCaFile()
  140. {
  141. static $memo;
  142. if ($memo === null) {
  143. $caFile = $this->_config->caFile();
  144. if (substr($caFile, 0, 7) !== 'phar://') {
  145. return $caFile;
  146. }
  147. $extractedCaFile = sys_get_temp_dir() . '/api_braintreegateway_com.ca.crt';
  148. if (!file_exists($extractedCaFile) || sha1_file($extractedCaFile) != sha1_file($caFile)) {
  149. if (!copy($caFile, $extractedCaFile)) {
  150. throw new Exception\SSLCaFileNotFound();
  151. }
  152. }
  153. $memo = $extractedCaFile;
  154. }
  155. return $memo;
  156. }
  157. }
  158. class_alias('Braintree\Http', 'Braintree_Http');