Exemple #1
0
 /**
  * Change customer password action
  *
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute()
 {
     if (!$this->_formKeyValidator->validate($this->getRequest())) {
         $this->_redirect('*/*/edit');
         return;
     }
     if ($this->getRequest()->isPost()) {
         $customerId = $this->_getSession()->getCustomerId();
         $customer = $this->customerExtractor->extract('customer_account_edit', $this->_request);
         $this->_customerBuilder->populate($customer);
         $this->_customerBuilder->setId($customerId);
         $customer = $this->_customerBuilder->create();
         if ($this->getRequest()->getParam('change_password')) {
             $currPass = $this->getRequest()->getPost('current_password');
             $newPass = $this->getRequest()->getPost('password');
             $confPass = $this->getRequest()->getPost('confirmation');
             if (strlen($newPass)) {
                 if ($newPass == $confPass) {
                     try {
                         $this->_customerAccountService->changePassword($customerId, $currPass, $newPass);
                     } catch (AuthenticationException $e) {
                         $this->messageManager->addError($e->getMessage());
                     } catch (\Exception $e) {
                         $this->messageManager->addException($e, __('A problem was encountered trying to change password.'));
                     }
                 } else {
                     $this->messageManager->addError(__('Confirm your new password'));
                 }
             } else {
                 $this->messageManager->addError(__('New password field cannot be empty.'));
             }
         }
         try {
             $this->_customerDetailsBuilder->setCustomer($customer);
             $this->_customerAccountService->updateCustomer($this->_customerDetailsBuilder->create());
         } catch (AuthenticationException $e) {
             $this->messageManager->addError($e->getMessage());
         } catch (InputException $e) {
             $this->messageManager->addException($e, __('Invalid input'));
         } catch (\Exception $e) {
             $this->messageManager->addException($e, __('Cannot save the customer.') . $e->getMessage() . '<pre>' . $e->getTraceAsString() . '</pre>');
         }
         if ($this->messageManager->getMessages()->getCount() > 0) {
             $this->_getSession()->setCustomerFormData($this->getRequest()->getPost());
             $this->_redirect('*/*/edit');
             return;
         }
         $this->messageManager->addSuccess(__('The account information has been saved.'));
         $this->_redirect('customer/account');
         return;
     }
     $this->_redirect('*/*/edit');
 }
 /**
  * {@inheritdoc}
  */
 public function updateCustomerDetailsByEmail($customerEmail, CustomerDetails $customerDetails, $websiteId = null)
 {
     $customerData = $customerDetails->getCustomer();
     $customerId = $this->getCustomerByEmail($customerEmail, $websiteId)->getId();
     if ($customerData->getId() && $customerData->getId() !== $customerId) {
         throw new StateException('Altering the customer ID is not permitted');
     }
     $customerData = $this->customerBuilder->populate($customerData)->setId($customerId)->create();
     $customerDetails = $this->customerDetailsBuilder->populate($customerDetails)->setCustomer($customerData)->create();
     return $this->updateCustomer($customerDetails);
 }
