/** * @When I add the address to my address book */ public function iAddTheAddressToMyAddressBook() { try { $this->customerAddress = $this->addressRepository->save($this->customerAddress); $this->saveAddressException = null; } catch (LocalizedException $e) { $this->saveAddressException = $e; } }
public function testSaveAddressesCustomerIdInvalid() { $proposedAddress = $this->_createSecondAddress()->setCustomerId('this_is_not_a_valid_id'); try { $this->repository->save($proposedAddress); $this->fail('Expected exception not thrown'); } catch (NoSuchEntityException $nsee) { $this->assertEquals('No such entity with customerId = this_is_not_a_valid_id', $nsee->getMessage()); } }
/** *Set additional information for shipping address * * @param \Magento\Checkout\Model\ShippingInformationManagement $subject * @param callable $proceed * * @return \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function aroundSaveAddressInformation(\Magento\Checkout\Model\ShippingInformationManagement $subject, $proceed, $cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation) { try { $carrierCode = $addressInformation->getShippingCarrierCode(); $methodCode = $addressInformation->getShippingMethodCode(); $shippingMethod = $carrierCode . '_' . $methodCode; $quote = $this->quoteRepository->getActive($cartId); $address = $quote->getShippingAddress(); $validation = $this->checkoutSession->getShipAddressValidation(); if (is_array($validation) && isset($validation['key'])) { if (isset($validation['validation_status'])) { $additionalDetail['address_valid'] = $validation['validation_status']; $address->setValidationStatus($validation['validation_status']); } if (isset($validation['destination_type'])) { $additionalDetail['destination_type'] = $validation['destination_type']; $address->setDestinationType($validation['destination_type']); } $this->checkoutSession->setShipAddressValidation(null); } $address->save(); $additionalDetail = new \Magento\Framework\DataObject(); $extAttributes = $addressInformation->getShippingAddress()->getExtensionAttributes(); //push out event so other modules can save their data TODO add carrier_group_id $this->eventManager->dispatch('shipperhq_additional_detail_checkout', ['address_extn_attributes' => $extAttributes, 'additional_detail' => $additionalDetail, 'carrier_code' => $carrierCode]); $additionalDetailArray = $additionalDetail->convertToArray(); $this->shipperLogger->postDebug('ShipperHQ Shipper', 'processing additional detail ', $additionalDetail); $this->carrierGroupHelper->saveCarrierGroupInformation($address, $shippingMethod, $additionalDetailArray); } catch (\Exception $e) { $this->shipperLogger->postCritical('Shipperhq_Shipper', 'Shipping Information Plugin', 'Exception raised ' . $e->getMessage()); } $result = $proceed($cartId, $addressInformation); if ($address->getCustomerId()) { $customerAddresses = $quote->getCustomer()->getAddresses(); foreach ($customerAddresses as $oneAddress) { if ($oneAddress->getId() == $address->getCustomerAddressId()) { if ($address->getValidationStatus()) { $oneAddress->setCustomAttribute('validation_status', $address->getValidationStatus()); } if ($address->getDestinationType()) { $oneAddress->setCustomAttribute('destination_type', $address->getDestinationType()); } $this->addressRepository->save($oneAddress); } } } return $result; }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function createAccountWithPasswordHash(CustomerInterface $customer, $hash, $redirectUrl = '') { // This logic allows an existing customer to be added to a different store. No new account is created. // The plan is to move this logic into a new method called something like 'registerAccountWithStore' if ($customer->getId()) { $customer = $this->customerRepository->get($customer->getEmail()); $websiteId = $customer->getWebsiteId(); if ($this->isCustomerInStore($websiteId, $customer->getStoreId())) { throw new InputException(__('This customer already exists in this store.')); } // Existing password hash will be used from secured customer data registry when saving customer } // Make sure we have a storeId to associate this customer with. if (!$customer->getStoreId()) { if ($customer->getWebsiteId()) { $storeId = $this->storeManager->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId(); } else { $storeId = $this->storeManager->getStore()->getId(); } $customer->setStoreId($storeId); } // Update 'created_in' value with actual store name if ($customer->getId() === null) { $storeName = $this->storeManager->getStore($customer->getStoreId())->getName(); $customer->setCreatedIn($storeName); } $customerAddresses = $customer->getAddresses() ?: []; $customer->setAddresses(null); try { // If customer exists existing hash will be used by Repository $customer = $this->customerRepository->save($customer, $hash); } catch (AlreadyExistsException $e) { throw new InputMismatchException(__('A customer with the same email already exists in an associated website.')); } catch (LocalizedException $e) { throw $e; } try { foreach ($customerAddresses as $address) { $address->setCustomerId($customer->getId()); $this->addressRepository->save($address); } } catch (InputException $e) { $this->customerRepository->delete($customer); throw $e; } $customer = $this->customerRepository->getById($customer->getId()); $newLinkToken = $this->mathRandom->getUniqueHash(); $this->changeResetPasswordLinkToken($customer, $newLinkToken); $this->sendEmailConfirmation($customer, $redirectUrl); return $customer; }
public function aroundSaveAddressInformation(\Magento\Checkout\Model\ShippingInformationManagement $subject, \Closure $proceed, $cartId, ShippingInformationInterface $addressInformation) { // Only validate address if module is enabled $quote = $this->quoteRepository->getActive($cartId); $storeId = $quote->getStoreId(); if (!$this->config->isModuleEnabled($storeId)) { $paymentDetails = $proceed($cartId, $addressInformation); $this->ensureTaxCalculationSuccess($storeId); return $paymentDetails; } // Only validate address if address validation is enabled if (!$this->config->isAddressValidationEnabled($storeId)) { $paymentDetails = $proceed($cartId, $addressInformation); $this->ensureTaxCalculationSuccess($storeId); return $paymentDetails; } // If quote is virtual, getShippingAddress will return billing address, so no need to check if quote is virtual $shippingAddress = $addressInformation->getShippingAddress(); $shippingInformationExtension = $addressInformation->getExtensionAttributes(); $errorMessage = null; $validAddress = null; $customerAddress = null; $quoteAddress = null; $shouldValidateAddress = true; if (!is_null($shippingInformationExtension)) { $shouldValidateAddress = $shippingInformationExtension->getShouldValidateAddress(); } $customerAddressId = $shippingAddress->getCustomerAddressId(); $enabledAddressValidationCountries = explode(',', $this->config->getAddressValidationCountriesEnabled($storeId)); if (!in_array($shippingAddress->getCountryId(), $enabledAddressValidationCountries)) { $shouldValidateAddress = false; } if ($shouldValidateAddress) { try { $validAddress = $this->validationInteraction->validateAddress($shippingAddress, $storeId); } catch (AddressValidateException $e) { $errorMessage = $e->getMessage(); } catch (\SoapFault $e) { // If there is a SoapFault, it will have already been logged, so just disable address validation, as we // don't want to display SoapFault error message to user $shouldValidateAddress = false; } catch (\Exception $e) { $this->avaTaxLogger->error('Error in validating address in aroundSaveAddressInformation: ' . $e->getMessage()); // Continue without address validation $shouldValidateAddress = false; } } // Determine which address to save to the customer or shipping addresses if (!is_null($validAddress)) { $quoteAddress = $validAddress; } else { $quoteAddress = $shippingAddress; } try { /* * Regardless of whether address was validated by AvaTax, if the address is a customer address then we need * to save that address on the customer record. The reason for this is that when a user is on the "Review * & Payments" step and they are selecting between "Valid" and "Original" address options, the selected * address information is submitted to this API so that the customer address is updated and tax * calculation is affected accordingly. */ if ($customerAddressId) { // Update the customer address $customerAddress = $this->customerAddressRepository->getById($customerAddressId); $mergedCustomerAddress = $this->addressInteraction->copyQuoteAddressToCustomerAddress($quoteAddress, $customerAddress); $this->customerAddressRepository->save($mergedCustomerAddress); } else { // Update the shipping address $addressInformation->setShippingAddress($quoteAddress); } } catch (\Exception $e) { // There may be scenarios in which the above address updating may fail, in which case we should just do // nothing $this->avaTaxLogger->error('Error in saving address: ' . $e->getMessage()); // Continue without address validation $shouldValidateAddress = false; } $returnValue = $proceed($cartId, $addressInformation); $this->ensureTaxCalculationSuccess($storeId); if (!$shouldValidateAddress) { return $returnValue; } $paymentDetailsExtension = $returnValue->getExtensionAttributes(); if (is_null($paymentDetailsExtension)) { $paymentDetailsExtension = $this->paymentDetailsExtensionFactory->create(); } if (!is_null($validAddress)) { $paymentDetailsExtension->setValidAddress($validAddress); } else { $paymentDetailsExtension->setErrorMessage($errorMessage); } $paymentDetailsExtension->setOriginalAddress($shippingAddress); $returnValue->setExtensionAttributes($paymentDetailsExtension); return $returnValue; }