예제 #1
0
파일: Packets.cs 프로젝트: borntolead/a2hat
        public byte[] Execute(HatContext context)
        {
            if (context == null || context.User == null)
            {
                return(null);
            }

            HatUser user = context.User;
        }
예제 #2
0
        public IActionResult Count()
        {
            HatUser hatUser = null;

            if (User.Identity.IsAuthenticated)
            {
                hatUser = _userManager.FindByNameAsync(User.Identity.Name).Result;
            }
            Cart cart = CartService.GetCart(_context, Request, Response, hatUser);

            return(Json(cart.CartItems.Sum(x => x.Quantity)));
        }
예제 #3
0
        public IActionResult Index()
        {
            HatUser hatUser = null;

            if (User.Identity.IsAuthenticated)
            {
                hatUser = _userManager.FindByNameAsync(User.Identity.Name).Result;
            }
            Cart cart = CartService.GetCart(_context, Request, Response, hatUser);

            return(View(cart));
        }
예제 #4
0
        public void TestMethod()
        {
            HatUser u = new HatUser();

            u.Login = "******";

            IFactory <HatUser> factory = HatUserFactory.Instance();

            factory.Save(u);

            HatUser ut = factory.Load(new object())[0];

            Assert.AreEqual(u.Login, ut.Login);
        }
예제 #5
0
        public IActionResult Details(int id, int color, int size)
        {
            HatUser hatUser = null;

            if (User.Identity.IsAuthenticated)
            {
                hatUser = _userManager.FindByNameAsync(User.Identity.Name).Result;
            }
            Cart cart = CartService.GetCart(_context, this.Request, this.Response, hatUser);

            //If the user has previously added this item to the cart, try to find it:
            CartItem item = cart.CartItems
                            .FirstOrDefault(ci => ci.ProductID == id &&
                                            ci.ProductColor.ID == color &&
                                            ci.ProductSize.ID == size);

            //Otherwise, this is the first time this product has been added
            //Create a new line item and look up details from the Product table
            if (item == null)
            {
                Product product = _context.Products
                                  .Include(p => p.ProductSizes)
                                  .Include(p => p.ProductColors)
                                  .FirstOrDefault(p => p.ID == id);
                item = new CartItem
                {
                    ProductID    = product.ID,
                    ProductSize  = product.ProductSizes.FirstOrDefault(s => s.ID == size),
                    ProductColor = product.ProductColors.FirstOrDefault(c => c.ID == color),
                    Quantity     = 0
                };
                cart.CartItems.Add(item);
            }

            //Add 1 to the quantity.
            item.Quantity++;

            //This command Inserts/Updates/Deletes all of the information from SQL
            //Until you call Save, all of your changes are "queued"
            _context.SaveChanges();

            //Go to the cart page-- when you get there, you should be able to use
            //the cart cookie to fetch the cart.
            return(RedirectToAction("Index", "Cart"));
        }
예제 #6
0
 public void Init()
 {
     User = new HatUser("testUser");
 }
예제 #7
0
파일: Packets.cs 프로젝트: borntolead/a2hat
        public byte[] Execute(HatContext context)
        {
            // search login into database

            // if found, then return account data
            // else return fail or create new account.

            HatUserFactory factory = HatUserFactory.Instance();

            HatUser u = factory.LoadOne(Encoding.Default.GetBytes(Login));

            if (u != null)
            {
                var sha  = new SHA1CryptoServiceProvider();
                var hash = sha.ComputeHash(Encoding.Default.GetBytes(Password));

                if (u.Password != BitConverter.ToString(hash))
                {
                    return(NetworkHelper.ClientMessageBuild(ClientOperation.SendMessage,
                                                            ClientMessage.M_INVALID_LOGIN_PASSWORD));
                }
            }
            else
            {
                if (Hat.Configuration.IsRegistrationAllowed)
                {
                    u = new HatUser();

                    u.Login = Login;

                    var sha  = new SHA1CryptoServiceProvider();
                    var hash = sha.ComputeHash(Encoding.Default.GetBytes(Password));

                    u.Password = BitConverter.ToString(hash);

                    factory.Save(u);
                }
                else
                {
                    return(NetworkHelper.ClientMessageBuild(ClientOperation.SendMessage,
                                                            ClientMessage.M_INVALID_LOGIN_PASSWORD));
                }
            }

            context.User = u;

            var characters = u.GetCharacters();

            var chars = new byte[characters.Count * 8 + 9];

            using (var mem = new MemoryStream(chars))
            {
                var bw = new BinaryWriter(mem);

                bw.Write((byte)0xCE);
                bw.Write((characters.Count * 8) + 4);
                bw.Write(Consts.HatIdentifier);
                foreach (var user in characters)
                {
                    bw.Write(user.CharacterId);
                }

                return(chars);
            }
        }
