| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug; |
| 4 |
|
| 5 |
use Payplug\Exception\ConfigurationException; |
| 6 |
|
| 7 |
/** |
| 8 |
* The client Payplug |
| 9 |
*/ |
| 10 |
class Payplug |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var Payplug|null The default authentication that should be used when no authentication is |
| 14 |
* provided. |
| 15 |
*/ |
| 16 |
private static $_defaultConfiguration = null; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var string The token. |
| 20 |
*/ |
| 21 |
private $_token; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var string The API Version. |
| 25 |
*/ |
| 26 |
private $_apiVersion; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor for a authentication. |
| 30 |
* |
| 31 |
* @param string $token The token |
| 32 |
* @param string $api_version The API Version |
| 33 |
* |
| 34 |
* @throws Exception\ConfigurationException when token is not set |
| 35 |
*/ |
| 36 |
public function __construct($token, $apiVersion = null) |
| 37 |
{ |
| 38 |
if (!is_string($token)) { |
| 39 |
throw new Exception\ConfigurationException('Expected string values for token.'); |
| 40 |
} |
| 41 |
$this->_token = $token; |
| 42 |
|
| 43 |
// if no given version then set a default |
| 44 |
$this->_apiVersion = $apiVersion ? $apiVersion : '2019-06-14'; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* Initializes a Authentication and sets it as the new default global authentication. |
| 50 |
* It also performs some checks before saving the authentication. |
| 51 |
* |
| 52 |
* <pre> |
| 53 |
* Expected array format for argument $authentication : |
| 54 |
* $authentication['TOKEN'] = 'YOUR TOKEN' |
| 55 |
* </pre> |
| 56 |
* |
| 57 |
* @param string $token the authentication token |
| 58 |
* |
| 59 |
* @return Payplug the new client authentication |
| 60 |
* |
| 61 |
* @throws Exception\ConfigurationException |
| 62 |
* @deprecated Use Payplug::init(array('secretKey' => 'token', 'apiVersion' => 'version')) |
| 63 |
*/ |
| 64 |
public static function setSecretKey($token) |
| 65 |
{ |
| 66 |
if (!is_string($token)) { |
| 67 |
throw new Exception\ConfigurationException('Expected string values for the token.'); |
| 68 |
} |
| 69 |
|
| 70 |
$clientConfiguration = new Payplug($token); |
| 71 |
|
| 72 |
self::setDefaultConfiguration($clientConfiguration); |
| 73 |
|
| 74 |
return $clientConfiguration; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Initializes a Authentication and sets it as the new default global authentication. |
| 79 |
* It also performs some checks before saving the authentication and set the API version |
| 80 |
* |
| 81 |
* @param array $props |
| 82 |
* @return Payplug |
| 83 |
* @throws ConfigurationException |
| 84 |
*/ |
| 85 |
public static function init($props) |
| 86 |
{ |
| 87 |
$secretKey = isset($props['secretKey']) && $props['secretKey'] ? $props['secretKey'] : null; |
| 88 |
$apiVersion = isset($props['apiVersion']) && $props['apiVersion'] ? $props['apiVersion'] : null; |
| 89 |
|
| 90 |
if (!$secretKey) { |
| 91 |
throw new Exception\ConfigurationException('Expected string values for the token.'); |
| 92 |
} |
| 93 |
|
| 94 |
$clientConfiguration = new Payplug($secretKey, $apiVersion); |
| 95 |
|
| 96 |
self::setDefaultConfiguration($clientConfiguration); |
| 97 |
|
| 98 |
return $clientConfiguration; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Gets the token corresponding to the mode currently in use (Live token or Test token). |
| 103 |
* |
| 104 |
* @return string The current token |
| 105 |
*/ |
| 106 |
public function getToken() |
| 107 |
{ |
| 108 |
return $this->_token; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Sets the API Version corresponding to the module currently in use. |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
public function getApiVersion() |
| 117 |
{ |
| 118 |
return $this->_apiVersion; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Gets the default global authentication. |
| 123 |
* |
| 124 |
* @return Payplug The last client authentication |
| 125 |
* |
| 126 |
* @throws Exception\ConfigurationNotSetException when the global authentication was not set. |
| 127 |
*/ |
| 128 |
public static function getDefaultConfiguration() |
| 129 |
{ |
| 130 |
if (self::$_defaultConfiguration === null) { |
| 131 |
throw new Exception\ConfigurationNotSetException('Unable to find an authentication.'); |
| 132 |
} |
| 133 |
|
| 134 |
return self::$_defaultConfiguration; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Sets the new default client authentication. This authentication will be used when no authentication is explicitly |
| 139 |
* passed to methods. |
| 140 |
* |
| 141 |
* @param Payplug $defaultConfiguration the new default authentication |
| 142 |
*/ |
| 143 |
public static function setDefaultConfiguration($defaultConfiguration) |
| 144 |
{ |
| 145 |
self::$_defaultConfiguration = $defaultConfiguration; |
| 146 |
} |
| 147 |
} |
| 148 |
|