| 1 |
<?php |
| 2 |
namespace Payplug; |
| 3 |
|
| 4 |
/** |
| 5 |
* |
| 6 |
*/ |
| 7 |
class Authentication |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Retrieve existing API keys for an user, using his email and password. |
| 11 |
* This function is for user-friendly interface purpose only. |
| 12 |
* You should probably not use this more than once, login/password MUST NOT be stored and API Keys are enough to interact with API. |
| 13 |
* |
| 14 |
* @param string $email the user email |
| 15 |
* @param string $password the user password |
| 16 |
* |
| 17 |
* @return null|array the API keys |
| 18 |
* |
| 19 |
* @throws Exception\BadRequestException |
| 20 |
*/ |
| 21 |
public static function getKeysByLogin($email, $password) |
| 22 |
{ |
| 23 |
$httpClient = new Core\HttpClient(null); |
| 24 |
$response = $httpClient->post( |
| 25 |
Core\APIRoutes::getRoute(Core\APIRoutes::KEY_RESOURCE), |
| 26 |
array('email' => $email, 'password' => $password), |
| 27 |
false |
| 28 |
); |
| 29 |
return $response; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Retrieve account info. |
| 34 |
* |
| 35 |
* @param Payplug $payplug the client configuration |
| 36 |
* |
| 37 |
* @return null|array the account settings |
| 38 |
* |
| 39 |
* @throws Exception\ConfigurationNotSetException |
| 40 |
*/ |
| 41 |
public static function getAccount(Payplug $payplug = null) |
| 42 |
{ |
| 43 |
if ($payplug === null) { |
| 44 |
$payplug = Payplug::getDefaultConfiguration(); |
| 45 |
} |
| 46 |
|
| 47 |
$httpClient = new Core\HttpClient($payplug); |
| 48 |
$response = $httpClient->get(Core\APIRoutes::getRoute(Core\APIRoutes::ACCOUNT_RESOURCE)); |
| 49 |
|
| 50 |
return $response; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Retrieve the account permissions |
| 55 |
* |
| 56 |
* @param Payplug $payplug the client configuration |
| 57 |
* |
| 58 |
* @return null|array the account permissions |
| 59 |
* |
| 60 |
* @throws Exception\ConfigurationNotSetException |
| 61 |
*/ |
| 62 |
public static function getPermissions(Payplug $payplug = null) |
| 63 |
{ |
| 64 |
if ($payplug === null) { |
| 65 |
$payplug = Payplug::getDefaultConfiguration(); |
| 66 |
} |
| 67 |
|
| 68 |
$httpClient = new Core\HttpClient($payplug); |
| 69 |
$response = $httpClient->get(Core\APIRoutes::getRoute(Core\APIRoutes::ACCOUNT_RESOURCE)); |
| 70 |
|
| 71 |
return $response['httpResponse']['permissions']; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Retrieve the account permissions, using email and password. |
| 76 |
* This function is for user-friendly interface purpose only. |
| 77 |
* You should probably not use this more than once, login/password MUST NOT be stored and API Keys are enough to interact with API. |
| 78 |
* |
| 79 |
* @param string $email the user email |
| 80 |
* @param string $password the user password |
| 81 |
* |
| 82 |
* @return null|array the account permissions |
| 83 |
* |
| 84 |
* @throws Exception\ConfigurationNotSetException |
| 85 |
*/ |
| 86 |
public static function getPermissionsByLogin($email, $password) |
| 87 |
{ |
| 88 |
$keys = self::getKeysByLogin($email, $password); |
| 89 |
$payplug = Payplug::setSecretKey($keys['httpResponse']['secret_keys']['live']); |
| 90 |
$httpClient = new Core\HttpClient($payplug); |
| 91 |
$response = $httpClient->get(Core\APIRoutes::getRoute(Core\APIRoutes::ACCOUNT_RESOURCE)); |
| 92 |
|
| 93 |
return $response['httpResponse']['permissions']; |
| 94 |
} |
| 95 |
} |
| 96 |
|