lk.zachem.info/app/Modules/Bitrix/Models/BitrixSender.php
Thekindbull 4a9f232723 - добавил авто-создание токенов в табл bx_id
- убрал политику на создание организации
2025-11-12 06:43:00 +08:00

104 lines
2.9 KiB
PHP

<?php
namespace Modules\Bitrix\Models;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Modules\Bitrix\Enums\BitrixWebhooksEnum;
use Modules\Bitrix\Models\BitrixWebhooks;
class BitrixSender
{
private $callbackUrl = false;
protected $data;
public $resultData;
public function __construct()
{
}
public function setCallbackUrl($callbackUrl)
{
$this->callbackUrl = $callbackUrl;
}
protected function getUrl()
{
$webhookEnumItem = false;
$childClass = get_class($this);
switch ( $childClass )
{
case SendAgent::class:
$webhookEnumItem = BitrixWebhooksEnum::CREATE_AGENT;
break;
case SendClient::class:
$webhookEnumItem = BitrixWebhooksEnum::CREATE_CONTACT;
break;
case SendCompany::class:
$webhookEnumItem = BitrixWebhooksEnum::CREATE_COMPANY;
break;
case SendDeal::class:
$webhookEnumItem = BitrixWebhooksEnum::CREATE_DEAL;
break;
case SendDealAgent::class:
$webhookEnumItem = BitrixWebhooksEnum::ADD_CLIENT_TO_DEAL;
break;
case SendDealClient::class:
$webhookEnumItem = BitrixWebhooksEnum::ADD_CLIENT_TO_DEAL;
break;
}
if ($webhookEnumItem)
{
return BitrixWebhooks::getUrlByName($webhookEnumItem);
}
else
{
throw new \Exception('Undefined url of webhook for ' . $childClass . ' class');
}
}
public function send()
{
if ($this->callbackUrl)
{
$this->data['callbackUrl'] = $this->callbackUrl;
}
$postdata = http_build_query(
$this->data
);
$opts = array(
'ssl' => array(
'verify_peer' => true,
'verify_peername' => true
),
'http' => array(
'method' => 'POST',
'header' =>
'Content-type: application/x-www-form-urlencoded' . "\r\n" .
'',
'content' => $postdata
)
);
try
{
$context = stream_context_create($opts);
$result = file_get_contents($this->getUrl(), false, $context);
$result = json_decode($result, $associative = true);
$this->resultData = $result;
if (is_array($this->resultData) && array_key_exists('result', $result))
{
return true;
}
else
{
return false;
}
}
catch (\Exception $e)
{
Log::build([
'driver' => 'single',
'path' => storage_path('logs/bitrix.log'),
])->error($e->getMessage());
return false;
}
}
}