Ejemplo n.º 1
0
        public async Task <MOrder> CreateOrderAsync(OrderParams orderParams)
        {
            MOrder order = MOrder.FromParams(orderParams);

            order.Seller = await usersRepository.GetUserAsync <Seller>(order.SellerId);

            order.Status          = OrderConstants.Status.Pending;
            order.Currency        = "USD";
            order.PaymentMethod   = orderParams.PaymentMethod;
            order.PaymentReceiver = order.GetReceiver(order.PaymentMethod);


            foreach (var itemParams in orderParams.Items)
            {
                MOrderItem item = MOrderItem.FromParams(itemParams);

                item.Product = await productsRepository.GetProductAsync(item.ProductId, order.BuyerId);

                if (item.Product == null)
                {
                    return(null);
                }

                if (order.SellerId != item.Product.SellerId)
                {
                    return(null);
                }

                if (item.Product.Customer != null)
                {
                    return(null);
                }

                item.ProductName = item.Product.Name;
                item.Price       = item.Product.Price;

                order.TotalPrice += item.Price;
                order.Items.Add(item);
            }

            Payment payment = Payment.Create(order.PaymentMethod, string.Empty,
                                             order.GetReceiver(order.PaymentMethod), order.Currency, order.TotalPrice);

            foreach (MOrderItem item in order.Items)
            {
                payment.AddItem(item.ProductName, 1, item.Price);
            }

            order.PaymentId = await PaymentGatewayClient.CreatePaymentAsync(payment);

            return(await ordersRepository.AddOrderAsync(order));
        }