예제 #1
0
        public JsonResult Payment(long id)
        {
            var         message     = "";
            UserSession userSession = SessionHelper.GetSession(AppSettingConstant.LoginSessionCustomer) as UserSession;

            if (userSession != null)
            {
                var user = _userRepository.Find(u => u.Status.Equals(Status.Active) && u.Username.Equals(userSession.Username));
                if (user != null)
                {
                    var orderSession = SessionHelper.GetSession(AppSettingConstant.CheckOutSession) as Order;
                    if (orderSession != null)
                    {
                        var card = user.Customer.CreditCards.FirstOrDefault(c => c.CreditCardId == id & c.Status == Status.Active);
                        if (card != null)
                        {
                            var bankCredit = _bankService.CheckCard(AESEncrytDecry.DecryptStringAES(card.CreditNumber), card.Expire, card.CVC);
                            if (bankCredit != null)
                            {
                                var    cart      = SessionHelper.GetSession(AppSettingConstant.CartSession) as List <CartItem>;
                                var    amount    = cart.Sum(c => c.Quantity * (c.Product.ProductPrice + c.Material.Price));
                                string rootPath  = "~/Images/Upload/";
                                var    extenPath = string.Format("{0}/{1}_{2}", user.Username, DateTime.Now.Second, DateTime.Now.Millisecond);
                                string path      = Server.MapPath(rootPath + extenPath); //Path

                                //Check if directory exist
                                if (!System.IO.Directory.Exists(path))
                                {
                                    System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist
                                }
                                foreach (var item in cart as List <CartItem> )
                                {
                                    string imgPath    = Path.Combine(path, item.ImageTitle);
                                    byte[] imageBytes = Convert.FromBase64String(item.Image);
                                    System.IO.File.WriteAllBytes(imgPath, imageBytes);
                                }
                                if (bankCredit.Balance >= amount)
                                {
                                    bankCredit.Balance -= amount.Value;
                                    List <OrderDetail> orderDetails = new List <OrderDetail>();
                                    foreach (var item in cart)
                                    {
                                        OrderDetail orderDetail = new OrderDetail()
                                        {
                                            MaterialId = item.Material.Id,
                                            ProductId  = item.Product.ProductId,
                                            Quantity   = item.Quantity,
                                            Option     = item.Option,
                                            Image      = item.ImageTitle
                                        };
                                        orderDetails.Add(orderDetail);
                                    }
                                    orderSession.CreditCardId = card.CreditCardId;
                                    orderSession.CreatedAt    = DateTime.Now;
                                    orderSession.Status       = OrderStatus.Pending;
                                    orderSession.CustomerId   = user.CustomerId;
                                    orderSession.FolderImage  = extenPath;
                                    orderSession.IsCancel     = true;
                                    var transac = _orderServiceTrans.TransactionPayment(orderSession, orderDetails, bankCredit);
                                    if (transac != null)
                                    {
                                        var       addressDetails = _addressRepository.Find(a => a.AddressId == orderSession.AddressId);
                                        MailOrder model          = new MailOrder(cart, transac.OrderId, user.Email, addressDetails.AddressDetails, user.Customer.CustomerName, card.CreditNumber, transac.FolderImage, amount.Value, transac.PhoneNumber);
                                        var       body           = ViewToString.RenderRazorViewToString(this, "MailOrder", model);
                                        var       bodyAdmin      = ViewToString.RenderRazorViewToString(this, "MailBackAdmin", model);
                                        string    mailAdmin      = ConfigurationManager.AppSettings["mailadmin"];
                                        Task.Factory.StartNew((() =>
                                        {
                                            SendEmail.Send(mailAdmin, bodyAdmin, "New order notification!");
                                            SendEmail.Send(user.Email, body, "Your order information!");
                                        }));
                                        SessionHelper.Delete(AppSettingConstant.CartSession);
                                        TempData["Success"] = "Order Success";
                                        return(Json(new { status = true, message, transac.OrderId }, JsonRequestBehavior.AllowGet));
                                    }
                                    else
                                    {
                                        if (System.IO.File.Exists(path))
                                        {
                                            System.IO.File.Delete(path);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(Json(new { status = false, message }, JsonRequestBehavior.AllowGet));
        }