Ejemplo n.º 1
0
        public async Task <IActionResult> Shipping(DeliveryInformationVm model)
        {
            var currentUser = await _workContext.GetCurrentUser();

            // TODO Handle error messages
            if (!ModelState.IsValid && (model.ShippingAddressId == 0))
            {
                PopulateShippingForm(model, currentUser);
                return(View(model));
            }

            var cart = await _cartRepository
                       .Query()
                       .Where(x => x.UserId == currentUser.Id && x.IsActive).FirstOrDefaultAsync();

            if (cart == null)
            {
                throw new ApplicationException($"Cart of user {currentUser.Id} cannot be found");
            }

            cart.ShippingData = JsonConvert.SerializeObject(model);
            await _cartRepository.SaveChangesAsync();

            return(Redirect("~/checkout/payment"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateOrder(long cartId, [FromBody] DeliveryInformationVm deliveryInformationVm)
        {
            var currentUser = await _workContext.GetCurrentUser();

            var cart = await _cartRepository.Query().FirstOrDefaultAsync(x => x.Id == cartId);

            if (cart == null)
            {
                return(NotFound());
            }

            if (cart.CreatedById != currentUser.Id)
            {
                return(Forbid());
            }

            cart.ShippingData = JsonConvert.SerializeObject(deliveryInformationVm);
            var orderCreateResult = await _orderService.CreateOrder(cart.Id, "CashOnDelivery", 0);

            if (!orderCreateResult.Success)
            {
                return(BadRequest(orderCreateResult.Error));
            }

            return(Created($"/api/orders/{orderCreateResult.Value.Id}", new { id = orderCreateResult.Value.Id }));
        }
        public async Task <JsonResult> RazorpayCheckout(long userId, DeliveryInformationVm model)
        {
            //var currentUser = await _workContext.GetCurrentUser();
            var cart = await _cartService.GetActiveCart(userId);

            if (cart == null)
            {
                // return NotFound();
            }

            if (cart.CreatedById != userId)
            {
                // return Forbid();
            }

            cart.ShippingData = JsonConvert.SerializeObject(model);
            cart.OrderNote    = model.OrderNote;
            _cartRepository.SaveChanges();

            //  var calculatedFee = CalculateFee(cart);
            // var orderCreateResult = await _orderService.CreateOrder(cart.Id, "Razorpay", calculatedFee);
            var orderCreateResult = await _orderService.CreateOrder(cart.Id, "Razorpay", 0);

            //if (!orderCreateResult.Success)
            //{
            //    TempData["Error"] = orderCreateResult.Error;
            //    return Redirect("~/checkout/payment");
            //}
            orderCreateResult.Value = null;
            return(Json(new { success = orderCreateResult.Success, error = orderCreateResult.Error }));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Shipping()
        {
            //despues de login vamos aqui
            // codigo origen
            //var currentUser = await _workContext.GetCurrentUser();
            //debug este procediiento, devuelve carrito vacio.
            //var cart = await _cartService.GetActiveCartDetails(currentUser.Id);
            //if (cart == null || !cart.Items.Any())
            //{
            //    return Redirect("~/");
            //}

            //var model = new DeliveryInformationVm();

            //PopulateShippingForm(model, currentUser);

            var currentUser = await _workContext.GetCurrentUser();

            var cart = DataCart();

            //cargamos los datos de nuestro carrito
            if (cart == null || !cart.Items.Any())
            {
                return(Redirect("~/"));
            }

            var model = new DeliveryInformationVm();

            PopulateShippingForm(model, currentUser);

            return(View(model));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Shipping()
        {
            var model = new DeliveryInformationVm();

            var currentUser = await _workContext.GetCurrentUser();

            PopulateShippingForm(model, currentUser);

            return(View(model));
        }
Ejemplo n.º 6
0
        private void PopulateShippingForm(DeliveryInformationVm model, User currentUser)
        {
            model.ExistingShippingAddresses = _userAddressRepository
                                              .Query()
                                              .Where(x => (x.AddressType == AddressType.Shipping) && (x.UserId == currentUser.Id))
                                              .Select(x => new ShippingAddressVm
            {
                UserAddressId       = x.Id,
                ContactName         = x.Address.ContactName,
                Phone               = x.Address.Phone,
                AddressLine1        = x.Address.AddressLine1,
                CityName            = x.Address.City,
                ZipCode             = x.Address.ZipCode,
                DistrictName        = x.Address.District.Name,
                StateOrProvinceName = x.Address.StateOrProvince.Name,
                CountryName         = x.Address.Country.Name,
                IsCityEnabled       = x.Address.Country.IsCityEnabled,
                IsZipCodeEnabled    = x.Address.Country.IsZipCodeEnabled,
                IsDistrictEnabled   = x.Address.Country.IsDistrictEnabled
            }).ToList();

            model.ShippingAddressId = currentUser.DefaultShippingAddressId ?? 0;

            model.UseShippingAddressAsBillingAddress = true;

            model.NewAddressForm.ShipableContries = _countryRepository.Query()
                                                    .Where(x => x.IsShippingEnabled)
                                                    .OrderBy(x => x.Name)
                                                    .Select(x => new SelectListItem
            {
                Text  = x.Name,
                Value = x.Id.ToString()
            }).ToList();

            if (model.NewAddressForm.ShipableContries.Count == 1)
            {
                var onlyShipableCountryId = model.NewAddressForm.ShipableContries.First().Value;

                model.NewAddressForm.StateOrProvinces = _stateOrProvinceRepository
                                                        .Query()
                                                        .Where(x => x.CountryId == onlyShipableCountryId)
                                                        .OrderBy(x => x.Name)
                                                        .Select(x => new SelectListItem
                {
                    Text  = x.Name,
                    Value = x.Id.ToString()
                }).ToList();
            }
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> DeliveryInformation(DeliveryInformationVm model)
        {
            var currentUser = await _workContext.GetCurrentUser();

            // TODO Handle error messages
            if (!ModelState.IsValid && (model.ShippingAddressId == 0))
            {
                PopulateShippingForm(model, currentUser);
                return(View(model));
            }

            Address billingAddress;
            Address shippingAddress;

            if (model.ShippingAddressId == 0)
            {
                var address = new Address
                {
                    AddressLine1      = model.NewAddressForm.AddressLine1,
                    AddressLine2      = model.NewAddressForm.AddressLine2,
                    ContactName       = model.NewAddressForm.ContactName,
                    CountryId         = model.NewAddressForm.CountryId,
                    StateOrProvinceId = model.NewAddressForm.StateOrProvinceId,
                    DistrictId        = model.NewAddressForm.DistrictId,
                    City       = model.NewAddressForm.City,
                    PostalCode = model.NewAddressForm.PostalCode,
                    Phone      = model.NewAddressForm.Phone
                };

                var userAddress = new UserAddress
                {
                    Address     = address,
                    AddressType = AddressType.Shipping,
                    UserId      = currentUser.Id
                };

                _userAddressRepository.Add(userAddress);

                billingAddress = shippingAddress = address;
            }
            else
            {
                billingAddress = shippingAddress = _userAddressRepository.Query().Where(x => x.Id == model.ShippingAddressId).Select(x => x.Address).First();
            }

            await _orderService.CreateOrder(currentUser, model.ShippingMethod, billingAddress, shippingAddress);

            return(RedirectToAction("OrderConfirmation"));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> DeliveryInformation()
        {
            var model = new DeliveryInformationVm();

            var currentUser = await _workContext.GetCurrentUser();

            model.ExistingShippingAddresses = _userAddressRepository
                                              .Query()
                                              .Where(x => (x.AddressType == AddressType.Shipping) && (x.UserId == currentUser.Id))
                                              .Select(x => new ShippingAddressVm
            {
                UserAddressId       = x.Id,
                ContactName         = x.Address.ContactName,
                Phone               = x.Address.Phone,
                AddressLine1        = x.Address.AddressLine1,
                AddressLine2        = x.Address.AddressLine1,
                DistrictName        = x.Address.District.Name,
                StateOrProvinceName = x.Address.StateOrProvince.Name,
                CountryName         = x.Address.Country.Name
            }).ToList();

            model.ShippingAddressId = currentUser.DefaultShippingAddressId ?? 0;

            model.NewAddressForm.ShipableContries = _countryRepository.Query()
                                                    .Where(x => x.IsShippingEnabled)
                                                    .OrderBy(x => x.Name)
                                                    .Select(x => new SelectListItem
            {
                Text  = x.Name,
                Value = x.Id.ToString()
            }).ToList();

            if (model.NewAddressForm.ShipableContries.Count == 1)
            {
                var onlyShipableCountryId = long.Parse(model.NewAddressForm.ShipableContries.First().Value);
                model.NewAddressForm.StateOrProvinces = _stateOrProvinceRepository
                                                        .Query()
                                                        .Where(x => x.CountryId == onlyShipableCountryId)
                                                        .OrderBy(x => x.Name)
                                                        .Select(x => new SelectListItem
                {
                    Text  = x.Name,
                    Value = x.Id.ToString()
                }).ToList();
            }

            return(View(model));
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> Shipping()
        {
            var currentUser = await _workContext.GetCurrentUser();

            var cart = await _cartService.GetActiveCartDetails(currentUser.Id);

            if (cart == null || !cart.Items.Any())
            {
                return(Redirect("~/"));
            }

            var model = new DeliveryInformationVm();

            PopulateShippingForm(model, currentUser);

            return(View(model));
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> DeliveryInformation(DeliveryInformationVm model)
        {
            if (!ModelState.IsValid && model.ShippingAddressId == 0)
            {
                return(View(model));
            }

            var currentUser = await _workContext.GetCurrentUser();

            if (model.ShippingAddressId == 0)
            {
                var address = new Address
                {
                    AddressLine1      = model.NewAddressForm.AddressLine1,
                    ContactName       = model.NewAddressForm.ContactName,
                    CountryId         = 1,
                    StateOrProvinceId = model.NewAddressForm.StateOrProvinceId,
                    DistrictId        = model.NewAddressForm.DistrictId,
                    Phone             = model.NewAddressForm.Phone
                };

                var userAddress = new UserAddress
                {
                    Address     = address,
                    AddressType = AddressType.Shipping,
                    UserId      = currentUser.Id
                };

                _userAddressRepository.Add(userAddress);
                currentUser.CurrentShippingAddress = userAddress;
            }
            else
            {
                currentUser.CurrentShippingAddress = _userAddressRepository.Query().FirstOrDefault(x => x.Id == model.ShippingAddressId);
            }
            _orderService.CreateOrder(currentUser);

            return(RedirectToAction("OrderConfirmation"));
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> Shipping(DeliveryInformationVm model)
        {
            var currentUser = await _workContext.GetCurrentUser();

            // TODO Handle error messages
            if ((!model.NewAddressForm.IsValid() && model.ShippingAddressId == 0) ||
                (!model.NewBillingAddressForm.IsValid() && !model.UseShippingAddressAsBillingAddress && model.BillingAddressId == 0))
            {
                PopulateShippingForm(model, currentUser);
                return(View(model));
            }

            var cart = await _cartService.GetActiveCart(currentUser.Id);

            if (cart == null)
            {
                throw new ApplicationException($"Cart of user {currentUser.Id} cannot be found");
            }

            cart.ShippingData = JsonConvert.SerializeObject(model);
            await _cartRepository.SaveChangesAsync();

            return(Redirect("~/checkout/payment"));
        }