Exemple #3
0
 /**
  * Update Customer name in Quote
  */
 protected function _updateQuoteCustomerName()
 {
     /** @var $emptyAddress \Magento\Sales\Model\Quote\Address */
     $emptyAddress = $this->_quoteAddressFactory->create();
     $emptyAddress->setFirstname(null);
     $emptyAddress->setLastname(null);
     $this->_block->getQuote()->setBillingAddress($emptyAddress);
     $customerData = $this->_customerService->getCustomer(self::FIXTURE_CUSTOMER_ID);
     $customerData = $this->_customerBuilder->populate($customerData)->setFirstname(self::SAMPLE_FIRST_NAME)->setLastname(self::SAMPLE_LAST_NAME)->create();
     $this->_block->getQuote()->setCustomerData($customerData);
     $this->_block->getQuote()->save();
     $this->assertEquals(self::SAMPLE_FIRST_NAME, $this->_block->getFirstname());
     $this->assertEquals(self::SAMPLE_LAST_NAME, $this->_block->getLastname());
 }
 /**
  * @magentoDbIsolation enabled
  */
 public function testCreateCustomerNewThenUpdateFirstName()
 {
     $email = '*****@*****.**';
     $storeId = 1;
     $firstname = 'Tester';
     $lastname = 'McTest';
     $groupId = 1;
     $this->_customerBuilder->setStoreId($storeId)->setEmail($email)->setFirstname($firstname)->setLastname($lastname)->setGroupId($groupId);
     $newCustomerEntity = $this->_customerBuilder->create();
     $customerDetails = $this->_customerDetailsBuilder->setCustomer($newCustomerEntity)->create();
     $customer = $this->_customerAccountService->createCustomer($customerDetails, 'aPassword');
     $this->_customerBuilder->populate($customer);
     $this->_customerBuilder->setFirstname('Tested');
     $customerDetails = $this->_customerDetailsBuilder->setCustomer($this->_customerBuilder->create())->create();
     $this->_customerAccountService->updateCustomer($customerDetails);
     $customer = $this->_customerAccountService->getCustomer($customer->getId());
     $this->assertEquals('Tested', $customer->getFirstname());
     $this->assertEquals($lastname, $customer->getLastname());
 }
Exemple #5
0
 public function testPopulateFromPrototypeVsArray()
 {
     $customerFromArray = $this->_customerBuilder->populateWithArray(array(Customer::FIRSTNAME => self::FIRSTNAME, Customer::LASTNAME => self::LASTNAME, Customer::EMAIL => self::EMAIL, Customer::ID => self::ID, 'entity_id' => self::ID))->create();
     $customerFromPrototype = $this->_customerBuilder->populate($customerFromArray)->create();
     $this->assertEquals($customerFromArray->__toArray(), $customerFromPrototype->__toArray());
 }
Exemple #6
0
 public function testPopulateException()
 {
     $addressData = $this->_objectManager->getObject('Magento\\Customer\\Service\\V1\\Data\\AddressBuilder', ['valueBuilder' => $this->_valueBuilder, 'regionBuilder' => $this->_objectManager->getObject('\\Magento\\Customer\\Service\\V1\\Data\\RegionBuilder'), 'metadataService' => $this->_addressMetadataService])->create();
     $this->_customerBuilder->populate($addressData);
 }
Exemple #7
0
 /**
  * Prepare customer data for order creation.
  *
  * Create customer if not created using data from customer form.
  * Create customer billing/shipping address if necessary using data from customer address forms.
  * Set customer data to quote.
  *
  * @return $this
  */
 public function _prepareCustomer()
 {
     if ($this->getQuote()->getCustomerIsGuest()) {
         return $this;
     }
     /** @var $store \Magento\Store\Model\Store */
     $store = $this->getSession()->getStore();
     $customerDataObject = $this->getQuote()->getCustomerData();
     if ($customerDataObject->getId() && !$this->_customerIsInStore($store)) {
         /** Create a new customer record if it is not available in the specified store */
         $customerDataObject = $this->_customerBuilder->populate($customerDataObject)->setId(null)->setStoreId($store->getId())->setWebsiteId($store->getWebsiteId())->setCreatedAt(null)->create();
         $customerDataObject = $this->_validateCustomerData($customerDataObject);
     } else {
         if (!$customerDataObject->getId()) {
             /** Create new customer */
             $customerBillingAddressDataObject = $this->getBillingAddress()->exportCustomerAddressData();
             $customerDataObject = $this->_customerBuilder->populate($customerDataObject)->setSuffix($customerBillingAddressDataObject->getSuffix())->setFirstname($customerBillingAddressDataObject->getFirstname())->setLastname($customerBillingAddressDataObject->getLastname())->setMiddlename($customerBillingAddressDataObject->getMiddlename())->setPrefix($customerBillingAddressDataObject->getPrefix())->setStoreId($store->getId())->setEmail($this->_getNewCustomerEmail())->create();
             $customerDataObject = $this->_validateCustomerData($customerDataObject);
         }
     }
     if ($this->getBillingAddress()->getSaveInAddressBook()) {
         $this->_prepareCustomerAddress($customerDataObject, $this->getBillingAddress());
     }
     if (!$this->getQuote()->isVirtual() && $this->getShippingAddress()->getSaveInAddressBook()) {
         $this->_prepareCustomerAddress($customerDataObject, $this->getShippingAddress());
     }
     $this->getQuote()->updateCustomerData($customerDataObject);
     $customerData = \Magento\Framework\Service\ExtensibleDataObjectConverter::toFlatArray($customerDataObject);
     foreach ($this->_createCustomerForm($customerDataObject)->getUserAttributes() as $attribute) {
         if (isset($customerData[$attribute->getAttributeCode()])) {
             $quoteCode = sprintf('customer_%s', $attribute->getAttributeCode());
             $this->getQuote()->setData($quoteCode, $customerData[$attribute->getAttributeCode()]);
         }
     }
     return $this;
 }
