mirror of
https://github.com/retailcrm/opencart-module.git
synced 2024-11-24 06:06:06 +03:00
Resend orders with new customer
This commit is contained in:
commit
fb5d969ccc
@ -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);
|
||||||
|
$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);
|
$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);
|
||||||
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user