Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
accountvkontakte.php
1<?php
2
4
16use PhpParser\Node\Expr\Isset_;
17
24{
25 public const TYPE_CODE = LeadAds\Service::TYPE_VKONTAKTE;
26
27 public const RESOURCE_LEAD = 'LEAD';
28
29 public const URL_ACCOUNT_LIST = 'https://vk.com/groups?tab=admin';
30
31 public const URL_INFO = 'https://vk.com/page-19542789_53868676';
32
33 protected static $listRowMap = array(
34 'ID' => 'ID',
35 'NAME' => 'NAME',
36 );
37
45 public function getRowById(string $id)
46 {
47 $list = $this->getList();
48 while ($row = $list->fetch())
49 {
50 if ($row['ID'] === $id)
51 {
52 return $row;
53 }
54 }
55
56 return null;
57 }
58
64 public function getList(): Response
65 {
66 // https://vk.com/dev/groups.get
67 $response = $this->getRequest()->send(array(
68 'methodName' => 'leadads.groups.list',
69 'parameters' => array(
70 'fields' => 'id,name',
71 'extended' => 1,
72 'filter' => 'admin'
73 )
74 ));
75 $items = $response->getData();
76 $items = (!empty($items['items']) && is_array($items['items'])) ? $items['items'] : [];
77
78 $response->setData($items);
79
80 return $response;
81 }
82
88 public function getProfile(): ?array
89 {
90 $response = $this->getRequest()->send([
91 'methodName' => 'leadads.profile',
92 'parameters' => []
93 ]);
94
95 if ($response->isSuccess() && $data = $response->fetch())
96 {
97 $result = [
98 'ID' => $data['ID'],
99 'NAME' => $data['FIRST_NAME'] . ' ' . $data['LAST_NAME'],
100 ];
101
102 $result['LINK'] = 'https://ads.vk.com/hq/leadforms/';
103 $result['PICTURE'] = (Context::getCurrent()->getRequest()->isHttps() ? 'https' : 'http')
104 . '://'
105 . Context::getCurrent()->getServer()->getHttpHost() . '/bitrix/images/seo/integration/vklogo.svg';
106
107 return $result;
108 }
109
110 return null;
111 }
112
113 public function checkNewAuthInfo(): bool
114 {
115 $response = $this->checkAuthInfo();
116 if (!$response->isSuccess())
117 {
118 return true;
119 }
120 $data = $response->getData();
121 if (isset($data['hasAuthInfo']) && isset($data['hasProfile']))
122 {
123 if (!$data['hasAuthInfo'])
124 {
125 return true;
126 }
127
128 return $data['hasProfile'];
129 }
130
131 return true;
132 }
133
134 public function logout()
135 {
136 $response = $this->unsubscribeToLeadAdding();
137 if ($response->isSuccess())
138 {
139 $data = $response->getData();
140 if (isset($data['id']) && isset($data['type']))
141 {
142 $this->unregisterCode((int)$response->getData()['id'], $response->getData()['type']);
143 }
144
145 if (isset($data['form_ids']) && is_array($data['form_ids']))
146 {
147 return (new Result())->setData(['formIds' => $data['form_ids']]);
148 }
149 }
150
151 return new Result();
152 }
153
154 public function loginCompletion()
155 {
156 $response = $this->subcribeToLeadAdding(self::RESOURCE_LEAD);
157 if ($response->isSuccess())
158 {
159 return $this->registerCode((int)$response->getData()['id'], self::RESOURCE_LEAD);
160 }
161
162 return (new Result())->addError(new Error('Failed to perform all actions after authorization'));
163 }
164
165 public function hasPageAccount()
166 {
167 return false;
168 }
169
170 protected function subcribeToLeadAdding(string $resource): Response
171 {
172 $remoteServiceUrl = Service::SERVICE_URL;
173 $webhookQueryParams = http_build_query(
174 [
175 'code' => LeadAds\Service::getEngineCode(static::TYPE_CODE),
176 'action' => 'web_hook',
177 ]
178 );
179
180 return $this->getRequest()->send([
181 'methodName' => 'leadads.subscribe.lead.add',
182 'parameters' => [
183 'resource' => $resource,
184 'callback_url' => "{$remoteServiceUrl}/register/?{$webhookQueryParams}",
185 ]
186 ]);
187 }
188
189 private function registerCode(int $confirmCode, string $resource)
190 {
192
193 $isRegistered = $this->registerWebHook($confirmCode,
194 [
195 'SECURITY_CODE' => Random::getString(32),
196 'CONFIRMATION_CODE' => $resource,
197 ]
198 );
199
200 if (!$isRegistered)
201 {
202 $response->addError(new Error('Can not register web hook.'));
203
204 return $response;
205 }
206
207 return $response;
208 }
209
210 protected function registerWebHook($confirmCode, array $parameters = []): bool
211 {
212 return WebHook\Service::create(
213 \Bitrix\Seo\LeadAds\Service::getEngineCode(static::TYPE_CODE),
214 $confirmCode
215 )->register($parameters);
216 }
217
218 protected function unsubscribeToLeadAdding()
219 {
220 return $this->getRequest()->send([
221 'methodName' => 'leadads.unsubscribe.lead.add',
222 'parameters' => []
223 ]);
224 }
225
226 protected function unregisterCode(int $externalId, string $type)
227 {
228 $webHook = $this->getWebHookByExternalId($externalId, $type);
229 if ($webHook)
230 {
231 WebHook\Internals\WebHookTable::delete($webHook['ID']);
232 }
233
234 return new Result();
235 }
236
237 protected function getWebHookByExternalId($externalId, $type)
238 {
239 return WebHook\Internals\WebHookTable::getRow([
240 'filter' => [
241 'EXTERNAL_ID' => $externalId,
242 'TYPE' => $type,
243 ]
244 ]);
245 }
246
247 protected function checkAuthInfo()
248 {
249 return $this->getRequest()->send([
250 'methodName' => 'leadads.account.check.auth',
251 'parameters' => []
252 ]);
253 }
254}
static getCurrent()
Definition context.php:241
static getEngineCode($type)
Definition service.php:129
unregisterCode(int $externalId, string $type)
registerWebHook($confirmCode, array $parameters=[])