Exemple #8
0
 /**
  * Validate customer data and set some its data for further usage in quote
  *
  * Will return either true or array with error messages
  *
  * @param array $data
  * @return bool|array
  */
 protected function _validateCustomerData(array $data)
 {
     $quote = $this->getQuote();
     $isCustomerNew = !$quote->getCustomerId();
     $customer = $quote->getCustomerData();
     $customerData = \Magento\Framework\Service\EavDataObjectConverter::toFlatArray($customer);
     /** @var Form $customerForm */
     $customerForm = $this->_formFactory->create(CustomerMetadata::ENTITY_TYPE_CUSTOMER, 'checkout_register', $customerData, $this->_request->isAjax(), Form::IGNORE_INVISIBLE, array());
     if ($isCustomerNew) {
         $customerRequest = $customerForm->prepareRequest($data);
         $customerData = $customerForm->extractData($customerRequest);
     }
     $customerErrors = $customerForm->validateData($customerData);
     if ($customerErrors !== true) {
         return array('error' => -1, 'message' => implode(', ', $customerErrors));
     }
     if (!$isCustomerNew) {
         return true;
     }
     $this->_customerBuilder->populateWithArray($customerData);
     $customer = $this->_customerBuilder->create();
     if ($quote->getCheckoutMethod() == self::METHOD_REGISTER) {
         // We always have $customerRequest here, otherwise we would have been kicked off the function several
         // lines above
         $password = $customerRequest->getParam('customer_password');
         if ($password != $customerRequest->getParam('confirm_password')) {
             return ['error' => -1, 'message' => __('Password and password confirmation are not equal.')];
         }
         $quote->setPasswordHash($this->_customerAccountService->getPasswordHash($password));
     } else {
         // set NOT LOGGED IN group id explicitly,
         // otherwise copyFieldsetToTarget('customer_account', 'to_quote') will fill it with default group id value
         $this->_customerBuilder->populate($customer);
         $this->_customerBuilder->setGroupId(CustomerGroupServiceInterface::NOT_LOGGED_IN_ID);
         $customer = $this->_customerBuilder->create();
     }
     //validate customer
     $attributes = $customerForm->getAllowedAttributes();
     $result = $this->_customerAccountService->validateCustomerData($customer, $attributes);
     if (!$result->isValid()) {
         return ['error' => -1, 'message' => implode(', ', $result->getMessages())];
     }
     // copy customer/guest email to address
     $quote->getBillingAddress()->setEmail($customer->getEmail());
     // copy customer data to quote
     $this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', \Magento\Framework\Service\EavDataObjectConverter::toFlatArray($customer), $quote);
     return true;
 }