PageRenderTime 65ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/CORP-RESELLER/shopping-cart-lite
PHP | 346 lines | 188 code | 35 blank | 123 comment | 13 complexity | e24e8308f6516961806701198df8bb30 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Configuration registry
  5. *
  6. * @package Braintree
  7. * @subpackage Utility
  8. * @copyright 2014 Braintree, a division of PayPal, Inc.
  9. */
  10. class Braintree_Configuration extends Braintree
  11. {
  12. /**
  13. * Braintree API version to use
  14. * @access public
  15. */
  16. const API_VERSION = 4;
  17. /**
  18. * @var array array of config properties
  19. * @access protected
  20. * @static
  21. */
  22. private static $_cache = array(
  23. 'environment' => '',
  24. 'merchantId' => '',
  25. 'publicKey' => '',
  26. 'privateKey' => '',
  27. );
  28. /**
  29. *
  30. * @access protected
  31. * @static
  32. * @var array valid environments, used for validation
  33. */
  34. private static $_validEnvironments = array(
  35. 'development',
  36. 'sandbox',
  37. 'production',
  38. 'qa',
  39. );
  40. /**
  41. * resets configuration to default
  42. * @access public
  43. * @static
  44. */
  45. public static function reset()
  46. {
  47. self::$_cache = array (
  48. 'environment' => '',
  49. 'merchantId' => '',
  50. 'publicKey' => '',
  51. 'privateKey' => '',
  52. );
  53. }
  54. /**
  55. * performs sanity checks when config settings are being set
  56. *
  57. * @ignore
  58. * @access protected
  59. * @param string $key name of config setting
  60. * @param string $value value to set
  61. * @throws InvalidArgumentException
  62. * @throws Braintree_Exception_Configuration
  63. * @static
  64. * @return boolean
  65. */
  66. private static function validate($key=null, $value=null)
  67. {
  68. if (empty($key) && empty($value)) {
  69. throw new InvalidArgumentException('nothing to validate');
  70. }
  71. if ($key === 'environment' &&
  72. !in_array($value, self::$_validEnvironments) ) {
  73. throw new Braintree_Exception_Configuration('"' .
  74. $value . '" is not a valid environment.');
  75. }
  76. if (!isset(self::$_cache[$key])) {
  77. throw new Braintree_Exception_Configuration($key .
  78. ' is not a valid configuration setting.');
  79. }
  80. if (empty($value)) {
  81. throw new InvalidArgumentException($key . ' cannot be empty.');
  82. }
  83. return true;
  84. }
  85. private static function set($key, $value)
  86. {
  87. // this method will raise an exception on invalid data
  88. self::validate($key, $value);
  89. // set the value in the cache
  90. self::$_cache[$key] = $value;
  91. }
  92. private static function get($key)
  93. {
  94. // throw an exception if the value hasn't been set
  95. if (isset(self::$_cache[$key]) &&
  96. (empty(self::$_cache[$key]))) {
  97. throw new Braintree_Exception_Configuration(
  98. $key.' needs to be set.'
  99. );
  100. }
  101. if (array_key_exists($key, self::$_cache)) {
  102. return self::$_cache[$key];
  103. }
  104. // return null by default to prevent __set from overloading
  105. return null;
  106. }
  107. private static function setOrGet($name, $value = null)
  108. {
  109. if (!empty($value) && is_array($value)) {
  110. $value = $value[0];
  111. }
  112. if (!empty($value)) {
  113. self::set($name, $value);
  114. } else {
  115. return self::get($name);
  116. }
  117. return true;
  118. }
  119. /**#@+
  120. * sets or returns the property after validation
  121. * @access public
  122. * @static
  123. * @param string $value pass a string to set, empty to get
  124. * @return mixed returns true on set
  125. */
  126. public static function environment($value = null)
  127. {
  128. return self::setOrGet(__FUNCTION__, $value);
  129. }
  130. public static function merchantId($value = null)
  131. {
  132. return self::setOrGet(__FUNCTION__, $value);
  133. }
  134. public static function publicKey($value = null)
  135. {
  136. return self::setOrGet(__FUNCTION__, $value);
  137. }
  138. public static function privateKey($value = null)
  139. {
  140. return self::setOrGet(__FUNCTION__, $value);
  141. }
  142. /**#@-*/
  143. /**
  144. * returns the full merchant URL based on config values
  145. *
  146. * @access public
  147. * @static
  148. * @param none
  149. * @return string merchant URL
  150. */
  151. public static function merchantUrl()
  152. {
  153. return self::baseUrl() .
  154. self::merchantPath();
  155. }
  156. /**
  157. * returns the base braintree gateway URL based on config values
  158. *
  159. * @access public
  160. * @static
  161. * @param none
  162. * @return string braintree gateway URL
  163. */
  164. public static function baseUrl()
  165. {
  166. return self::protocol() . '://' .
  167. self::serverName() . ':' .
  168. self::portNumber();
  169. }
  170. /**
  171. * sets the merchant path based on merchant ID
  172. *
  173. * @access protected
  174. * @static
  175. * @param none
  176. * @return string merchant path uri
  177. */
  178. public static function merchantPath()
  179. {
  180. return '/merchants/'.self::merchantId();
  181. }
  182. /**
  183. * sets the physical path for the location of the CA certs
  184. *
  185. * @access public
  186. * @static
  187. * @param none
  188. * @return string filepath
  189. */
  190. public static function caFile($sslPath = NULL)
  191. {
  192. $sslPath = $sslPath ? $sslPath : DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR .
  193. 'ssl' . DIRECTORY_SEPARATOR;
  194. $caPath = realpath(
  195. dirname(__FILE__) .
  196. $sslPath . 'api_braintreegateway_com.ca.crt'
  197. );
  198. if (!file_exists($caPath))
  199. {
  200. throw new Braintree_Exception_SSLCaFileNotFound();
  201. }
  202. return $caPath;
  203. }
  204. /**
  205. * returns the port number depending on environment
  206. *
  207. * @access public
  208. * @static
  209. * @param none
  210. * @return int portnumber
  211. */
  212. public static function portNumber()
  213. {
  214. if (self::sslOn()) {
  215. return 443;
  216. }
  217. return getenv("GATEWAY_PORT") ? getenv("GATEWAY_PORT") : 3000;
  218. }
  219. /**
  220. * returns http protocol depending on environment
  221. *
  222. * @access public
  223. * @static
  224. * @param none
  225. * @return string http || https
  226. */
  227. public static function protocol()
  228. {
  229. return self::sslOn() ? 'https' : 'http';
  230. }
  231. /**
  232. * returns gateway server name depending on environment
  233. *
  234. * @access public
  235. * @static
  236. * @param none
  237. * @return string server domain name
  238. */
  239. public static function serverName()
  240. {
  241. switch(self::environment()) {
  242. case 'production':
  243. $serverName = 'api.braintreegateway.com';
  244. break;
  245. case 'qa':
  246. $serverName = 'gateway.qa.braintreepayments.com';
  247. break;
  248. case 'sandbox':
  249. $serverName = 'api.sandbox.braintreegateway.com';
  250. break;
  251. case 'development':
  252. default:
  253. $serverName = 'localhost';
  254. break;
  255. }
  256. return $serverName;
  257. }
  258. public static function authUrl()
  259. {
  260. switch(self::environment()) {
  261. case 'production':
  262. $serverName = 'https://auth.venmo.com';
  263. break;
  264. case 'qa':
  265. $serverName = 'https://auth.qa.venmo.com';
  266. break;
  267. case 'sandbox':
  268. $serverName = 'https://auth.sandbox.venmo.com';
  269. break;
  270. case 'development':
  271. default:
  272. $serverName = 'http://auth.venmo.dev:9292';
  273. break;
  274. }
  275. return $serverName;
  276. }
  277. /**
  278. * returns boolean indicating SSL is on or off for this session,
  279. * depending on environment
  280. *
  281. * @access public
  282. * @static
  283. * @param none
  284. * @return boolean
  285. */
  286. public static function sslOn()
  287. {
  288. switch(self::environment()) {
  289. case 'development':
  290. $ssl = false;
  291. break;
  292. case 'production':
  293. case 'qa':
  294. case 'sandbox':
  295. default:
  296. $ssl = true;
  297. break;
  298. }
  299. return $ssl;
  300. }
  301. /**
  302. * log message to default logger
  303. *
  304. * @param string $message
  305. *
  306. */
  307. public static function logMessage($message)
  308. {
  309. error_log('[Braintree] ' . $message);
  310. }
  311. }