/vendor/braintree/braintree_php/lib/Braintree/Http.php

https://gitlab.com/cividesk-civicrm/com.cividesk.payment.braintree · PHP · 193 lines · 160 code · 26 blank · 7 comment · 41 complexity · 3440f599ee47ba57a6f08958763af14e 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, $this->_config->timeout());
  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. $proxyUser = $this->_config->getProxyUser();
  124. $proxyPwd= $this->_config->getProxyPassword();
  125. curl_setopt($curl, CURLOPT_PROXY, $proxyHost . ':' . $proxyPort);
  126. if(!empty($proxyType)) {
  127. curl_setopt($curl, CURLOPT_PROXYTYPE, $proxyType);
  128. }
  129. if($this->_config->isAuthenticatedProxy()) {
  130. curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyUser . ':' . $proxyPwd);
  131. }
  132. }
  133. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  134. $response = curl_exec($curl);
  135. $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  136. $error_code = curl_errno($curl);
  137. if ($error_code == 28 && $httpStatus == 0) {
  138. throw new Exception\Timeout();
  139. }
  140. curl_close($curl);
  141. if ($this->_config->sslOn()) {
  142. if ($httpStatus == 0) {
  143. throw new Exception\SSLCertificate();
  144. }
  145. }
  146. return ['status' => $httpStatus, 'body' => $response];
  147. }
  148. private function getCaFile()
  149. {
  150. static $memo;
  151. if ($memo === null) {
  152. $caFile = $this->_config->caFile();
  153. if (substr($caFile, 0, 7) !== 'phar://') {
  154. return $caFile;
  155. }
  156. $extractedCaFile = sys_get_temp_dir() . '/api_braintreegateway_com.ca.crt';
  157. if (!file_exists($extractedCaFile) || sha1_file($extractedCaFile) != sha1_file($caFile)) {
  158. if (!copy($caFile, $extractedCaFile)) {
  159. throw new Exception\SSLCaFileNotFound();
  160. }
  161. }
  162. $memo = $extractedCaFile;
  163. }
  164. return $memo;
  165. }
  166. }
  167. class_alias('Braintree\Http', 'Braintree_Http');