Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
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
45 public function setAccessToken($accessToken)
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
58 public function setRefreshToken($refreshToken)
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 {
91 $response = $this->makeRequest($method, $params);
92
93 if (isset($response["error"]) && $response["error"] === "expired_token")
94 {
95 if ($this->refreshAccessToken())
96 {
97 $response = $this->makeRequest($method, $params);
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 if (!Application::getInstance()->isUtfMode())
124 {
125 $params = Encoding::convertEncoding($params, SITE_CHARSET, "UTF-8");
126 }
127
128 $this->log("\\----------\n");
129 $this->log(['endpoint'=>$endPoint.$method,'params'=>$params]);
130 $this->log([$endPoint.$method.'?'.http_build_query($params)]);
131
132 $success = $httpClient->post($endPoint.$method, $params);
133 if (!$success)
134 {
135 throw new SystemException("Wrong Rest Response. ".$endPoint.$method);
136 }
137
138 $result = $httpClient->getResult();
139
140 $this->log(['result'=>$result]);
141 $this->log("\n ----------//\n");
142
143 try
144 {
145 $response = Json::decode($result);
146 }
147 catch (\Exception $exception)
148 {
149 throw new SystemException(
150 "Wrong Rest Response. ".$endPoint.$method."\n\n".mb_substr($result, 0, 1024)
151 );
152 }
153
154 return $response;
155 }
156
157 protected function refreshAccessToken()
158 {
160 if ($refreshToken !== null)
161 {
162 $oauthClient = new OAuth\Bitrix24();
163 $response = $oauthClient->getAccessToken(
164 "refresh_token",
165 ["refresh_token" => $this->refreshToken]
166 );
167
168 if (!isset($response["error"]) && is_array($response))
169 {
170 $this->setAccessToken($response["access_token"]);
171 $this->setRefreshToken($response["refresh_token"]);
172
173 return true;
174 }
175 }
176
177 return false;
178 }
179
184 protected function log($params)
185 {
186 if($this->isOnLog() == false)
187 {
188 return;
189 }
190
191 $dir = $_SERVER['DOCUMENT_ROOT'].static::LOG_DIR;
192 if(is_dir($dir) || @mkdir($dir, BX_DIR_PERMISSIONS))
193 {
194 $f = fopen($dir.'/'.static::LOG_PATH, "a+");
195 fwrite($f, print_r($params, true));
196 fclose($f);
197 }
198 }
199
200 protected function isOnLog()
201 {
202 return \Bitrix\Main\Config\Option::get("sale", "log_integration_b24_rest_client", 'N') == 'Y';
203 }
204}