private async Task SaveAsync() { if (ShippingAddressViewModel.ValidateForm()) { string errorMessage = string.Empty; try { await ShippingAddressViewModel.ProcessFormAsync(); _navigationService.GoBack(); } catch (ModelValidationException mvex) { DisplayValidationErrors(mvex.ValidationResult); } catch (Exception ex) { errorMessage = string.Format(CultureInfo.CurrentCulture, _resourceLoader.GetString("GeneralServiceErrorMessage"), Environment.NewLine, ex.Message); } if (!string.IsNullOrWhiteSpace(errorMessage)) { await _alertMessageService.ShowAsync(errorMessage, _resourceLoader.GetString("ErrorServiceUnreachable")); } } }
private async Task ProcessFormAsync() { if (UseSameAddressAsShipping) { BillingAddressViewModel.Address = new Address { Id = Guid.NewGuid().ToString(), AddressType = AddressType.Billing, FirstName = ShippingAddressViewModel.Address.FirstName, MiddleInitial = ShippingAddressViewModel.Address.MiddleInitial, LastName = ShippingAddressViewModel.Address.LastName, StreetAddress = ShippingAddressViewModel.Address.StreetAddress, OptionalAddress = ShippingAddressViewModel.Address.OptionalAddress, City = ShippingAddressViewModel.Address.City, State = ShippingAddressViewModel.Address.State, ZipCode = ShippingAddressViewModel.Address.ZipCode, Phone = ShippingAddressViewModel.Address.Phone }; } try { await ShippingAddressViewModel.ProcessFormAsync(); await BillingAddressViewModel.ProcessFormAsync(); await PaymentMethodViewModel.ProcessFormAsync(); } catch (ModelValidationException) { // Handle validation exceptions when the order is created. } var user = _accountService.SignedInUser; var shoppingCart = await _shoppingCartRepository.GetShoppingCartAsync(); try { // Create an order with the values entered in the form await _orderRepository.CreateBasicOrderAsync(user.UserName, shoppingCart, ShippingAddressViewModel.Address, BillingAddressViewModel.Address, PaymentMethodViewModel.PaymentMethod); _navigationService.Navigate("CheckoutSummary", null); } catch (ModelValidationException mvex) { DisplayOrderErrorMessages(mvex.ValidationResult); if (_shippingAddressViewModel.Address.Errors.Errors.Count > 0) { IsShippingAddressInvalid = true; } if (_billingAddressViewModel.Address.Errors.Errors.Count > 0 && !UseSameAddressAsShipping) { IsBillingAddressInvalid = true; } if (_paymentMethodViewModel.PaymentMethod.Errors.Errors.Count > 0) { IsPaymentMethodInvalid = true; } } }