Beispiel #1
0
        public ActionResult Pago(int?id)
        {
            using (var tran = db.Database.BeginTransaction())
            {
                var cart = ShoppingCart.GetCart(this.HttpContext);
                var productosCarritos = cart.GetCartItems();

                //var idCarrito = cart.ShoppingCartId;
                var idCarrito = User.Identity.Name;

                var totalCarrito = cart.GetTotal();


                try
                {
                    foreach (var item in productosCarritos)
                    {
                        int cantidad = item.Cantidad;


                        var stockProducto = db.Productos.Find(item.ProductoId).Stock;

                        var nombre    = db.Productos.Find(item.ProductoId).Descripcion;
                        var Productos = db.Productos.FirstOrDefault(p => p.ProductoId == item.Producto.ProductoId);


                        if (cantidad > stockProducto)
                        {
                            tran.Rollback();

                            TempData["mensaje"] = "No hay Stock suficiente de " + item.Producto.Descripcion;

                            return(RedirectToAction("IndexError", "ShoppingCart"));
                        }

                        else
                        {
                            stockProducto = stockProducto - cantidad;

                            var model = new PedidosCliente();

                            model.Cantidad   = totalCarrito;
                            ViewData["Cant"] = totalCarrito;

                            if (idCarrito != "")
                            {
                                var nombreUsuario   = db.Users.SingleOrDefault(c => c.Email == idCarrito).Nombres;
                                var apellidoUsuario = db.Users.SingleOrDefault(c => c.Email == idCarrito).Apellido;
                                var telefonoUsuario = db.Users.SingleOrDefault(c => c.Email == idCarrito).PhoneNumber;
                                var DNIUsuario      = db.Users.SingleOrDefault(c => c.Email == idCarrito).DNI;

                                model.Email    = idCarrito;
                                model.Nombres  = nombreUsuario;
                                model.Apellido = apellidoUsuario;
                                model.Telefono = telefonoUsuario;
                                model.DNI      = DNIUsuario;


                                ViewData["Usuario"]  = User.Identity.Name;
                                ViewData["Nombre"]   = nombreUsuario;
                                ViewData["Apellido"] = apellidoUsuario;
                                ViewData["Telefono"] = telefonoUsuario;
                                ViewData["DNI"]      = DNIUsuario;
                            }
                            else
                            {
                                model.Email    = idCarrito;
                                model.Nombres  = "";
                                model.Apellido = "";
                                model.Telefono = "";
                                model.DNI      = "";
                            }

                            //Mercado Pago

                            MercadoPago.SDK.ClientId     = "1782351866703164";
                            MercadoPago.SDK.ClientSecret = "6nNax7XMsv6y4qiKxDhEJ4zmZZlCmlCN";

                            // Create a preference object
                            Preference preference = new Preference();
                            //# Adding an item object
                            preference.Items.Add(
                                new Item()
                            {
                                Id         = model.Id.ToString(),
                                Title      = "Registrar Pago de: " + model.NombreCompleto,
                                Quantity   = 1,
                                CurrencyId = 0,
                                UnitPrice  = totalCarrito
                            }
                                );
                            // Setting a payer object as value for Payer property
                            preference.Payer = new Payer()
                            {
                                Email   = model.Email,
                                Name    = model.Nombres,
                                Surname = model.Apellido,
                                Phone   = new Phone
                                {
                                    Number = model.Telefono
                                },

                                Date_created = model.FechaCreacion,


                                Identification = new Identification
                                {
                                    Type   = "DNI",
                                    Number = model.DNI,
                                }
                            };
                            // Save and posti   ng preference
                            preference.Save();

                            model.UrlMercadoPago = preference.InitPoint;
                            ViewData["MP"]       = model.UrlMercadoPago;



                            db.SaveChanges();
                            tran.Commit();

                            return(View());
                        }
                    }
                }


                catch (Exception ex)
                {
                    if (ex.InnerException != null &&
                        ex.InnerException.InnerException != null &&
                        ex.InnerException.InnerException.Message.Contains("No"))
                    {
                        ModelState.AddModelError(string.Empty, "The field already exists");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "The field already exists 2");
                    }

                    return(RedirectToAction("Index", "ShoppingCart"));
                }
                tran.Rollback();
                return(View());
            }
        }
