1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
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
124
return
$params
;
125
}
126
127
public
function
refreshToken
(
$refreshToken
)
128
{
129
$result
=
new
Result
();
130
131
if
(
$refreshToken
==
''
)
132
{
133
$result
->addError(
new
Error
(
'Refresh token is empty'
));
134
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_EMPTY'
);
135
}
136
137
if
(!$this->clientId || !$this->clientSecret)
138
{
139
$result
->addError(
new
Error
(
'No client credentials for refresh token'
));
140
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_CLIENT_ID_EMPTY'
);
141
}
142
143
if
(
$result
->isSuccess())
144
{
145
$request
=
OAuthService::SERVICE_URL
.
'/oauth/token/'
.
'?'
.http_build_query(
146
[
147
'grant_type'
=>self::B24_APP_GRANT_TYPE,
148
'client_id'
=>$this->clientId,
149
'client_secret'
=>$this->clientSecret,
150
'refresh_token'
=>
$refreshToken
151
]);
152
153
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_REQUEST'
, var_export(
$request
,
true
));
154
155
$httpClient = $this->
getHttpClient
();
156
$httpResult = $httpClient->get(
$request
);
157
158
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_PROCESS_RESULT'
, var_export([
159
'result'
=>$httpResult,
160
'status'
=>$httpClient->getStatus()
161
],
true
));
162
163
$respons = $this->
prepareResponse
($httpResult);
164
if
($respons)
165
{
166
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_PROCESS_RESULT_SUCCESS'
);
167
168
if
(isset($respons[
'error'
]))
169
{
170
$result
->addError(
new
Error
($respons[
'error_description'
], mb_strtoupper($respons[
'error'
])));
171
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_RESULT_ERROR'
);
172
}
173
else
174
{
175
$result
->setData([
'DATA'
=>$respons]);
176
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_RESULT_SUCCESS'
, var_export($respons,
true
));
177
}
178
}
179
else
180
{
181
$result
->addError(
new
Error
(
'Strange answer from Bitrix Service! '
,
'STRANGE_ANSWER_REFRESH_TOKEN'
));
182
LoggerDiag::addMessage
(
'CLIENT_REFRESH_TOKEN_PROCESS_RESULT_ERROR'
);
183
}
184
}
185
186
return
$result
;
187
}
188
189
public
function
checkAccessToken
($accessToken)
190
{
191
$result
=
new
Result
();
192
193
if
(!
Loader::includeModule
(
'rest'
))
194
$result
->addError(
new
Error
(
'Module REST is not included'
));
195
196
if
(
$result
->isSuccess())
197
{
198
if
(!\
Bitrix
\
Rest
\
OAuthService::getEngine
()->isRegistered())
199
{
200
try
201
{
202
\Bitrix\Rest\OAuthService::register
();
203
}
204
catch
(\
Bitrix
\
Main
\
SystemException
$e)
205
{
206
$result
->addError(
new
Error
(
'OAuthServiceError'
,
' OAUTH_SERVICE_ERROR'
));
207
}
208
}
209
210
if
(
$result
->isSuccess())
211
{
212
$client =
\Bitrix\Rest\OAuthService::getEngine
()->getClient();
213
$respons = $client->call(
'app.info'
, [
'auth'
=> $accessToken]);
214
if
(isset($respons[
'error'
]))
215
$result
->addError(
new
Error
($respons[
'error_description'
], mb_strtoupper($respons[
'error'
])));
216
}
217
}
218
return
$result
;
219
}
220
}
$request
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения
catalog_reindex.php:36
Bitrix\Main\ArgumentException
Определения
ArgumentException.php:9
Bitrix\Main\Error
Определения
error.php:15
Bitrix\Main\Loader
Определения
loader.php:13
Bitrix\Main\Loader\includeModule
static includeModule($moduleName)
Определения
loader.php:67
Bitrix\Main\SystemException
Определения
SystemException.php:9
Bitrix\Main\Text\Encoding
Определения
encoding.php:8
Bitrix\Main\Web\HttpClient
Определения
httpclient.php:24
Bitrix\Main\Web\Json
Определения
json.php:9
Bitrix\Rest\OAuthService
Определения
oauthservice.php:38
Bitrix\Rest\OAuthService\getEngine
static getEngine()
Определения
oauthservice.php:49
Bitrix\Rest\OAuthService\register
static register()
Определения
oauthservice.php:59
Bitrix\Rest\OAuthService\SERVICE_URL
const SERVICE_URL
Определения
oauthservice.php:39
Bitrix\Sale\Rest\Synchronization\Client
Определения
client.php:18
Bitrix\Sale\Rest\Synchronization\Client\checkAccessToken
checkAccessToken($accessToken)
Определения
client.php:189
Bitrix\Sale\Rest\Synchronization\Client\$refreshToken
$refreshToken
Определения
client.php:22
Bitrix\Sale\Rest\Synchronization\Client\getHttpClient
getHttpClient()
Определения
client.php:36
Bitrix\Sale\Rest\Synchronization\Client\$clientSecret
$clientSecret
Определения
client.php:20
Bitrix\Sale\Rest\Synchronization\Client\prepareRequest
prepareRequest($params)
Определения
client.php:117
Bitrix\Sale\Rest\Synchronization\Client\__construct
__construct($clientId, $clientSecret, $serviceUrl)
Определения
client.php:29
Bitrix\Sale\Rest\Synchronization\Client\call
call($methodName, $additionalParams=[])
Определения
client.php:48
Bitrix\Sale\Rest\Synchronization\Client\prepareResponse
prepareResponse($result)
Определения
client.php:105
Bitrix\Sale\Rest\Synchronization\Client\HTTP_SOCKET_TIMEOUT
const HTTP_SOCKET_TIMEOUT
Определения
client.php:24
Bitrix\Sale\Rest\Synchronization\Client\$clientId
$clientId
Определения
client.php:19
Bitrix\Sale\Rest\Synchronization\Client\HTTP_STREAM_TIMEOUT
const HTTP_STREAM_TIMEOUT
Определения
client.php:25
Bitrix\Sale\Rest\Synchronization\Client\$serviceUrl
$serviceUrl
Определения
client.php:21
Bitrix\Sale\Rest\Synchronization\Client\refreshToken
refreshToken($refreshToken)
Определения
client.php:127
Bitrix\Sale\Rest\Synchronization\Client\B24_APP_GRANT_TYPE
const B24_APP_GRANT_TYPE
Определения
client.php:27
Bitrix\Sale\Rest\Synchronization\Client\getRequestUrl
getRequestUrl($methodName)
Определения
client.php:43
Bitrix\Sale\Rest\Synchronization\Client\SERVICE_PATH
const SERVICE_PATH
Определения
client.php:26
Bitrix\Sale\Rest\Synchronization\LoggerDiag\addMessage
static addMessage($messageId, $message='')
Определения
loggerdiag.php:37
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
$result
$result
Определения
get_property_values.php:14
Bitrix\Main\Rest
Определения
handlers.php:8
Bitrix\Main
Bitrix\Sale\Discount\Result
Определения
compatibleformat.php:2
Bitrix\Sale\Rest\Synchronization
Определения
client.php:4
Bitrix
$params
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения
template.php:799
bitrix
modules
sale
lib
rest
synchronization
client.php
Создано системой
1.14.0