Exemplo n.º 1
0
        public async Task <IActionResult> ThanhToanVnPay()
        {
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            var hostname = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}";

            var oder = new Oder();

            oder.ID           = DateTime.Now.Ticks;
            oder.ShipName     = "VNPay User";
            oder.ShipMobile   = "VNPay User";
            oder.ShipAddress  = "VNPay User";
            oder.ShipEmail    = "VNPay User";
            oder.CustomerID   = user.Id;
            oder.CreatedDate  = DateTime.Now;
            oder.CheckOutType = "VNPay";

            var cart     = SessionHelper.Get <List <Item> >(HttpContext.Session, "cart");
            var subTotal = cart.Sum(item => (item.Product.DonGia - item.Product.DonGia * item.Product.GiamGia / 100) * item.Quantity);

            oder.Total = Math.Round(subTotal);

            try
            {
                var id = Insert(oder);
                foreach (var item in cart)
                {
                    var oderDetail = new OderDetail();
                    oderDetail.MaHH     = item.Product.MaHH;
                    oderDetail.OderID   = id;
                    oderDetail.Gia      = item.Product.DonGia;
                    oderDetail.Quantity = item.Quantity;
                    Insert1(oderDetail);

                    var hanghoas = _context.HangHoas.Where(x => x.MaHH == item.Product.MaHH).First();

                    hanghoas.DaMua += item.Quantity;
                    _context.Update(hanghoas);
                    _context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.Write(e);
                return(View("ThatBai"));
            }

            var oderInfo = "";

            foreach (var item in cart)
            {
                var itemGiamGia = item.Product.DonGia * item.Product.GiamGia / 100;
                var itemGia     = Math.Round((item.Product.DonGia - itemGiamGia) * item.Quantity, 0);
                oderInfo = oderInfo + item.Quantity.ToString() + " " + item.Product.TenHH + " " + itemGia.ToString() + "VND; ";
            }

            if (oderInfo.Length > 0)
            {
                oderInfo = oderInfo.Remove(oderInfo.Length - 2, 2);
            }


            _vnPayLibrary.AddRequestData("vnp_Version", "2.0.0");
            _vnPayLibrary.AddRequestData("vnp_Command", "pay");
            _vnPayLibrary.AddRequestData("vnp_TmnCode", _tmnCode);
            _vnPayLibrary.AddRequestData("vnp_Amount", (oder.Total * 100).ToString());
            _vnPayLibrary.AddRequestData("vnp_BankCode", "");
            _vnPayLibrary.AddRequestData("vnp_CreateDate", oder.CreatedDate.ToString("yyyyMMddHHmmss"));
            _vnPayLibrary.AddRequestData("vnp_CurrCode", "VND");
            _vnPayLibrary.AddRequestData("vnp_IpAddr", _utils.GetIpAddress());
            _vnPayLibrary.AddRequestData("vnp_Locale", "vn");
            _vnPayLibrary.AddRequestData("vnp_OrderInfo", oderInfo);
            _vnPayLibrary.AddRequestData("vnp_OrderType", "130001"); //default value: other
            _vnPayLibrary.AddRequestData("vnp_ReturnUrl", $"{hostname}/cart/ket-qua-vnpay");
            _vnPayLibrary.AddRequestData("vnp_TxnRef", oder.ID.ToString());

            string paymentUrl = _vnPayLibrary.CreateRequestUrl(_url, _hashSecret);

            return(Redirect(paymentUrl));
        }