Configuration.php 16 KB

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