Example #1
0
        public bool SendOrderMailToOrderTeam(OrederInfo orderDetails)
        {
            var         result = false;
            MailAddress to     = new MailAddress(ConfigurationManager.AppSettings["OrderTeamEmailId"]);
            MailMessage msg    = new MailMessage(_from, to);

            var messageBuilder = new StringBuilder();

            messageBuilder.AppendLine("New Order Detail");
            messageBuilder.AppendLine("User Details:");
            messageBuilder.AppendLine($"User Id : {orderDetails.User.UserId}, Name : {orderDetails.User.Name}");

            messageBuilder.AppendLine("Product Details:");
            messageBuilder.AppendLine($"Product Id : {orderDetails.ProductId}, Quantity : {orderDetails.Quantity}");

            messageBuilder.AppendLine($"Delivery Address: {orderDetails.User.DeliveryAddress}");

            msg.Body    = messageBuilder.ToString();
            msg.Subject = "New Order Details";
            try
            {
                _mailClient.Send(msg);
                result = true;
            }
            catch (Exception)
            {
            }
            return(result);
        }
        public void Send_Order_Mail_Is_Tried_Thrice_And_Payment_IS_Reverted_With_Error_Message()
        {
            var ErrorMessageKey      = "Order_Failed";
            var mailSendingAttempt   = 0;
            var orderDetails         = new OrederInfo();
            var expectedCreditCardNo = "Some Credit Card No";
            var unitProductPrice     = 100;

            orderDetails.CreditCardNo = expectedCreditCardNo;
            orderDetails.Quantity     = 5;
            decimal expectedAmountToRevert = 500;

            string  passedCardNoToRevert = string.Empty;
            decimal passedAmountToRevert = 0;

            _inventoryHelper.Setup(x => x.CheckInventory(It.IsAny <string>(), It.IsAny <int>())).Returns(true);
            _inventoryHelper.Setup(x => x.GetProcuctPrice(It.IsAny <string>())).Returns(unitProductPrice);

            _creditCardHelper.Setup(x => x.ValidateCreditCard(It.IsAny <string>())).Returns(true);
            _paymentServiceHelper.Setup(x => x.ChargePayment(It.IsAny <string>(), It.IsAny <decimal>())).Returns(true);
            _paymentServiceHelper.Setup(x => x.RevertPayment(It.IsAny <string>(), It.IsAny <decimal>())).Callback <string, decimal>((x, y) => { passedCardNoToRevert = x; passedAmountToRevert = y; });
            _mailsHelper.Setup(x => x.SendOrderMailToOrderTeam(It.IsAny <OrederInfo>())).Callback(() => mailSendingAttempt++).Returns(false);

            var result = _orderProcessor.ProcessOrder(orderDetails).Result;

            Assert.AreEqual(mailSendingAttempt, 3);
            Assert.AreEqual(expectedCreditCardNo, passedCardNoToRevert);
            Assert.AreEqual(expectedAmountToRevert, passedAmountToRevert);
            Assert.IsFalse(result.Result);
            Assert.IsTrue(result.ErrorMessage.ContainsKey(ErrorMessageKey));
            Assert.AreEqual(result.ErrorMessage[ErrorMessageKey], "Unable to Process Order, Please try again later");
        }
Example #3
0
        public Task <OrderProcessingResult> ProcessOrder(OrederInfo orderDetails)
        {
            var result = new OrderProcessingResult();
            //check availability
            var isAvailable = _helperFactory.GetInventoryHelper().CheckInventory(orderDetails.ProductId, orderDetails.Quantity);

            //validate credit card
            if (isAvailable)
            {
                var isCreditCardValid = _helperFactory.GetCreditCardHelper().ValidateCreditCard(orderDetails.CreditCardNo);
                if (isCreditCardValid)
                {
                    decimal unitPrice      = _helperFactory.GetInventoryHelper().GetProcuctPrice(orderDetails.ProductId);
                    decimal amountToCharge = unitPrice * orderDetails.Quantity;
                    //deduct payment
                    bool isPaymentSucceeded = _helperFactory.GetPaymentHelper().ChargePayment(orderDetails.CreditCardNo, amountToCharge);
                    if (isPaymentSucceeded)
                    {
                        //Send Email
                        var mailAttempt = 1;
                        var isMailSent  = false;
                        while (!isMailSent && mailAttempt++ <= 3)
                        {
                            isMailSent = _helperFactory.GetMailHelper().SendOrderMailToOrderTeam(orderDetails);
                        }
                        if (!isMailSent)
                        {
                            //ideally should be retried to revert payment.
                            _helperFactory.GetPaymentHelper().RevertPayment(orderDetails.CreditCardNo, amountToCharge);
                            result.ErrorMessage.Add("Order_Failed", "Unable to Process Order, Please try again later");
                        }
                        else
                        {
                            result.Result = true;
                        }
                    }
                    else
                    {
                        result.ErrorMessage.Add("Payment_Failed", "Payment Failed on credit card");
                    }
                }
                else
                {
                    result.ErrorMessage.Add("Credit_Card_Not_Valid", "Credit card is not valid");
                }
            }
            else
            {
                result.ErrorMessage.Add("Err_Not_Enough_Inventory", "Required Inventory Not Available");
            }
            return(Task.FromResult(result));
        }
        public void When_Mail_Is_Sent_ProcessOrder_Returns_True_And_No_Error_Message()
        {
            var orderDetails = new OrederInfo();

            _inventoryHelper.Setup(x => x.CheckInventory(It.IsAny <string>(), It.IsAny <int>())).Returns(true);

            _creditCardHelper.Setup(x => x.ValidateCreditCard(It.IsAny <string>())).Returns(true);
            _paymentServiceHelper.Setup(x => x.ChargePayment(It.IsAny <string>(), It.IsAny <decimal>())).Returns(true);
            _paymentServiceHelper.Setup(x => x.RevertPayment(It.IsAny <string>(), It.IsAny <decimal>()));
            _mailsHelper.Setup(x => x.SendOrderMailToOrderTeam(It.IsAny <OrederInfo>())).Returns(true);

            var result = _orderProcessor.ProcessOrder(orderDetails).Result;

            Assert.IsTrue(result.Result);
            Assert.AreEqual(0, result.ErrorMessage.Count);
        }