protected void shippingDetails_Click(object sender, EventArgs e)
        {
            var cart  = new ShoppingCartActions();
            var items = cart.GetCartItems();

            Order order = new Order {
                FirstName = FirstName.Text, OrderDate = DateTime.Now, LastName = LastName.Text, Address = Address.Text, City = City.Text, Country = Country.Text, PostalCode = PostalCode.Text, State = State.Text, Email = Email.Text, HasBeenShipped = true, Phone = Phone.Text, Total = cart.GetTotal()
            };

            db.Orders.Add(order);
            db.SaveChanges();
            order.OrderDetails = new List <OrderDetail>();
            foreach (var item in items)
            {
                var details = new OrderDetail {
                    OrderId = order.OrderId, ProductId = item.ProductId, Quantity = item.Quantity, UnitPrice = db.Products.Find(item.ProductId).UnitPrice, Username = order.Email
                };
                order.OrderDetails.Add(details);
            }

            db.Entry(order).State = EntityState.Modified;
            db.SaveChanges();
            cart.EmptyCart();
            Response.Redirect("~/Checkout/CheckoutComplete.aspx");
        }
Exemple #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if ((string)Session["userCheckoutCompleted"] != "true")
                {
                    Session["userCheckoutCopleted"] = string.Empty;
                    Response.Redirect("CheckoutError.aspx?" + "Desc=Unvalidate%20Checkout.");
                }

                NVPAPICaller payPalCaller = new NVPAPICaller();

                string   retMsg             = "";
                string   token              = "";
                string   finalPaymentAmount = "";
                string   PayerID            = "";
                NVPCodec decoder            = new NVPCodec();

                token              = Session["token"].ToString();
                PayerID            = Session["payerID"].ToString();
                finalPaymentAmount = (Convert.ToInt32(Session["payment_amt"]) * 100).ToString();

                bool ret = payPalCaller.DoCheckoutPayment(finalPaymentAmount, token, PayerID, ref decoder, ref retMsg);
                if (ret)
                {
                    string PaymentConfirmation = decoder["PAYMENTINFO_0_TRANSACTIONID"].ToString();
                    TransactionId.Text = PaymentConfirmation;

                    ProductContext _db            = new ProductContext();
                    int            currentOrderId = -1;
                    if (Session["currentOrderID"].ToString() != string.Empty)
                    {
                        currentOrderId = Convert.ToInt32(Session["currentOrderID"]);
                    }
                    Order myCurrentOrder;
                    if (currentOrderId >= 0)
                    {
                        myCurrentOrder = _db.Orders.Single(
                            o => o.OrderId == currentOrderId);

                        myCurrentOrder.PaymentTransactionId = PaymentConfirmation;
                        _db.SaveChanges();
                    }

                    using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
                    {
                        usersShoppingCart.EmptyCart();
                    }

                    Session["currentOrderId"] = string.Empty;
                }
                else
                {
                    Response.Redirect("CheckoutError.aspx?" + retMsg);
                }
            }
        }
Exemple #3
0
        protected void CheckoutBtn_Click(object sender, EventArgs e)
        {
            using (ShoppingCartActions usersShoppingCart =
                       new ShoppingCartActions())
            {
                usersShoppingCart.EmptyCart();
            }

            CartList.Columns.Clear();
            LabelTotalText.Text      = null;
            lblTotal.Text            = null;
            UpdateBtn.Visible        = false;
            CheckoutImageBtn.Visible = false;
            server.Text = "Shopping Cart is Empty";
            Response.Redirect("ShoppingCart.aspx");
        }
 /**When user clicks "purchase", iterate through all items in customer's shopping cart and
  * send product id, product name, and unit price to shopping cart action file to create
  * 'purchased' event in Cosmos DB**/
 protected void CheckoutBtn_Click(object sender, EventArgs e)
 {
     using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
     {
         foreach (var item in usersShoppingCart.GetCartItems())
         {
             int    productId   = item.ProductId;
             string productName = item.Product.ProductName;
             double unitPrice   = Convert.ToDouble(item.Product.UnitPrice);
             for (int k = 0; k < item.Quantity; k++)
             {
                 usersShoppingCart.PurchaseProduct(productId, productName, unitPrice);
             }
         }
         usersShoppingCart.EmptyCart();
         /**Redirect user to page that thanks them for their purchase**/
         Response.Redirect("~/PurchasePage.aspx");
     }
 }
 public ActionResult Purchase()
 {
     using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
     {
         if (usersShoppingCart.GetCartItems().Count != 0 && this.User.Identity.IsAuthenticated)
         {
             ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext()
                                    .GetUserManager <ApplicationUserManager>()
                                    .FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
             ViewBag.FirstName = user.FirstName;
             ViewBag.LastName  = user.LastName;
             ViewBag.Address   = user.Address;
             usersShoppingCart.EmptyCart();
             return(View());
         }
         else
         {
             return(RedirectToAction("Index", "Home"));
         }
     }
 }
