예제 #1
0
        public async Task <IActionResult> RemoveSavedAddress(ManageDeliveryAddressesViewModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            DeliveryAddress addressOnDeathrow = await _addressManager.FindAddressByZipcodeAsync(model.RemovalZipcode);

            int resultCode = await _addressManager.RemoveDeliveryAddressAsync(user, addressOnDeathrow);

            Debug.WriteLine("Code is: " + addressOnDeathrow.Zipcode);
            StatusMessage = "Delivery address removed";

            return(RedirectToAction(nameof(ManageDeliveryAddresses)));
        }
예제 #2
0
        public async Task <IActionResult> MakePurchase(OrderViewModel model, string totalCost)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            Debug.WriteLine("ZipConf is: " + model.ZipConfirmation);
            //This crap definitely needs to be reworked
            string[] costSplit = totalCost.Split(Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator));
            int      costOut   = 0;

            Int32.TryParse(costSplit[0] + costSplit[1], out costOut);

            var user = await _userManager.GetUserAsync(HttpContext.User);

            string jsonPost =
                "{\"amount\":" + costOut +
                ",\"number\": \"" + model.CardNumber +
                "\",\"holder\": \"" + model.FirstName + " " + model.LastName +
                "\",\"exp_year\": " + model.Exp_Year +
                ",\"exp_month\": " + model.Exp_Month +
                ",\"cvv\": \"" + model.CVV + "\"}";

            //Touching anything 'Http' beyond this point is likely to result in some browsers flipping their shit out
            HttpClientHandler handler = new HttpClientHandler()
            {
                PreAuthenticate       = true,
                UseDefaultCredentials = false,
            };

            HttpClient client = new HttpClient(handler);

            client.DefaultRequestHeaders.Authorization
                = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{"technologines"}:{"platformos"}")));

            //Create POST content from data
            StringContent jContent = new StringContent(jsonPost, Encoding.UTF8, "application/json");

            jContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "https://mock-payment-processor.appspot.com/v1/payment/")
            {
                Content = jContent,
            };

            DeliveryAddress confirmAddress = await _addressManager.FindAddressByZipcodeAsync(model.ZipConfirmation);

            if (confirmAddress != null) //If address exists
            {
                //Send payment request
                HttpResponseMessage response = await client.SendAsync(req);

                if ((int)response.StatusCode == 201) //Success
                {
                    ShoppingCart shoppingCart = null;

                    shoppingCart = await _shoppingCartService.FindShoppingCartByIdAsync((int)user.ShoppingCartId);

                    user.ShoppingCartId = null;
                    await _userManager.UpdateAsync(user);

                    Order newOrder = new Order();

                    newOrder.ShoppingCartId = shoppingCart.Id;
                    newOrder.User           = user;
                    newOrder.TotalPrice     = Convert.ToDecimal(totalCost);
                    newOrder.Address        = confirmAddress.Country + ", " + confirmAddress.County + " county, " +
                                              confirmAddress.City + " - " + confirmAddress.Address + ", " + confirmAddress.Zipcode;
                    newOrder.CardNumber   = model.CardNumber;
                    newOrder.PurchaseDate = DateTime.Now;
                    newOrder.StatusCode   = 1; //1 - Purchased 2 - Confirmed etc.

                    int addOrderResult = await _orderService.CreateOrderAsync(newOrder);

                    //Todo redirect to some other page or show something
                }
            }

            //Todo something on checkout page if transaction fails etc.
            return(RedirectToAction("Index", "Order"));
        }