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 pay = null;
                            pay = CreatePayment(email, PaymentMethod.credit_card, amount, description);
                            if (pay != null)
                            {
                                var payId = pay.id;
                                var state = pay.state;
                                DateTime createdDateTime = Convert.ToDateTime(pay.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, payId, 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 pay = null;

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

            }

            if (model.PaymentTypes == null)
            {
                model.PaymentTypes = RegisterPaymentTypes();
            }
            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()
        {
            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]"];
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }

            return View(model);
        }