Resend orders with new customer

This commit is contained in:
Alex Lushpai 2020-08-03 15:31:26 +03:00 committed by GitHub
commit fb5d969ccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 34 deletions

View File

@ -37,30 +37,7 @@ class ModelExtensionRetailcrmOrder extends Model {
} }
if (!isset($order['customer']['externalId']) && !isset($order['customer']['id'])) { if (!isset($order['customer']['externalId']) && !isset($order['customer']['id'])) {
$new_customer = array( $res = $this->createCustomer($data, $retailcrmApiClient);
'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']
)
);
}
$res = $retailcrmApiClient->customersCreate($new_customer);
if ($res->isSuccessful() && isset($res['id'])) { if ($res->isSuccessful() && isset($res['id'])) {
$order['customer']['id'] = $res['id']; $order['customer']['id'] = $res['id'];
} }
@ -68,12 +45,28 @@ class ModelExtensionRetailcrmOrder extends Model {
if ($create) { if ($create) {
$order = self::filterRecursive($order); $order = self::filterRecursive($order);
$retailcrmApiClient->ordersCreate($order); $response = $retailcrmApiClient->ordersCreate($order);
if (isset($response['errors']['customer.externalId'])) {
$res = $this->createCustomer($data, $retailcrmApiClient);
if ($res->isSuccessful() && isset($res['id'])) {
$order['customer']['id'] = $res['id'];
}
$retailcrmApiClient->ordersCreate($order);
}
} else { } else {
$order_payment = reset($order['payments']); $order_payment = reset($order['payments']);
unset($order['payments']); unset($order['payments']);
$order = self::filterRecursive($order); $order = self::filterRecursive($order);
$response = $retailcrmApiClient->ordersEdit($order); $response = $retailcrmApiClient->ordersEdit($order);
if (isset($response['errors']['customer.externalId'])) {
$res = $this->createCustomer($data, $retailcrmApiClient);
if ($res->isSuccessful() && isset($res['id'])) {
$order['customer']['id'] = $res['id'];
}
$response = $retailcrmApiClient->ordersEdit($order);
}
if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) { if ($this->settings[$this->moduleTitle . '_apiversion'] == 'v5' && $response->isSuccessful()) {
$this->updatePayment($order_payment, $order['externalId'], $retailcrmApiClient); $this->updatePayment($order_payment, $order['externalId'], $retailcrmApiClient);
@ -110,9 +103,9 @@ class ModelExtensionRetailcrmOrder extends Model {
$shippingModule = $shippingCode[0]; $shippingModule = $shippingCode[0];
if (isset($this->settings[$this->moduleTitle . '_delivery'][$order_data['shipping_code']])) { 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])) { } elseif (isset($this->settings[$this->moduleTitle . '_delivery'][$shippingModule])) {
$delivery_code = $this->settings[$this->moduleTitle . '_delivery'][$shippingModule]; $delivery_code = $this->settings[$this->moduleTitle . '_delivery'][$shippingModule];
} }
} }
@ -423,4 +416,37 @@ class ModelExtensionRetailcrmOrder extends Model {
} }
return $haystack; return $haystack;
} }
/**
* @param array $data
* @param \RetailcrmProxy $retailcrmApiClient
*
* @return ApiResponse
*/
private function createCustomer($data, $retailcrmApiClient)
{
$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 $retailcrmApiClient->customersCreate($customer);
}
} }

View File

@ -71,6 +71,32 @@ class ModelRetailcrmOrderCatalogTest extends TestCase
} }
$orderProcess = $this->orderModel->processOrder($order); $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); $orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order);
$this->assertArrayHasKey('status', $orderSend); $this->assertArrayHasKey('status', $orderSend);
@ -103,6 +129,7 @@ class ModelRetailcrmOrderCatalogTest extends TestCase
$this->assertArrayHasKey('customer', $orderSend); $this->assertArrayHasKey('customer', $orderSend);
$this->assertArrayHasKey('externalId', $orderSend['customer']); $this->assertArrayHasKey('externalId', $orderSend['customer']);
$this->assertEquals(self::CUSTOMER_ID, $orderSend['customer']['externalId']); $this->assertEquals(self::CUSTOMER_ID, $orderSend['customer']['externalId']);
$this->assertEquals(1, $orderSend['customer']['id']);
$this->assertArrayHasKey('payments', $orderSend); $this->assertArrayHasKey('payments', $orderSend);
$this->assertEquals('cod', $orderSend['payments'][0]['type']); $this->assertEquals('cod', $orderSend['payments'][0]['type']);
$this->assertNotEmpty($orderSend['payments']); $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('ordersGet')->willReturn($ordersGetResponse);
$this->apiClientMock->expects($this->any())->method('customersCreate')->willReturn($orderEditResponse); $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); $orderProcess = $this->orderModel->processOrder($order);
$orderSend = $this->orderModel->sendToCrm($orderProcess, $this->apiClientMock, $order, false); $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('Rostov-na-Donu', $orderSend['delivery']['address']['region']);
$this->assertEquals('111111', $orderSend['delivery']['address']['index']); $this->assertEquals('111111', $orderSend['delivery']['address']['index']);
$this->assertArrayHasKey('items', $orderSend); $this->assertArrayHasKey('items', $orderSend);
$this->assertEquals(1, $orderSend['customer']['id']);
foreach($orderSend['items'] as $item) { foreach($orderSend['items'] as $item) {
$this->assertArrayHasKey('priceType', $item); $this->assertArrayHasKey('priceType', $item);
@ -209,12 +251,12 @@ class ModelRetailcrmOrderCatalogTest extends TestCase
array( array(
'success' => true, 'success' => true,
"pagination"=> [ "pagination"=> [
"limit"=>20, "limit"=>20,
"totalCount"=> 0, "totalCount"=> 0,
"currentPage"=> 1, "currentPage"=> 1,
"totalPageCount"=> 0 "totalPageCount"=> 0
], ],
"customers"=> [] "customers"=> []
) )
) )
); );