48 public static function create($type, $externalId)
66 $this->data = self::getData($this->type, $this->externalId);
69 protected static function answer(array $answer)
73 $APPLICATION->restartBuffer();
74 header(
'Content-Type:application/json; charset=UTF-8');
76 echo Json::encode($answer);
78 \CMain::finalActions();
82 protected static function answerError($code =
null, $text =
null)
86 $code = self::ANSWER_ERROR_SYSTEM;
91 $errorMessages = array(
92 self::ANSWER_ERROR_SYSTEM =>
'Error.',
93 self::ANSWER_ERROR_NO_CODE =>
'Parameter `code` not found.',
94 self::ANSWER_ERROR_NO_EXT_ID =>
'Parameter `externalId` not found.',
95 self::ANSWER_ERROR_NO_SEC_CODE =>
'Parameter `sec` not found.',
96 self::ANSWER_ERROR_WRONG_SEC_CODE =>
'Wrong `sec` parameter.',
99 $text = $errorMessages[$code];
103 'error' => array(
'code' => $code,
'text' => $text),
121 public static function listen()
123 $request = Context::getCurrent()->getRequest();
124 $type = $request->get(
'code');
127 self::answerError(self::ANSWER_ERROR_NO_CODE);
131 $securityCode = $request->get(
'sec');
134 self::answerError(self::ANSWER_ERROR_NO_SEC_CODE);
137 $externalId = $request->get(
'externalId');
140 self::answerError(self::ANSWER_ERROR_NO_EXT_ID);
146 $payload = Json::decode($request->get(
'payload'));
147 $payload = (
new Payload\Batch())->setArray($payload);
149 catch (ArgumentException $e)
151 self::answerError(self::ANSWER_ERROR_NO_PAYLOAD);
154 $instance = self::create($type, $externalId);
155 if (!$instance->checkSecurityCode($securityCode))
157 self::answerError(self::ANSWER_ERROR_WRONG_SEC_CODE);
162 $instance->handle($payload);
164 catch (\Exception $e)
166 self::answerError($e->getCode(), $e->getMessage());
172 foreach ($instance->getErrorCollection()->toArray() as $error)
175 self::answerError($error->getCode(), $error->getMessage());
187 public function handle(Payload\Batch $payload)
189 $this->payload = $payload;
201 public function register(array $parameters = [])
206 'TYPE' => $this->type,
207 'EXTERNAL_ID' => $this->externalId,
209 if (!empty($parameters[
'SECURITY_CODE']))
211 $addParameters[
'SECURITY_CODE'] = $parameters[
'SECURITY_CODE'];
213 $addResult = Internals\WebHookTable::add($addParameters);
214 if (!$addResult->isSuccess())
219 $this->data = self::getData($this->type, $this->externalId);
222 $result = self::queryHookRegister(
223 'seo.client.webhook.register',
225 'CODE' => $this->data[
'TYPE'],
226 'EXTERNAL_ID' => $this->data[
'EXTERNAL_ID'],
227 'SECURITY_CODE' => $this->data[
'SECURITY_CODE'],
228 'CONFIRMATION_CODE' => isset($parameters[
'CONFIRMATION_CODE']) ?
229 $parameters[
'CONFIRMATION_CODE']
240 return self::queryHookRegister(
241 'seo.client.form.register',
243 'FORM_ID' => $formId,
250 return self::queryHookRegister(
251 'seo.client.form.unregister',
253 'FORM_ID' => $formId,
263 public function remove()
265 $result = self::queryHookRegister(
266 'seo.client.webhook.remove',
268 'CODE' => $this->type,
269 'EXTERNAL_ID' => $this->externalId
277 $deleteResult = Internals\WebHookTable::delete($this->data[
'ID']);
278 $result = $deleteResult->isSuccess();
285 protected static function getData($type, $externalId)
287 $list = Internals\WebHookTable::getList(array(
294 return $list->fetch();
299 $engine =
new EngineBitrix();
300 if (!$engine->isRegistered())
305 $response = $engine->getInterface()->getTransport()->call($methodName, $parameters);
306 return (isset($response[
'result'][
'RESULT']) && $response[
'result'][
'RESULT']);
317 return ($this->data && $this->data[
'SECURITY_CODE'] === $securityCode);
327 return $this->errorCollection;
330 protected function sendEvent()
332 $event =
new Event(
'seo',
'OnWebHook', array(
333 'PAYLOAD' => $this->payload,
336 foreach ($event->getResults() as $result)
338 $parameters = $result->getParameters();
339 if (!empty($parameters[
'ERROR_COLLECTION']))
342 $resultErrorCollection = $parameters[
'ERROR_COLLECTION'];
343 $this->errorCollection->add($resultErrorCollection->toArray());