1
0
mirror of synced 2024-11-25 14:56:09 +03:00

Изменена логика генерации externalId для оплат

This commit is contained in:
Pavel 2019-05-31 15:51:40 +03:00
parent 65e5085275
commit 057a529170
7 changed files with 101 additions and 14 deletions

View File

@ -1,5 +1,6 @@
## 2019-07-01 v.2.4.8
* Исправлена отправка пустого заказа при удалении заказа из СMS
* Изменена логика генерации внешнего идентификатора оплат для сохранения его уникальности в пределах системы
## 2019-03-28 v.2.4.7
* Добавлено удаление в системе типа цены у товара для неактивного типа цены на сайте

View File

@ -700,7 +700,7 @@ class ApiClient
}
return $this->client->makeRequest(
sprintf('/orders/payments/%s/edit', $payment[$by]),
sprintf('/orders/payments/%s/edit', urlencode($payment[$by])),
Client::METHOD_POST,
$this->fillSite(
$site,

View File

@ -268,6 +268,67 @@ class RCrmActions
return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET);
}
/**
* Extracts payment ID or client ID from payment externalId
* Payment ID - pass nothing or 'id' as second argument
* Client ID - pass 'client_id' as second argument
*
* @param $externalId
* @param string $data
* @return bool|string
*/
public static function getFromPaymentExternalId($externalId, $data = 'id')
{
switch ($data) {
case 'id':
if (false === strpos($externalId, '_')) {
return $externalId;
} else {
return substr($externalId, 0, strpos($externalId, '_'));
}
break;
case 'client_id':
if (false === strpos($externalId, '_')) {
return '';
} else {
return substr($externalId, strpos($externalId, '_'), count($externalId));
}
break;
}
return '';
}
/**
* Generates payment external ID
*
* @param $id
*
* @return string
*/
public static function generatePaymentExternalId($id)
{
return sprintf(
'%s_%s',
$id,
COption::GetOptionString(self::$MODULE_ID, 'client_id', 0)
);
}
/**
* Returns true if provided externalId in new format (id_clientId)
*
* @param $externalId
* @return bool
*/
public static function isNewExternalId($externalId)
{
return !(false === strpos($externalId, '_'));
}
/**
* Unserialize array
*

View File

@ -267,10 +267,15 @@ class RetailCrmEvent
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;
}
}
}
}
if (!empty($arPayment['PAY_SYSTEM_ID']) && isset($optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']])) {
$paymentToCrm = array(
@ -279,7 +284,7 @@ class RetailCrmEvent
);
if (!empty($arPayment['ID'])) {
$paymentToCrm['externalId'] = $arPayment['ID'];
$paymentToCrm['externalId'] = RCrmActions::generatePaymentExternalId($arPayment['ID']);
}
if (!empty($arPayment['DATE_PAID'])) {
@ -303,12 +308,28 @@ class RetailCrmEvent
return false;
}
if (!array_key_exists($arPayment['ID'], $paymentsExternalIds)) {
$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)) {
RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site);
} elseif (array_key_exists($arPayment['ID'], $paymentsExternalIds) && $paymentsExternalIds[$arPayment['ID']]['type'] == $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]) {
} elseif ($paymentData['type'] == $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]) {
$paymentToCrm['externalId'] = $paymentData['externalId'];
RCrmActions::apiMethod($api, 'paymentEditByExternalId', __METHOD__, $paymentToCrm, $site);
} elseif (array_key_exists($arPayment['ID'], $paymentsExternalIds) && $paymentsExternalIds[$arPayment['ID']]['type'] != $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]) {
RCrmActions::apiMethod($api, 'ordersPaymentDelete', __METHOD__, $paymentsExternalIds[$arPayment['ID']]['id']);
} elseif ($paymentData['type'] != $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']]) {
RCrmActions::apiMethod(
$api,
'ordersPaymentDelete',
__METHOD__,
$paymentData['id']
);
RCrmActions::apiMethod($api, 'ordersPaymentCreate', __METHOD__, $paymentToCrm, $site);
}
@ -356,7 +377,10 @@ class RetailCrmEvent
if (isset($orderCrm['order']['payments']) && $orderCrm['order']['payments']) {
foreach ($orderCrm['order']['payments'] as $payment) {
if (isset($payment['externalId']) && $payment['externalId'] == $event->getId()) {
if (isset($payment['externalId'])
&& ($payment['externalId'] == $event->getId()
|| RCrmActions::getFromPaymentExternalId($payment['externalId']) == $event->getId())
) {
RCrmActions::apiMethod($api, 'ordersPaymentDelete', __METHOD__, $payment['id']);
}
}

View File

@ -762,12 +762,13 @@ class RetailCrmHistory
if (!empty($newHistoryPayments)) {
foreach ($newOrder->getPaymentCollection() as $orderPayment) {
if (array_key_exists($orderPayment->getField('XML_ID'), $newHistoryPayments)) {
$paymentExternalId = $orderPayment->getId();
$paymentId = $orderPayment->getId();
$paymentExternalId = RCrmActions::generatePaymentExternalId($paymentId);
if ($paymentExternalId) {
$newHistoryPayments[$orderPayment->getField('XML_ID')]['externalId'] = $paymentExternalId;
RCrmActions::apiMethod($api, 'paymentEditById', __METHOD__, $newHistoryPayments[$orderPayment->getField('XML_ID')]);
\Bitrix\Sale\Internals\PaymentTable::update($paymentExternalId, array('XML_ID' => ''));
\Bitrix\Sale\Internals\PaymentTable::update($paymentId, array('XML_ID' => ''));
}
}
}
@ -1140,7 +1141,7 @@ class RetailCrmHistory
foreach ($paymentsCrm['payments'] as $paymentCrm) {
if (isset($paymentCrm['externalId']) && !empty($paymentCrm['externalId'])) {
//find the payment
$nowPayment = $paymentsList[$paymentCrm['externalId']];
$nowPayment = $paymentsList[RCrmActions::getFromPaymentExternalId($paymentCrm['externalId'])];
//update data
if ($nowPayment instanceof \Bitrix\Sale\Payment) {
$nowPayment->setField('SUM', $paymentCrm['amount']);

View File

@ -205,7 +205,7 @@ class RetailCrmOrder
'amount' => $payment['SUM']
);
if (!empty($payment['ID'])) {
$pm['externalId'] = $payment['ID'];
$pm['externalId'] = RCrmActions::generatePaymentExternalId($payment['ID']);
}
if (!empty($payment['DATE_PAID'])) {
$pm['paidAt'] = new \DateTime($payment['DATE_PAID']);

View File

@ -1,5 +1,5 @@
<?
$arModuleVersion = array(
"VERSION" => "2.4.7",
"VERSION_DATE" => "2019-05-23 16:30:00"
"VERSION" => "2.4.8",
"VERSION_DATE" => "2019-06-03 13:10:00"
);