1
0
mirror of synced 2024-11-25 23:06:08 +03:00
bitrix-module/intaro.retailcrm/classes/general/events/RetailCrmEvent.php

600 lines
22 KiB
PHP
Raw Normal View History

2016-09-15 16:42:10 +03:00
<?php
2020-04-24 13:18:18 +03:00
2016-09-15 16:42:10 +03:00
/**
2019-02-28 14:45:39 +03:00
* Class RetailCrmEvent
2016-09-15 16:42:10 +03:00
*/
2016-10-04 17:57:39 +03:00
class RetailCrmEvent
2018-12-26 11:41:06 +03:00
{
2016-09-15 16:42:10 +03:00
protected static $MODULE_ID = 'intaro.retailcrm';
protected static $CRM_API_HOST_OPTION = 'api_host';
protected static $CRM_API_KEY_OPTION = 'api_key';
protected static $CRM_ORDER_TYPES_ARR = 'order_types_arr';
protected static $CRM_DELIVERY_TYPES_ARR = 'deliv_types_arr';
protected static $CRM_PAYMENT_TYPES = 'pay_types_arr';
protected static $CRM_PAYMENT_STATUSES = 'pay_statuses_arr';
protected static $CRM_PAYMENT = 'payment_arr'; //order payment Y/N
protected static $CRM_ORDER_LAST_ID = 'order_last_id';
protected static $CRM_ORDER_PROPS = 'order_props';
protected static $CRM_LEGAL_DETAILS = 'legal_details';
protected static $CRM_CUSTOM_FIELDS = 'custom_fields';
protected static $CRM_CONTRAGENT_TYPE = 'contragent_type';
protected static $CRM_ORDER_FAILED_IDS = 'order_failed_ids';
protected static $CRM_SITES_LIST = 'sites_list';
2020-04-24 13:18:18 +03:00
protected static $CRM_CC = 'cc';
protected static $CRM_CORP_NAME = 'nickName-corporate';
protected static $CRM_CORP_ADRES = 'adres-corporate';
2018-12-26 11:41:06 +03:00
2016-09-15 16:42:10 +03:00
/**
2019-02-28 14:45:39 +03:00
* @param $arFields
2018-12-26 11:41:06 +03:00
*
2019-02-28 14:45:39 +03:00
* @return bool
* @throws InvalidArgumentException
2016-09-15 16:42:10 +03:00
*/
2016-10-04 17:57:39 +03:00
function OnAfterUserUpdate($arFields)
2019-02-28 14:45:39 +03:00
{
2016-10-04 17:57:39 +03:00
if (isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) {
2019-02-28 14:45:39 +03:00
return false;
2016-09-15 16:42:10 +03:00
}
2018-12-26 11:41:06 +03:00
2016-10-04 17:57:39 +03:00
if (!$arFields['RESULT']) {
2019-02-28 14:45:39 +03:00
return false;
2016-09-15 16:42:10 +03:00
}
2018-12-26 11:41:06 +03:00
2020-04-24 13:18:18 +03:00
$optionsSitesList = RetailcrmConfigProvider::getSitesList();
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
2016-10-03 16:56:59 +03:00
$resultOrder = RetailCrmUser::customerEdit($arFields, $api, $optionsSitesList);
2019-02-28 14:45:39 +03:00
2016-10-04 17:57:39 +03:00
if (!$resultOrder) {
2016-10-03 16:56:59 +03:00
RCrmActions::eventLog('RetailCrmEvent::OnAfterUserUpdate', 'RetailCrmUser::customerEdit', 'error update customer');
}
2019-02-28 14:45:39 +03:00
return true;
}
2016-09-15 16:42:10 +03:00
/**
* onUpdateOrder
2019-02-28 14:45:39 +03:00
*
2020-04-24 13:18:18 +03:00
* @param mixed $ID - Order id
2016-09-15 16:42:10 +03:00
* @param mixed $arFields - Order arFields
*/
2016-10-04 17:57:39 +03:00
function onUpdateOrder($ID, $arFields)
{
if (isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY']) {
2019-02-28 14:45:39 +03:00
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = false;
2016-09-15 16:42:10 +03:00
return;
2019-02-28 14:45:39 +03:00
}
2018-12-26 11:41:06 +03:00
2017-09-04 11:36:04 +03:00
$GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] = true;
if (($arFields['CANCELED'] == 'Y')
2020-04-24 13:18:18 +03:00
&& (sizeof($arFields['BASKET_ITEMS']) == 0)
&& (sizeof($arFields['ORDER_PROP']) == 0)
) {
$GLOBALS['ORDER_DELETE_USER_ADMIN'] = true;
}
2016-09-15 16:42:10 +03:00
return;
}
2018-12-26 11:41:06 +03:00
2016-09-15 16:42:10 +03:00
/**
* orderDelete
2019-02-28 14:45:39 +03:00
*
2016-09-15 16:42:10 +03:00
* @param object $event - Order object
*/
2016-10-04 17:57:39 +03:00
function orderDelete($event)
{
2017-09-04 11:36:04 +03:00
$GLOBALS['RETAILCRM_ORDER_DELETE'] = true;
2016-09-15 16:42:10 +03:00
return;
}
2018-12-26 11:41:06 +03:00
2016-09-15 16:42:10 +03:00
/**
2019-02-28 14:45:39 +03:00
* @param $event
2018-12-26 11:41:06 +03:00
*
* @return bool
* @throws \Bitrix\Main\ObjectPropertyException
* @throws \Bitrix\Main\SystemException
* @throws \Bitrix\Main\ArgumentException
2016-09-15 16:42:10 +03:00
*/
2016-10-04 17:57:39 +03:00
function orderSave($event)
{
if (true == $GLOBALS['ORDER_DELETE_USER_ADMIN']) {
return false;
}
2019-02-28 14:45:39 +03:00
if ($GLOBALS['RETAILCRM_ORDER_OLD_EVENT'] === false
&& $GLOBALS['RETAILCRM_ORDER_DELETE'] === true
) {
return false;
}
2017-09-04 11:36:04 +03:00
2019-07-30 09:58:15 +03:00
if ($GLOBALS['RETAIL_CRM_HISTORY'] === true) {
return false;
}
2019-02-28 14:45:39 +03:00
if (!CModule::IncludeModule('iblock')) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'iblock', 'module not found');
2016-09-15 16:42:10 +03:00
2019-02-28 14:45:39 +03:00
return false;
}
2017-09-04 11:36:04 +03:00
2019-02-28 14:45:39 +03:00
if (!CModule::IncludeModule("sale")) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'sale', 'module not found');
2016-09-15 16:42:10 +03:00
2019-02-28 14:45:39 +03:00
return false;
}
2017-09-04 11:36:04 +03:00
2019-02-28 14:45:39 +03:00
if (!CModule::IncludeModule("catalog")) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'catalog', 'module not found');
2016-09-15 16:42:10 +03:00
2019-02-28 14:45:39 +03:00
return false;
}
2017-09-04 11:36:04 +03:00
2020-04-24 13:18:18 +03:00
//exists getParameter("ENTITY")
2019-02-28 14:45:39 +03:00
if (method_exists($event, 'getId')) {
$obOrder = $event;
} elseif (method_exists($event, 'getParameter')) {
$obOrder = $event->getParameter("ENTITY");
} else {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'events', 'event error');
2016-09-15 16:42:10 +03:00
2019-02-28 14:45:39 +03:00
return false;
}
2019-02-28 14:45:39 +03:00
$arOrder = RetailCrmOrder::orderObjToArr($obOrder);
2016-09-15 16:42:10 +03:00
2020-04-24 13:18:18 +03:00
$api = new RetailCrm\ApiClient(RetailcrmConfigProvider::getApiUrl(), RetailcrmConfigProvider::getApiKey());
2017-09-04 11:36:04 +03:00
2019-02-28 14:45:39 +03:00
//params
2020-04-24 13:18:18 +03:00
$optionsOrderTypes = RetailcrmConfigProvider::getOrderTypes();
$optionsDelivTypes = RetailcrmConfigProvider::getDeliveryTypes();
$optionsPayTypes = RetailcrmConfigProvider::getPaymentTypes();
$optionsPayStatuses = RetailcrmConfigProvider::getPaymentStatuses(); // --statuses
$optionsPayment = RetailcrmConfigProvider::getPayment();
$optionsSitesList = RetailcrmConfigProvider::getSitesList();
$optionsOrderProps = RetailcrmConfigProvider::getOrderProps();
$optionsLegalDetails = RetailcrmConfigProvider::getLegalDetails();
$optionsContragentType = RetailcrmConfigProvider::getContragentTypes();
$optionsCustomFields = RetailcrmConfigProvider::getCustomFields();
//corp cliente swich
$optionCorpClient = RetailcrmConfigProvider::getCorporateClientStatus();
2019-02-28 14:45:39 +03:00
$arParams = RCrmActions::clearArr(array(
2020-04-24 13:18:18 +03:00
'optionsOrderTypes' => $optionsOrderTypes,
'optionsDelivTypes' => $optionsDelivTypes,
'optionsPayTypes' => $optionsPayTypes,
'optionsPayStatuses' => $optionsPayStatuses,
'optionsPayment' => $optionsPayment,
'optionsOrderProps' => $optionsOrderProps,
'optionsLegalDetails' => $optionsLegalDetails,
2019-02-28 14:45:39 +03:00
'optionsContragentType' => $optionsContragentType,
2020-04-24 13:18:18 +03:00
'optionsSitesList' => $optionsSitesList,
'optionsCustomFields' => $optionsCustomFields
2019-02-28 14:45:39 +03:00
));
//many sites?
if ($optionsSitesList) {
if (array_key_exists($arOrder['LID'], $optionsSitesList) && $optionsSitesList[$arOrder['LID']] !== null) {
$site = $optionsSitesList[$arOrder['LID']];
} else {
return false;
2016-09-15 16:42:10 +03:00
}
2019-02-28 14:45:39 +03:00
} elseif (!$optionsSitesList) {
$site = null;
}
2016-09-15 16:42:10 +03:00
2019-02-28 14:45:39 +03:00
//new order?
$orderCrm = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $arOrder['ID'], $site);
if (isset($orderCrm['order'])) {
$methodApi = 'ordersEdit';
$arParams['crmOrder'] = $orderCrm['order'];
} else {
$methodApi = 'ordersCreate';
}
2020-04-24 13:18:18 +03:00
$orderCompany = null;
if ("Y" === $optionCorpClient && $optionsContragentType[$arOrder['PERSON_TYPE_ID']] == 'legal-entity') {
2020-04-24 13:18:18 +03:00
//corparate cliente
$nickName = '';
$address = '';
$corpAddress = '';
$contragent = array();
$userCorp = array();
$corpName = RetailcrmConfigProvider::getCorporateClientName();
$corpAddress = RetailcrmConfigProvider::getCorporateClientAddress();
foreach ($arOrder['PROPS']['properties'] as $prop) {
if ($prop['CODE'] == $corpName) {
$nickName = $prop['VALUE'][0];
}
if ($prop['CODE'] == $corpAddress) {
$address = $prop['VALUE'][0];
}
if (!empty($optionsLegalDetails)
&& $search = array_search($prop['CODE'], $optionsLegalDetails[$arOrder['PERSON_TYPE_ID']])
) {
$contragent[$search] = $prop['VALUE'][0];//legal order data
}
}
if (!empty($contragentType)) {
$contragent['contragentType'] = $contragentType;
}
$customersCorporate = false;
$response = $api->customersCorporateList(array('companyName' => $nickName));
if ($response && $response->getStatusCode() == 200) {
$customersCorporate = $response['customersCorporate'];
$singleCorp = reset($customersCorporate);
if (!empty($singleCorp)) {
$userCorp['customerCorporate'] = $singleCorp;
$companiesResponse = $api->customersCorporateCompanies(
$singleCorp['id'],
array(),
null,
null,
'id',
$site
);
if ($companiesResponse && $companiesResponse->isSuccessful()) {
$orderCompany = array_reduce(
$companiesResponse['companies'],
function ($carry, $item) use ($nickName) {
if (is_array($item) && $item['name'] == $nickName) {
$carry = $item;
}
return $carry;
},
null
);
}
}
} else {
RCrmActions::eventLog(
'RetailCrmEvent::orderSave',
'ApiClient::customersCorporateList',
'error during fetching corporate customers'
);
2017-09-04 11:36:04 +03:00
2019-02-28 14:45:39 +03:00
return false;
2016-09-15 16:42:10 +03:00
}
2020-04-24 13:18:18 +03:00
//user
$userCrm = RCrmActions::apiMethod($api, 'customersGet', __METHOD__, $arOrder['USER_ID'], $site);
if (!isset($userCrm['customer'])) {
$arUser = Bitrix\Main\UserTable::getById($arOrder['USER_ID'])->fetch();
if (!empty($address)) {
$arUser['PERSONAL_STREET'] = $address;
}
$resultUser = RetailCrmUser::customerSend($arUser, $api, "individual", true, $site);
if (!$resultUser) {
RCrmActions::eventLog(
__CLASS__ . '::' . __METHOD__,
'RetailCrmUser::customerSend',
'error during creating customer'
);
return false;
}
$userCrm = array('customer' => array('externalId' => $arOrder['USER_ID']));
2020-04-24 13:18:18 +03:00
}
if (!isset($userCorp['customerCorporate'])) {
$resultUserCorp = RetailCrmCorporateClient::clientSend(
$arOrder,
$api,
$optionsContragentType[$arOrder['PERSON_TYPE_ID']],
true,
false,
$site
);
Logger::getInstance()->write($resultUserCorp, 'resultUserCorp');
if (!$resultUserCorp) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmCorporateClient::clientSend', 'error during creating client');
return false;
}
$arParams['customerCorporate'] = $resultUserCorp;
$arParams['orderCompany'] = isset($resultUserCorp['mainCompany']) ? $resultUserCorp['mainCompany'] : null;
$customerCorporateAddress = array();
$customerCorporateCompany = array();
$addressResult = null;
$companyResult = null;
if (!empty($address)) {
//TODO address builder add
$customerCorporateAddress = array(
'name' => $nickName,
'isMain' => true,
'text' => $address
);
$addressResult = $api->customersCorporateAddressesCreate($resultUserCorp['id'], $customerCorporateAddress, 'id', $site);
}
$customerCorporateCompany = array(
'name' => $nickName,
'isMain' => true,
'contragent' => $contragent
);
if (!empty($addressResult)) {
$customerCorporateCompany['address'] = array(
'id' => $addressResult['id']
);
}
$companyResult = $api->customersCorporateCompaniesCreate($resultUserCorp['id'], $customerCorporateCompany, 'id', $site);
$customerCorporateContact = array(
'isMain' => true,
'customer' => array(
'externalId' => $arOrder['USER_ID'],
'site' => $site
)
);
if (!empty($companyResult)) {
$orderCompany = array(
'id' => $companyResult['id']
);
2020-04-24 13:18:18 +03:00
$customerCorporateContact['companies'] = array(
array(
'company' => $orderCompany
2020-04-24 13:18:18 +03:00
)
);
}
$api->customersCorporateContactsCreate(
2020-04-24 13:18:18 +03:00
$resultUserCorp['id'],
$customerCorporateContact,
'id',
$site
);
$arParams['orderCompany'] = array_merge(
$customerCorporateCompany,
array('id' => $companyResult['id'])
);
} else {
2020-04-24 13:18:18 +03:00
RetailCrmCorporateClient::addCustomersCorporateAddresses(
$userCorp['customerCorporate']['id'],
$nickName,
$address,
$api,
$site = null
);
$arParams['customerCorporate'] = $userCorp['customerCorporate'];
if (!empty($orderCompany)) {
$arParams['orderCompany'] = $orderCompany;
}
}
$arParams['contactExId'] = $userCrm['customer']['externalId'];
} else {
//user
$userCrm = RCrmActions::apiMethod($api, 'customersGet', __METHOD__, $arOrder['USER_ID'], $site);
if (!isset($userCrm['customer'])) {
$arUser = Bitrix\Main\UserTable::getById($arOrder['USER_ID'])->fetch();
$resultUser = RetailCrmUser::customerSend($arUser, $api, $optionsContragentType[$arOrder['PERSON_TYPE_ID']], true, $site);
if (!$resultUser) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmUser::customerSend', 'error during creating customer');
return false;
}
}
2019-02-28 14:45:39 +03:00
}
2016-09-15 16:42:10 +03:00
2019-02-28 14:45:39 +03:00
//order
$resultOrder = RetailCrmOrder::orderSend($arOrder, $api, $arParams, true, $site, $methodApi);
2020-04-24 13:18:18 +03:00
2019-02-28 14:45:39 +03:00
if (!$resultOrder) {
RCrmActions::eventLog('RetailCrmEvent::orderSave', 'RetailCrmOrder::orderSend', 'error during creating order');
return false;
2016-09-15 16:42:10 +03:00
}
2017-09-04 11:36:04 +03:00
return true;
2016-09-15 16:42:10 +03:00
}
2017-10-24 11:41:11 +03:00
/**
2019-02-28 14:45:39 +03:00
* @param \Bitrix\Sale\Payment $event
*
2020-04-24 13:18:18 +03:00
* @return bool
2019-02-28 14:45:39 +03:00
* @throws InvalidArgumentException
*
2017-10-24 11:41:11 +03:00
*/
function paymentSave($event)
2018-05-23 12:19:59 +03:00
{
2017-10-25 12:34:33 +03:00
$apiVersion = COption::GetOptionString(self::$MODULE_ID, 'api_version', 0);
2018-05-23 12:19:59 +03:00
2018-12-26 11:41:06 +03:00
/** @var \Bitrix\Sale\Order $order */
$order = $event->getCollection()->getOrder();
if ((isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY'])
|| $apiVersion != 'v5'
|| $order->isNew()
) {
2019-02-28 14:45:39 +03:00
return false;
2017-10-24 11:41:11 +03:00
}
2020-04-24 13:18:18 +03:00
$optionsSitesList = RetailcrmConfigProvider::getSitesList();
$optionsPaymentTypes = RetailcrmConfigProvider::getPaymentTypes();
$optionsPayStatuses = RetailcrmConfigProvider::getPayment();
2017-10-24 11:41:11 +03:00
$arPayment = array(
2020-04-24 13:18:18 +03:00
'ID' => $event->getId(),
'ORDER_ID' => $event->getField('ORDER_ID'),
'PAID' => $event->getField('PAID'),
2017-10-24 11:41:11 +03:00
'PAY_SYSTEM_ID' => $event->getField('PAY_SYSTEM_ID'),
2020-04-24 13:18:18 +03:00
'SUM' => $event->getField('SUM'),
'LID' => $order->getSiteId(),
'DATE_PAID' => $event->getField('DATE_PAID'),
2017-10-24 11:41:11 +03:00
);
2017-10-26 16:30:50 +03:00
if ($optionsSitesList) {
2018-01-12 11:14:33 +03:00
if (array_key_exists($arPayment['LID'], $optionsSitesList) && $optionsSitesList[$arPayment['LID']] !== null) {
$site = $optionsSitesList[$arPayment['LID']];
2017-10-26 16:30:50 +03:00
} else {
2019-02-28 14:45:39 +03:00
return false;
2017-10-26 16:30:50 +03:00
}
} elseif (!$optionsSitesList) {
2017-10-24 11:41:11 +03:00
$site = null;
}
2017-10-24 11:41:11 +03:00
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
$api = new RetailCrm\ApiClient($api_host, $api_key);
$orderCrm = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $arPayment['ORDER_ID'], $site);
if (isset($orderCrm['order'])) {
$payments = $orderCrm['order']['payments'];
}
2018-10-31 16:25:53 +03:00
if ($payments) {
foreach ($payments as $payment) {
if (isset($payment['externalId'])) {
if (RCrmActions::isNewExternalId($payment['externalId'])) {
$paymentsExternalIds[RCrmActions::getFromPaymentExternalId($payment['externalId'])] =
$payment;
} else {
$paymentsExternalIds[$payment['externalId']] = $payment;
}
2017-10-24 11:41:11 +03:00
}
}
2018-10-31 16:25:53 +03:00
}
2017-10-24 11:41:11 +03:00
2018-10-31 16:25:53 +03:00
if (!empty($arPayment['PAY_SYSTEM_ID']) && isset($optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']])) {
$paymentToCrm = array(
'type' => $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]
2018-10-31 16:25:53 +03:00
);
2017-10-24 11:41:11 +03:00
2018-10-31 16:25:53 +03:00
if (!empty($arPayment['ID'])) {
$paymentToCrm['externalId'] = RCrmActions::generatePaymentExternalId($arPayment['ID']);
2018-10-31 16:25:53 +03:00
}
2017-10-24 11:41:11 +03:00
2018-10-31 16:25:53 +03:00
if (!empty($arPayment['DATE_PAID'])) {
if (is_object($arPayment['DATE_PAID'])) {
$culture = new Bitrix\Main\Context\Culture(array("FORMAT_DATETIME" => "YYYY-MM-DD HH:MI:SS"));
$paymentToCrm['paidAt'] = $arPayment['DATE_PAID']->toString($culture);
} elseif (is_string($arPayment['DATE_PAID'])) {
$paymentToCrm['paidAt'] = $arPayment['DATE_PAID'];
2017-10-24 11:41:11 +03:00
}
2018-10-31 16:25:53 +03:00
}
2017-10-24 11:41:11 +03:00
2018-10-31 16:25:53 +03:00
if (!empty($optionsPayStatuses[$arPayment['PAID']])) {
$paymentToCrm['status'] = $optionsPayStatuses[$arPayment['PAID']];
2017-11-20 15:47:11 +03:00
}
2018-10-31 16:25:53 +03:00
if (!empty($arPayment['ORDER_ID'])) {
$paymentToCrm['order']['externalId'] = $arPayment['ORDER_ID'];
2017-10-24 11:41:11 +03:00
}
if (RetailcrmConfigProvider::shouldSendPaymentAmount()) {
$paymentToCrm['amount'] = $arPayment['SUM'];
}
2018-10-31 16:25:53 +03:00
} else {
RCrmActions::eventLog('RetailCrmEvent::paymentSave', 'payments', 'OrderID = ' . $arPayment['ID'] . '. Payment not found.');
2019-02-28 14:45:39 +03:00
return false;
2018-10-31 16:25:53 +03:00
}
$arPaymentExtId = RCrmActions::generatePaymentExternalId($arPayment['ID']);
if (array_key_exists($arPaymentExtId, $paymentsExternalIds)) {
$paymentData = $paymentsExternalIds[$arPaymentExtId];
} elseif (array_key_exists($arPayment['ID'], $paymentsExternalIds)) {
$paymentData = $paymentsExternalIds[$arPayment['ID']];
} else {
$paymentData = array();
}
if (empty($paymentData)) {
2018-10-31 16:25:53 +03:00
RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site);
} elseif ($paymentData['type'] == $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]) {
$paymentToCrm['externalId'] = $paymentData['externalId'];
2018-10-31 16:25:53 +03:00
RCrmActions::apiMethod($api, 'paymentEditByExternalId', __METHOD__, $paymentToCrm, $site);
} elseif ($paymentData['type'] != $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]) {
RCrmActions::apiMethod(
$api,
'ordersPaymentDelete',
__METHOD__,
$paymentData['id']
);
2018-10-31 16:25:53 +03:00
RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site);
2017-10-24 11:41:11 +03:00
}
2019-02-28 14:45:39 +03:00
return true;
2017-10-24 11:41:11 +03:00
}
/**
2019-02-28 14:45:39 +03:00
* @param \Bitrix\Sale\Payment $event
*
* @throws InvalidArgumentException
2017-10-24 11:41:11 +03:00
*/
function paymentDelete($event)
2018-01-12 11:14:33 +03:00
{
2017-10-25 12:34:33 +03:00
$apiVersion = COption::GetOptionString(self::$MODULE_ID, 'api_version', 0);
2017-11-20 15:47:11 +03:00
2018-12-26 11:41:06 +03:00
if ((isset($GLOBALS['RETAIL_CRM_HISTORY']) && $GLOBALS['RETAIL_CRM_HISTORY'])
|| $apiVersion != 'v5'
|| !$event->getId()
) {
2017-10-24 11:41:11 +03:00
return;
}
$optionsSitesList = unserialize(COption::GetOptionString(self::$MODULE_ID, self::$CRM_SITES_LIST, 0));
$arPayment = array(
2020-04-24 13:18:18 +03:00
'ID' => $event->getId(),
'ORDER_ID' => $event->getField('ORDER_ID'),
'LID' => $event->getCollection()->getOrder()->getSiteId()
);
2018-01-12 11:14:33 +03:00
if ($optionsSitesList) {
if (array_key_exists($arPayment['LID'], $optionsSitesList) && $optionsSitesList[$arPayment['LID']] !== null) {
$site = $optionsSitesList[$arPayment['LID']];
} else {
return;
}
} elseif (!$optionsSitesList) {
$site = null;
}
2017-10-24 11:41:11 +03:00
$api_host = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_HOST_OPTION, 0);
$api_key = COption::GetOptionString(self::$MODULE_ID, self::$CRM_API_KEY_OPTION, 0);
$api = new RetailCrm\ApiClient($api_host, $api_key);
$orderCrm = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $arPayment['ORDER_ID'], $site);
2017-10-24 11:41:11 +03:00
if (isset($orderCrm['order']['payments']) && $orderCrm['order']['payments']) {
foreach ($orderCrm['order']['payments'] as $payment) {
if (isset($payment['externalId'])
&& ($payment['externalId'] == $event->getId()
|| RCrmActions::getFromPaymentExternalId($payment['externalId']) == $event->getId())
) {
2017-10-24 11:41:11 +03:00
RCrmActions::apiMethod($api, 'ordersPaymentDelete', __METHOD__, $payment['id']);
}
}
}
}
2019-02-28 14:45:39 +03:00
}