Пример #1
0
        public async Task <ActionResult> Create([Bind(Include = "BookID,Title,Year,Price,Quantity,CoverImage,Description,DateCreated,rowguid,NewColumn")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Books.Add(book);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(book));
        }
        public async Task <ActionResult> Index(Models.CheckoutDetails model, string addressId)
        {
            Guid cartID = Guid.Parse(Request.Cookies["CartID"].Value);

            model.CurrentCart = db.Carts.Find(cartID);
            model.Addresses   = new Braintree.Address[0];

            if (ModelState.IsValid)
            {
                string  trackingNumber = Guid.NewGuid().ToString().Substring(0, 8);
                decimal tax            = (model.CurrentCart.Cart_Books.Sum(x => x.Book.Price * x.Quantity) ?? 0) * .1025m;
                decimal subtotal       = model.CurrentCart.Cart_Books.Sum(x => x.Book.Price * x.Quantity) ?? 0;
                decimal shipping       = model.CurrentCart.Cart_Books.Sum(x => x.Quantity);
                decimal total          = subtotal + tax + shipping;

                #region pay for order
                PAAPaymentService payments = new PAAPaymentService();
                string            email    = User.Identity.IsAuthenticated ? User.Identity.Name : model.ContactEmail;
                string            message  = await payments.AuthorizeCard(email, total, tax, trackingNumber, addressId, model.CardholderName, model.CVV, model.CreditCardNumber, model.ExpirationMonth, model.ExpirationYear);

                #endregion

                #region save order
                if (string.IsNullOrEmpty(message))
                {
                    Order o = new Order
                    {
                        DateCreated         = DateTime.UtcNow,
                        DateLastModified    = DateTime.UtcNow,
                        TrackingNumber      = trackingNumber,
                        ShippingAndHandling = shipping,
                        Tax                = tax,
                        SubTotal           = subtotal,
                        Email              = model.ContactEmail,
                        PurchaserName      = model.ContactName,
                        ShippingAddress1   = model.ShippingAddress,
                        ShippingCity       = model.ShippingCity,
                        ShippingPostalCode = model.ShippingPostalCode,
                        ShippingState      = model.ShippingState
                    };
                    db.Orders.Add(o);

                    await db.SaveChangesAsync();

                    #endregion

                    #region send email

                    PAAEmailService emailService = new PAAEmailService();
                    await emailService.SendAsync(new Microsoft.AspNet.Identity.IdentityMessage
                    {
                        Subject     = "Your receipt for order " + trackingNumber,
                        Destination = model.ContactEmail,
                        Body        = "Thank you for shopping"
                    });

                    #endregion

                    #region Reset Cart
                    Response.SetCookie(new HttpCookie("cartID")
                    {
                        Expires = DateTime.UtcNow
                    });

                    db.Cart_Books.RemoveRange(model.CurrentCart.Cart_Books);
                    db.Carts.Remove(model.CurrentCart);
                    db.SaveChanges();

                    #endregion
                    return(RedirectToAction("Index", "Receipt", new { id = trackingNumber }));
                }
                ModelState.AddModelError("CreditCardNumber", message);
            }
            return(View(model));
        }