Exemplo n.º 1
0
        /// <summary>
        /// Deletes a flummery from the cart
        /// </summary>
        /// <param name="productId">
        /// int: the product ID of the item to be deleted from the cart
        /// </param>
        /// <returns>
        /// Task<IActionResult>: a View directing back to the cart
        /// </returns>
        public async Task <IActionResult> OnPost(int productId)
        {
            var currentUser = await _signInManager.UserManager.GetUserAsync(User);

            var cart = await _cart.GetUserCart(currentUser.Id);

            if (cart.CartItems.Count <= 1)
            {
                await _cart.Delete(currentUser.Id);
            }
            await _cartItem.Delete(cart.Id, productId);

            return(RedirectToPage("/Cart/View"));
        }
        /// <summary>
        /// Send a checkout request with the provided information to AuthorizeNet
        /// </summary>
        /// <returns>Redirect to Checkout Success page if transation successful, show ModelState errors on form if not</returns>
        public async Task <IActionResult> OnPost()
        {
            #region AddressBuilding
            var currentUser = await _userManager.GetUserAsync(User);

            customerAddressType billingAddress = new customerAddressType
            {
                address   = Input.BillingAddress,
                firstName = Input.FirstName,
                lastName  = Input.LastName,
                email     = currentUser.Email,
                state     = Input.BillingState,
                zip       = Input.BillingZip,
                city      = Input.BillingCity,
            };
            customerAddressType shippingAddress = new customerAddressType();
            if (Input.SameBillingAndShipping)
            {
                shippingAddress = billingAddress;
            }
            else
            {
                shippingAddress.address   = Input.ShippingAddress;
                shippingAddress.firstName = Input.FirstName;
                shippingAddress.lastName  = Input.LastName;
                shippingAddress.email     = currentUser.Email;
                shippingAddress.state     = Input.ShippingState;
                shippingAddress.zip       = Input.ShippingZip;
                shippingAddress.city      = Input.ShippingCity;
            }
            #endregion
            if (ModelState.IsValid)
            {
                var cart = await _cart.GetUserCart(currentUser.Id);

                if (cart != null && cart.CartItems != null)
                {
                    creditCardType card = new creditCardType()
                    {
                        cardNumber     = Input.CardNumber,
                        expirationDate = "1220",
                        cardCode       = "555"
                    };

                    TransactionResponse result = _payment.Run(card, billingAddress, cart.CartItems);
                    if (result.Successful)
                    {
                        decimal         total     = 0;
                        List <CartItem> cartItems = new List <CartItem>();
                        foreach (var item in cart.CartItems)
                        {
                            cartItems.Add(item);
                            total += item.Qty * item.Product.Price;
                        }

                        OrderCart order = new OrderCart()
                        {
                            CartId          = cart.Id,
                            UserId          = currentUser.Id,
                            FirstName       = billingAddress.firstName,
                            LastName        = billingAddress.lastName,
                            Date            = DateTime.Now.ToString(),
                            BillingAddress  = billingAddress.address,
                            BillingCity     = billingAddress.city,
                            BillingState    = billingAddress.state,
                            BillingZip      = billingAddress.zip,
                            ShippingAddress = shippingAddress.address,
                            ShippingCity    = shippingAddress.city,
                            ShippingState   = shippingAddress.state,
                            ShippingZip     = shippingAddress.zip
                        };
                        await _order.Create(order);

                        order.CartItems = new List <OrderCartItem>();
                        foreach (var item in cartItems)
                        {
                            OrderCartItem orderItem = new OrderCartItem()
                            {
                                OrderCartId = cart.Id,
                                ProductId   = item.ProductId,
                                Qty         = item.Qty
                            };
                            order.CartItems.Add(orderItem);
                            await _orderItem.Create(orderItem);

                            await _cartItem.Delete(item.CartId, item.ProductId);
                        }

                        await _cart.Delete(currentUser.Id);

                        await BuildCheckoutEmail(currentUser.Email, $"{order.FirstName} {order.LastName}", cartItems, $"{shippingAddress.address} {shippingAddress.city}, {shippingAddress.state} {shippingAddress.zip}", total);

                        if (Input.SaveBillingAddress)
                        {
                            currentUser.Address = Input.BillingAddress;
                            currentUser.City    = Input.BillingCity;
                            currentUser.State   = Input.BillingState;
                            currentUser.Zip     = Input.BillingZip;
                            if (Input.BillingOptionalAddition != null || Input.BillingOptionalAddition != "")
                            {
                                currentUser.OptionalAddress = Input.BillingOptionalAddition;
                            }
                            await _userManager.UpdateAsync(currentUser);
                        }

                        return(RedirectToPage("/Checkout/Success", new { response = result.Response, cartId = cart.Id }));
                    }
                    else
                    {
                        ModelState.AddModelError("", result.Response);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Something went wrong! We couldn't find anything in your cart.");
                }
            }
            else
            {
                ModelState.AddModelError("", "Invalid input. Please try again.");
            }
            return(Page());
        }