Exemplo n.º 1
0
        public override void Initialize(CommercePipelineEvents events, NameValueCollection parameters)
        {
            events.AdjustOrder += context =>
            {
                foreach (var lineItem in context.OrderData.LineItems)
                {
                    #region 1 - Check for promotion

                    if (context.CommerceEvents.OrderItemProcessed != null)
                    {
                        var e = new OrderItemProcessedEventArgs(context.Customer, lineItem, null);
                        context.CommerceEvents.OrderItemProcessed(e);

                        if (e.Cancel)
                        {
                            // do something important
                            throw new ApplicationException(e.Message);
                        }
                    }

                    #endregion

                    #region 2 - Get product

                    var product = context.StoreRepository.Products.FirstOrDefault(item => item.Sku == lineItem.Sku);
                    if (product == null)
                    {
                        throw new ApplicationException($"Sku {lineItem.Sku} not found in store inventory.");
                    }

                    #endregion

                    #region 3 - Get line item by sku

                    var inventoryOnHand = context.StoreRepository.ProductInventory.FirstOrDefault(
                        item => item.Sku == lineItem.Sku);
                    if (inventoryOnHand == null)
                    {
                        throw new ApplicationException(
                                  $"Error attempting to determine on-hand inventory quantity for product {lineItem.Sku}.");
                    }

                    #endregion

                    #region 4 - Check if line item in stock

                    if (inventoryOnHand.QuantityInStock < lineItem.Quantity)
                    {
                        throw new ApplicationException(
                                  $"Not enough quantity on-hand to satisfy product {lineItem.Sku} purchase of {lineItem.Quantity} units.");
                    }
                    inventoryOnHand.QuantityInStock -= lineItem.Quantity;

                    Console.WriteLine($"\tInventory for product {lineItem.Sku} reduced by {lineItem.Quantity} units.");
                    Console.WriteLine();

                    #endregion
                }
            };
        }
 private void OrderItemProcessed(OrderItemProcessedEventArgs e)
 {
     if (e.OrderLineItemData.Sku == 102)
     {
         e.OrderLineItemData.PurchasePrice -= 30.00;
     }
 }
Exemplo n.º 3
0
 private void OnOrderItemProcessed(OrderItemProcessedEventArgs args)
 {
     if (args.LineItem.Id == 102)
     {
         args.LineItem.UnitPrice -= 20;
     }
 }
Exemplo n.º 4
0
 private void OrderItemProcessed(OrderItemProcessedEventArgs e)
 {
     if (e.OrderLineItemData.Sku == 101)
     {
         Console.WriteLine("Sku 101 was purchassed on {0}", DateTime.Now);
     }
 }
 private void OnOrderItemProcessed(OrderItemProcessedEventArgs e)
 {
     //This line is for update status of discount or something new about products.
     //if(e.OrderLineItemData.Sku == 102)
     //{
     //    e.OrderLineItemData.PurchasePrice -= 30.00;
     //}
 }
Exemplo n.º 6
0
 public void OnOrderItemProcessed(OrderItemProcessedEventArgs e)
 {
     if (_MinerRepository == null)
     {
         _MinerRepository = new MinerRepository();
     }
     //Update Sku miner
     //if (e.OrderLineItemData.Sku == 101)
     //{
     //    Console.WriteLine("Sku 101 was purchased on {0} and the data was mined.", DateTime.Now.ToString());
     //}
 }
