BraintreeConfiguration

Configuration registry.

Defined (1)

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

/includes/lib/Braintree/lib/Braintree/Configuration.php  
  1. class Configuration 
  2. public static $global; 
  3.  
  4. private $_environment = null; 
  5. private $_merchantId = null; 
  6. private $_publicKey = null; 
  7. private $_privateKey = null; 
  8. private $_clientId = null; 
  9. private $_clientSecret = null; 
  10. private $_accessToken = null; 
  11. private $_proxyHost = null; 
  12. private $_proxyPort = null; 
  13. private $_proxyType = null; 
  14. private $_proxyUser = null; 
  15. private $_proxyPassword = null; 
  16. private $_timeout = 60; 
  17. private $_sslVersion = null; 
  18.  
  19. /** 
  20. * Braintree API version to use 
  21. * @access public 
  22. */ 
  23. const API_VERSION = 4; 
  24.  
  25. public function __construct($attribs = []) 
  26. foreach ($attribs as $kind => $value) { 
  27. if ($kind == 'environment') { 
  28. CredentialsParser::assertValidEnvironment($value); 
  29. $this->_environment = $value; 
  30. if ($kind == 'merchantId') { 
  31. $this->_merchantId = $value; 
  32. if ($kind == 'publicKey') { 
  33. $this->_publicKey = $value; 
  34. if ($kind == 'privateKey') { 
  35. $this->_privateKey = $value; 
  36.  
  37. if (isset($attribs['clientId']) || isset($attribs['accessToken'])) { 
  38. if (isset($attribs['environment']) || isset($attribs['merchantId']) || isset($attribs['publicKey']) || isset($attribs['privateKey'])) { 
  39. throw new Exception\Configuration('Cannot mix OAuth credentials (clientId, clientSecret, accessToken) with key credentials (publicKey, privateKey, environment, merchantId).'); 
  40. $parsedCredentials = new CredentialsParser($attribs); 
  41.  
  42. $this->_environment = $parsedCredentials->getEnvironment(); 
  43. $this->_merchantId = $parsedCredentials->getMerchantId(); 
  44. $this->_clientId = $parsedCredentials->getClientId(); 
  45. $this->_clientSecret = $parsedCredentials->getClientSecret(); 
  46. $this->_accessToken = $parsedCredentials->getAccessToken(); 
  47.  
  48. /** 
  49. * resets configuration to default 
  50. * @access public 
  51. */ 
  52. public static function reset() 
  53. self::$global = new Configuration(); 
  54.  
  55. public static function gateway() 
  56. return new Gateway(self::$global); 
  57.  
  58. public static function environment($value=null) 
  59. if (empty($value)) { 
  60. return self::$global->getEnvironment(); 
  61. CredentialsParser::assertValidEnvironment($value); 
  62. self::$global->setEnvironment($value); 
  63.  
  64. public static function merchantId($value=null) 
  65. if (empty($value)) { 
  66. return self::$global->getMerchantId(); 
  67. self::$global->setMerchantId($value); 
  68.  
  69. public static function publicKey($value=null) 
  70. if (empty($value)) { 
  71. return self::$global->getPublicKey(); 
  72. self::$global->setPublicKey($value); 
  73.  
  74. public static function privateKey($value=null) 
  75. if (empty($value)) { 
  76. return self::$global->getPrivateKey(); 
  77. self::$global->setPrivateKey($value); 
  78.  
  79. /** 
  80. * Sets or gets the read timeout to use for making requests. 
  81. * @param integer $value If provided, sets the read timeout 
  82. * @return integer The read timeout used for connecting to Braintree 
  83. */ 
  84. public static function timeout($value=null) 
  85. if (empty($value)) { 
  86. return self::$global->getTimeout(); 
  87. self::$global->setTimeout($value); 
  88.  
  89. /** 
  90. * Sets or gets the SSL version to use for making requests. See 
  91. * http://php.net/manual/en/function.curl-setopt.php for possible 
  92. * CURLOPT_SSLVERSION values. 
  93. * @param integer $value If provided, sets the SSL version 
  94. * @return integer The SSL version used for connecting to Braintree 
  95. */ 
  96. public static function sslVersion($value=null) 
  97. if (empty($value)) { 
  98. return self::$global->getSslVersion(); 
  99. self::$global->setSslVersion($value); 
  100.  
  101. /** 
  102. * Sets or gets the proxy host to use for connecting to Braintree 
  103. * @param string $value If provided, sets the proxy host 
  104. * @return string The proxy host used for connecting to Braintree 
  105. */ 
  106. public static function proxyHost($value = null) 
  107. if (empty($value)) { 
  108. return self::$global->getProxyHost(); 
  109. self::$global->setProxyHost($value); 
  110.  
  111. /** 
  112. * Sets or gets the port of the proxy to use for connecting to Braintree 
  113. * @param string $value If provided, sets the port of the proxy 
  114. * @return string The port of the proxy used for connecting to Braintree 
  115. */ 
  116. public static function proxyPort($value = null) 
  117. if (empty($value)) { 
  118. return self::$global->getProxyPort(); 
  119. self::$global->setProxyPort($value); 
  120.  
  121. /** 
  122. * Sets or gets the proxy type to use for connecting to Braintree. This value 
  123. * can be any of the CURLOPT_PROXYTYPE options in PHP cURL. 
  124. * @param string $value If provided, sets the proxy type 
  125. * @return string The proxy type used for connecting to Braintree 
  126. */ 
  127. public static function proxyType($value = null) 
  128. if (empty($value)) { 
  129. return self::$global->getProxyType(); 
  130. self::$global->setProxyType($value); 
  131.  
  132. /** 
  133. * Specifies whether or not a proxy is properly configured 
  134. * @return bool true if a proxy is configured properly, false if not 
  135. */ 
  136. public static function isUsingProxy() 
  137. $proxyHost = self::$global->getProxyHost(); 
  138. $proxyPort = self::$global->getProxyPort(); 
  139. return !empty($proxyHost) && !empty($proxyPort); 
  140.  
  141. public static function proxyUser($value = null) 
  142. if (empty($value)) { 
  143. return self::$global->getProxyUser(); 
  144. self::$global->setProxyUser($value); 
  145.  
  146. public static function proxyPassword($value = null) 
  147. if (empty($value)) { 
  148. return self::$global->getProxyPassword(); 
  149. self::$global->setProxyPassword($value); 
  150.  
  151. /** 
  152. * Specified whether or not a username and password have been provided for 
  153. * use with an authenticated proxy 
  154. * @return bool true if both proxyUser and proxyPassword are present 
  155. */ 
  156. public static function isAuthenticatedProxy() 
  157. $proxyUser = self::$global->getProxyUser(); 
  158. $proxyPwd = self::$global->getProxyPassword(); 
  159. return !empty($proxyUser) && !empty($proxyPwd); 
  160.  
  161. public static function assertGlobalHasAccessTokenOrKeys() 
  162. self::$global->assertHasAccessTokenOrKeys(); 
  163.  
  164. public function assertHasAccessTokenOrKeys() 
  165. if (empty($this->_accessToken)) { 
  166. if (empty($this->_merchantId)) { 
  167. throw new Exception\Configuration('Braintree\\Configuration::merchantId needs to be set (or accessToken needs to be passed to Braintree\\Gateway).'); 
  168. } else if (empty($this->_environment)) { 
  169. throw new Exception\Configuration('Braintree\\Configuration::environment needs to be set.'); 
  170. } else if (empty($this->_publicKey)) { 
  171. throw new Exception\Configuration('Braintree\\Configuration::publicKey needs to be set.'); 
  172. } else if (empty($this->_privateKey)) { 
  173. throw new Exception\Configuration('Braintree\\Configuration::privateKey needs to be set.'); 
  174.  
  175. public function assertHasClientCredentials() 
  176. $this->assertHasClientId(); 
  177. $this->assertHasClientSecret(); 
  178.  
  179. public function assertHasClientId() 
  180. if (empty($this->_clientId)) { 
  181. throw new Exception\Configuration('clientId needs to be passed to Braintree\\Gateway.'); 
  182.  
  183. public function assertHasClientSecret() 
  184. if (empty($this->_clientSecret)) { 
  185. throw new Exception\Configuration('clientSecret needs to be passed to Braintree\\Gateway.'); 
  186.  
  187. public function getEnvironment() 
  188. return $this->_environment; 
  189.  
  190. /** 
  191. * Do not use this method directly. Pass in the environment to the constructor. 
  192. */ 
  193. public function setEnvironment($value) 
  194. $this->_environment = $value; 
  195.  
  196. public function getMerchantId() 
  197. return $this->_merchantId; 
  198.  
  199. /** 
  200. * Do not use this method directly. Pass in the merchantId to the constructor. 
  201. */ 
  202. public function setMerchantId($value) 
  203. $this->_merchantId = $value; 
  204.  
  205. public function getPublicKey() 
  206. return $this->_publicKey; 
  207.  
  208. public function getClientId() 
  209. return $this->_clientId; 
  210.  
  211. /** 
  212. * Do not use this method directly. Pass in the publicKey to the constructor. 
  213. */ 
  214. public function setPublicKey($value) 
  215. $this->_publicKey = $value; 
  216.  
  217. public function getPrivateKey() 
  218. return $this->_privateKey; 
  219.  
  220. public function getClientSecret() 
  221. return $this->_clientSecret; 
  222.  
  223. /** 
  224. * Do not use this method directly. Pass in the privateKey to the constructor. 
  225. */ 
  226. public function setPrivateKey($value) 
  227. $this->_privateKey = $value; 
  228.  
  229. private function setProxyHost($value) 
  230. $this->_proxyHost = $value; 
  231.  
  232. public function getProxyHost() 
  233. return $this->_proxyHost; 
  234.  
  235. private function setProxyPort($value) 
  236. $this->_proxyPort = $value; 
  237.  
  238. public function getProxyPort() 
  239. return $this->_proxyPort; 
  240.  
  241. private function setProxyType($value) 
  242. $this->_proxyType = $value; 
  243.  
  244. public function getProxyType() 
  245. return $this->_proxyType; 
  246.  
  247. private function setProxyUser($value) 
  248. $this->_proxyUser = $value; 
  249.  
  250. public function getProxyUser() 
  251. return $this->_proxyUser; 
  252.  
  253. private function setProxyPassword($value) 
  254. $this->_proxyPassword = $value; 
  255.  
  256. public function getProxyPassword() 
  257. return $this->_proxyPassword; 
  258.  
  259. private function setTimeout($value) 
  260. $this->_timeout = $value; 
  261.  
  262. public function getTimeout() 
  263. return $this->_timeout; 
  264.  
  265. private function setSslVersion($value) 
  266. $this->_sslVersion = $value; 
  267.  
  268. private function getSslVersion() 
  269. return $this->_sslVersion; 
  270.  
  271. public function getAccessToken() 
  272. return $this->_accessToken; 
  273.  
  274. public function isAccessToken() 
  275. return !empty($this->_accessToken); 
  276.  
  277. public function isClientCredentials() 
  278. return !empty($this->_clientId); 
  279. /** 
  280. * returns the base braintree gateway URL based on config values 
  281. * @access public 
  282. * @param none 
  283. * @return string braintree gateway URL 
  284. */ 
  285. public function baseUrl() 
  286. return sprintf('%s://%s:%d', $this->protocol(), $this->serverName(), $this->portNumber()); 
  287.  
  288. /** 
  289. * sets the merchant path based on merchant ID 
  290. * @access protected 
  291. * @param none 
  292. * @return string merchant path uri 
  293. */ 
  294. public function merchantPath() 
  295. return '/merchants/' . $this->_merchantId; 
  296.  
  297. /** 
  298. * sets the physical path for the location of the CA certs 
  299. * @access public 
  300. * @param none 
  301. * @return string filepath 
  302. */ 
  303. public function caFile($sslPath = NULL) 
  304. $sslPath = $sslPath ? $sslPath : DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 
  305. 'ssl' . DIRECTORY_SEPARATOR; 
  306. $caPath = __DIR__ . $sslPath . 'api_braintreegateway_com.ca.crt'; 
  307.  
  308. if (!file_exists($caPath)) 
  309. throw new Exception\SSLCaFileNotFound(); 
  310.  
  311. return $caPath; 
  312.  
  313. /** 
  314. * returns the port number depending on environment 
  315. * @access public 
  316. * @param none 
  317. * @return int portnumber 
  318. */ 
  319. public function portNumber() 
  320. if ($this->sslOn()) { 
  321. return 443; 
  322. return getenv("GATEWAY_PORT") ? getenv("GATEWAY_PORT") : 3000; 
  323.  
  324. /** 
  325. * returns http protocol depending on environment 
  326. * @access public 
  327. * @param none 
  328. * @return string http || https 
  329. */ 
  330. public function protocol() 
  331. return $this->sslOn() ? 'https' : 'http'; 
  332.  
  333. /** 
  334. * returns gateway server name depending on environment 
  335. * @access public 
  336. * @param none 
  337. * @return string server domain name 
  338. */ 
  339. public function serverName() 
  340. switch($this->_environment) { 
  341. case 'production': 
  342. $serverName = 'api.braintreegateway.com'; 
  343. break; 
  344. case 'qa': 
  345. $serverName = 'gateway.qa.braintreepayments.com'; 
  346. break; 
  347. case 'sandbox': 
  348. $serverName = 'api.sandbox.braintreegateway.com'; 
  349. break; 
  350. case 'development': 
  351. case 'integration': 
  352. default: 
  353. $serverName = 'localhost'; 
  354. break; 
  355.  
  356. return $serverName; 
  357.  
  358. public function authUrl() 
  359. switch($this->_environment) { 
  360. case 'production': 
  361. $serverName = 'https://auth.venmo.com'; 
  362. break; 
  363. case 'qa': 
  364. $serverName = 'https://auth.qa.venmo.com'; 
  365. break; 
  366. case 'sandbox': 
  367. $serverName = 'https://auth.sandbox.venmo.com'; 
  368. break; 
  369. case 'development': 
  370. case 'integration': 
  371. default: 
  372. $serverName = 'http://auth.venmo.dev:9292'; 
  373. break; 
  374.  
  375. return $serverName; 
  376.  
  377. /** 
  378. * returns boolean indicating SSL is on or off for this session,  
  379. * depending on environment 
  380. * @access public 
  381. * @param none 
  382. * @return boolean 
  383. */ 
  384. public function sslOn() 
  385. switch($this->_environment) { 
  386. case 'integration': 
  387. case 'development': 
  388. $ssl = false; 
  389. break; 
  390. case 'production': 
  391. case 'qa': 
  392. case 'sandbox': 
  393. default: 
  394. $ssl = true; 
  395. break; 
  396.  
  397. return $ssl; 
  398.  
  399. /** 
  400. * log message to default logger 
  401. * @param string $message 
  402. */ 
  403. public function logMessage($message) 
  404. error_log('[Braintree] ' . $message);