From 057a529170f6c0f9642d53ebd57575c2d73dd49a Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 31 May 2019 15:51:40 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=B3=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20externalId=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BE=D0=BF=D0=BB=D0=B0=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../classes/general/ApiClient_v5.php | 2 +- .../classes/general/RCrmActions.php | 61 +++++++++++++++++++ .../classes/general/events/RetailCrmEvent.php | 38 +++++++++--- .../general/history/RetailCrmHistory_v5.php | 7 ++- .../general/order/RetailCrmOrder_v5.php | 2 +- intaro.retailcrm/install/version.php | 4 +- 7 files changed, 101 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 308926bd..b1dba487 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 2019-07-01 v.2.4.8 * Исправлена отправка пустого заказа при удалении заказа из СMS +* Изменена логика генерации внешнего идентификатора оплат для сохранения его уникальности в пределах системы ## 2019-03-28 v.2.4.7 * Добавлено удаление в системе типа цены у товара для неактивного типа цены на сайте diff --git a/intaro.retailcrm/classes/general/ApiClient_v5.php b/intaro.retailcrm/classes/general/ApiClient_v5.php index 9476f2df..90f519f4 100644 --- a/intaro.retailcrm/classes/general/ApiClient_v5.php +++ b/intaro.retailcrm/classes/general/ApiClient_v5.php @@ -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, diff --git a/intaro.retailcrm/classes/general/RCrmActions.php b/intaro.retailcrm/classes/general/RCrmActions.php index 0ebf2589..324a2eaa 100644 --- a/intaro.retailcrm/classes/general/RCrmActions.php +++ b/intaro.retailcrm/classes/general/RCrmActions.php @@ -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 * diff --git a/intaro.retailcrm/classes/general/events/RetailCrmEvent.php b/intaro.retailcrm/classes/general/events/RetailCrmEvent.php index fc924dd3..1497a6ce 100644 --- a/intaro.retailcrm/classes/general/events/RetailCrmEvent.php +++ b/intaro.retailcrm/classes/general/events/RetailCrmEvent.php @@ -267,7 +267,12 @@ class RetailCrmEvent if ($payments) { foreach ($payments as $payment) { if (isset($payment['externalId'])) { - $paymentsExternalIds[$payment['externalId']] = $payment; + if (RCrmActions::isNewExternalId($payment['externalId'])) { + $paymentsExternalIds[RCrmActions::getFromPaymentExternalId($payment['externalId'])] = + $payment; + } else { + $paymentsExternalIds[$payment['externalId']] = $payment; + } } } } @@ -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']); } } diff --git a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php index fe451d6f..1e5f6def 100644 --- a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php +++ b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php @@ -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']); diff --git a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php index 1f7ce7ba..15e88449 100644 --- a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php +++ b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php @@ -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']); diff --git a/intaro.retailcrm/install/version.php b/intaro.retailcrm/install/version.php index d3b44a29..2a0899db 100644 --- a/intaro.retailcrm/install/version.php +++ b/intaro.retailcrm/install/version.php @@ -1,5 +1,5 @@ "2.4.7", - "VERSION_DATE" => "2019-05-23 16:30:00" + "VERSION" => "2.4.8", + "VERSION_DATE" => "2019-06-03 13:10:00" ); From 366e3e2b1d465819e486d38fe985375948a95649 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 3 Jun 2019 13:14:35 +0300 Subject: [PATCH 2/7] Closes: #61 --- intaro.retailcrm/install/version.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/intaro.retailcrm/install/version.php b/intaro.retailcrm/install/version.php index 2a0899db..a0b9cc0a 100644 --- a/intaro.retailcrm/install/version.php +++ b/intaro.retailcrm/install/version.php @@ -1,5 +1,5 @@ "2.4.8", - "VERSION_DATE" => "2019-06-03 13:10:00" + "VERSION_DATE" => "2019-06-03 13:14:00" ); From 5eb64d9c844713819f00220c2d4a5ef76bdd119f Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 2 Jul 2019 13:46:13 +0300 Subject: [PATCH 3/7] fix: don't remove payments after changes in retailCRM --- .../classes/general/history/RetailCrmHistory_v5.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php index 1e5f6def..b397592d 100644 --- a/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php +++ b/intaro.retailcrm/classes/general/history/RetailCrmHistory_v5.php @@ -1141,7 +1141,8 @@ class RetailCrmHistory foreach ($paymentsCrm['payments'] as $paymentCrm) { if (isset($paymentCrm['externalId']) && !empty($paymentCrm['externalId'])) { //find the payment - $nowPayment = $paymentsList[RCrmActions::getFromPaymentExternalId($paymentCrm['externalId'])]; + $nowPaymentId = RCrmActions::getFromPaymentExternalId($paymentCrm['externalId']); + $nowPayment = $paymentsList[$nowPaymentId]; //update data if ($nowPayment instanceof \Bitrix\Sale\Payment) { $nowPayment->setField('SUM', $paymentCrm['amount']); @@ -1153,7 +1154,7 @@ class RetailCrmHistory $nowPayment->setField('PAID', $optionsPayment[$paymentCrm['status']]); } - unset($paymentsList[$paymentCrm['externalId']]); + unset($paymentsList[$nowPaymentId]); } } else { $newHistoryPayments[$paymentCrm['id']] = $paymentCrm; From 376024e2bdc5a223e943b39225bd773a23e5d460 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 3 Jul 2019 13:11:20 +0300 Subject: [PATCH 4/7] Backward compatibility with previously created payments --- intaro.retailcrm/classes/general/RCrmActions.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/intaro.retailcrm/classes/general/RCrmActions.php b/intaro.retailcrm/classes/general/RCrmActions.php index 324a2eaa..351a4549 100644 --- a/intaro.retailcrm/classes/general/RCrmActions.php +++ b/intaro.retailcrm/classes/general/RCrmActions.php @@ -302,6 +302,17 @@ class RCrmActions return ''; } + /** + * Returns true if provided externalId in new format (id_clientId) + * + * @param $externalId + * @return bool + */ + public static function isNewExternalId($externalId) + { + return !(false === strpos($externalId, '_')); + } + /** * Generates payment external ID * From d65e248ba84da279e2b44b194facac2216f3ced1 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 3 Jul 2019 13:34:33 +0300 Subject: [PATCH 5/7] Remove copied method --- intaro.retailcrm/classes/general/RCrmActions.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/intaro.retailcrm/classes/general/RCrmActions.php b/intaro.retailcrm/classes/general/RCrmActions.php index 351a4549..1ab08b0f 100644 --- a/intaro.retailcrm/classes/general/RCrmActions.php +++ b/intaro.retailcrm/classes/general/RCrmActions.php @@ -329,17 +329,6 @@ class RCrmActions ); } - /** - * 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 * From 24f862d93f1231b43846d942711737b692cee39a Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 3 Jul 2019 13:36:50 +0300 Subject: [PATCH 6/7] Update description.ru --- intaro.retailcrm/description.ru | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/intaro.retailcrm/description.ru b/intaro.retailcrm/description.ru index 52758530..2e195072 100644 --- a/intaro.retailcrm/description.ru +++ b/intaro.retailcrm/description.ru @@ -1 +1,2 @@ -- Добавлено удаление неактивного типа цены у товара в системе +- Исправлена отправка пустого заказа при удалении заказа из СMS +- Изменена логика генерации внешнего идентификатора оплат для сохранения его уникальности в пределах системы \ No newline at end of file From c9cf33cfbba568113d82fd8626b51e1108c74e75 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 3 Jul 2019 14:03:14 +0300 Subject: [PATCH 7/7] Fix MySQL error in Travis CI --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b6a84236..fd43b599 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,7 +19,7 @@ env: before_script: # Change MySQL root password - - echo "USE mysql;\nUPDATE user SET password=PASSWORD('root') WHERE user='root';\nFLUSH PRIVILEGES;\n" | mysql -u root + - echo "USE mysql;\nUPDATE user SET authentication_string=PASSWORD('root') WHERE user='root';\nFLUSH PRIVILEGES;\n" | mysql -u root - composer install - bash tests/bin/before_script.sh