Пример #1
0
        public async Task <IActionResult> Checkout(CheckoutViewModel model)
        {
            var sessions = HttpContext.Session.Get <List <ShoppingCartViewModel> >(CommonConstants.CartSession);

            if (ModelState.IsValid)
            {
                if (sessions != null)
                {
                    var details = new List <BillDetailViewModel>();

                    decimal totalMoney = 0;
                    bool    canBuy     = true;
                    foreach (var item in sessions)
                    {
                        totalMoney += item.Price * item.Quantity;

                        details.Add(new BillDetailViewModel()
                        {
                            Price     = item.Price,
                            ColorId   = item.Color.Id,
                            SizeId    = item.Size.Id,
                            Quantity  = item.Quantity,
                            ProductId = item.Product.Id
                        });

                        canBuy = await _quantityService.SellProduct(item.Product.Id, item.Quantity);

                        if (!canBuy)
                        {
                            break;
                        }
                    }

                    if (canBuy)
                    {
                        var billViewModel = new BillViewModel()
                        {
                            CustomerMobile  = model.CustomerMobile,
                            BillStatus      = BillStatus.New,
                            CustomerAddress = model.CustomerAddress,
                            CustomerName    = model.CustomerName,
                            CustomerMessage = model.CustomerMessage,
                            BillDetails     = details,
                            DateCreated     = DateTime.Now,
                            PaymentMethod   = model.PaymentMethod
                        };
                        if (User.Identity.IsAuthenticated)
                        {
                            billViewModel.CustomerId = Guid.Parse(User.GetSpecificDefault("UserId"));
                        }
                        var dataReturn = await _billService.Create(billViewModel);

                        _billService.Save();
                        HttpContext.Session.Set(CommonConstants.BillSession, dataReturn);
                        string vnp_Returnurl         = _configuration["VNPAY:vnp_Returnurl"];  //URL nhan ket qua tra ve
                        string vnp_Url               = _configuration["VNPAY:vnp_Url"];        //URL thanh toan cua VNPAY
                        string vnp_TmnCode           = _configuration["VNPAY:vnp_TmnCode"];    //Ma website
                        string vnp_HashSecret        = _configuration["VNPAY:vnp_HashSecret"]; //Chuoi bi mat
                        HttpContextAccessor accessor = new HttpContextAccessor();
                        VnPayLibrary        vnpay    = new VnPayLibrary(accessor);
                        vnpay.AddRequestData("vnp_Version", "2.0.0");
                        vnpay.AddRequestData("vnp_Command", "pay");
                        vnpay.AddRequestData("vnp_TmnCode", vnp_TmnCode);
                        vnpay.AddRequestData("vnp_Locale", "vn");
                        vnpay.AddRequestData("vnp_CurrCode", "VND");
                        vnpay.AddRequestData("vnp_TxnRef", DateTime.Now.Ticks.ToString());
                        vnpay.AddRequestData("vnp_OrderInfo", "Noi dung thanh toan:20190421030204");
                        vnpay.AddRequestData("vnp_OrderType", "topup"); //default value: other
                        vnpay.AddRequestData("vnp_Amount", ((int)totalMoney * 100).ToString());
                        vnpay.AddRequestData("vnp_ReturnUrl", vnp_Returnurl);
                        vnpay.AddRequestData("vnp_IpAddr", vnpay.GetIpAddress());
                        vnpay.AddRequestData("vnp_CreateDate", DateTime.Now.ToString("yyyyMMddHHmmss"));
                        vnpay.AddRequestData("vnp_BankCode", "NCB");
                        string paymentUrl = vnpay.CreateRequestUrl(vnp_Url, vnp_HashSecret);
                        return(new OkObjectResult(new
                        {
                            status = true,
                            url = paymentUrl
                        }));
                    }

                    return(new OkObjectResult(new
                    {
                        status = false,
                        message = "Hiện số lượng sản phẩm không đủ."
                    }));
                }

                model.Carts = sessions;
                HttpContext.Session.Set(CommonConstants.CartSession, new List <ShoppingCartViewModel>());
                return(View(model));
            }
            return(new OkObjectResult(""));
        }