public ActionResult PlaceOrders()
        {
            var model = new PlaceOrdersModels();

            model.PaymentTypes = RegisterPaymentTypes();
            model.PaymentType  = string.Empty;

            if (Request.QueryString["order[amount]"] != null && Request.QueryString["order[description]"] != null)
            {
                model.Amount      = Request.QueryString["order[amount]"];
                model.Description = Request.QueryString["order[description]"];
            }

            return(View(model));
        }
        private SelectListItem[] RegisterPaymentTypes()
        {
            var model = new PlaceOrdersModels();

            model.PaymentTypes = new[]
            {
                new SelectListItem {
                    Text = "credit_card", Value = "credit_card"
                },
                new SelectListItem {
                    Text = "paypal", Value = "paypal"
                },
            };
            return(model.PaymentTypes);
        }
        public ActionResult PlaceOrders(PlaceOrdersModels model)
        {
            if (ModelState.IsValid)
            {
                if (Request.QueryString["order[amount]"] != null && Request.QueryString["order[description]"] != null)
                {
                    model.Amount = Request.QueryString["order[amount]"];
                    model.Description = Request.QueryString["order[description]"];

                    var email = User.Identity.Name.Trim();
                    var userID = GetSignedInUserID(email);

                    if (model.PaymentType != null)
                    {
                        if (model.PaymentType.Trim() == "credit_card")
                        {
                            var amount = model.Amount.Trim();
                            var description = model.Description.Trim();
                            Payment pymnt = null;
                            pymnt = CreatePayment(email, PaymentMethod.credit_card, amount, description);
                            if (pymnt != null)
                            {
                                var pymntID = pymnt.id;
                                var state = pymnt.state;
                                DateTime createdDateTime = Convert.ToDateTime(pymnt.create_time);
                                var createdAt = createdDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                                var updatedAt = createdDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                                bool isSuccess = Insert(userID, pymntID, state, amount, description, createdAt, updatedAt);
                                if (isSuccess)
                                {
                                    if (state.Trim().ToLower().Equals("approved"))
                                    {
                                        string requestUrl = Request.Url.OriginalString;
                                        string authority = Request.Url.Authority;
                                        string dnsSafeHost = Request.Url.DnsSafeHost;

                                        if (Request.UrlReferrer != null && Request.UrlReferrer.Scheme == "https")
                                        {
                                            requestUrl = requestUrl.Replace("http://", "https://");
                                            requestUrl = requestUrl.Replace(authority, dnsSafeHost);
                                        }
                                        return new RedirectResult("~/Orders/Orders?Status=approved");
                                    }
                                    else
                                    {
                                        ModelState.AddModelError(string.Empty, "Order failed.");
                                    }
                                }
                            }
                            else
                            {
                                ModelState.AddModelError(string.Empty, "Order failed.");
                            }
                        }
                        else if (model.PaymentType.Trim() == "paypal")
                        {
                            DateTime createdDateTime = DateTime.Now;
                            var createdAt = createdDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                            var updatedAt = createdDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                            bool isSuccess = Insert(userID, createdAt, updatedAt);
                            if (isSuccess)
                            {
                                int orderID = GetSignedInUserLastInsertedOrderID(userID);
                                string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/Orders/Orders?";
                                string requestUrl = Request.Url.OriginalString;
                                string returnUrl = baseURI + "Success=True&OrderID=" + orderID;
                                string cancelUrl = baseURI + "Success=False&OrderID=" + orderID;
                                var amount = model.Amount.Trim();
                                var description = model.Description.Trim();
                                Payment pymnt = null;

                                pymnt = CreatePayment(email, PaymentMethod.paypal, amount, description, returnUrl, cancelUrl);
                                if (pymnt != null)
                                {
                                    var pymntID = pymnt.id;
                                    var state = pymnt.state;
                                    var updatedAtDateTime = Convert.ToDateTime(pymnt.create_time);
                                    var pymntUpdatedAt = updatedAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                                    bool isUpdateSuccess = Update(orderID, pymntID, state, amount, description, pymntUpdatedAt);
                                    if (isUpdateSuccess)
                                    {
                                        string dredirectUrl = GetApprovalURL(pymnt);
                                        return new RedirectResult(dredirectUrl);
                                    }
                                }
                                else
                                {
                                    ModelState.AddModelError(string.Empty, "Order failed.");
                                }
                            }
                        }
                    }
                }

            }

            if (model.PaymentTypes == null)
            {
                model.PaymentTypes = RegisterPaymentTypes();
            }
            return View(model);
        }
        public ActionResult PlaceOrders()
        {
            var model = new PlaceOrdersModels();
            model.PaymentTypes = RegisterPaymentTypes();
            model.PaymentType = string.Empty;

            if (Request.QueryString["order[amount]"] != null && Request.QueryString["order[description]"] != null)
            {
                model.Amount = Request.QueryString["order[amount]"];
                model.Description = Request.QueryString["order[description]"];
            }

            return View(model);
        }
 private SelectListItem[] RegisterPaymentTypes()
 {
     var model = new PlaceOrdersModels();
     model.PaymentTypes = new[]
     {
         new SelectListItem { Text = "credit_card", Value = "credit_card" }, 
         new SelectListItem { Text = "paypal", Value = "paypal" },
     };
     return model.PaymentTypes;
 }
        public ActionResult PlaceOrders(PlaceOrdersModels model)
        {
            if (ModelState.IsValid)
            {
                if (Request.QueryString["order[amount]"] != null && Request.QueryString["order[description]"] != null)
                {
                    model.Amount      = Request.QueryString["order[amount]"];
                    model.Description = Request.QueryString["order[description]"];

                    var email  = User.Identity.Name.Trim();
                    var userID = GetSignedInUserID(email);

                    if (model.PaymentType != null)
                    {
                        if (model.PaymentType.Trim() == "credit_card")
                        {
                            var     amount      = model.Amount.Trim();
                            var     description = model.Description.Trim();
                            Payment pymnt       = null;
                            pymnt = CreatePayment(email, PaymentMethod.credit_card, amount, description);
                            if (pymnt != null)
                            {
                                var      pymntID         = pymnt.id;
                                var      state           = pymnt.state;
                                DateTime createdDateTime = Convert.ToDateTime(pymnt.create_time);
                                var      createdAt       = createdDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                                var      updatedAt       = createdDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                                bool     isSuccess       = Insert(userID, pymntID, state, amount, description, createdAt, updatedAt);
                                if (isSuccess)
                                {
                                    if (state.Trim().ToLower().Equals("approved"))
                                    {
                                        string requestUrl  = Request.Url.OriginalString;
                                        string authority   = Request.Url.Authority;
                                        string dnsSafeHost = Request.Url.DnsSafeHost;

                                        if (Request.UrlReferrer != null && Request.UrlReferrer.Scheme == "https")
                                        {
                                            requestUrl = requestUrl.Replace("http://", "https://");
                                            requestUrl = requestUrl.Replace(authority, dnsSafeHost);
                                        }
                                        return(new RedirectResult("~/Orders/Orders?Status=approved"));
                                    }
                                    else
                                    {
                                        ModelState.AddModelError(string.Empty, "Order failed.");
                                    }
                                }
                            }
                            else
                            {
                                ModelState.AddModelError(string.Empty, "Order failed.");
                            }
                        }
                        else if (model.PaymentType.Trim() == "paypal")
                        {
                            DateTime createdDateTime = DateTime.Now;
                            var      createdAt       = createdDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                            var      updatedAt       = createdDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                            bool     isSuccess       = Insert(userID, createdAt, updatedAt);
                            if (isSuccess)
                            {
                                int     orderID     = GetSignedInUserLastInsertedOrderID(userID);
                                string  baseURI     = Request.Url.Scheme + "://" + Request.Url.Authority + "/Orders/Orders?";
                                string  requestUrl  = Request.Url.OriginalString;
                                string  returnUrl   = baseURI + "Success=True&OrderID=" + orderID;
                                string  cancelUrl   = baseURI + "Success=False&OrderID=" + orderID;
                                var     amount      = model.Amount.Trim();
                                var     description = model.Description.Trim();
                                Payment pymnt       = null;

                                pymnt = CreatePayment(email, PaymentMethod.paypal, amount, description, returnUrl, cancelUrl);
                                if (pymnt != null)
                                {
                                    var  pymntID           = pymnt.id;
                                    var  state             = pymnt.state;
                                    var  updatedAtDateTime = Convert.ToDateTime(pymnt.create_time);
                                    var  pymntUpdatedAt    = updatedAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                                    bool isUpdateSuccess   = Update(orderID, pymntID, state, amount, description, pymntUpdatedAt);
                                    if (isUpdateSuccess)
                                    {
                                        string dredirectUrl = GetApprovalURL(pymnt);
                                        return(new RedirectResult(dredirectUrl));
                                    }
                                }
                                else
                                {
                                    ModelState.AddModelError(string.Empty, "Order failed.");
                                }
                            }
                        }
                    }
                }
            }

            if (model.PaymentTypes == null)
            {
                model.PaymentTypes = RegisterPaymentTypes();
            }
            return(View(model));
        }