public ActionResult Orders()
        {
            var model = new OrdersCollection();
            IEnumerable<Order> modelIEnumerable = null;
            if (Request.QueryString["OrderId"] != null && Request.QueryString["Success"] != null)
            {
                var orderId = Request.QueryString["OrderId"];
                var payerId = Request.QueryString["PayerId"];
                var isSuccess = Convert.ToBoolean(Request.QueryString["Success"]);
                if (isSuccess)
                {
                    PaymentExecution payExecution = new PaymentExecution();
                    payExecution.payer_id = payerId;
                    Payment paymnt = new Payment();
                    paymnt.id = GetOrdersPaymentId(orderId);
                    Payment pay = null;
                    try
                    {
                        pay = paymnt.Execute(Api, payExecution);
                        if (pay != null && pay.state.Trim().ToLower().Equals("approved"))
                        {
                            var state = pay.state.Trim();
                            var updatedAtDateTime = Convert.ToDateTime(pay.create_time);
                            var updatedAt = updatedAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                            var ordId = Convert.ToInt32(orderId);
                            bool isUpdated = Update(ordId, state, updatedAt);
                        }
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }
                else
                {
                    orderId = Request.QueryString["OrderId"];
                    var updatedAtDateTime = DateTime.Now;
                    var updatedAt = updatedAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                    bool isUpdated = Update(Convert.ToInt32(orderId), "cancelled", updatedAt);
                }
            }

            var email = User.Identity.Name.Trim();
            int userId = GetSignedInUserId(email);
            DataTable datTable = GetOrders(userId);
            if (datTable != null && datTable.Rows.Count > 0)
            {
                model.Orders = (from DataRow row in datTable.Rows
                                select new Order
                                {
                                    OrderId = Convert.ToInt32(row["id"]),
                                    UserId = Convert.ToInt32(row["user_id"]),
                                    PaymentId = row["payment_id"].ToString(),
                                    State = row["state"].ToString(),
                                    AmountInUSD = row["amount"].ToString(),
                                    Description = row["description"].ToString(),
                                    CreatedDateTime = Convert.ToDateTime(row["created_at"]),
                                    UpdatedDateTime = Convert.ToDateTime(row["updated_at"]),

                                }).ToList();

                modelIEnumerable = model.Orders;
            }
            return View(modelIEnumerable);
        }
Ejemplo n.º 2
0
        public ActionResult Orders()
        {
            var model = new OrdersCollection();
            IEnumerable <Order> modelIEnumerable = null;

            if (Request.QueryString["OrderId"] != null && Request.QueryString["Success"] != null)
            {
                var orderID   = Request.QueryString["OrderId"];
                var payerID   = Request.QueryString["PayerId"];
                var isSuccess = Convert.ToBoolean(Request.QueryString["Success"]);
                if (isSuccess)
                {
                    PaymentExecution pymntExecution = new PaymentExecution();
                    pymntExecution.payer_id = payerID;
                    Payment pymnt = new Payment();
                    pymnt.id = GetOrdersPaymentID(orderID);
                    Payment pay = null;
                    try
                    {
                        pay = pymnt.Execute(Api, pymntExecution);
                        if (pay != null && pay.state.Trim().ToLower().Equals("approved"))
                        {
                            var  state             = pay.state.Trim();
                            var  updatedAtDateTime = Convert.ToDateTime(pay.create_time);
                            var  updatedAt         = updatedAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                            var  ordID             = Convert.ToInt32(orderID);
                            bool isUpdated         = Update(ordID, state, updatedAt);
                        }
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }
                else
                {
                    orderID = Request.QueryString["OrderId"];
                    var  updatedAtDateTime = DateTime.Now;
                    var  updatedAt         = updatedAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
                    bool isUpdated         = Update(Convert.ToInt32(orderID), "cancelled", updatedAt);
                }
            }

            var       email    = User.Identity.Name.Trim();
            int       userID   = GetSignedInUserID(email);
            DataTable datTable = GetOrders(userID);

            if (datTable != null && datTable.Rows.Count > 0)
            {
                model.Orders = (from DataRow row in datTable.Rows
                                select new Order
                {
                    OrderID = Convert.ToInt32(row["id"]),
                    UserID = Convert.ToInt32(row["user_id"]),
                    PaymentID = row["payment_id"].ToString(),
                    State = row["state"].ToString(),
                    AmountInUSD = row["amount"].ToString(),
                    Description = row["description"].ToString(),
                    CreatedDateTime = Convert.ToDateTime(row["created_at"]),
                    UpdatedDateTime = Convert.ToDateTime(row["updated_at"]),
                }).ToList();

                modelIEnumerable = model.Orders;
            }
            return(View(modelIEnumerable));
        }