Configuration.php in Commerce Braintree 7
File
braintree_php/lib/Braintree/Configuration.php
View source
<?php
class Braintree_Configuration extends Braintree {
const API_VERSION = 2;
private static $_cache = array(
'environment' => '',
'merchantId' => '',
'publicKey' => '',
'privateKey' => '',
);
private static $_validEnvironments = array(
'development',
'sandbox',
'production',
'qa',
);
public static function reset() {
self::$_cache = array(
'environment' => '',
'merchantId' => '',
'publicKey' => '',
'privateKey' => '',
);
}
private static function validate($key = null, $value = null) {
if (empty($key) && empty($value)) {
throw new InvalidArgumentException('nothing to validate');
}
if ($key === 'environment' && !in_array($value, self::$_validEnvironments)) {
throw new Braintree_Exception_Configuration('"' . $value . '" is not a valid environment.');
}
if (!isset(self::$_cache[$key])) {
throw new Braintree_Exception_Configuration($key . ' is not a valid configuration setting.');
}
if (empty($value)) {
throw new InvalidArgumentException($key . ' cannot be empty.');
}
return true;
}
private static function set($key, $value) {
self::validate($key, $value);
self::$_cache[$key] = $value;
}
private static function get($key) {
if (isset(self::$_cache[$key]) && empty(self::$_cache[$key])) {
throw new Braintree_Exception_Configuration($key . ' needs to be set');
}
if (array_key_exists($key, self::$_cache)) {
return self::$_cache[$key];
}
return null;
}
private static function setOrGet($name, $value = null) {
if (!empty($value) && is_array($value)) {
$value = $value[0];
}
if (!empty($value)) {
self::set($name, $value);
}
else {
return self::get($name);
}
return true;
}
public static function environment($value = null) {
return self::setOrGet(__FUNCTION__, $value);
}
public static function merchantId($value = null) {
return self::setOrGet(__FUNCTION__, $value);
}
public static function publicKey($value = null) {
return self::setOrGet(__FUNCTION__, $value);
}
public static function privateKey($value = null) {
return self::setOrGet(__FUNCTION__, $value);
}
public static function merchantUrl() {
return self::baseUrl() . self::merchantPath();
}
public static function baseUrl() {
return self::protocol() . '://' . self::serverName() . ':' . self::portNumber();
}
public static function merchantPath() {
return '/merchants/' . self::merchantId();
}
public static function caFile($sslPath = NULL) {
$sslPath = $sslPath ? $sslPath : DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'ssl' . DIRECTORY_SEPARATOR;
switch (self::environment()) {
case 'production':
$caPath = realpath(dirname(__FILE__) . $sslPath . 'www_braintreegateway_com.ca.crt');
break;
case 'qa':
case 'sandbox':
default:
$caPath = realpath(dirname(__FILE__) . $sslPath . 'sandbox_braintreegateway_com.ca.crt');
break;
}
if (!file_exists($caPath)) {
throw new Braintree_Exception_SSLCaFileNotFound();
}
return $caPath;
}
public static function portNumber() {
if (self::sslOn()) {
return 443;
}
return getenv("GATEWAY_PORT") ? getenv("GATEWAY_PORT") : 3000;
}
public static function protocol() {
return self::sslOn() ? 'https' : 'http';
}
public static function serverName() {
switch (self::environment()) {
case 'production':
$serverName = 'www.braintreegateway.com';
break;
case 'qa':
$serverName = 'qa.braintreegateway.com';
break;
case 'sandbox':
$serverName = 'sandbox.braintreegateway.com';
break;
case 'development':
default:
$serverName = 'localhost';
break;
}
return $serverName;
}
public static function sslOn() {
switch (self::environment()) {
case 'development':
$ssl = false;
break;
case 'production':
case 'qa':
case 'sandbox':
default:
$ssl = true;
break;
}
return $ssl;
}
public static function logMessage($message) {
error_log('[Braintree] ' . $message);
}
}