Exemplo n.º 7
0
        void OnOrderItemProcessed(OrderItemProcessedEventArgs e)
        {
            DateTime today = DateTime.Now;

            if (today >= _PromotionStartDate && today <= _PromotionEndDate)
            {
                if (e.OrderLineItemData.Sku == 102)
                {
                    // great news, special on the Asus Motherboard - $30 off from $479
                    e.OrderLineItemData.PurchasePrice -= 30.00;
                }
            }
        }
        public bool ProcessOrder(Order order)
        {
            var paymentStatus = false;

            if (_customerValidator.ValidateCustomer(order.Customer))
            {
                foreach (var lineItem in order.LineItems)
                {
                    // raise event to check for product promotions
                    if (_events.OrderItemProcessed != null)
                    {
                        var args = new OrderItemProcessedEventArgs {
                            LineItem = lineItem
                        };
                        _events.OrderItemProcessed(args);

                        if (args.Cancel)
                        {
                            return(paymentStatus);
                        }
                    }

                    // updating store inventory
                    _storeRepository.UpdateInventoryForProduct(lineItem);
                }

                // processing the order payment
                paymentStatus = _paymentProcessor.ProcessPayment(order.PaymentDetails);

                // log if order processing fails
                if (!paymentStatus)
                {
                    _logger.Log($"Order with Order_Id: {order.Id} could not be placed.");
                }

                // notifying the customer for order status
                _customerNotifier.NotifyCustomer(paymentStatus);
            }
            return(paymentStatus);
        }
        void OnAdjustInventory(CommerceContext context)
        {
            foreach (OrderLineItemData lineItem in context.OrderData.LineItems)
            {
                // process added order line item modules
                if (context.Events.OrderItemProcessed != null)
                {
                    OrderItemProcessedEventArgs args = new OrderItemProcessedEventArgs(context.Customer, lineItem);
                    context.Events.OrderItemProcessed(args);
                    if (args.Cancel)
                    {
                        throw new ApplicationException(args.MessageText);
                    }
                }

                Product product = context.StoreRepository.Products.Where(item => item.Sku == lineItem.Sku).FirstOrDefault();
                if (product == null)
                {
                    throw new ApplicationException(string.Format("Sku {0} not found in store inventory.", lineItem.Sku));
                }

                Inventory inventoryOnHand = context.StoreRepository.ProductInventory.Where(item => item.Sku == lineItem.Sku).FirstOrDefault();
                if (inventoryOnHand == null)
                {
                    throw new ApplicationException(string.Format("Error attempting to determine on-hand inventory quantity for product {0}.", lineItem.Sku));
                }

                if (inventoryOnHand.QuantityInStock < lineItem.Quantity)
                {
                    throw new ApplicationException(string.Format("Not enough quantity on-hand to satisfy product {0} purchase of {1} units.", lineItem.Sku, lineItem.Quantity));
                }

                inventoryOnHand.QuantityInStock -= lineItem.Quantity;
                Console.WriteLine("Inventory for product {0} reduced by {1} units.", lineItem.Sku, lineItem.Quantity);
            }
        }
        public void ProcessOrder(OrderData orderData)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    Customer customer = _StoreRepository.GetCustomerByEmail(orderData.CustomerEmail);
                    if (customer == null)
                    {
                        throw new ApplicationException(string.Format("No customer on file with email {0}.", orderData.CustomerEmail));
                    }

                    foreach (OrderLineItemData lineItem in orderData.LineItems)
                    {
                        if (_CommerceEvents.OrderItemProcessed != null)
                        {
                            OrderItemProcessedEventArgs args = new OrderItemProcessedEventArgs(customer, lineItem, _StoreRepository);
                            _CommerceEvents.OrderItemProcessed(args);
                            if (args.Cancel)
                            {
                                throw new ApplicationException(args.MessageText);
                            }
                        }

                        Product product = _StoreRepository.Products.Where(item => item.Sku == lineItem.Sku).FirstOrDefault();
                        if (product == null)
                        {
                            throw new ApplicationException(string.Format("Sku {0} not found in store inventory.", lineItem.Sku));
                        }

                        Inventory inventoryOnHand = _StoreRepository.ProductInventory.Where(item => item.Sku == lineItem.Sku).FirstOrDefault();
                        if (inventoryOnHand == null)
                        {
                            throw new ApplicationException(string.Format("Error attempting to determine on-hand inventory quantity for product {0}.", lineItem.Sku));
                        }

                        if (inventoryOnHand.QuantityInStock < lineItem.Quantity)
                        {
                            throw new ApplicationException(string.Format("Not enough quantity on-hand to satisfy product {0} purchase of {1} units.", lineItem.Sku, lineItem.Quantity));
                        }

                        inventoryOnHand.QuantityInStock -= lineItem.Quantity;
                        Console.WriteLine("Inventory for product {0}({2}) reduced by {1} units at {3}.", lineItem.Sku, lineItem.Quantity, product.Description, DateTime.Now.ToString());
                    }

                    // Update customer records with purchase
                    foreach (OrderLineItemData lineItem in orderData.LineItems)
                    {
                        for (int i = 0; i < lineItem.Quantity; i++)
                        {
                            customer.Purchases.Add(new PurchasedItem()
                            {
                                Sku = lineItem.Sku, PurchasePrice = lineItem.PurchasePrice, PurchasedOn = DateTime.Now
                            });
                        }
                        Console.WriteLine("Added {0} unit(s) or product {1} to customer's purchase history.", lineItem.Quantity, lineItem.Sku);
                    }

                    // Process customer credit card
                    double amount = 0;
                    foreach (OrderLineItemData lineItem in orderData.LineItems)
                    {
                        amount += (lineItem.PurchasePrice * lineItem.Quantity);
                    }

                    bool paymentSuccess = _PaymentProcessor.ProcessCreditCard(customer.Name, orderData.CreditCard, orderData.ExpirationDate, amount);
                    if (!paymentSuccess)
                    {
                        throw new ApplicationException(string.Format("Credit card {0} could not be processed.", orderData.CreditCard));
                    }

                    // Send invoice email
                    _Mailer.SendInvoiceEmail(orderData);

                    scope.Complete();
                }
            }
            catch (Exception)
            {
                _Mailer.SendRejectionEmail(orderData);
                throw;
            }
        }