BraintreeHttp

Braintree HTTP Client processes Http requests using curl.

Defined (1)

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

/classes/lib/Braintree/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, 60); 
  72. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $httpVerb); 
  73. curl_setopt($curl, CURLOPT_URL, $url); 
  74. curl_setopt($curl, CURLOPT_ENCODING, 'gzip'); 
  75.  
  76. $headers = $this->_getHeaders($curl); 
  77. $headers[] = 'User-Agent: Braintree PHP Library ' . Version::get(); 
  78. $headers[] = 'X-ApiVersion: ' . Configuration::API_VERSION; 
  79.  
  80. $authorization = $this->_getAuthorization(); 
  81. if (isset($authorization['user'])) { 
  82. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
  83. curl_setopt($curl, CURLOPT_USERPWD, $authorization['user'] . ':' . $authorization['password']); 
  84. } else if (isset($authorization['token'])) { 
  85. $headers[] = 'Authorization: Bearer ' . $authorization['token']; 
  86. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
  87.  
  88. // curl_setopt($curl, CURLOPT_VERBOSE, true); 
  89. if ($this->_config->sslOn()) { 
  90. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 
  91. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); 
  92. curl_setopt($curl, CURLOPT_CAINFO, $this->getCaFile()); 
  93.  
  94. if(!empty($requestBody)) { 
  95. curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody); 
  96.  
  97. if($this->_config->isUsingProxy()) { 
  98. $proxyHost = $this->_config->getProxyHost(); 
  99. $proxyPort = $this->_config->getProxyPort(); 
  100. $proxyType = $this->_config->getProxyType(); 
  101. curl_setopt($curl, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort); 
  102. if(!empty($proxyType)) { 
  103. curl_setopt($curl, CURLOPT_PROXYTYPE, $proxyType); 
  104.  
  105. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
  106. $response = curl_exec($curl); 
  107. $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
  108. curl_close($curl); 
  109. if ($this->_config->sslOn()) { 
  110. if ($httpStatus == 0) { 
  111. throw new Exception\SSLCertificate(); 
  112. return ['status' => $httpStatus, 'body' => $response]; 
  113.  
  114. private function getCaFile() 
  115. static $memo; 
  116.  
  117. if ($memo === null) { 
  118. $caFile = $this->_config->caFile(); 
  119.  
  120. if (substr($caFile, 0, 7) !== 'phar://') { 
  121. return $caFile; 
  122.  
  123. $extractedCaFile = sys_get_temp_dir() . '/api_braintreegateway_com.ca.crt'; 
  124.  
  125. if (!file_exists($extractedCaFile) || sha1_file($extractedCaFile) != sha1_file($caFile)) { 
  126. if (!copy($caFile, $extractedCaFile)) { 
  127. throw new Exception\SSLCaFileNotFound(); 
  128. $memo = $extractedCaFile; 
  129.  
  130. return $memo;