Exemple #6
0
        public async Task <ActionResult> Complete(OrderModel order)
        {
            try
            {
                // TODO: Complete the payment processing via the gateway and update the order...
                NVPAPICaller gatewayCaller = new NVPAPICaller();

                string   token = "";
                string   finalPaymentAmount = "";
                NVPCodec decoder            = new NVPCodec();

                token = Session["token"].ToString();
                //PayerID = Session["payerId"].ToString();
                //finalPaymentAmount = Session["payment_amt"].ToString();
                finalPaymentAmount = order.Total.ToString("C2");

                bool ret = true;// gatewayCaller.DoCheckoutPayment(finalPaymentAmount, token, ref decoder);
                if (ret)
                {
                    //// Retrieve PayPal confirmation value.
                    string PaymentConfirmation = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 10);// decoder[NVPProperties.Properties.TRANSACTIONID].ToString();
                    order.PaymentTransactionId = PaymentConfirmation;

                    // Get the current order id.
                    int currentOrderId = -1;
                    if (Session["currentOrderId"] != null && Session["currentOrderId"].ToString() != string.Empty)
                    {
                        currentOrderId = Convert.ToInt32(Session["currentOrderID"]);
                    }

                    Order myCurrentOrder;

                    if (currentOrderId >= 0)
                    {
                        myCurrentOrder = await SqlDbHelper.GetOrder(currentOrderId);

                        myCurrentOrder.PaymentTransactionId = PaymentConfirmation;

                        await SqlDbHelper.SaveOrder(myCurrentOrder);

                        // Queue up a receipt generation request, asynchronously.
                        await new AzureQueueHelper().QueueReceiptRequest(myCurrentOrder);

                        // Report successful event to Application Insights.
                        var eventProperties = new Dictionary <string, string>();
                        eventProperties.Add("CustomerEmail", order.Email);
                        eventProperties.Add("OrderTotal", finalPaymentAmount);
                        eventProperties.Add("PaymentTransactionId", PaymentConfirmation);
                        TelemetryHelper.TrackEvent("OrderCompleted", eventProperties);
                    }

                    // Clear shopping cart.
                    using (ShoppingCartActions usersShoppingCart =
                               new ShoppingCartActions(cartId, items, categories))
                    {
                        await usersShoppingCart.EmptyCart();
                    }

                    // Clear order id.
                    Session["currentOrderId"] = string.Empty;
                }
                else
                {
                    var error = gatewayCaller.PopulateGatewayErrorModel(decoder);

                    // Report failed event to Application Insights.
                    Exception ex = new Exception(error.ToString());
                    ex.Source = "Contoso.Apps.Movies.Web.CheckoutController.cs";
                    TelemetryHelper.TrackException(ex);

                    // Redirect to the checkout error view:
                    return(RedirectToAction("Error", error));
                }
            }
            catch (WebException wex)
            {
                ExceptionUtility.LogException(wex, "CheckoutController.cs Complete Action");

                var error = new CheckoutErrorModel
                {
                    ErrorCode = wex.Message
                };

                if (wex.Response != null && wex.Response.GetType() == typeof(HttpWebResponse))
                {
                    // Extract the response body from the WebException's HttpWebResponse:
                    error.LongMessage = ((HttpWebResponse)wex.Response).StatusDescription;
                }

                // Redirect to the checkout error view:
                return(RedirectToAction("Error", error));
            }
            catch (Exception ex)
            {
                ExceptionUtility.LogException(ex, "CheckoutController.cs Complete Action");

                var error = new CheckoutErrorModel
                {
                    ErrorCode = ex.Message
                };

                // Redirect to the checkout error view:
                return(RedirectToAction("Error", error));
            }

            return(View(order));
        }
        public async Task<ActionResult> Complete(OrderModel order)
        {
            try
            {
                // TODO: Complete the payment processing via the gateway and update the order...
                var gatewayCaller = new NVPAPICaller(_config, HttpContext);
                var finalPaymentAmount = string.Empty;
                var decoder = new NVPCodec();

                var token = HttpContext.Session.GetString("token");
                //PayerID = Session["payerId"].ToString();
                //finalPaymentAmount = Session["payment_amt"].ToString();
                finalPaymentAmount = order.Total.ToString("C2");

                var ret = gatewayCaller.DoCheckoutPayment(finalPaymentAmount, token, ref decoder);
                if (ret)
                {
                    // Retrieve PayPal confirmation value.
                    string PaymentConfirmation = decoder[NVPProperties.Properties.TRANSACTIONID].ToString();
                    order.PaymentTransactionId = PaymentConfirmation;

                    // Get the current order id.
                    int currentOrderId = -1;
                    if (HttpContext.Session.GetInt32("currentOrderId") != null && HttpContext.Session.GetInt32("currentOrderId")?.ToString() != string.Empty)
                    {
                        currentOrderId = Convert.ToInt32(HttpContext.Session.GetInt32("currentOrderId"));
                    }
                    Order myCurrentOrder;
                    if (currentOrderId >= 0)
                    {
                        // Get the order based on order id.
                        myCurrentOrder = _db.Orders.Single(o => o.OrderId == currentOrderId);
                        // Update the order to reflect payment has been completed.
                        myCurrentOrder.PaymentTransactionId = PaymentConfirmation;
                        // Save to DB.
                        await _db.SaveChangesAsync();

                        // Queue up a receipt generation request, asynchronously.
                        await new AzureQueueHelper(_config).QueueReceiptRequest(myCurrentOrder);

                        // Report successful event to Application Insights.
                        var eventProperties = new Dictionary<string, string>();
                        eventProperties.Add("CustomerEmail", myCurrentOrder.Email);
                        eventProperties.Add("OrderTotal", finalPaymentAmount);
                        eventProperties.Add("PaymentTransactionId", PaymentConfirmation);
                        TelemetryHelper.TrackEvent("OrderCompleted", eventProperties);
                    }

                    // Clear shopping cart.
                    var usersShoppingCart = new ShoppingCartActions(_db, CartId);
                    
                    await usersShoppingCart.EmptyCart();
                    

                    // Clear order id.
                    HttpContext.Session.Remove("currentOrderId");
                }
                else
                {
                    var error = gatewayCaller.PopulateGatewayErrorModel(decoder);

                    // Report failed event to Application Insights.
                    Exception ex = new Exception(error.ToString());
                    ex.Source = "Contoso.Apps.SportsLeague.Web.CheckoutController.cs";
                    TelemetryHelper.TrackException(ex);

                    // Redirect to the checkout error view:
                    return RedirectToAction("Error", error);
                }
            }
            catch (WebException wex)
            {
                ExceptionUtility.LogException(wex, "CheckoutController.cs Complete Action");

                var error = new CheckoutErrorModel
                {
                    ErrorCode = wex.Message
                };

                if (wex.Response != null && wex.Response.GetType() == typeof(HttpWebResponse))
                {
                    // Extract the response body from the WebException's HttpWebResponse:
                    error.LongMessage = ((HttpWebResponse)wex.Response).StatusDescription;
                }

                // Redirect to the checkout error view:
                return RedirectToAction("Error", error);
            }
            catch (Exception ex)
            {
                ExceptionUtility.LogException(ex, "CheckoutController.cs Complete Action");

                var error = new CheckoutErrorModel
                {
                    ErrorCode = ex.Message
                };

                // Redirect to the checkout error view:
                return RedirectToAction("Error", error);
            }

            return View(order);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Verify user has completed the checkout process.
                if ((string)Session["userCheckoutCompleted"] != "true")
                {
                    Session["userCheckoutCompleted"] = string.Empty;
                    Response.Redirect("CheckoutError.aspx?" + "Desc=Unvalidated%20Checkout.");
                }

                NVPAPICaller payPalCaller = new NVPAPICaller();

                string   retMsg             = "";
                string   token              = "";
                string   finalPaymentAmount = "";
                string   PayerID            = "";
                NVPCodec decoder            = new NVPCodec();

                token              = Session["token"].ToString();
                PayerID            = Session["payerId"].ToString();
                finalPaymentAmount = Session["payment_amt"].ToString();

                bool ret = payPalCaller.DoCheckoutPayment(finalPaymentAmount, token, PayerID, ref decoder, ref retMsg);
                if (ret)
                {
                    // Retrieve PayPal confirmation value.
                    string PaymentConfirmation = decoder["PAYMENTINFO_0_TRANSACTIONID"].ToString();
                    TransactionId.Text = PaymentConfirmation;

                    ApplicationDbContext _db = new ApplicationDbContext();
                    // Get the current order id.
                    int currentOrderId = -1;
                    var temp           = Session["currentOrderId"];
                    if (Convert.ToString(temp) != string.Empty)
                    {
                        currentOrderId = Convert.ToInt32(temp);
                    }
                    Order myCurrentOrder;
                    if (currentOrderId >= 0)
                    {
                        // Get the order based on order id.
                        myCurrentOrder = _db.Orders.Single(o => o.OrderId == currentOrderId);
                        // Update the order to reflect payment has been completed.
                        myCurrentOrder.PaymentTransactionId = PaymentConfirmation;
                        // Save to DB.
                        _db.SaveChanges();
                    }

                    // Clear shopping cart.
                    using (ShoppingCartActions usersShoppingCart =
                               new ShoppingCartActions())
                    {
                        usersShoppingCart.EmptyCart();
                    }

                    // Clear order id.
                    Session["currentOrderId"] = string.Empty;
                }
                else
                {
                    Response.Redirect("CheckoutError.aspx?" + retMsg);
                }
            }
        }
