public void UpdateActivationCode(ActivationCode activationCode, string deviceId, string deviceModel,
                                         string OsType, string OsVersion)
        {
            activationCode.IsUsed           = true;
            activationCode.UsingDate        = DateTime.Now;
            activationCode.IsActive         = false;
            activationCode.LastModifiedDate = DateTime.Now;

            db.Entry(activationCode).State = EntityState.Modified;
            db.SaveChanges();
        }
        public ActivationCode IsValidActivationCode(Guid userId, string activationCode)
        {
            int code = Convert.ToInt32(activationCode);

            ActivationCode oActivationCode = db.ActivationCodes.Where(current => current.UserId == userId && current.Code == code && current.IsUsed == false &&
                                                                      current.IsActive == true && current.ExpireDate > DateTime.Now).FirstOrDefault();

            if (oActivationCode != null)
            {
                return(oActivationCode);
            }
            else
            {
                return(null);
            }
        }
        public int CreateActivationCode(Guid userId)
        {
            DeactiveOtherActivationCode(userId);

            int            code           = RandomCode();
            ActivationCode activationCode = new ActivationCode();

            activationCode.UserId       = userId;
            activationCode.Code         = code;
            activationCode.ExpireDate   = DateTime.Now.AddDays(2);
            activationCode.IsUsed       = false;
            activationCode.IsActive     = true;
            activationCode.CreationDate = DateTime.Now;
            activationCode.IsDeleted    = false;

            db.ActivationCodes.Add(activationCode);
            return(code);
        }
        public ActionResult Finalize(string notes, string email, string cellNumber, string activationCode, string city, string address, string postal)
        {
            try
            {
                cellNumber = cellNumber.Replace("۰", "0").Replace("۱", "1").Replace("۲", "2").Replace("۳", "3").Replace("۴", "4").Replace("۵", "5").Replace("۶", "6").Replace("v", "7").Replace("۸", "8").Replace("۹", "9");

                activationCode = activationCode.Replace("۰", "0").Replace("۱", "1").Replace("۲", "2").Replace("۳", "3").Replace("۴", "4").Replace("۵", "5").Replace("۶", "6").Replace("v", "7").Replace("۸", "8").Replace("۹", "9");

                User user = db.Users.Where(current => current.CellNum == cellNumber).FirstOrDefault();

                if (user != null)
                {
                    ActivationCode activation = IsValidActivationCode(user.Id, activationCode);

                    if (activation != null)
                    {
                        ActivateUser(user, activationCode);
                        UpdateActivationCode(activation, null, null, null, null);
                        db.SaveChanges();

                        List <ProductInCart> productInCarts = GetProductInBasketByCoockie();

                        Order order = ConvertCoockieToOrder(productInCarts, user.Id, notes, email, city, address, postal);

                        RemoveCookie();

                        string res = "";

                        if (order.TotalAmount == 0)
                        {
                            res = "freecallback?orderid=" + order.Id;
                        }

                        else
                        {
                            res = zp.ZarinPalRedirect(order, order.TotalAmount);
                        }

                        return(Json(res, JsonRequestBehavior.AllowGet));
                    }

                    if (user.IsActive && user.Password == activationCode)
                    {
                        List <ProductInCart> productInCarts = GetProductInBasketByCoockie();

                        Order order = ConvertCoockieToOrder(productInCarts, user.Id, notes, email, city, address, postal);

                        RemoveCookie();

                        string res = "";

                        if (order.TotalAmount == 0)
                        {
                            res = "freecallback?orderid=" + order.Id;
                        }

                        else
                        {
                            res = zp.ZarinPalRedirect(order, order.TotalAmount);
                        }

                        return(Json(res, JsonRequestBehavior.AllowGet));
                    }
                }
                return(Json("invalid", JsonRequestBehavior.AllowGet));
            }

            catch (Exception e)
            {
                return(Json("false", JsonRequestBehavior.AllowGet));
            }
        }