PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

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

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