Пример #1
0
        public ActionResult Create(CheckoutModel checkout)
        {
            var items = new List <Item>();

            try
            {
                var proxy = new ServiceProxyB2CClient();

                var currentUser = System.Web.HttpContext.Current.User as CustomPrincipal;
                if (currentUser == null)
                {
                    return(RedirectToAction("Index", "Account"));
                }

                var clientConfiguration = new MemcachedClientConfiguration {
                    Protocol = MemcachedProtocol.Binary
                };
                clientConfiguration.Servers.Add(new IPEndPoint(IPAddress.Parse("192.168.99.100"), 32768));
                using (var cartCacheClient = new MemcachedClient(clientConfiguration))
                {
                    checkout.Cart = cartCacheClient.Get <Cart>("Cart-" + currentUser.UserName);
                    foreach (var item in checkout.Cart.Items)
                    {
                        checkout.Total = checkout.Total + item.Cantidad * (item.Producto.tipo_espectaculo.precio + item.Producto.tipo_hospedaje.precio + item.Producto.tipo_transporte.precio);
                    }
                    if (checkout.Cart == null)
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }

                items.AddRange(checkout.Cart.Items.Select(i => new Item
                {
                    precio      = i.Producto.tipo_espectaculo.precio + i.Producto.tipo_hospedaje.precio + i.Producto.tipo_transporte.precio,
                    cantidad    = i.Cantidad,
                    id_prod     = i.Producto.id_producto,
                    nombre_prod = i.Producto.descripcion
                }));
                var orden = new Orden
                {
                    precio      = checkout.Total,
                    estatus     = EstatusOrden.VALIDACION,
                    fecha_orden = DateTime.Now,
                    id_cliente  = checkout.CustomerId,
                    item        = items.ToArray()
                };

                var orderId = proxy.CrearOrdenes(orden);
                if (orderId == null)
                {
                    return(View());
                }
                using (var cartCacheClient = new MemcachedClient(clientConfiguration))
                {
                    cartCacheClient.Remove("Cart-" + currentUser.UserName);
                }
                return(RedirectToAction("Index", "Orden", new { OrderId = orderId }));
            }
            catch
            {
                return(View());
            }
        }
        public ActionResult Pagar()
        {
            if (!ModelState.IsValid)
            {
                return(View("Index"));
            }

            //consulto el usuario logueado
            var currentUser = System.Web.HttpContext.Current.User as CustomPrincipal;

            if (currentUser == null)
            {
                return(RedirectToAction("Index", "Account"));
            }

            var clientConfiguration = new MemcachedClientConfiguration {
                Protocol = MemcachedProtocol.Binary
            };

            clientConfiguration.Servers.Add(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 32769));
            ServiceProxyB2C.CrearOrdenResponse response = new ServiceProxyB2C.CrearOrdenResponse();
            using (var client = new MemcachedClient(clientConfiguration))
            {
                //consulto el cache del usuario logueado
                var cart = client.Get <Cart>("Cart-" + currentUser.UserName);

                if (cart != null)
                {
                    var proxy = new ServiceProxyB2CClient();

                    // se crea una nueva orden
                    Orden orden = new Orden();

                    // se deja el estado en validacion
                    orden.estatus     = EstatusOrden.VALIDACION;
                    orden.fecha_orden = DateTime.Now;

                    // se crea una nueva lista de items del carrito
                    List <ServiceProxyB2C.Item> lstitem = new List <ServiceProxyB2C.Item>();
                    foreach (Models.Item item in cart.Items)
                    {
                        ServiceProxyB2C.Item itemorden = new ServiceProxyB2C.Item();
                        itemorden.id_prod = item.Producto.id_producto;
                        // el servicio pide el nombre del producto, en el carrito no hay se coloca el nombre del espectaculo
                        itemorden.nombre_prod = item.Producto.espectaculo;

                        // en el servicio se pide el precio, se deja un valor fijo para ajustar modelo
                        itemorden.precio   = 100000;
                        itemorden.cantidad = item.Cantidad;
                        lstitem.Add(itemorden);
                    }
                    orden.item = lstitem.ToArray();

                    response = proxy.CrearOrdenes(orden);
                }
                else
                {
                    response.id_orden               = "";
                    response.estatus_orden          = EstatusOrden.RECHAZADA;
                    response.estatus_ordenSpecified = false;
                }
            }
            return(View("_Compra", response));
        }