Exemple #9
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Verify user has completed the checkout process
                if (Session["UserCheckoutCompleted"].ToString() != "true")
                {
                    Session["UserCheckoutCompleted"] = string.Empty;
                    Response.Redirect("/Checkout/CheckoutError.aspx?Desc=Unvalidated@20Checkout.");
                }

                var payPalCaller       = new NVPAPICaller();
                var retMsg             = "";
                var token              = "";
                var finalPaymentAmount = "";
                var payerId            = "";
                var decoder            = new NVPCodec();

                token              = Session["token"].ToString();
                payerId            = Session["payerId"].ToString();
                finalPaymentAmount = Session["payment_amt"].ToString();

                var ret = payPalCaller.DoCheckoutPayment(finalPaymentAmount, token, payerId, ref decoder, ref retMsg);
                if (ret)
                {
                    // Retrieve PayPal confirmation value
                    var PaymentConfirmation = decoder["PAYMENTINFO_0_TRANSACTIONID"];
                    TransactionId.Text = PaymentConfirmation;

                    var _db = new ProductContext();
                    // Get the current order id
                    var currentOrderId = -1;
                    if (Session["currentOrderId"].ToString() != string.Empty)
                    {
                        currentOrderId = Convert.ToInt32(Session["CurrentOrderId"]);
                    }
                    Order myCurrentOrder;
                    if (currentOrderId >= 0)
                    {
                        // Get the order based on order id
                        myCurrentOrder = _db.Orders.Single(o => o.OrderId == currentOrderId);
                        // Update the order to reflect payment has been completed
                        myCurrentOrder.PaymentTransactionId = PaymentConfirmation;
                        // Save to db
                        _db.SaveChanges();
                    }

                    // Clear shopping cart
                    using (var usersShoppingCart = new ShoppingCartActions()) {
                        usersShoppingCart.EmptyCart();
                    }

                    // Clear order id
                    Session["CurrentOrderId"] = string.Empty;
                }
                else
                {
                    Response.Redirect($"/Checkout/CheckoutError.aspx?{retMsg}");
                }
            }
        }