Exemplo n.º 1
0
        public static async Task <Order> BuildOrderAsync(Models.Cart cart, IPrestashopApi prestashopApi)
        {
            var prestaCart = await CartToPost.BuildCartAsync(cart, prestashopApi);

            var postedCart = await prestashopApi.PostCart(prestaCart);

            var customer = (await prestashopApi.GetCustomerById(cart.User.PrestashopId.Value)).First();

            var customerAddresses = await prestashopApi.GetAddressByCustomer(cart.User.PrestashopId.Value);

            var customerAddress = customerAddresses.First();

            var order = new PostOrder(customer, postedCart.Cart, customerAddress);

            double totalPrice = 0;

            foreach (CartRow row in prestaCart.Cart.Rows.Rows)
            {
                var product = (await prestashopApi.GetProductById(row.ProductId)).First();

                totalPrice += product.Price;

                order.OrderRows.Rows.Add(
                    new OrderRow(product, row.Quantity)
                    );
            }

            order.TotalProducts        = totalPrice;
            order.TotalProductsWithTax = totalPrice * 1.21;
            order.TotalPaidTaxExcluded = totalPrice;
            order.TotalPaidTaxIncluded = totalPrice * 1.21;
            order.TotalPaid            = totalPrice * 1.21;

            var prestaOrder = new OrderToPost {
                Order = order
            };

            System.Xml.Serialization.XmlSerializer writer =
                new System.Xml.Serialization.XmlSerializer(typeof(OrderToPost));

            var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//SerializationOverview.xml";

            System.IO.FileStream file = System.IO.File.Create(path);

            writer.Serialize(file, prestaOrder);
            file.Close();

            var postedOrder = await prestashopApi.PostOrder(prestaOrder);

            return(await Task.FromResult(postedOrder.Order));
        }
Exemplo n.º 2
0
        public static async Task <CartToPost> BuildCartAsync(Models.Cart cart, IPrestashopApi prestashopApi)
        {
            var customer = (await prestashopApi.GetCustomerById(cart.User.PrestashopId.Value)).First();

            var prestaCart = new CartToPost()
            {
                Cart = new PostCart(customer)
            };

            var rowCollection = new CartRowCollection();

            foreach (Models.OrderLine line in cart.OrderLine)
            {
                var row = new CartRow(line.ProductId, line.Amount);
                rowCollection.Rows.Add(row);
            }

            prestaCart.Cart.Rows = rowCollection;

            return(await Task.FromResult(prestaCart));
        }