BraintreeHttp

Braintree HTTP Client processes Http requests using curl.

Defined (1)

The class is defined in the following location(s).

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