Exemple #1
0
        public async Task <IActionResult> CancelOrder(long id)
        {
            var order = _context.Orders.Where(x => x.Id == id).SingleOrDefault();
            var user  = await _userManager.GetUserAsync(User);

            OrderHelper.UpdateOrderStatus(id, OrderHelper.StatusId(MasterStrings.OrderCancelled, _context), _context);

            var customerMessage = MasterStrings.Header;

            customerMessage += "<body>";
            customerMessage += string.Format(MasterStrings.CustomerCancellation, user.Forename);
            customerMessage += MasterStrings.SpamMessage;
            customerMessage += string.Format(MasterStrings.CustomerOrderLink, id);
            customerMessage += "</body></html> ";

            var storeMessage = MasterStrings.Header;

            storeMessage += "<body>";
            storeMessage += string.Format(MasterStrings.StoreCancellation, MasterStrings.StoreName);
            storeMessage += MasterStrings.SpamMessage;
            storeMessage += string.Format(MasterStrings.StoreOrderLink, id);
            storeMessage += "</body></html> ";

            await _emailSender.SendEmailAsync(user.Email, "Order Confirmation: " + id, customerMessage);

            await _emailSender.SendEmailAsync(MasterStrings.AdminEmail, "Order Cancellation: " + id, storeMessage);

            return(ControllerHelper.RedirectToLocal(this, "/Orders/Index?successMessage=Your order has been cancelled."));
        }
Exemple #2
0
        private async Task <UpdateResult> UpdateOrder(long id, string statusType)
        {
            var order = _context.Orders.
                        Where(x => x.Id == id).
                        Include(x => x.OrderItems).
                        SingleOrDefault();

            var user = await _userManager.FindByIdAsync(order.UserId);

            // update order status
            OrderHelper.UpdateOrderStatus(id, OrderHelper.StatusId(statusType, _context), _context);

            var orderSummary = MasterStrings.OrderSummaryStart;

            foreach (var item in order.OrderItems)
            {
                item.Product  = OrderHelper.GetProduct(item, _context);
                orderSummary += "<tr><td>" + item.Product.Title + "</td></tr>";
            }

            orderSummary += MasterStrings.OrderSummaryFinish;


            // send confirmation email
            switch (statusType)
            {
            case MasterStrings.AwaitingStock:
                await _emailSender.SendEmailAsync(user.Email, "News about your order: " + id, AwaitingStockMessage(order.Id, user, orderSummary));

                break;

            case MasterStrings.OrderDispatched:
                await _emailSender.SendEmailAsync(user.Email, "Dispatch Confirmation: " + id, OrderDispatchedMessage(order.Id, user, orderSummary));

                break;

            case MasterStrings.CancellationCompleted:
                await _emailSender.SendEmailAsync(user.Email, "Order Cancellation: " + id, OrderCancelledMessage(order.Id, user, orderSummary));

                break;

            case MasterStrings.OrderReturned:
                await _emailSender.SendEmailAsync(user.Email, "Return: " + id, OrderReturnMessage(order.Id, user, orderSummary));

                break;

            default:
                break;
            }

            return(UpdateResult.Success);
        }
        private async Task <UpdateResult> PlaceOrder(long id)
        {
            var user = await _userManager.GetUserAsync(User);

            // update order status
            OrderHelper.UpdateOrderStatus(id, OrderHelper.StatusId(MasterStrings.OrderPlaced, _context), _context);

            var orderSummary = MasterStrings.OrderSummaryStart;

            var order = _context.Orders.
                        Where(x => x.Id == id).
                        Include(x => x.OrderItems).
                        SingleOrDefault();

            foreach (var item in order.OrderItems)
            {
                item.Product  = OrderHelper.GetProduct(item, _context);
                orderSummary += "<tr><td>" + item.Product.Title + "</td></tr>";
                item.Product.Stock--;
                _context.Update(item.Product);
            }

            _context.SaveChanges();

            orderSummary += MasterStrings.OrderSummaryFinish;

            // send confirmation email
            var customerMessage = MasterStrings.Header;

            customerMessage += "<body>";
            customerMessage += string.Format(MasterStrings.CustomerNewOrder, user.Forename);
            customerMessage += MasterStrings.SpamMessage;
            customerMessage += string.Format(MasterStrings.CustomerOrderLink, id);
            customerMessage += orderSummary;
            customerMessage += "</body></html> ";

            await _emailSender.SendEmailAsync(user.Email, "Order Confirmation: " + id, customerMessage);

            // send shop notification
            var storeMessage = MasterStrings.Header;

            storeMessage += "<body>";
            storeMessage += string.Format(MasterStrings.StoreNewOrder, MasterStrings.StoreName);
            storeMessage += string.Format(MasterStrings.StoreOrderLink, order.Id);
            storeMessage += orderSummary;
            storeMessage += "</body></html> ";

            await _emailSender.SendEmailAsync(MasterStrings.AdminEmail, "New Order: " + id, storeMessage);

            return(UpdateResult.Success);
        }
        public async Task <IActionResult> Contact(ContactViewModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Name) ||
                string.IsNullOrWhiteSpace(model.Email) ||
                string.IsNullOrWhiteSpace(model.Subject) ||
                string.IsNullOrWhiteSpace(model.Message)
                )
            {
                model.FailureMessage = "Please complete all fields.";

                return(View(model));
            }

            if (string.IsNullOrWhiteSpace(model.RecaptchaResponse))
            {
                model.FailureMessage = "Something went wrong, please try again.";

                return(View(model));
            }

            if (!RecaptchaHelper.IsReCaptchValid(model.RecaptchaResponse))
            {
                model.FailureMessage = "Something went wrong, please try again.";

                return(View(model));
            }

            var messageText = string.Format("Message from {0}{1}{2}", model.Email, System.Environment.NewLine, model.Message);
            await _emailSender.SendEmailAsync(MasterStrings.AdminEmail, model.Subject, messageText);

            var redirectUrl = string.Format("/Home/Contact?successMessage=Your message '{0}' has been sent.", model.Subject);

            return(ControllerHelper.RedirectToLocal(this, redirectUrl));
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                if (string.IsNullOrWhiteSpace(Input.RecaptchaResponse))
                {
                    Input.FailureMessage = "Something went wrong, please try again.";
                    Input.Titles         = CustomerTitle.GetTitles(_context.Titles.Where(c => c.Id > 0).OrderBy(x => x.Value).ToList());
                    return(Page());
                }

                if (!RecaptchaHelper.IsReCaptchValid(Input.RecaptchaResponse))
                {
                    Input.FailureMessage = "Something went wrong, please try again.";
                    Input.Titles         = CustomerTitle.GetTitles(_context.Titles.Where(c => c.Id > 0).OrderBy(x => x.Value).ToList());
                    return(Page());
                }

                var user = new ApplicationUser {
                    UserName = Input.Email,
                    Email    = Input.Email,
                    Forename = Input.Forename,
                    Surname  = Input.Surname,
                    Title    = Input.Title
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            Input.FailureMessage = "Something went wrong, please try again.";
            Input.Titles         = CustomerTitle.GetTitles(_context.Titles.Where(c => c.Id > 0).OrderBy(x => x.Value).ToList());
            return(Page());
        }