Exemple #1
0
        public ActionResult Payment(PaymentParam model)
        {
            try
            {
                if (model != null)
                {
                    if (Request.Cookies["MemberLoginCookie"] == null)
                    {
                        return(Json(new PaymentResult {
                            Status = -1, Uri = ""
                        }));
                    }
                    HttpCookie          reqCookies = Request.Cookies["MemberLoginCookie"];
                    ResponseMemberLogin login      = JsonConvert.DeserializeObject <ResponseMemberLogin>(reqCookies.Value.ToString().UrlDecode());
                    if (login == null || login.ID == 0)
                    {
                        return(Json(new PaymentResult {
                            Status = -1, Uri = ""
                        }));
                    }
                    if (Session["ListShoppingCart"] == null)
                    {
                        return(Json(new PaymentResult {
                            Status = -1, Uri = ""
                        }));
                    }
                    List <Cart> carts = (List <Cart>)Session["ListShoppingCart"];
                    if (carts == null || carts.Count() <= 0)
                    {
                        return(Json(new PaymentResult {
                            Status = -1, Uri = ""
                        }));
                    }

                    List <string> codes  = new List <string>();
                    List <string> id     = new List <string>();
                    double        amount = 0;


                    string Code = "";
                    Code += RandomUtils.RandomString(7, 9, true, true, false);
                    if (_orderBusiness.CheckExistsCode(Code))
                    {
                        Code = RandomUtils.RandomString(7, 9, true, true, false);
                    }
                    Code = Code.ToUpperCase(true);
                    OrderView order = new OrderView();
                    order.Amount  = carts.Sum(x => x.Total);
                    order.Code    = Code;
                    order.Date    = DateTime.Now;
                    order.Member  = login.ID;
                    order.Receipt = model.ReceiptID;
                    //order.Reduce=carts.Sum(x=>x.re)
                    order.StatusOrder   = 0;
                    order.StatusPayment = 0;
                    order.Total         = carts.Sum(x => x.Total);
                    order.TypePayment   = model.TypePay;
                    if (_orderBusiness.Add(order))
                    {
                        _orderBusiness.Save();

                        foreach (var d in carts)
                        {
                            OrderDetailView detail = new OrderDetailView();
                            detail.Code    = Code;
                            detail.Price   = d.Price;
                            detail.Product = d.ID;
                            detail.Qty     = d.Qty;
                            detail.Reduce  = 0;
                            detail.Total   = d.Total;
                            if (_orderDetailBusiness.Add(detail))
                            {
                                _orderDetailBusiness.Save();
                            }
                        }
                    }
                    amount = order.Amount;

                    Session["ListShoppingCart"] = null;
                    if (model.TypePay == 2)
                    {
                        return(Json(new PaymentResult {
                            Status = 1, Uri = $"/xac-nhan-dat-hang.html?code={Code}"
                        }));
                    }
                    if (model.TypePay == 0)
                    {
                        string uri = CreateRequestPaymentPort((amount * 100).ToString(), Code, login.Name);
                        return(Json(new PaymentResult {
                            Status = 1, Uri = uri
                        }));
                    }
                    if (model.TypePay == 1)
                    {
                        string uri = CreateRequestPaymentPortGlobal((amount * 100).ToString(), Code, login.Name);
                        return(Json(new PaymentResult {
                            Status = 1, Uri = uri
                        }));
                    }
                }
                return(Json(new PaymentResult {
                    Status = -1, Uri = ""
                }));
            }
            catch (Exception)
            {
                return(Json(new PaymentResult {
                    Status = -1, Uri = ""
                }));
            }
        }