diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index d7e7c35..5e7d531 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -83,7 +83,6 @@ if (!class_exists('WC_Retailcrm_Base')) { add_action('wp_ajax_generate_icml', array($this, 'generate_icml')); add_action('wp_ajax_upload_selected_orders', array($this, 'upload_selected_orders')); add_action('admin_print_footer_scripts', array($this, 'ajax_generate_icml'), 99); - add_action('woocommerce_created_customer', array($this, 'create_customer'), 10, 1); add_action('woocommerce_update_customer', array($this, 'update_customer'), 10, 1); add_action('user_register', array($this, 'create_customer'), 10, 2); add_action('profile_update', array($this, 'update_customer'), 10, 2); @@ -267,63 +266,26 @@ if (!class_exists('WC_Retailcrm_Base')) { /** * Create customer in retailCRM * - * @param int $customer_id - * * @codeCoverageIgnore There is a task for analysis + * + * @param int $customerId + * * @return void - * @throws \Exception + * @throws Exception */ - public function create_customer($customer_id) + public function create_customer($customerId) { if (WC_Retailcrm_Plugin::history_running() === true) { return; } - $client = $this->getApiClient(); + if (empty($customerId)) { + WC_Retailcrm_Logger::add('Error: Customer externalId is empty'); - if (empty($client)) { return; } - $wcCustomer = new WC_Customer($customer_id); - $email = $wcCustomer->get_billing_email(); - - if (empty($email)) { - $email = $wcCustomer->get_email(); - } - - if (empty($email)) { - return; - } else { - $wcCustomer->set_billing_email($email); - $wcCustomer->save(); - } - - $response = $client->customersList(array('email' => $email)); - - if ( - !empty($response) - && $response->isSuccessful() - && isset($response['customers']) - && count($response['customers']) > 0 - ) { - $customers = $response['customers']; - $customer = reset($customers); - - if (isset($customer['id'])) { - $this->customers->updateCustomerById($customer_id, $customer['id']); - $builder = new WC_Retailcrm_WC_Customer_Builder(); - $builder - ->setWcCustomer($wcCustomer) - ->setPhones(isset($customer['phones']) ? $customer['phones'] : array()) - ->setAddress(isset($customer['address']) ? $customer['address'] : false) - ->build() - ->getResult() - ->save(); - } - } else { - $this->customers->createCustomer($customer_id); - } + $this->customers->registerCustomer($customerId); } /** @@ -331,23 +293,28 @@ if (!class_exists('WC_Retailcrm_Base')) { * * @codeCoverageIgnore Check in another tests * - * @param int $customer_id + * @param int $customerId + * + * @return void + * @throws Exception */ - public function update_customer($customer_id) + public function update_customer($customerId) { if (WC_Retailcrm_Plugin::history_running() === true) { return; } - if (empty($customer_id)) { + if (empty($customerId)) { + WC_Retailcrm_Logger::add('Error: Customer externalId is empty'); + return; } - $this->customers->updateCustomer($customer_id); + $this->customers->updateCustomer($customerId); } /** - * Create order in retailCRM from admin panel + * Create order in RetailCRM from admin panel * * @codeCoverageIgnore Check in another tests * diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index 995191f..ce9f229 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -18,25 +18,25 @@ if (!class_exists('WC_Retailcrm_Customers')) : protected $retailcrm; /** @var array */ - protected $retailcrm_settings = array(); + protected $retailcrm_settings = []; /** @var WC_Retailcrm_Customer_Address */ protected $customer_address; /** @var array */ - private $customer = array(); + private $customer = []; /** @var array */ - private $customerCorporate = array(); + private $customerCorporate = []; /** @var array */ - private $customerCorporateCompany = array(); + private $customerCorporateCompany = []; /** @var array */ - private $customerCorporateAddress = array(); + private $customerCorporateAddress = []; /**@var array */ - private $customFields = array(); + private $customFields = []; /** * WC_Retailcrm_Customers constructor. @@ -80,6 +80,64 @@ if (!class_exists('WC_Retailcrm_Customers')) : return $this->retailcrm->getCorporateEnabled(); } + /** + * Customer can registration on site, we need: + * 1. Check by email if the customer already exists in CRM - then update the customer details. + * 2. If the customer is not in CRM, then create a new customer. + * + * @param int $customerId + * + * @return void|null + * @throws Exception + */ + public function registerCustomer($customerId) + { + if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { + return null; + } + $wcCustomer = new WC_Customer($customerId); + $email = $wcCustomer->get_billing_email(); + + if (empty($email)) { + $email = $wcCustomer->get_email(); + } + + if (empty($email)) { + WC_Retailcrm_Logger::add('Error: Customer email is empty, externalId: ' . $wcCustomer->get_id()); + + return null; + } else { + $wcCustomer->set_billing_email($email); + $wcCustomer->save(); + } + + $response = $this->retailcrm->customersList(['email' => $email]); + + if ($response->isSuccessful() && !empty($response['customers'])) { + $customers = $response['customers']; + $customer = reset($customers); + + if (isset($customer['id'])) { + $this->updateCustomerById($customerId, $customer['id']); + + $builder = new WC_Retailcrm_WC_Customer_Builder(); + $builder + ->setWcCustomer($wcCustomer) + ->setPhones(isset($customer['phones']) ? $customer['phones'] : array()) + ->setAddress(isset($customer['address']) ? $customer['address'] : false) + ->build() + ->getResult() + ->save(); + + WC_Retailcrm_Logger::add('Customer was edited, externalId: ' . $wcCustomer->get_id()); + } + } else { + $this->createCustomer($customerId); + + WC_Retailcrm_Logger::add('Customer was created, externalId: ' . $wcCustomer->get_id()); + } + } + /** * Create customer in CRM * @@ -119,18 +177,18 @@ if (!class_exists('WC_Retailcrm_Customers')) : /** * Update customer in CRM * - * @param $customer_id + * @param $customerId * * @return void|\WC_Customer * @throws \Exception */ - public function updateCustomer($customer_id) + public function updateCustomer($customerId) { if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { return; } - $customer = $this->wcCustomerGet($customer_id); + $customer = $this->wcCustomerGet($customerId); if ($this->isCustomer($customer)) { $this->processCustomer($customer); @@ -143,19 +201,19 @@ if (!class_exists('WC_Retailcrm_Customers')) : /** * Update customer in CRM by ID * - * @param int $customer_id + * @param int $customerId * @param int|string $crmCustomerId * * @return void|\WC_Customer * @throws \Exception */ - public function updateCustomerById($customer_id, $crmCustomerId) + public function updateCustomerById($customerId, $crmCustomerId) { if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { return; } - $customer = $this->wcCustomerGet($customer_id); + $customer = $this->wcCustomerGet($customerId); if ($this->isCustomer($customer)) { $this->processCustomer($customer); diff --git a/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php b/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php index ffa45ec..a11408a 100644 --- a/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php +++ b/src/include/customer/woocommerce/class-wc-retailcrm-wc-customer-builder.php @@ -1,4 +1,5 @@ customer->set_billing_state(self::arrayValue( $address, 'region', - $this->customer->get_billing_state()) - ); + $this->customer->get_billing_state() + )); $this->customer->set_billing_postcode(self::arrayValue( $address, 'index', - $this->customer->get_billing_postcode()) - ); + $this->customer->get_billing_postcode() + )); $this->customer->set_billing_country(self::arrayValue( $address, 'country', - $this->customer->get_billing_country()) - ); + $this->customer->get_billing_country() + )); $this->customer->set_billing_city(self::arrayValue( $address, 'city', - $this->customer->get_billing_city()) - ); + $this->customer->get_billing_city() + )); } return $this; @@ -206,7 +207,7 @@ class WC_Retailcrm_WC_Customer_Builder extends WC_Retailcrm_Abstract_Builder /** * Throws an exception if internal state is not ready for data building. * - * @throws \RuntimeException + * @throws RuntimeException */ private function checkBuilderValidity() { diff --git a/tests/datasets/data-customers-retailcrm.php b/tests/datasets/data-customers-retailcrm.php index f4945ca..46501b5 100644 --- a/tests/datasets/data-customers-retailcrm.php +++ b/tests/datasets/data-customers-retailcrm.php @@ -15,19 +15,63 @@ namespace datasets; */ class DataCustomersRetailCrm { - public static function getCustomerAddress() - { - return array( - 'success' => true, - 'addresses' => array ( - 'id' => 3503, - 'index' => 144566, + public static function getCustomerAddress() { + return [ + 'success' => true, + 'addresses' => [ + 'id' => 3503, + 'index' => 144566, 'countryIso' => 'ES', - 'region' => 'Region', - 'city' => 'City', - 'text' => 'street Test 777', - ) - ); + 'region' => 'Region', + 'city' => 'City', + 'text' => 'street Test 777', + ] + ]; + } + + + public static function getEmptyCustomersList() { + return [ + 'success' => true, + 'pagination' => [ + 'limit' => 20, + 'totalCount' => 0, + 'currentPage' => 1, + 'totalPageCount' => 0 + ], + 'customers' => [], + ]; + } + + public static function getCustomersList() { + return [ + 'success' => true, + 'pagination' => [ + 'limit' => 20, + 'totalCount' => 0, + 'currentPage' => 1, + 'totalPageCount' => 0 + ], + 'customers' => [ + [ + 'type' => 'customer', + 'id' => 4228, + 'externalId' => 2, + 'isContact' => false, + 'email' => 'madrid@mail.es', + 'phones' => [['number' => '+3456234235']], + 'addresses' => [ + 'id' => 3503, + 'index' => 144566, + 'countryIso' => 'ES', + 'region' => 'Region', + 'city' => 'City', + 'text' => 'street Test 777', + ] + ] + + ] + ]; } } diff --git a/tests/test-wc-retailcrm-customers.php b/tests/test-wc-retailcrm-customers.php index 4f71b07..9cbf2c4 100644 --- a/tests/test-wc-retailcrm-customers.php +++ b/tests/test-wc-retailcrm-customers.php @@ -45,7 +45,8 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper 'customersCorporateAddressesCreate', 'customersCorporateCompaniesCreate', 'getSingleSiteForKey', - 'customersCorporateAddresses' + 'customersCorporateAddresses', + 'customersList' )) ->getMock(); @@ -115,6 +116,37 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper } } + /** + * @param $retailcrm + * + * @dataProvider dataProviderApiClient + */ + public function test_create_customer_registration($retailcrm) + { + if ($retailcrm) { + $this->buildResponseCustomersList($retailcrm, DataCustomersRetailCrm::getEmptyCustomersList()); + } + + $retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm); + $id = $retailcrmCustomer->registerCustomer($this->customer->get_id()); + $customer = $retailcrmCustomer->getCustomer(); + + if ($retailcrm) { + $this->assertArrayHasKey('firstName', $customer); + $this->assertArrayHasKey('createdAt', $customer); + $this->assertArrayHasKey('email', $customer); + $this->assertNotEmpty($customer['externalId']); + $this->assertNotEmpty($customer['createdAt']); + $this->assertNotEmpty($customer['firstName']); + $this->assertNotEmpty($customer['email']); + $this->assertEquals($customer['firstName'], $this->customer->get_first_name()); + $this->assertEquals($customer['email'], $this->customer->get_email()); + } else { + $this->assertEquals(null, $id); + $this->assertEquals(array(), $customer); + } + } + public function test_create_customer_empty_data() { $retailcrmCustomer = $this->getRetailcrmCustomer($this->apiMock); @@ -130,6 +162,37 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper * * @dataProvider dataProviderApiClient */ + public function test_update_customer_registration($retailcrm) + { + if ($retailcrm) { + $this->buildResponseCustomersList($retailcrm, DataCustomersRetailCrm::getCustomersList()); + } + + $retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm); + $wcCustomer = $retailcrmCustomer->registerCustomer($this->customer->get_id()); + $customer = $retailcrmCustomer->getCustomer(); + + if ($retailcrm) { + $this->assertArrayHasKey('externalId', $customer); + $this->assertArrayHasKey('firstName', $customer); + $this->assertArrayHasKey('createdAt', $customer); + $this->assertArrayHasKey('email', $customer); + $this->assertNotEmpty($customer['externalId']); + $this->assertNotEmpty($customer['createdAt']); + $this->assertNotEmpty($customer['firstName']); + $this->assertNotEmpty($customer['email']); + } else { + $this->assertEquals(null, $wcCustomer); + $this->assertEquals(array(), $customer); + } + } + + /** + * @param $retailcrm + * + * @dataProvider dataProviderApiClient + * @throws Exception + */ public function test_update_customer($retailcrm) { $retailcrmCustomer = $this->getRetailcrmCustomer($retailcrm); @@ -280,4 +343,25 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper new WC_Retailcrm_Customer_Address() ); } + + /** + * @param $retailcrm + * @param $response + * + * @return void + */ + private function buildResponseCustomersList($retailcrm, $response) + { + // Mock response for get customers list + $responseCustomersList = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') + ->disableOriginalConstructor() + ->setMethods(array('isSuccessful')) + ->getMock(); + + $this->setMockResponse($responseCustomersList, 'isSuccessful', true); + $responseCustomersList->setResponse($response); + + //Set responseCustomersList mock for apiMock + $this->setMockResponse($retailcrm, 'customersList', $responseCustomersList); + } }