Пример #1
0
        public async Task <OperationResult> InvoiceWebhookPayPal(string orderId, string token)
        {
            Order order = await _orderRepository.GetAsync(orderId);

            Invoice invoice = await _invoiceRepository.GetInvoiceByOrderAsync(order);

            await _paymentProviderFactory.CreatePaymentProvider(PaymentProvider.PayPal).ConfirmOrder(invoice);

            Delivery delivery = await _deliveryRepository.GetDeliveryAsync(order);

            order.Status  = OrderStatus.Payed;
            order.PayDate = DateTime.Now;

            await _orderRepository.UpdateAsync(order);

            delivery.Status = DeliveryStatus.Pending;

            await _deliveryRepository.UpdateAsync(delivery);

            if (order.Type == OrderType.ProductSale)
            {
                Sale sale = await _saleRepository.GetSaleAsync(order);

                sale.Status = SaleStatus.Payed;
                await _saleRepository.UpdateAsync(sale);

                SaleDetail saleDetail = await _saleDetailRepository.GetBySaleAsync(sale);

                IEnumerable <Product> products = _productRepository.GetAvailableProducts(sale.Products.Distinct());
                foreach (Product product in products)
                {
                    product.Stock -= saleDetail.Products.Where(x => x.ProductId == product.Id).Sum(x => x.Quantity);

                    if (product.Stock <= 0)
                    {
                        product.IsActive = false;
                    }

                    await _productRepository.UpdateAsync(product);
                }
            }


            return(OperationResult.Success());
        }
Пример #2
0
        public async Task <OperationResult <SaleOrderDto> > CreateSaleAsync(CreateSaleInput input)
        {
            var validationResult = await _saleValidator.CreateSaleAsync(input);

            if (validationResult.IsSuccess)
            {
                IEnumerable <Product> products = _productRepository.GetAvailableProducts(input.Products.Select(x => x.ProductId));

                Customer customer = await _customerRepository.GetCustomer(input.CustomerEmail);

                AddressCustomer addressCustomer = await _addressRepository.GetByCustomerAsync(input.CustomerEmail);

                Address address = addressCustomer.Addresses.Single(x => x.Id == input.AddressId);

                CouponCode couponCode = null;

                if (!string.IsNullOrWhiteSpace(input.CouponCode))
                {
                    couponCode = await _couponCodeRepository.GetCouponAsync(input.CouponCode);
                }

                Order order = new()
                {
                    Status        = OrderStatus.Created,
                    CreationDate  = DateTime.Now,
                    CustomerEmail = input.CustomerEmail,
                    Type          = OrderType.ProductSale,
                    TotalAmount   = CalculateTotalAmount(couponCode, products, customer)
                };

                order = await _orderRepository.CreateAsync(order);

                Sale sale = new()
                {
                    CouponCode = input.CouponCode,
                    OrderId    = order.Id,
                    Products   = products.Select(x => x.Id),
                    Status     = SaleStatus.PendingPayment
                };

                sale = await _saleRepository.CreateAsync(sale);

                SaleDetail saleDetail = new()
                {
                    SaleId   = sale.Id,
                    Products = input.Products
                };

                saleDetail = await _saleDetailRepository.CreateAsync(saleDetail);

                IPaymentProvider paymentProvider = _paymentProviderFactory.CreatePaymentProvider(input.PaymentProvider);

                Invoice invoice = await paymentProvider.CreateInvoice(order);

                invoice = await _invoiceRepository.CreateAsync(invoice);

                Delivery delivery = new()
                {
                    AddressId = input.AddressId,
                    OrderId   = order.Id,
                    Status    = DeliveryStatus.Created
                };

                delivery = await _deliveryRepository.CreateAsync(delivery);

                return(OperationResult <SaleOrderDto> .Success(new(sale.ConvertToDto(), order.ConvertToDto(), invoice.ConvertToDto())));
            }

            return(OperationResult <SaleOrderDto> .Fail(validationResult));
        }

        private static decimal CalculateTotalAmount(CouponCode couponCode, IEnumerable <Product> products, Customer customer)
        {
            decimal totalAmount = products.Sum(x => x.Price);

            if (couponCode is null)
            {
                return(totalAmount);
            }

            if (totalAmount < couponCode.MinAmount || totalAmount > couponCode.MaxAmount)
            {
                return(totalAmount);
            }

            if (DateTime.Today < couponCode.DateStart || DateTime.Today > couponCode.DateExpire)
            {
                return(totalAmount);
            }

            List <Product> productsToOff = new();


            if (couponCode.Customers?.Contains(customer.Email) ?? false)
            {
                productsToOff.AddRange(products);
            }
            else
            {
                if (couponCode.Categories?.Any() ?? false)
                {
                    productsToOff.AddRange(products.Where(x => couponCode.Categories.Contains(x.Category)));
                }

                if (couponCode.SubCategories?.Any() ?? false)
                {
                    productsToOff.AddRange(products.Where(x => couponCode.SubCategories.Any(y => y.Category == x.Category && y.SubCategory == x.SubCategory)));
                }

                if (couponCode.Products?.Any() ?? false)
                {
                    productsToOff.AddRange(products.Where(x => couponCode.Products.Contains(x.Id)));
                }
            }

            decimal valueOff = couponCode.Type switch
            {
                CouponCodeOffType.Percentage => (couponCode.Value / productsToOff.Sum(x => x.Price)) * 100,
                CouponCodeOffType.SpecificAmount => productsToOff.Sum(x => x.Price) - couponCode.Value,
                _ => throw new ArgumentException("CouponCodeOffType invalido.", nameof(couponCode))
            };


            return(totalAmount - valueOff);
        }
    }
}