BraintreeConfiguration

Configuration registry.

Defined (1)

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

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