Esempio n. 1
0
        public bool SendOderConfirmEmail(OrderEmailModel order)
        {
            if (order == null)
            {
                return(false);
            }

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);

            client.EnableSsl             = true;
            client.UseDefaultCredentials = false;
            client.Credentials           = new NetworkCredential("*****@*****.**", "bangbangzz371");

            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("*****@*****.**", "ILovePaint - Order Confirmation");
            mail.To.Add("*****@*****.**");
            mail.Subject = $"ILovePaint #{order.Order.ID} - We have received your order!";

            mail.IsBodyHtml = true;

            EmailTemplateProcessor mailProcessor = new EmailTemplateProcessor(_env);
            string contents = mailProcessor.GenerateOrderConfirmMail(order);

            mail.Body = contents;

            client.Send(mail);

            return(true);
        }
        public string GenerateOrderConfirmMail(OrderEmailModel order)
        {
            var    path     = _env.WebRootPath + "\\Email_Templates\\order_confirm.html";
            string contents = "";


            using (StreamReader reader = File.OpenText(path))
            {
                contents = reader.ReadToEnd();
            }

            string orderItemTableBody = GenerateOrderItemTable(order.OrderItems);

            contents = string.Format(contents, order.Order.ID,
                                     String.Format("{0:dddd, d MMMM yyyy}", order.Order.OrderDate),
                                     order.Order.FullName,
                                     order.Order.PhoneNumber,
                                     order.Order.Email,
                                     order.Order.Address,
                                     orderItemTableBody);

            return(contents);
        }
        public async Task <IActionResult> PostOrder([FromBody] OrderModel orderData)
        {
            if (orderData == null || orderData.OrderItems == null || orderData.OrderItems.Count == 0 ||
                orderData.Order == null)
            {
                return(BadRequest("Invalid order information"));
            }

            float total = 0;


            var newOrder = _orderService.AddOrder(orderData);

            EmailHandler          emailHandler    = new EmailHandler(_env);
            List <OrderItemEmail> orderItemEmails = new List <OrderItemEmail>();

            foreach (var item in orderData.OrderItems)
            {
                total += item.Amount;
                var i = new OrderItemEmail
                {
                    ProductName = item.ProductName,
                    Quantity    = item.Quantity,
                    Amount      = item.Amount,
                    UnitPrice   = item.UnitPrice,
                    VolumeValue = item.VolumeValue,
                    ColorName   = item.ColorName,
                    ColorCode   = item.ColorCode
                };
                orderItemEmails.Add(i);
            }

            OrderEmailModel orderEmail = new OrderEmailModel
            {
                Order      = newOrder,
                OrderItems = orderItemEmails
            };

            emailHandler.SendOderConfirmEmail(orderEmail);

            // plus reward points for member after purchasing products

            var rewardPointSuccess = "";

            if (User.Claims != null && User.Claims.ToList().Count > 0)
            {
                var userId = User.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).FirstOrDefault().Value;
                var user   = await _userManager.FindByIdAsync(userId);

                if (user != null)
                {
                    var updatedUser = _userService.AddRewardPoints(user, (int)Math.Floor(total / 10000));
                    if (updatedUser != null)
                    {
                        rewardPointSuccess = "Success";
                    }
                    else
                    {
                        rewardPointSuccess = "Fail";
                    }
                }
            }

            return(Ok(new
            {
                message = "Order has been sent",
                confirm_email = "sent",
                order = newOrder,
                addRewardPoints = rewardPointSuccess
            }));
        }