public async Task <Order_Receipt> Create(Order_ReceiptCreateDto dto)
        {
            var delivery = await repository.context.DeliveryMethods.FirstOrDefaultAsync(x => x.Id == dto.DeliveryId);

            var cart = await shoppingCartService.GetCartByUserName(dto.AccountId);

            var items = cart.Items;

            foreach (var item in items)
            {
                var book = await repository.context.Books.FirstOrDefaultAsync(x => x.Id == item.BookId);

                if (book.QuantityInStock < item.Quantity)
                {
                    return(null);
                }
                book.QuantityInStock = book.QuantityInStock - item.Quantity;
                await repository.context.SaveChangesAsync();
            }
            var     OrderItems = _mapper.Map <List <OrderItem> >(items);
            decimal total      = 0;

            foreach (var item in items)
            {
                total = total + item.TotalPrice;
            }
            var entity = new Order_Receipt
            {
                FullName       = dto.FullName,
                Phone          = dto.Phone,
                Email          = dto.Email,
                AccountId      = dto.AccountId,
                CreatedAt      = dto.CreatedAt,
                OrderItems     = OrderItems,
                DeliveryMethod = delivery,
                Status         = OrderStatus.Paid,
                TotalPrice     = total + delivery.Price,
            };

            cart.ClearItems();
            repository.Add(entity);
            repository.context.SaveChanges();
            return(entity);
        }
        public Order_Receipt Update(Order_ReceiptUpdateDto dto)
        {
            // var isExist = GetDetail(dto.Name);
            // if (isExist != null && dto.Id != isExist.Id)
            // {
            //     throw new Exception(dto.Name + " existed");
            // }

            var entity = new Order_Receipt
            {
                // Id = dto.Id,
                // CreatedAt = dto.CreatedAt,
                // TotalPrice = dto.TotalPrice,

                // AccountId = dto.AccountId,

                // Books = dto.Books
            };

            return(repository.Update(entity));
        }
        public async Task <OrderPaymentIntent> CreatePaymentIntentAsync(Order_Receipt order)
        {
            StripeConfiguration.ApiKey = _config["StripeSettings:SecretKey"];

            var service = new PaymentIntentService();

            var options = new PaymentIntentCreateOptions
            {
                Amount             = Convert.ToInt64(order.TotalPrice) * 100,
                Currency           = "usd",
                PaymentMethodTypes = new List <string> {
                    "card"
                }
            };

            var confirmOptions = new PaymentIntentConfirmOptions
            {
                PaymentMethod = "pm_card_visa"
            };

            var intent = await service.CreateAsync(options);

            order.PaymentIntent = new OrderPaymentIntent
            {
                PaymentIndentId = intent.Id,
                ClientSecret    = intent.ClientSecret
            };
            service.Confirm(
                intent.Id,
                confirmOptions
                );

            repository.Update(order);

            return(order.PaymentIntent);
        }