예제 #1
0
        public async Task <IActionResult> Checkout([FromForm] CartModel cart)
        {
            try
            {
                if (User.Identity.IsAuthenticated)
                {
                    UserEntity ue = _usersRepo.GetUserById(cart.UserId);

                    if (!string.IsNullOrEmpty(ue.CreditCardNumber) && !string.IsNullOrEmpty(ue.CreditCardCompany))
                    {
                        PurchaseOrder purchaseOrder = _shoppingRepo.CreateClientPurchase(_mapper.Map <ShoppingCart>(GetCartFromSession()));

                        this.AddInformationMessage("Purchase order created");

                        HttpContext.Session.Remove(Utility.SessionExtensions.SessionCart);

                        if (ue.EmailConfirmed)
                        {
                            bool emailSent = await _emailSender.SendPurchaseCreatedEmailAsync(ue.Email,
                                                                                              ue.FirstName,
                                                                                              ue.LastName,
                                                                                              _mapper.Map <OrderModel>(purchaseOrder));

                            if (emailSent)
                            {
                                this.AddInformationMessage($"Email confirming purchase order sent to '{ue.Email}'");
                            }
                            else
                            {
                                this.AddErrorMessage($"Failed to send email confirming purchase order to '{ue.Email}'");
                            }
                        }
                        else
                        {
                            this.AddWarningMessage($"Your email '{ue.Email}' isn't confirmed, so no email will be sent about the purchase order.");
                        }

                        return(RedirectToAction("Details", "Purchase", new { id = purchaseOrder.Id }));
                    }
                    else
                    {
                        this.AddInformationMessage("You need to fill your billing information before checkout");
                        return(RedirectToAction("Billing", "Account"));
                    }
                }
                else
                {
                    return(RedirectToAction("Login", "Account", new { ReturnUrl = "/Cart" }));
                }
            }
            catch (Exception ex)
            {
                string errorGuid = Guid.NewGuid().ToString();
                this.AddErrorMessage($"An error ocurred while processing your request. Provide the Id: '{errorGuid}' for traceability");
                _logger.LogError(ex.Message, new { errorGuid, ex });
            }

            return(RedirectToAction("Index"));
        }