Пример #1
0
        public async Task RejectOrderAsync(Order order, RejectOrderDTO rejectOrder)
        {
            order.Status       = OrderStatus.Rejected;
            order.RejectReason = rejectOrder.RejectReason;

            foreach (var orderItem in order.OrderItems)
            {
                var specificProduct = await _context.Products.FindAsync(orderItem.ProductId);

                if (specificProduct == null)
                {
                    continue;
                }

                var newStock = specificProduct.Stock + orderItem.Count;

                specificProduct.Stock = newStock;

                await _context.SaveChangesAsync();

                await _bus.Publish(new ProductUpdateStockMessage
                {
                    SpecificProductId = specificProduct.Id,
                    Stock             = newStock
                });
            }

            await _context.SaveChangesAsync();
        }
Пример #2
0
        public async Task <Order> CreateOrderAsync(CreateOrderDTO createOrder)
        {
            StripeConfiguration.ApiKey = "sk_test_9lbV8iZ2EjD5TiOzTAFURm7H00TCPFae4M";
            List <OrderItem> orderItemsToAdd = new List <OrderItem>();
            double           totalPrice      = 0;

            foreach (var orderItem in createOrder.OrderItems)
            {
                var product = await _context.Products.FindAsync(orderItem.ProductId);

                product.Stock -= orderItem.Count;
                await _bus.Publish(new ProductUpdateStockMessage
                {
                    SpecificProductId = product.Id,
                    Stock             = product.Stock
                });

                totalPrice += product.Price * orderItem.Count;

                orderItemsToAdd.Add(
                    new OrderItem
                {
                    Count        = orderItem.Count,
                    ProductId    = product.Id,
                    ProductName  = orderItem.ProductName,
                    ProductPrice = product.Price,
                    ProductSize  = orderItem.ProductSize,
                    ImageUrl     = orderItem.ImageUrl
                }
                    );
            }
            var order = new Order
            {
                UserId      = createOrder.UserId,
                Address     = createOrder.Address,
                CreatedAt   = DateTime.UtcNow.AddHours(7),
                Email       = createOrder.Email,
                UserName    = createOrder.UserName,
                PhoneNumber = createOrder.PhoneNumber,
                TotalPrice  = totalPrice,
                OrderItems  = orderItemsToAdd,
                Status      = OrderStatus.New,
                PaymentType = createOrder.PaymentType
            };
            await _context.Orders.AddAsync(order);

            await _context.SaveChangesAsync();

            return(order);
        }
Пример #3
0
        public async Task <PaymentIntent> ApplyPaymentToOrder(Order order)
        {
            var options = new PaymentIntentCreateOptions
            {
                Currency = "vnd",
                Amount   = (long?)order.TotalPrice
            };

            var service       = new PaymentIntentService();
            var paymentIntent = service.Create(options);

            order.PaymentIntentId = paymentIntent.Id;
            await _context.SaveChangesAsync();

            return(paymentIntent);
        }
Пример #4
0
 public async Task DeliveredOrderAsync(Order order)
 {
     order.Status = OrderStatus.Done;
     await _context.SaveChangesAsync();
 }
Пример #5
0
 public async Task AcceptOrderAsync(Order order)
 {
     order.Status = OrderStatus.Accepted;
     await _context.SaveChangesAsync();
 }