public void BeforeEach()
        {
            _productRepo = Substitute.For<IProductRepository>();
            _orderFulfillmentService = Substitute.For<IOrderFulfillmentService>();
            _customerRepository = Substitute.For<ICustomerRepository>();
            _taxRateService = Substitute.For<ITaxRateService>();
            _emailService = Substitute.For<IEmailService>();

            _subject = new OrderService(_orderFulfillmentService,
                _customerRepository,
                _taxRateService,
                _emailService);

            _bestCustomer = new Customer
            {
                CustomerId = 42,
                PostalCode = "12345",
                Country = "Merica"
            };

            _listOfTaxEntries = new List<TaxEntry>
            {
                new TaxEntry {Description = "High Tax", Rate = (decimal) 0.60},
                new TaxEntry {Description = "Low Tax", Rate = (decimal) 0.10}
            };

            _orderConfirmation = new OrderConfirmation
            {
                OrderId = 1234,
                OrderNumber = "hello"
            };
            _customerRepository.Get(_bestCustomer.CustomerId.Value).Returns(_bestCustomer);
            _taxRateService.GetTaxEntries(_bestCustomer.PostalCode, _bestCustomer.Country).Returns(_listOfTaxEntries);
        }
Example #2
0
        public OrderSummary PlaceOrder(Order order)
        {
            if (order.OrderItemsAreUnique() == false)
            {
                throw new OrderItemsAreNotUniqueException();
            }

            if (ProductsAreInStock(order, _productRepository) == false)
            {
                throw new OrderItemsAreNotInStockException();
            }

            OrderConfirmation      orderConfirmation = _orderFulfillmentService.Fulfill(order);
            IEnumerable <TaxEntry> taxEntries        = _taxRateService.GetTaxEntries(_postalCode, _country);

            decimal netTotal   = order.GetOrderTotal();
            decimal orderTotal = netTotal + GetTotalTax(taxEntries);

            OrderSummary orderSummary = new OrderSummary
            {
                OrderId     = orderConfirmation.OrderId,
                OrderNumber = orderConfirmation.OrderNumber,
                CustomerId  = orderConfirmation.CustomerId,
                OrderItems  = order.OrderItems,
                NetTotal    = netTotal,
                Taxes       = taxEntries,
                Total       = orderTotal
            };

            _emailService.SendOrderConfirmationEmail(orderConfirmation.CustomerId, orderConfirmation.OrderId);

            return(orderSummary);
        }
        public OrderSummary PlaceOrder(Order order)
        {
            // Check duplicate SKU
            if (order.HasNoDuplicateSku() == false)
            {
                throw new ArgumentException(DuplicateSkuError);
            }

            // Get customer information
            int customerId = order.CustomerId ?? -1;

            if (customerId == -1)
            {
                throw new ArgumentException(NoCustomerIdError);
            }
            Customer customer = _customerService.Get(customerId);

            // Get tax rate
            IEnumerable <TaxEntry> tax = _taxService.GetTaxEntries(customer.PostalCode, customer.Country);
            decimal totalTax           = 0;

            foreach (TaxEntry entry in tax)
            {
                totalTax += entry.Rate;
            }

            // Check product stock and calculate totals
            decimal netTotal = 0;
            decimal total    = 0;

            foreach (OrderItem items in order.OrderItems)
            {
                if (_productRepo.IsInStock(items.Product.Sku) == false)
                {
                    throw new ArgumentException(NoStockError);
                }

                netTotal += items.Product.Price * items.Quantity;
            }
            total = totalTax / 100 * netTotal;

            // All checks pass, get confirmation properties
            OrderConfirmation confirmation = _fulfillmentService.Fulfill(order);

            // Create new summary with required information filled in
            OrderSummary summary = new OrderSummary
            {
                OrderId     = confirmation.OrderId,
                OrderNumber = confirmation.OrderNumber,
                NetTotal    = netTotal,
                Total       = total
            };

            _emailService.SendOrderConfirmationEmail(customerId, summary.OrderId);

            return(summary);
        }
Example #4
0
 private static void SendConfirmationEmail(IEmailService emailService, OrderConfirmation orderConfirmation)
 {
     emailService.SendOrderConfirmationEmail(orderConfirmation.CustomerId, orderConfirmation.OrderId);
 }