예제 #8
0
        public async Task <IActionResult> Index(CheckoutViewModel model, string braintreeNonce)
        {
            ViewData["clientToken"] = await _braintreeGateway.ClientToken.GenerateAsync();

            if (string.IsNullOrEmpty(braintreeNonce))
            {
                this.ModelState.AddModelError("nonce", "We're unable to validate this credit card");
            }

            if (this.ModelState.IsValid)
            {
                HatUser hatUser = null;
                if (User.Identity.IsAuthenticated)
                {
                    hatUser = _userManager.FindByNameAsync(User.Identity.Name).Result;
                }
                Cart cart = CartService.GetCart(_context, Request, Response, hatUser);

                if (cart.CartItems.Count > 0)
                {
                    var orderId = Guid.NewGuid().ToString().Substring(0, 8);
                    Braintree.TransactionRequest transactionRequest = new Braintree.TransactionRequest();
                    transactionRequest.PaymentMethodNonce  = braintreeNonce;
                    transactionRequest.PurchaseOrderNumber = orderId;
                    transactionRequest.Amount          = cart.CartItems.Sum(x => x.Quantity * x.Product.Price);
                    transactionRequest.ShippingAddress = new Braintree.AddressRequest
                    {
                        StreetAddress   = model.ShippingStreet1,
                        ExtendedAddress = model.ShippingStreet2,
                        PostalCode      = model.ShippingPostalCode,
                        //CountryName = model.ShippingCountry,  //This thing is picky about casing
                        FirstName = model.ContactName.Split(' ').First(),
                        LastName  = model.ContactName.Contains(' ') ? string.Join(' ', model.ContactName.Split(' ').Skip(1)) : "",
                        Locality  = model.ShippingCity,
                        Region    = model.ShippingRegion
                    };
                    transactionRequest.Customer = new Braintree.CustomerRequest
                    {
                        Email = hatUser != null ? hatUser.Email : model.ContactEmail,
                    };
                    transactionRequest.LineItems = cart.CartItems.Select(x => new Braintree.TransactionLineItemRequest
                    {
                        Name         = x.Product.Name,
                        Description  = x.Product.Description,
                        ProductCode  = x.ProductID.ToString(),
                        Quantity     = x.Quantity,
                        UnitAmount   = x.Product.Price,
                        TotalAmount  = x.Product.Price * x.Quantity,
                        LineItemKind = Braintree.TransactionLineItemKind.DEBIT
                    }).ToArray();

                    Braintree.Result <Braintree.Transaction> transactionResult = _braintreeGateway.Transaction.Sale(transactionRequest);
                    if (transactionResult.IsSuccess())
                    {
                        //TODO: Get a lot more info here, validate credit card + address, save it to a database
                        Order order = new Order();
                        order.ID                 = orderId;
                        order.OrderDate          = DateTime.Now.ToString();
                        order.ContactEmail       = model.ContactEmail;
                        order.ContactName        = model.ContactName;
                        order.ContactPhoneNumber = model.ContactPhoneNumber;
                        order.ShippingCity       = model.ShippingCity;
                        order.ShippingCountry    = model.ShippingCountry;
                        order.ShippingPostalCode = model.ShippingPostalCode;
                        order.ShippingRegion     = model.ShippingRegion;
                        order.ShippingStreet1    = model.ShippingStreet1;
                        order.ShippingStreet2    = model.ShippingStreet2;


                        order.OrderItems = cart.CartItems.Select(ci => new OrderItem
                        {
                            ProductID   = ci.ProductID,
                            Color       = ci.ProductColor != null ? ci.ProductColor.Color : null,
                            Description = ci.Product.Description,
                            Name        = ci.Product.Name,
                            Price       = ci.Product.Price,
                            Quantity    = ci.Quantity,
                            Size        = ci.ProductSize != null ? ci.ProductSize.Size : null
                        }).ToArray();

                        _context.CartItems.RemoveRange(cart.CartItems);
                        _context.Carts.Remove(cart);
                        Response.Cookies.Delete("HatShopCartInfo");
                        _context.Orders.Add(order);
                        if (hatUser != null)
                        {
                            order.HatUser = hatUser;
                        }

                        _context.SaveChanges();
                        await _emailSender.SendEmailAsync(model.ContactEmail, "Receipt for order #" + order.ID, "Thanks for your order!");

                        return(RedirectToAction("index", "receipt", new { id = order.ID }));
                    }
                }
                ModelState.AddModelError("cart", "There was a problem processing your cart");
            }
            return(View(model));
        }