Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
client.php
1
<?php
2
3
4
namespace
Bitrix\Sale\Rest\Synchronization
;
5
6
7
use
Bitrix\Main\ArgumentException
;
8
use
Bitrix\Main\Error
;
9
use
Bitrix\Main\Loader
;
10
use
Bitrix\Main\SystemException
;
11
use
Bitrix\Main\Text\Encoding
;
12
use
Bitrix\Main\Web\HttpClient
;
13
use
Bitrix\Main\Web\Json
;
14
use
Bitrix\Rest\OAuthService
;
15
use
Bitrix\Sale\Result
;
16
17
class
Client
18
{
19
protected
$clientId
;
20
protected
$clientSecret
;
21
protected
$serviceUrl
;
22
protected
$refreshToken
;
23
24
const
HTTP_SOCKET_TIMEOUT
= 10;
25
const
HTTP_STREAM_TIMEOUT
= 10;
26
const
SERVICE_PATH
=
"/rest/"
;
27
const
B24_APP_GRANT_TYPE
=
'refresh_token'
;
28
29
public
function
__construct
(
$clientId
,
$clientSecret
,
$serviceUrl
)
30
{
31
$this->clientId =
$clientId
;
32
$this->clientSecret =
$clientSecret
;
33
$this->serviceUrl =
$serviceUrl
;
34
}
35
36
protected
function
getHttpClient
()
37
{
38
return
new
HttpClient
(array(
39
'socketTimeout'
=> static::HTTP_SOCKET_TIMEOUT,
40
'streamTimeout'
=> static::HTTP_STREAM_TIMEOUT,
41
));
42
}
43
protected
function
getRequestUrl
($methodName)
44
{
45
return
$this->serviceUrl.self::SERVICE_PATH.$methodName;
46
}
47
48
public
function
call
($methodName, $additionalParams=[])
49
{
50
$result =
new
Result
();
51
52
if
($this->clientId && $this->clientSecret)
53
{
54
$httpClient = $this->
getHttpClient
();
55
56
$additionalParams = $this->
prepareRequest
($additionalParams);
57
58
LoggerDiag::addMessage
(
'CLIENT_CALL_REQUEST'
, var_export([
59
'getRequestUrl'
=>$this->
getRequestUrl
($methodName),
60
'additionalParams'
=>$additionalParams,
61
],
true
));
62
63
$httpResult = $httpClient->post(
64
$this->
getRequestUrl
($methodName),
65
$additionalParams
66
);
67
68
LoggerDiag::addMessage
(
'CLIENT_CALL_PROCESS_RESULT'
, var_export([
69
'result'
=>$httpResult,
70
'status'
=>$httpClient->getStatus()
71
],
true
));
72
73
$respons = $this->
prepareResponse
($httpResult);
74
75
if
($respons)
76
{
77
LoggerDiag::addMessage
(
'CLIENT_CALL_PROCESS_RESULT_SUCCESS'
);
78
79
if
(isset($respons[
'error'
]))
80
{
81
$result->addError(
new
Error
($respons[
'error_description'
], mb_strtoupper($respons[
'error'
])));
82
LoggerDiag::addMessage
(
'CLIENT_CALL_RESULT_ERROR'
);
83
}
84
else
85
{
86
$result->setData([
'DATA'
=>$respons]);
87
LoggerDiag::addMessage
(
'CLIENT_CALL_RESULT_SUCCESS'
, var_export($respons,
true
));
88
}
89
}
90
else
91
{
92
$result->addError(
new
Error
(
'Strange answer from Bitrix Service! '
.$httpResult,
'STRANGE_ANSWER'
));
93
LoggerDiag::addMessage
(
'CLIENT_CALL_PROCESS_RESULT_ERROR'
);
94
}
95
}
96
else
97
{
98
$result->addError(
new
Error
(
'No client credentials for refresh token'
));
99
LoggerDiag::addMessage
(
'CLIENT_CALL_CLIENT_ID_EMPTY'
);
100
}
101
102
return
$result;
103
}
104
105
protected
function
prepareResponse
($result)
106
{
107
try
108
{
109
return
Json::decode($result);
110
}
111
catch
(
ArgumentException
$e)
112
{
113
return
false
;
114
}
115
}
116
117
protected
function
prepareRequest
($params)
118
{
119
if
(!is_array($params))
120
{
121
$params = array();
122
}
123
else
124
{
125
$params = Encoding::convertEncoding($params, LANG_CHARSET,
"utf-8"
);
126
}
127
128
return
$params;
129
}
130
131
public
function
refreshToken
(
$refreshToken
)
132
{
133
$result =
new
Result
();
134
135
if
(
$refreshToken
==
''
)
136
{
137
$result->addError(
new
Error
(
'Refresh token is empty'
));
138
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_EMPTY'
);
139
}
140
141
if
(!$this->clientId || !$this->clientSecret)
142
{
143
$result->addError(
new
Error
(
'No client credentials for refresh token'
));
144
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_CLIENT_ID_EMPTY'
);
145
}
146
147
if
($result->isSuccess())
148
{
149
$request = OAuthService::SERVICE_URL.
'/oauth/token/'
.
'?'
.http_build_query(
150
[
151
'grant_type'
=>self::B24_APP_GRANT_TYPE,
152
'client_id'
=>$this->clientId,
153
'client_secret'
=>$this->clientSecret,
154
'refresh_token'
=>
$refreshToken
155
]);
156
157
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_REQUEST'
, var_export($request,
true
));
158
159
$httpClient = $this->
getHttpClient
();
160
$httpResult = $httpClient->get($request);
161
162
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_PROCESS_RESULT'
, var_export([
163
'result'
=>$httpResult,
164
'status'
=>$httpClient->getStatus()
165
],
true
));
166
167
$respons = $this->
prepareResponse
($httpResult);
168
if
($respons)
169
{
170
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_PROCESS_RESULT_SUCCESS'
);
171
172
if
(isset($respons[
'error'
]))
173
{
174
$result->addError(
new
Error
($respons[
'error_description'
], mb_strtoupper($respons[
'error'
])));
175
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_RESULT_ERROR'
);
176
}
177
else
178
{
179
$result->setData([
'DATA'
=>$respons]);
180
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_RESULT_SUCCESS'
, var_export($respons,
true
));
181
}
182
}
183
else
184
{
185
$result->addError(
new
Error
(
'Strange answer from Bitrix Service! '
,
'STRANGE_ANSWER_REFRESH_TOKEN'
));
186
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_PROCESS_RESULT_ERROR'
);
187
}
188
}
189
190
return
$result;
191
}
192
193
public
function
checkAccessToken
($accessToken)
194
{
195
$result =
new
Result
();
196
197
if
(!
Loader::includeModule
(
'rest'
))
198
$result->addError(
new
Error
(
'Module REST is not included'
));
199
200
if
($result->isSuccess())
201
{
202
if
(!\
Bitrix
\
Rest
\OAuthService::getEngine()->isRegistered())
203
{
204
try
205
{
206
\Bitrix\Rest\OAuthService::register();
207
}
208
catch
(\
Bitrix
\Main\
SystemException
$e)
209
{
210
$result->addError(
new
Error
(
'OAuthServiceError'
,
' OAUTH_SERVICE_ERROR'
));
211
}
212
}
213
214
if
($result->isSuccess())
215
{
216
$client = \Bitrix\Rest\OAuthService::getEngine()->getClient();
217
$respons = $client->call(
'app.info'
, [
'auth'
=> $accessToken]);
218
if
(isset($respons[
'error'
]))
219
$result->addError(
new
Error
($respons[
'error_description'
], mb_strtoupper($respons[
'error'
])));
220
}
221
}
222
return
$result;
223
}
224
}
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\DB\Result
Definition
result.php:19
Bitrix\Main\Error
Definition
error.php:14
Bitrix\Main\Loader
Definition
loader.php:12
Bitrix\Main\Loader\includeModule
static includeModule($moduleName)
Definition
loader.php:69
Bitrix\Main\ORM\Data\Result
Definition
result.php:16
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Main\Text\Encoding
Definition
encoding.php:8
Bitrix\Main\Web\HttpClient
Definition
httpclient.php:24
Bitrix\Main\Web\Json
Definition
json.php:11
Bitrix\Pull\Rest
Definition
rest.php:14
Bitrix\Rest\OAuthService
Definition
oauthservice.php:35
Bitrix\Sale\Rest\Synchronization\Client
Definition
client.php:18
Bitrix\Sale\Rest\Synchronization\Client\checkAccessToken
checkAccessToken($accessToken)
Definition
client.php:193
Bitrix\Sale\Rest\Synchronization\Client\$refreshToken
$refreshToken
Definition
client.php:22
Bitrix\Sale\Rest\Synchronization\Client\getHttpClient
getHttpClient()
Definition
client.php:36
Bitrix\Sale\Rest\Synchronization\Client\$clientSecret
$clientSecret
Definition
client.php:20
Bitrix\Sale\Rest\Synchronization\Client\prepareRequest
prepareRequest($params)
Definition
client.php:117
Bitrix\Sale\Rest\Synchronization\Client\__construct
__construct($clientId, $clientSecret, $serviceUrl)
Definition
client.php:29
Bitrix\Sale\Rest\Synchronization\Client\call
call($methodName, $additionalParams=[])
Definition
client.php:48
Bitrix\Sale\Rest\Synchronization\Client\prepareResponse
prepareResponse($result)
Definition
client.php:105
Bitrix\Sale\Rest\Synchronization\Client\HTTP_SOCKET_TIMEOUT
const HTTP_SOCKET_TIMEOUT
Definition
client.php:24
Bitrix\Sale\Rest\Synchronization\Client\$clientId
$clientId
Definition
client.php:19
Bitrix\Sale\Rest\Synchronization\Client\HTTP_STREAM_TIMEOUT
const HTTP_STREAM_TIMEOUT
Definition
client.php:25
Bitrix\Sale\Rest\Synchronization\Client\$serviceUrl
$serviceUrl
Definition
client.php:21
Bitrix\Sale\Rest\Synchronization\Client\refreshToken
refreshToken($refreshToken)
Definition
client.php:131
Bitrix\Sale\Rest\Synchronization\Client\B24_APP_GRANT_TYPE
const B24_APP_GRANT_TYPE
Definition
client.php:27
Bitrix\Sale\Rest\Synchronization\Client\getRequestUrl
getRequestUrl($methodName)
Definition
client.php:43
Bitrix\Sale\Rest\Synchronization\Client\SERVICE_PATH
const SERVICE_PATH
Definition
client.php:26
Bitrix\Sale\Rest\Synchronization\LoggerDiag\addMessage
static addMessage($messageId, $message='')
Definition
loggerdiag.php:37
Bitrix\Sale\Rest\Synchronization
Definition
client.php:4
Bitrix
modules
sale
lib
rest
synchronization
client.php
Создано системой
1.10.0