From 53767392e98f94929b03e7dffdee1f809d5a933c Mon Sep 17 00:00:00 2001 From: Daniel Weiser Date: Mon, 13 Jul 2020 14:49:59 +0300 Subject: [PATCH 1/3] resend orders with new customer --- .../model/extension/retailcrm/order.php | 65 ++++++++++++------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/src/upload/catalog/model/extension/retailcrm/order.php b/src/upload/catalog/model/extension/retailcrm/order.php index c8e67fb..f01a10c 100644 --- a/src/upload/catalog/model/extension/retailcrm/order.php +++ b/src/upload/catalog/model/extension/retailcrm/order.php @@ -37,28 +37,7 @@ class ModelExtensionRetailcrmOrder extends Model { } if (!isset($order['customer']['externalId']) && !isset($order['customer']['id'])) { - $new_customer = array( - 'firstName' => $data['firstname'], - 'lastName' => $data['lastname'], - 'email' => $data['email'], - 'createdAt' => $data['date_added'], - 'address' => array( - 'countryIso' => $data['payment_iso_code_2'], - 'index' => $data['payment_postcode'], - 'city' => $data['payment_city'], - 'region' => $data['payment_zone'], - 'text' => $data['payment_address_1'] . ' ' . $data['payment_address_2'] - ) - ); - - if (!empty($data['telephone'])) { - $new_customer['phones'] = array( - array( - 'number' => $data['telephone'] - ) - ); - } - + $new_customer = $this->createCustomer($data); $res = $retailcrmApiClient->customersCreate($new_customer); if ($res->isSuccessful() && isset($res['id'])) { @@ -68,12 +47,20 @@ class ModelExtensionRetailcrmOrder extends Model { if ($create) { $order = self::filterRecursive($order); - $retailcrmApiClient->ordersCreate($order); + $response = $retailcrmApiClient->ordersCreate($order); + if ($response->getStatusCode() === 400 && !empty($response->__get('errors')['customer.externalId'])) { + $order['customer'] = $this->createCustomer($data); + $response = $retailcrmApiClient->ordersEdit($order); + } } else { $order_payment = reset($order['payments']); unset($order['payments']); $order = self::filterRecursive($order); $response = $retailcrmApiClient->ordersEdit($order); + if ($response->getStatusCode() === 400 && !empty($response->__get('errors')['customer.externalId'])) { + $order['customer'] = $this->createCustomer($data); + $response = $retailcrmApiClient->ordersEdit($order); + } if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) { $this->updatePayment($order_payment, $order['externalId'], $retailcrmApiClient); @@ -423,4 +410,36 @@ class ModelExtensionRetailcrmOrder extends Model { } return $haystack; } + + /** + * @param array $data + * + * @return array + */ + private function createCustomer($data) + { + $customer = array( + 'firstName' => $data['firstname'], + 'lastName' => $data['lastname'], + 'email' => $data['email'], + 'createdAt' => $data['date_added'], + 'address' => array( + 'countryIso' => $data['payment_iso_code_2'], + 'index' => $data['payment_postcode'], + 'city' => $data['payment_city'], + 'region' => $data['payment_zone'], + 'text' => $data['payment_address_1'] . ' ' . $data['payment_address_2'] + ) + ); + + if (!empty($data['telephone'])) { + $customer['phones'] = array( + array( + 'number' => $data['telephone'] + ) + ); + } + + return $customer; + } } From caf55f67ada2ec6ae884a1824e26b144ec99372f Mon Sep 17 00:00:00 2001 From: Daniel Weiser Date: Wed, 15 Jul 2020 12:28:02 +0300 Subject: [PATCH 2/3] refactoring --- .../catalog/model/extension/retailcrm/order.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/upload/catalog/model/extension/retailcrm/order.php b/src/upload/catalog/model/extension/retailcrm/order.php index f01a10c..2400fdb 100644 --- a/src/upload/catalog/model/extension/retailcrm/order.php +++ b/src/upload/catalog/model/extension/retailcrm/order.php @@ -48,16 +48,16 @@ class ModelExtensionRetailcrmOrder extends Model { if ($create) { $order = self::filterRecursive($order); $response = $retailcrmApiClient->ordersCreate($order); - if ($response->getStatusCode() === 400 && !empty($response->__get('errors')['customer.externalId'])) { + if (isset($response['errors']['customer.externalId'])) { $order['customer'] = $this->createCustomer($data); - $response = $retailcrmApiClient->ordersEdit($order); + $response = $retailcrmApiClient->ordersCreate($order); } } else { $order_payment = reset($order['payments']); unset($order['payments']); $order = self::filterRecursive($order); $response = $retailcrmApiClient->ordersEdit($order); - if ($response->getStatusCode() === 400 && !empty($response->__get('errors')['customer.externalId'])) { + if (isset($response['errors']['customer.externalId'])) { $order['customer'] = $this->createCustomer($data); $response = $retailcrmApiClient->ordersEdit($order); } @@ -97,9 +97,9 @@ class ModelExtensionRetailcrmOrder extends Model { $shippingModule = $shippingCode[0]; if (isset($this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']])) { - $delivery_code = $this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']]; + $delivery_code = $this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']]; } elseif (isset($this->settings[$this->moduleTitle . '_delivery'][$shippingModule])) { - $delivery_code = $this->settings[$this->moduleTitle . '_delivery'][$shippingModule]; + $delivery_code = $this->settings[$this->moduleTitle . '_delivery'][$shippingModule]; } } @@ -414,7 +414,7 @@ class ModelExtensionRetailcrmOrder extends Model { /** * @param array $data * - * @return array + * @return array $customer */ private function createCustomer($data) { From 434ec7b431a608ac9737cd5f9ed6316d6781da4e Mon Sep 17 00:00:00 2001 From: Daniel Weiser Date: Wed, 15 Jul 2020 23:06:12 +0300 Subject: [PATCH 3/3] refactoring and tests --- .../model/extension/retailcrm/order.php | 25 ++++++--- .../ModelRetailcrmOrderCatalogTest.php | 56 ++++++++++++++++--- 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/upload/catalog/model/extension/retailcrm/order.php b/src/upload/catalog/model/extension/retailcrm/order.php index 2400fdb..c208388 100644 --- a/src/upload/catalog/model/extension/retailcrm/order.php +++ b/src/upload/catalog/model/extension/retailcrm/order.php @@ -37,9 +37,7 @@ class ModelExtensionRetailcrmOrder extends Model { } if (!isset($order['customer']['externalId']) && !isset($order['customer']['id'])) { - $new_customer = $this->createCustomer($data); - $res = $retailcrmApiClient->customersCreate($new_customer); - + $res = $this->createCustomer($data, $retailcrmApiClient); if ($res->isSuccessful() && isset($res['id'])) { $order['customer']['id'] = $res['id']; } @@ -49,8 +47,12 @@ class ModelExtensionRetailcrmOrder extends Model { $order = self::filterRecursive($order); $response = $retailcrmApiClient->ordersCreate($order); if (isset($response['errors']['customer.externalId'])) { - $order['customer'] = $this->createCustomer($data); - $response = $retailcrmApiClient->ordersCreate($order); + $res = $this->createCustomer($data, $retailcrmApiClient); + if ($res->isSuccessful() && isset($res['id'])) { + $order['customer']['id'] = $res['id']; + } + + $retailcrmApiClient->ordersCreate($order); } } else { $order_payment = reset($order['payments']); @@ -58,7 +60,11 @@ class ModelExtensionRetailcrmOrder extends Model { $order = self::filterRecursive($order); $response = $retailcrmApiClient->ordersEdit($order); if (isset($response['errors']['customer.externalId'])) { - $order['customer'] = $this->createCustomer($data); + $res = $this->createCustomer($data, $retailcrmApiClient); + if ($res->isSuccessful() && isset($res['id'])) { + $order['customer']['id'] = $res['id']; + } + $response = $retailcrmApiClient->ordersEdit($order); } @@ -413,10 +419,11 @@ class ModelExtensionRetailcrmOrder extends Model { /** * @param array $data + * @param \RetailcrmProxy $retailcrmApiClient * - * @return array $customer + * @return ApiResponse */ - private function createCustomer($data) + private function createCustomer($data, $retailcrmApiClient) { $customer = array( 'firstName' => $data['firstname'], @@ -440,6 +447,6 @@ class ModelExtensionRetailcrmOrder extends Model { ); } - return $customer; + return $retailcrmApiClient->customersCreate($customer); } } diff --git a/tests/catalog/ModelRetailcrmOrderCatalogTest.php b/tests/catalog/ModelRetailcrmOrderCatalogTest.php index 4a9bc93..c7a7b01 100644 --- a/tests/catalog/ModelRetailcrmOrderCatalogTest.php +++ b/tests/catalog/ModelRetailcrmOrderCatalogTest.php @@ -71,6 +71,32 @@ class ModelRetailcrmOrderCatalogTest extends TestCase } $orderProcess = $this->orderModel->processOrder($order); + $successResponse = new \RetailcrmApiResponse( + 200, + json_encode( + array( + 'success' => true, + 'id' => 1, + ) + ) + ); + + $orderCreateErrorResponse = new \RetailcrmApiResponse( + 400, + json_encode( + array( + 'errors' => array ( + 'customer.externalId' => "Customer with externalId=1 not found.", + ) + ) + ) + ); + + $this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn($successResponse); + $this->apiClientMock->expects($this->any())->method('ordersCreate')->will( + $this->onConsecutiveCalls($orderCreateErrorResponse, $successResponse) + ); + $orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order); $this->assertArrayHasKey('status', $orderSend); @@ -103,6 +129,7 @@ class ModelRetailcrmOrderCatalogTest extends TestCase $this->assertArrayHasKey('customer', $orderSend); $this->assertArrayHasKey('externalId', $orderSend['customer']); $this->assertEquals(self::CUSTOMER_ID, $orderSend['customer']['externalId']); + $this->assertEquals(1, $orderSend['customer']['id']); $this->assertArrayHasKey('payments', $orderSend); $this->assertEquals('cod', $orderSend['payments'][0]['type']); $this->assertNotEmpty($orderSend['payments']); @@ -145,9 +172,23 @@ class ModelRetailcrmOrderCatalogTest extends TestCase ) ); - $this->apiClientMock->expects($this->any())->method('ordersEdit')->willReturn($orderEditResponse); + $orderEditErrorResponse = new \RetailcrmApiResponse( + 400, + json_encode( + array( + 'errors' => array ( + 'customer.externalId' => "Customer with externalId=1 not found.", + ) + ) + ) + ); + $this->apiClientMock->expects($this->any())->method('ordersGet')->willReturn($ordersGetResponse); $this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn($orderEditResponse); + $this->apiClientMock->expects($this->any())->method('ordersEdit')->will( + $this->onConsecutiveCalls($orderEditErrorResponse, $orderEditResponse) + ); + $orderProcess = $this->orderModel->processOrder($order); $orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order, false); @@ -171,6 +212,7 @@ class ModelRetailcrmOrderCatalogTest extends TestCase $this->assertEquals('Rostov-na-Donu', $orderSend['delivery']['address']['region']); $this->assertEquals('111111', $orderSend['delivery']['address']['index']); $this->assertArrayHasKey('items', $orderSend); + $this->assertEquals(1, $orderSend['customer']['id']); foreach($orderSend['items'] as $item) { $this->assertArrayHasKey('priceType', $item); @@ -209,12 +251,12 @@ class ModelRetailcrmOrderCatalogTest extends TestCase array( 'success' => true, "pagination"=> [ - "limit"=>20, - "totalCount"=> 0, - "currentPage"=> 1, - "totalPageCount"=> 0 - ], - "customers"=> [] + "limit"=>20, + "totalCount"=> 0, + "currentPage"=> 1, + "totalPageCount"=> 0 + ], + "customers"=> [] ) ) );