forked from cardpay/php-sdk-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuthApiClient.php
151 lines (123 loc) · 4.02 KB
/
AuthApiClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* PHP SDK for Cardpay API v3. All rights reserved.
*/
namespace Cardpay\api;
use Cardpay\ApiException;
use Cardpay\model\ApiTokens;
class AuthApiClient
{
const API_TOKEN_MIN_VALIDITY = 10000;
const MILLISECONDS_IN_ONE_SECOND = 1000;
private $host;
private $terminalCode;
private $password;
/** @var TokensStorageApi */
private $tokensStorageApi;
/**
* AuthApiClient constructor.
* @param $host
* @param $terminalCode
* @param $password
* @param $tokensStorageApi
* @throws ApiException
*/
public function __construct($host, $terminalCode, $password, $tokensStorageApi)
{
$this->validateInputParams($terminalCode, $password, $tokensStorageApi);
$this->host = $host;
$this->terminalCode = $terminalCode;
$this->password = $password;
$this->tokensStorageApi = $tokensStorageApi;
}
/**
* @param $terminalCode
* @param $password
* @param $tokensStorageApi
* @throws ApiException
*/
private function validateInputParams($terminalCode, $password, $tokensStorageApi)
{
if (empty($terminalCode)) {
throw new ApiException('Empty terminal code');
}
if (empty($password)) {
throw new ApiException('Empty password');
}
if (null == $tokensStorageApi) {
throw new ApiException('TokensStorageApi is not set');
}
}
/**
* @return ApiTokens
* @throws ApiException
*/
public function obtainApiTokens()
{
if (false === $this->tokensStorageApi->areApiTokensSaved()) {
return $this->obtainTokensByPassword();
}
/** @var ApiTokens $apiTokens */
$apiTokens = $this->tokensStorageApi->readApiTokens();
// if refresh token is expired
if ($this->isTokenExpired($apiTokens->getRefreshExpiresIn())) {
return $this->obtainTokensByPassword();
}
// if access token is expired
if ($this->isTokenExpired($apiTokens->getExpiresIn())) {
$refreshToken = $apiTokens->getRefreshToken();
return $this->obtainTokensByRefreshToken($refreshToken);
}
return $apiTokens;
}
/**
* @return ApiTokens
* @throws ApiException
*/
public function obtainTokensByPassword()
{
$authApi = new AuthApi($this->host);
/** @var ApiTokens $apiTokens */
$apiTokens = $authApi->obtainTokens('password', $this->password, null, $this->terminalCode);
$this->saveApiTokens($apiTokens);
return $apiTokens;
}
/**
* @param string $refreshToken
* @return ApiTokens
* @throws ApiException
*/
public function obtainTokensByRefreshToken($refreshToken)
{
$authApi = new AuthApi($this->host);
/** @var ApiTokens $apiTokens */
$apiTokens = $authApi->obtainTokens('refresh_token', null, $refreshToken, null);
$this->saveApiTokens($apiTokens);
return $apiTokens;
}
/**
* @param ApiTokens $apiTokens
*/
private function saveApiTokens($apiTokens)
{
$currentTimeMilliseconds = $this->getCurrentTimeMilliseconds();
$accessTokenExpiresIn = $apiTokens->getExpiresIn();
$apiTokens->setExpiresIn($accessTokenExpiresIn * self::MILLISECONDS_IN_ONE_SECOND + $currentTimeMilliseconds);
$refreshTokenExpiresIn = $apiTokens->getRefreshExpiresIn();
$apiTokens->setRefreshExpiresIn($refreshTokenExpiresIn * self::MILLISECONDS_IN_ONE_SECOND + $currentTimeMilliseconds);
$this->tokensStorageApi->saveApiTokens($apiTokens);
}
/**
* @param $expiresAt
* @return bool
*/
private function isTokenExpired($expiresAt)
{
$currentTimeMilliseconds = $this->getCurrentTimeMilliseconds();
return $expiresAt - $currentTimeMilliseconds < self::API_TOKEN_MIN_VALIDITY;
}
private function getCurrentTimeMilliseconds()
{
return round(microtime(true) * self::MILLISECONDS_IN_ONE_SECOND);
}
}