|
|
|
@ -7,9 +7,9 @@ abstract class FormHandler { |
|
|
|
|
$this->nextHandler = $nextHandler; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function handle($data) { |
|
|
|
|
public function handle($data, $crmData) { |
|
|
|
|
if ($this->nextHandler) { |
|
|
|
|
return $this->nextHandler->handle($data); |
|
|
|
|
return $this->nextHandler->handle($data, $crmData); |
|
|
|
|
} |
|
|
|
|
return ['success' => true, 'message' => 'Форма обработана']; |
|
|
|
|
} |
|
|
|
@ -36,15 +36,30 @@ $utm_data = get_utm_data(); |
|
|
|
|
|
|
|
|
|
class b24Handler extends FormHandler { |
|
|
|
|
|
|
|
|
|
public function handle($data) { |
|
|
|
|
// Логика отправки в Mailchimp |
|
|
|
|
error_log("Отправка в b24: " . json_encode($data)); |
|
|
|
|
|
|
|
|
|
public function handle($data, $crmData) { |
|
|
|
|
$this->b24_add_lead( |
|
|
|
|
$crmData['phone'], |
|
|
|
|
$crmData['name'], |
|
|
|
|
$crmData['email'], |
|
|
|
|
$crmData['msg'], |
|
|
|
|
$crmData['url'], |
|
|
|
|
$crmData['stage'], |
|
|
|
|
$crmData['fName'], |
|
|
|
|
$crmData['order_total'], |
|
|
|
|
$crmData['user_id'], |
|
|
|
|
$crmData['method'], |
|
|
|
|
$crmData['form_title'], |
|
|
|
|
$crmData['is_subscribe'], |
|
|
|
|
$crmData['order_id'], |
|
|
|
|
); |
|
|
|
|
error_log('----------CRM_DATA----------------'); |
|
|
|
|
error_log(json_encode($crmData)); |
|
|
|
|
error_log('----------CRM_DATA----------------'); |
|
|
|
|
// Вызываем следующий обработчик в цепочке |
|
|
|
|
return parent::handle($data); |
|
|
|
|
return parent::handle($data, $crmData); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function b24_get_or_create_contact($phone, $name, $email, $utm, $userID = null, $isSubscribe) { |
|
|
|
|
function b24_get_or_create_contact($phone, $name, $email, $userID = null, $isSubscribe) { |
|
|
|
|
$contactId = null; |
|
|
|
|
$error = null; |
|
|
|
|
$foundContacts = []; |
|
|
|
@ -70,7 +85,7 @@ class b24Handler extends FormHandler { |
|
|
|
|
if (!empty($email)) $searchFilters[] = ['EMAIL' => $email]; |
|
|
|
|
|
|
|
|
|
foreach ($searchFilters as $filter) { |
|
|
|
|
$response = b24_request('crm.contact.list', [ |
|
|
|
|
$response = $this->b24_request('crm.contact.list', [ |
|
|
|
|
'filter' => $filter, |
|
|
|
|
'select' => ['ID', 'NAME', 'PHONE', 'EMAIL', 'UF_CRM_1744373080655', 'IM'] |
|
|
|
|
]); |
|
|
|
@ -92,8 +107,8 @@ class b24Handler extends FormHandler { |
|
|
|
|
$matches += 3; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!empty($phone) && valueExists($contact['PHONE'] ?? [], $phone)) $matches++; |
|
|
|
|
if (!empty($email) && valueExists($contact['EMAIL'] ?? [], $email)) $matches++; |
|
|
|
|
if (!empty($phone) && $this->valueExists($contact['PHONE'] ?? [], $phone)) $matches++; |
|
|
|
|
if (!empty($email) && $this->valueExists($contact['EMAIL'] ?? [], $email)) $matches++; |
|
|
|
|
|
|
|
|
|
if ($matches > $maxMatches) { |
|
|
|
|
$maxMatches = $matches; |
|
|
|
@ -109,11 +124,11 @@ class b24Handler extends FormHandler { |
|
|
|
|
$updateFields['NAME'] = $name; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!empty($phone) && !valueExists($bestMatch['PHONE'] ?? [], $phone)) { |
|
|
|
|
if (!empty($phone) && !$this->valueExists($bestMatch['PHONE'] ?? [], $phone)) { |
|
|
|
|
$updateFields['PHONE'][] = ['VALUE' => $phone, 'VALUE_TYPE' => 'MOBILE']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!empty($email) && !valueExists($bestMatch['EMAIL'] ?? [], $email)) { |
|
|
|
|
if (!empty($email) && !$this->valueExists($bestMatch['EMAIL'] ?? [], $email)) { |
|
|
|
|
$updateFields['EMAIL'][] = ['VALUE' => $email, 'VALUE_TYPE' => 'WORK']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -139,7 +154,7 @@ class b24Handler extends FormHandler { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!empty($updateFields)) { |
|
|
|
|
$updateResponse = b24_request('crm.contact.update', [ |
|
|
|
|
$updateResponse = $this->b24_request('crm.contact.update', [ |
|
|
|
|
'id' => $contactId, |
|
|
|
|
'fields' => $updateFields |
|
|
|
|
]); |
|
|
|
@ -179,7 +194,7 @@ class b24Handler extends FormHandler { |
|
|
|
|
$newContact['fields']['EMAIL'][] = ['VALUE' => $email, 'VALUE_TYPE' => 'WORK']; |
|
|
|
|
} |
|
|
|
|
if (!empty($utm)) { |
|
|
|
|
$newContact['fields']['UTM_SOURCE'] = $utm; |
|
|
|
|
$newContact['fields']['UTM_SOURCE'] = $utm_source; |
|
|
|
|
} |
|
|
|
|
if (!empty($userID)) { |
|
|
|
|
$newContact['fields']['UF_CRM_1744373080655'] = $userID; |
|
|
|
@ -196,7 +211,7 @@ class b24Handler extends FormHandler { |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$response = b24_request('crm.contact.add', $newContact); |
|
|
|
|
$response = $this->b24_request('crm.contact.add', $newContact); |
|
|
|
|
$contactId = $response['result'] ?? null; |
|
|
|
|
$error = $response['error'] ?? null; |
|
|
|
|
} |
|
|
|
@ -270,7 +285,7 @@ class b24Handler extends FormHandler { |
|
|
|
|
error_log('DEAL DATA: ' . json_encode($dealData)); |
|
|
|
|
|
|
|
|
|
// Отправляем данные в Bitrix24 |
|
|
|
|
$newDealResponse = b24_request($method, $dealData); |
|
|
|
|
$newDealResponse = $this->b24_request($method, $dealData); |
|
|
|
|
|
|
|
|
|
error_log('DEAL RESP: ' . json_encode($newDealResponse)); |
|
|
|
|
|
|
|
|
@ -297,9 +312,9 @@ class b24Handler extends FormHandler { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Function to add lead (wraps b24_get_or_create_contact and b24_send_lead) |
|
|
|
|
function b24_add_lead($phone, $name, $email, $msg, $utm, $url, $stage, $fName, $order_total = 0, $userID = null, $method = 'crm.deal.add', $form_title, $isSubscribe = false, $order_id = '') { |
|
|
|
|
$contact = $this->b24_get_or_create_contact($phone, $name, $email, $utm, $userID, $isSubscribe); |
|
|
|
|
|
|
|
|
|
function b24_add_lead($phone, $name, $email, $msg, $url, $stage, $fName, $order_total = 0, $userID = null, $method = 'crm.deal.add', $form_title, $isSubscribe = false, $order_id = '') { |
|
|
|
|
$contact = $this->b24_get_or_create_contact($phone, $name, $email, $userID, $isSubscribe); |
|
|
|
|
error_log(json_encode($contact)); |
|
|
|
|
if (!empty($contact['contact_id'])) { |
|
|
|
|
$contactId = $contact['contact_id']; |
|
|
|
|
} else { |
|
|
|
@ -326,7 +341,7 @@ class b24Handler extends FormHandler { |
|
|
|
|
$tg_username = get_user_meta($userID, 'tg_username', true); |
|
|
|
|
|
|
|
|
|
// Ищем контакт по ID пользователя |
|
|
|
|
$response = b24_request('crm.contact.list', [ |
|
|
|
|
$response = $this->b24_request('crm.contact.list', [ |
|
|
|
|
'filter' => ['UF_CRM_1744373080655' => $userID], |
|
|
|
|
'select' => ['ID', 'NAME', 'PHONE', 'EMAIL', 'UF_CRM_1744373080655', 'IM', 'UF_CRM_1744562461053'] |
|
|
|
|
]); |
|
|
|
@ -386,7 +401,7 @@ class b24Handler extends FormHandler { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Send update request |
|
|
|
|
$updateResponse = b24_request('crm.contact.update', [ |
|
|
|
|
$updateResponse = $this->b24_request('crm.contact.update', [ |
|
|
|
|
'id' => $contactId, |
|
|
|
|
'fields' => $updateFields |
|
|
|
|
]); |
|
|
|
@ -400,17 +415,17 @@ class b24Handler extends FormHandler { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class zohoHandler extends FormHandler { |
|
|
|
|
public function handle($data) { |
|
|
|
|
public function handle($data, $crmData) { |
|
|
|
|
// Логика отправки в HubSpot |
|
|
|
|
error_log("Отправка в Zoho: " . json_encode($data)); |
|
|
|
|
|
|
|
|
|
return parent::handle($data); |
|
|
|
|
return parent::handle($data, $crmData); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class tgHandler extends FormHandler { |
|
|
|
|
|
|
|
|
|
public function handle($data) { |
|
|
|
|
public function handle($data, $crmData) { |
|
|
|
|
|
|
|
|
|
global $site_env; |
|
|
|
|
if ($site_env->site_region == 'ru'){ |
|
|
|
@ -428,7 +443,7 @@ class tgHandler extends FormHandler { |
|
|
|
|
$this->sendToTelegram($botToken, $chatId, $message); |
|
|
|
|
|
|
|
|
|
// Вызываем следующий обработчик в цепочке |
|
|
|
|
return parent::handle($data); |
|
|
|
|
return parent::handle($data, $crmData); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Функция для отправки данных в Telegram |
|
|
|
@ -466,7 +481,7 @@ class tgHandler extends FormHandler { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class emailHandler extends FormHandler { |
|
|
|
|
public function handle($data) { |
|
|
|
|
public function handle($data, $crmData) { |
|
|
|
|
|
|
|
|
|
$to = 'pro@cosmopet.shop'; // Укажите email, на который нужно отправить данные |
|
|
|
|
$subject = 'Форма обработана'; // Тема письма |
|
|
|
@ -478,12 +493,13 @@ class emailHandler extends FormHandler { |
|
|
|
|
wp_mail($to, $subject, $message, $headers); |
|
|
|
|
|
|
|
|
|
// Вызываем следующий обработчик в цепочке |
|
|
|
|
return parent::handle($data); |
|
|
|
|
return parent::handle($data, $crmData); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class FormHandlerFactory { |
|
|
|
|
public static function getHandler(array $enabledHandlers) { |
|
|
|
|
global $site_env; |
|
|
|
|
$handler = null; |
|
|
|
|
if (in_array('email', $enabledHandlers)) { |
|
|
|
|
$handler = new emailHandler($handler); |
|
|
|
@ -494,7 +510,7 @@ class FormHandlerFactory { |
|
|
|
|
if (in_array('tg', $enabledHandlers)) { |
|
|
|
|
$handler = new tgHandler($handler); |
|
|
|
|
} |
|
|
|
|
if (in_array('b24', $enabledHandlers)) { |
|
|
|
|
if (in_array('b24', $enabledHandlers) && $site_env->site_region == 'ru') { |
|
|
|
|
$handler = new b24Handler($handler); |
|
|
|
|
} |
|
|
|
|
return $handler ?: new DefaultHandler(); |
|
|
|
|