/** * Create customer account action * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { if ($this->_getSession()->isLoggedIn() || !$this->isRegistrationAllowed()) { $this->_redirect('*/*/'); return; } if (!$this->getRequest()->isPost()) { $url = $this->_createUrl()->getUrl('*/*/create', array('_secure' => true)); $this->getResponse()->setRedirect($this->_redirect->error($url)); return; } $this->_session->regenerateId(); try { $customer = $this->customerExtractor->extract('customer_account_create', $this->_request); $address = $this->_extractAddress(); $addresses = is_null($address) ? array() : array($address); $password = $this->getRequest()->getParam('password'); $redirectUrl = $this->_getSession()->getBeforeAuthUrl(); $customerDetails = $this->_customerDetailsBuilder->setCustomer($customer)->setAddresses($addresses)->create(); $customer = $this->_customerAccountService->createCustomer($customerDetails, $password, $redirectUrl); if ($this->getRequest()->getParam('is_subscribed', false)) { $this->_subscriberFactory->create()->subscribeCustomerById($customer->getId()); } $this->_eventManager->dispatch('customer_register_success', array('account_controller' => $this, 'customer' => $customer)); $confirmationStatus = $this->_customerAccountService->getConfirmationStatus($customer->getId()); if ($confirmationStatus === CustomerAccountServiceInterface::ACCOUNT_CONFIRMATION_REQUIRED) { $email = $this->_customerHelperData->getEmailConfirmationUrl($customer->getEmail()); // @codingStandardsIgnoreStart $this->messageManager->addSuccess(__('Account confirmation is required. Please, check your email for the confirmation link. To resend the confirmation email please <a href="%1">click here</a>.', $email)); // @codingStandardsIgnoreEnd $url = $this->_createUrl()->getUrl('*/*/index', array('_secure' => true)); $this->getResponse()->setRedirect($this->_redirect->success($url)); } else { $this->_getSession()->setCustomerDataAsLoggedIn($customer); $url = $this->_welcomeCustomer($customer); $this->getResponse()->setRedirect($this->_redirect->success($url)); } return; } catch (StateException $e) { $url = $this->_createUrl()->getUrl('customer/account/forgotpassword'); // @codingStandardsIgnoreStart $message = __('There is already an account with this email address. If you are sure that it is your email address, <a href="%1">click here</a> to get your password and access your account.', $url); // @codingStandardsIgnoreEnd $this->messageManager->addError($message); } catch (InputException $e) { $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage())); foreach ($e->getErrors() as $error) { $this->messageManager->addError($this->escaper->escapeHtml($error->getMessage())); } } catch (\Exception $e) { $this->messageManager->addException($e, __('Cannot save the customer.')); } $this->_getSession()->setCustomerFormData($this->getRequest()->getPost()); $defaultUrl = $this->_createUrl()->getUrl('*/*/create', array('_secure' => true)); $this->getResponse()->setRedirect($this->_redirect->error($defaultUrl)); }
/** * @magentoAppArea frontend * @magentoDataFixture Magento/Customer/_files/customer.php * @expectedException \Magento\Framework\Exception\StateException */ public function testUpdateCustomerDetailsByEmailWithException() { $customerId = 1; $customerDetails = $this->_customerAccountService->getCustomerDetails($customerId); $email = $customerDetails->getCustomer()->getEmail(); $customerData = array_merge($customerDetails->getCustomer()->__toArray(), ['firstname' => 'fname', 'id' => 1234567]); $this->_customerBuilder->populateWithArray($customerData); $this->_customerDetailsBuilder->setCustomer($this->_customerBuilder->create())->setAddresses([]); $this->_customerAccountService->updateCustomerDetailsByEmail($email, $this->_customerDetailsBuilder->create()); }
/** * {@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); }
/** * 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'); }
public function connectByCreatingAccount($facebookId, $token, $email, $firstName, $lastName) { $customerDetails = array('firstname' => $firstName, 'lastname' => $lastName, 'email' => $email, 'sendemail' => 0, 'confirmation' => 0, 'custom_attributes' => array(array(\Magento\Framework\Service\Data\AttributeValue::ATTRIBUTE_CODE => 'inchoo_socialconnect_fid', \Magento\Framework\Service\Data\AttributeValue::VALUE => $facebookId), array(\Magento\Framework\Service\Data\AttributeValue::ATTRIBUTE_CODE => 'inchoo_socialconnect_ftoken', \Magento\Framework\Service\Data\AttributeValue::VALUE => serialize($token)))); $customer = $this->_customerBuilder->populateWithArray($customerDetails)->create(); // Save customer $customerDetails = $this->_customerDetailsBuilder->setCustomer($customer)->setAddresses(null)->create(); $customerDataObject = $this->_customerAccountService->createCustomer($customerDetails); /* @var $customer \Magento\Customer\Service\V1\Data\Customer */ // Convert data object to customer model $customer = $this->_converter->createCustomerModel($customerDataObject); /* @var $customer \Magento\Customer\Model\Customer */ $customer->sendNewAccountEmail('confirmed', ''); $this->_customerSession->setCustomerAsLoggedIn($customer); }
/** * @magentoDataFixture Magento/Sales/_files/quote.php */ public function testSubmitOrderExistingCustomer() { $this->_prepareQuote(false); $customerDetails = $this->_customerDetailsBuilder->setCustomer($this->getSampleCustomerEntity())->setAddresses($this->getSampleAddressEntity())->create(); $customerData = $this->_customerAccountService->createCustomer($customerDetails, 'password'); $existingCustomerId = $customerData->getId(); $customerData = $this->_customerBuilder->mergeDataObjectWithArray($customerData, array(CustomerData::EMAIL => '*****@*****.**')); $addresses = $this->_customerAddressService->getAddresses($existingCustomerId); $this->_serviceQuote->getQuote()->setCustomerData($customerData); $this->_serviceQuote->getQuote()->setCustomerAddressData($addresses); $this->_serviceQuote->submitOrderWithDataObject(); $customerId = $this->_serviceQuote->getQuote()->getCustomerData()->getId(); $this->assertNotNull($customerId); //Make sure no new customer is created $this->assertEquals($existingCustomerId, $customerId); $customerData = $this->_customerAccountService->getCustomer($existingCustomerId); $this->assertEquals('*****@*****.**', $customerData->getEmail()); }
/** * @expectedException \Magento\Customer\Exception * @expectedExceptionMessage exception message */ public function testCreateCustomerWithPasswordHashException() { $storeId = 5; $customerData = array('id' => self::ID, 'email' => self::EMAIL, 'firstname' => self::FIRSTNAME, 'lastname' => self::LASTNAME, 'store_id' => $storeId, 'website_id' => self::WEBSITE_ID); $this->_customerBuilder->populateWithArray($customerData); $customerEntity = $this->_customerBuilder->create(); $customerDetails = $this->_customerDetailsBuilder->setCustomer($customerEntity)->create(); $this->_storeManagerMock->expects($this->once())->method('getStores')->will($this->returnValue(array())); $this->_converter = $this->getMock('Magento\\Customer\\Model\\Converter', array('getCustomerModel', 'createCustomerModel'), array(), '', false); $this->_converter->expects($this->once())->method('getCustomerModel')->will($this->returnValue($this->_customerModelMock)); $this->_converter->expects($this->once())->method('createCustomerModel')->will($this->throwException(new \Magento\Customer\Exception('exception message', 0))); $customerService = $this->_createService(); $customerService->createCustomerWithPasswordHash($customerDetails, '', ''); }
/** * Submit the quote. Quote submit process will create the order based on quote data * * @return \Magento\Sales\Model\Order * @throws \Exception */ public function submitOrderWithDataObject() { $this->_deleteNominalItems(); $this->_validate(); $quote = $this->_quote; $isVirtual = $quote->isVirtual(); $transaction = $this->_transactionFactory->create(); $customerData = null; if (!$quote->getCustomerIsGuest()) { $customerData = $quote->getCustomerData(); $addresses = $quote->getCustomerAddressData(); $customerDetails = $this->_customerDetailsBuilder->setCustomer($customerData)->setAddresses($addresses)->create(); if ($customerData->getId()) { $this->_customerAccountService->updateCustomer($customerDetails); } else { //for new customers $customerData = $this->_customerAccountService->createCustomerWithPasswordHash($customerDetails, $quote->getPasswordHash()); $addresses = $this->_customerAddressService->getAddresses($customerData->getId()); //Update quote address information foreach ($addresses as $address) { if ($address->isDefaultBilling()) { $quote->getBillingAddress()->setCustomerAddressData($address); } else { if ($address->isDefaultShipping()) { $quote->getShippingAddress()->setCustomerAddressData($address); } } } if ($quote->getShippingAddress() && $quote->getShippingAddress()->getSameAsBilling()) { $quote->getShippingAddress()->setCustomerAddressData($quote->getBillingAddress()->getCustomerAddressData()); } } $quote->setCustomerData($customerData)->setCustomerAddressData($addresses); } $transaction->addObject($quote); $quote->reserveOrderId(); if ($isVirtual) { $order = $this->_convertor->addressToOrder($quote->getBillingAddress()); } else { $order = $this->_convertor->addressToOrder($quote->getShippingAddress()); } $order->setBillingAddress($this->_convertor->addressToOrderAddress($quote->getBillingAddress())); if ($quote->getBillingAddress()->getCustomerAddressData()) { $order->getBillingAddress()->setCustomerAddressData($quote->getBillingAddress()->getCustomerAddressData()); } if (!$isVirtual) { $order->setShippingAddress($this->_convertor->addressToOrderAddress($quote->getShippingAddress())); if ($quote->getShippingAddress()->getCustomerAddressData()) { $order->getShippingAddress()->setCustomerAddressData($quote->getShippingAddress()->getCustomerAddressData()); } } $order->setPayment($this->_convertor->paymentToOrderPayment($quote->getPayment())); foreach ($this->_orderData as $key => $value) { $order->setData($key, $value); } foreach ($quote->getAllItems() as $item) { $orderItem = $this->_convertor->itemToOrderItem($item); if ($item->getParentItem()) { $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId())); } $order->addItem($orderItem); } if ($customerData) { $order->setCustomerId($customerData->getId()); } $order->setQuote($quote); $transaction->addObject($order); $transaction->addCommitCallback(array($order, 'place')); $transaction->addCommitCallback(array($order, 'save')); /** * We can use configuration data for declare new order status */ $this->_eventManager->dispatch('checkout_type_onepage_save_order', array('order' => $order, 'quote' => $quote)); $this->_eventManager->dispatch('sales_model_service_quote_submit_before', array('order' => $order, 'quote' => $quote)); try { $transaction->save(); $this->_inactivateQuote(); $this->_eventManager->dispatch('sales_model_service_quote_submit_success', array('order' => $order, 'quote' => $quote)); } catch (\Exception $e) { //reset order ID's on exception, because order not saved $order->setId(null); /** @var $item \Magento\Sales\Model\Order\Item */ foreach ($order->getItemsCollection() as $item) { $item->setOrderId(null); $item->setItemId(null); } $this->_eventManager->dispatch('sales_model_service_quote_submit_failure', array('order' => $order, 'quote' => $quote)); throw $e; } $this->_order = $order; return $order; }