public ActionResult CreatePost() { var user = _workContextAccessor.GetContext().CurrentUser; if (user == null) { // we should never be here, because the AuthorizeAttribute should // take care of anonymous users. return(new HttpUnauthorizedResult(T("Sign In to manage your saved addresses.").Text)); } var newAddress = new AddressEditViewModel(); if (!TryUpdateModel(newAddress) || !ValidateVM(newAddress)) { _transactionManager.Cancel(); // added the assignment of the lists because in case of error in the validation the properties are not populated newAddress.ShippingCountries = _addressConfigurationService.CountryOptions(AddressRecordType.ShippingAddress); newAddress.BillingCountries = _addressConfigurationService.CountryOptions(AddressRecordType.BillingAddress); newAddress.Errors.Add(T("It was impossible to validate your address.").Text); return(View(newAddress)); } // Convert the values of Country, City, and Province to strings and ids for // the AddressRecord. FixUpdate(newAddress); // save record _nwazetCommunicationService.AddAddress(newAddress.AddressRecord, user); _notifier.Information(T("Address created successfully.")); return(RedirectToAction("Edit", new { id = newAddress.AddressRecord.Id })); }
public ActionResult CreatePost() { var user = _workContextAccessor.GetContext().CurrentUser; if (user == null) { // we should never be here, because the AuthorizeAttribute should // take care of anonymous users. return(new HttpUnauthorizedResult(T("Sign In to manage your saved addresses.").Text)); } var newAddress = new AddressEditViewModel(); if (!TryUpdateModel(newAddress)) { _transactionManager.Cancel(); newAddress.Errors.Add(T("It was impossible to validate your address.").Text); return(View(newAddress)); } _nwazetCommunicationService.AddAddress(newAddress.AddressRecord, user); _notifier.Information(T("Address created successfully.")); return(RedirectToAction("Edit", new { id = newAddress.AddressRecord.Id })); }
public ActionResult IndexPOST(CheckoutViewModel model) { // Depending on whether shipping is required or not, the validation of what has been // input changes, because if there is no shipping there's no need for the shipping // address. var user = _workContextAccessor.GetContext().CurrentUser; if (!_checkoutHelperService.UserMayCheckout(user, out ActionResult redirect)) { if (redirect != null) { return(redirect); } return(Redirect(RedirectUrl)); } // check if the user is trying to reset the selected addresses to select different // ones. if (model.ResetAddresses) { // reset shipment to redo the calculation correctly, // removing from the total the shipment that will have to be reselected _shoppingCart.ShippingOption = null; ReinflateViewModelAddresses(model); // Put the model we validated in TempData so it can be reused in the next action. TempData["CheckoutViewModel"] = model; return(RedirectToAction("Index")); } model.ShippingRequired = IsShippingRequired(); //we'll reuse this // Ensure address types are initialized correctly model.BillingAddressVM.AddressType = AddressRecordType.BillingAddress; model.BillingAddressVM.AddressRecord.AddressType = AddressRecordType.BillingAddress; if (model.ShippingAddressVM != null) { model.ShippingAddressVM.AddressType = AddressRecordType.ShippingAddress; model.ShippingAddressVM.AddressRecord.AddressType = AddressRecordType.ShippingAddress; } // validate var validationSuccess = ValidateVM(model); if (!validationSuccess) { // don't move on, but rather leave the user on this form model.BillingAddressVM = CreateVM(AddressRecordType.BillingAddress, model.BillingAddressVM); if (model.ShippingRequired) { model.ShippingAddressVM = CreateVM(AddressRecordType.ShippingAddress, model.ShippingAddressVM); } InjectServices(model); if (user != null) { // also load the list of existing addresses for them model.ListAvailableBillingAddress = _nwazetCommunicationService.GetBillingByUser(user); // we are only going to load the shipping addresses if shipping is required if (model.ShippingRequired) { model.ListAvailableShippingAddress = _nwazetCommunicationService.GetShippingByUser(user); } } return(View(model)); } // in case validation is successful, if a user exists, try to store the // addresses they just configured. if (user != null) { if (model.BillingAddressVM != null && model.BillingAddressVM.AddressRecord != null) { var countryTP = _addressConfigurationService.GetCountry(model.BillingAddressVM.CountryId); model.BillingAddressVM.Country = _contentManager.GetItemMetadata(countryTP).DisplayText; if (model.BillingAddressVMListAddress > 0) { model.BillingAddressVM.AddressRecord.Id = model.BillingAddressVMListAddress; } _nwazetCommunicationService.AddAddress(model.BillingAddressVM.AddressRecord, user); } if (model.ShippingAddressVM != null && model.ShippingAddressVM.AddressRecord != null) { var countryTP = _addressConfigurationService.GetCountry(model.ShippingAddressVM.CountryId); model.ShippingAddressVM.Country = _contentManager.GetItemMetadata(countryTP).DisplayText; if (model.ShippingAddressVMListAddress > 0) { model.ShippingAddressVM.AddressRecord.Id = model.ShippingAddressVMListAddress; } _nwazetCommunicationService.AddAddress(model.ShippingAddressVM.AddressRecord, user); } if (!string.IsNullOrWhiteSpace(model.PhonePrefix) || !string.IsNullOrWhiteSpace(model.Phone)) { _nwazetCommunicationService.SetPhone(model.PhonePrefix, model.Phone, user); } } // In case validation is successful, depending on whether shipping is required, we // should redirect to a different action/step. // If shipping is required, we should redirect to an action that lets the user select // their preferred shipping method. If only one method is configured, the user should // still be made to go through that step. The selection of the list of available methods // can make use of the address the user has selected for shipping. // If no shipping is required we can move on to reviewing the order. // At this stage, since we also have the correct address, we can correctly compute TAX // TODO: compute VAT // Put the model we validated in TempData so it can be reused in the next action. TempData["CheckoutViewModel"] = model; if (IsShippingRequired()) { // Set values into the ShoppingCart storage var country = _addressConfigurationService ?.GetCountry(model.ShippingAddressVM.CountryId); _shoppingCart.Country = _contentManager.GetItemMetadata(country).DisplayText; _shoppingCart.ZipCode = model.ShippingAddressVM.PostalCode; return(RedirectToAction("Shipping")); } return(RedirectToAction("Review")); }