1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
base.php
См. документацию.
1<?php
3
11
12class Base
13{
14 const LOG_DIR = '/bitrix/modules/sale/lib/exchange/integration/log';
15 const LOG_PATH = 'rest_client.log';
16
17 protected $accessToken;
18 protected $refreshToken;
19 protected $endPoint;
20 protected $expiresIn;
21
22 public function __construct(array $settings)
23 {
24 if (isset($settings["accessToken"]))
25 {
26 $this->setAccessToken($settings["accessToken"]);
27 }
28
29 if (isset($settings["refreshToken"]))
30 {
31 $this->setRefreshToken($settings["refreshToken"]);
32 }
33
34 if (isset($settings["endPoint"]))
35 {
36 $this->setEndPoint($settings["endPoint"]);
37 }
38 }
39
40 public function getAccessToken()
41 {
42 return $this->accessToken;
43 }
44
46 {
47 if (is_string($accessToken) && !empty($accessToken))
48 {
49 $this->accessToken = $accessToken;
50 }
51 }
52
53 public function getRefreshToken()
54 {
55 return $this->refreshToken;
56 }
57
59 {
60 if (is_string($refreshToken) && !empty($refreshToken))
61 {
62 $this->refreshToken = $refreshToken;
63 }
64 }
65
66 public function getEndPoint()
67 {
68 return $this->endPoint;
69 }
70
71 public function setEndPoint($endPoint)
72 {
73 if (is_string($endPoint) && !empty($endPoint))
74 {
75 $this->endPoint = $endPoint;
76 }
77 }
78
79 public function getExpiresIn()
80 {
81 return $this->expiresIn;
82 }
83
84 public function setExpiresIn($expiresIn)
85 {
86 $this->expiresIn = $expiresIn;
87 }
88
89 public function call($method, $params = [])
90 {
92
93 if (isset($response["error"]) && $response["error"] === "expired_token")
94 {
95 if ($this->refreshAccessToken())
96 {
98 }
99 }
100
101 return $response;
102 }
103
104 protected function makeRequest($method, $params = [])
105 {
106 $accessToken = $this->getAccessToken();
107 if ($accessToken === null)
108 {
109 throw new ObjectPropertyException("Access Token must be set.");
110 }
111
112 $endPoint = $this->getEndPoint();
113 if ($endPoint === null)
114 {
115 throw new ObjectPropertyException("End Point URL must be set.");
116 }
117
118 $httpClient = new HttpClient();
119 $httpClient->setHeader("User-Agent", "Bitrix Integration B24");
120 $httpClient->setCharset("UTF-8");
121
122 $params["auth"] = $accessToken;
123
124 $this->log("\\----------\n");
125 $this->log(['endpoint'=>$endPoint.$method,'params'=>$params]);
126 $this->log([$endPoint.$method.'?'.http_build_query($params)]);
127
128 $success = $httpClient->post($endPoint.$method, $params);
129 if (!$success)
130 {
131 throw new SystemException("Wrong Rest Response. ".$endPoint.$method);
132 }
133
134 $result = $httpClient->getResult();
135
136 $this->log(['result'=>$result]);
137 $this->log("\n ----------//\n");
138
139 try
140 {
141 $response = Json::decode($result);
142 }
143 catch (\Exception $exception)
144 {
145 throw new SystemException(
146 "Wrong Rest Response. ".$endPoint.$method."\n\n".mb_substr($result, 0, 1024)
147 );
148 }
149
150 return $response;
151 }
152
153 protected function refreshAccessToken()
154 {
156 if ($refreshToken !== null)
157 {
158 $oauthClient = new OAuth\Bitrix24();
159 $response = $oauthClient->getAccessToken(
160 "refresh_token",
161 ["refresh_token" => $this->refreshToken]
162 );
163
164 if (!isset($response["error"]) && is_array($response))
165 {
166 $this->setAccessToken($response["access_token"]);
167 $this->setRefreshToken($response["refresh_token"]);
168
169 return true;
170 }
171 }
172
173 return false;
174 }
175
180 protected function log($params)
181 {
182 if($this->isOnLog() == false)
183 {
184 return;
185 }
186
187 $dir = $_SERVER['DOCUMENT_ROOT'].static::LOG_DIR;
188 if(is_dir($dir) || @mkdir($dir, BX_DIR_PERMISSIONS))
189 {
190 $f = fopen($dir.'/'.static::LOG_PATH, "a+");
191 fwrite($f, print_r($params, true));
192 fclose($f);
193 }
194 }
195
196 protected function isOnLog()
197 {
198 return \Bitrix\Main\Config\Option::get("sale", "log_integration_b24_rest_client", 'N') == 'Y';
199 }
200}
Определения json.php:9
setRefreshToken($refreshToken)
Определения base.php:58
makeRequest($method, $params=[])
Определения base.php:104
__construct(array $settings)
Определения base.php:22
setAccessToken($accessToken)
Определения base.php:45
call($method, $params=[])
Определения base.php:89
$f
Определения component_props.php:52
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
$success
Определения mail_entry.php:69
$settings
Определения product_settings.php:43
$dir
Определения quickway.php:303
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$response
Определения result.php:21
$method
Определения index.php:27