Beispiel #2
0
        public int CreateOrder(PedidosCliente customerOrder)
        {
            decimal orderTotal = 0;

            var cartItems = GetCartItems();

            ////Mercado Pago

            //SDK.ClientId = "1782351866703164";
            //SDK.ClientSecret = "6nNax7XMsv6y4qiKxDhEJ4zmZZlCmlCN";

            //// Create a preference object
            //Preference preference = new Preference();



            foreach (var item in cartItems)
            {
                var stockProductos = db.Productos.FirstOrDefault(p => p.ProductoId == item.Producto.ProductoId);

                stockProductos.Stock = stockProductos.Stock - item.Cantidad;

                var orderedProduct = new ProductosPedidos
                {
                    ProductoId      = item.ProductoId,
                    PedidoClienteId = customerOrder.Id,
                    Cantidad        = item.Cantidad
                };

                orderTotal += (item.Cantidad * item.Producto.Precio);

                db.ProductosPedidos.Add(orderedProduct);

                //    //# Adding an item object
                //    preference.Items.Add(
                //      new Item()
                //      {
                //          Id = item.ProductoId.ToString(),
                //          Title = "Nombre: " + customerOrder.NombreCompleto,
                //          Quantity = item.Cantidad,
                //          CurrencyId = 0,
                //          UnitPrice = item.Producto.Precio
                //      }
                //    );            }


                //// Setting a payer object as value for Payer property
                //preference.Payer = new Payer()
                //{
                //    Email = customerOrder.Email,
                //    Name = customerOrder.Nombres,
                //    Surname = customerOrder.Apellido,
                //    Phone = new Phone
                //    {
                //        Number = customerOrder.Telefono
                //    },

                //    Date_created = customerOrder.FechaCreacion,


                //    Identification = new Identification
                //    {
                //        Type = "DNI",
                //        Number = customerOrder.DNI,
                //    }
            }
            ;
            //// Save and posti   ng preference
            //preference.Save();

            customerOrder.Cantidad = orderTotal;
            //customerOrder.UrlMercadoPago = preference.InitPoint;


            db.SaveChanges();

            EmptyCart();

            return(customerOrder.Id);
        }
Beispiel #3
0
        public ActionResult Pago(FormCollection values)
        {
            using (var tran = db.Database.BeginTransaction())
            {
                var order = new PedidosCliente();

                TryUpdateModel(order);

                try
                {
                    //if (string.Equals(values["PromoCode"], PromoCode, StringComparison.OrdinalIgnoreCase) == false)
                    //{
                    //    return View(order);
                    //}
                    //else
                    //{

                    var masErrores = false;

                    if (String.IsNullOrEmpty(order.Apellido))
                    {
                        ModelState.AddModelError(string.Empty, "");
                        masErrores = true;
                    }

                    else if (String.IsNullOrEmpty(order.Nombres))
                    {
                        ModelState.AddModelError(string.Empty, "");
                        masErrores = true;
                    }

                    else if (String.IsNullOrEmpty(order.Email))
                    {
                        ModelState.AddModelError(string.Empty, "");
                        masErrores = true;
                    }

                    else if (String.IsNullOrEmpty(order.DNI))
                    {
                        ModelState.AddModelError(string.Empty, "");
                        masErrores = true;
                    }

                    if (masErrores)
                    {
                        return(View("Pago", order));
                    }

                    order.FechaCreacion = DateTime.Now;
                    order.Estado        = true;

                    var cart = ShoppingCart.GetCart(this.HttpContext);

                    var productosCarritos = cart.GetCartItems();
                    order.Cantidad = cart.GetTotal();

                    if (User.Identity.Name != "")
                    {
                        var nombreUsuario   = db.Users.SingleOrDefault(c => c.Email == User.Identity.Name).Nombres;
                        var apellidoUsuario = db.Users.SingleOrDefault(c => c.Email == User.Identity.Name).Apellido;
                        var telefonoUsuario = db.Users.SingleOrDefault(c => c.Email == User.Identity.Name).PhoneNumber;
                        var DNIUsuario      = db.Users.SingleOrDefault(c => c.Email == User.Identity.Name).DNI;

                        order.Email    = User.Identity.Name;
                        order.Nombres  = nombreUsuario;
                        order.Apellido = apellidoUsuario;
                        order.Telefono = telefonoUsuario;
                        order.DNI      = DNIUsuario;
                    }

                    MailMessage correo = new MailMessage();


                    string texto = "Detalle de su compra: " + Environment.NewLine;
                    foreach (var item in productosCarritos)
                    {
                        var Productos = db.Productos.FirstOrDefault(p => p.ProductoId == item.Producto.ProductoId);

                        int     cantidadProducto = item.Cantidad;
                        decimal precio           = Productos.Precio;

                        texto = texto + Environment.NewLine + "-" + Productos.Descripcion.ToString() + "- Cantidad: " + cantidadProducto + "- Precio: " + precio;
                    }

                    texto       = texto + Environment.NewLine + Environment.NewLine + "Total a pagar: " + order.Cantidad;
                    correo.Body = texto;

                    db.PedidosCliente.Add(order);
                    db.SaveChanges();

                    cart.CreateOrder(order);


                    correo.From = new MailAddress("*****@*****.**");
                    correo.To.Add(order.Email);
                    correo.Bcc.Add("*****@*****.**");

                    int id = order.Id;
                    correo.Subject = "Compra Nº " + order.Id;

                    correo.IsBodyHtml = false;
                    correo.Priority   = MailPriority.Normal;

                    SmtpClient smtp = new SmtpClient();

                    smtp.Host                  = "smtp.gmail.com";
                    smtp.Port                  = 25;
                    smtp.EnableSsl             = true;
                    smtp.UseDefaultCredentials = true;
                    string sCuentaCorreo   = "*****@*****.**";
                    string sPasswordCorreo = "mauseba.";
                    smtp.Credentials = new System.Net.NetworkCredential(sCuentaCorreo, sPasswordCorreo);

                    smtp.Send(correo);
                    ViewBag.Mensaje = "Correo enviado";



                    db.SaveChanges();//we have received the total amount lets update it
                    tran.Commit();

                    return(RedirectToAction("Completo", new { id = order.Id }));
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    ex.InnerException.ToString();
                    return(View(order));
                }
            }
        }