/wp-content/plugins/woocommerce-gateway-paypal-powered-by-braintree/braintree_sdk/lib/Braintree/Configuration.php

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