public ActionResult FinishOrderPaypal(CartDetailsModel cart)
        {
            //update product
            foreach (ProductInCartDetailsModel pro in cart.ProductsInCart)
            {
                Product p           = productRepo.GetProduct(pro.ProductID);
                int     newQuantity = p.Quantity - pro.Quantity;
                p.Quantity = newQuantity;
                if (newQuantity == 0)
                {
                    p.StatusCode = "OUTS";
                }
                productRepo.UpdateOrderedProduct(p);
            }
            //update bill status ->payed
            billRepo.UpdateBillStatus(cart.BillID, "PAY");
            //send mail for user here
            Session.Remove("CartDetails");
            TempData["PaymentMessage"] = "Your bill(bill ID:" + cart.BillID + ") is ordered success!!";
            TempData["BillID"]         = cart.BillID;

            string username = HttpContext.User.Identity.Name;
            User   user     = userRepo.GetUser(username);

            //send mail to user
            SendMailSSL.SendOrderedBillPayPalToUser(user.Email, cart, user.FullName);
            return(RedirectToAction("Index", "Product"));
        }
Exemple #2
0
        public int UpdateCart(int IDBill, int productID, int quantity)
        {
            Product p      = productRepo.GetProduct(productID);
            int     result = 0;

            try
            {
                result = CheckProductIsAvailableWithQuantity(p, quantity);
                if (result == 1)
                {
                    ProductsInBill prodInBill = productsInBillRepo.GetDetailsAProductInBill(IDBill, productID);
                    prodInBill.Quantity = quantity;
                    prodInBill.Total    = quantity * prodInBill.Price;
                    productsInBillRepo.UpdateProductInBill(prodInBill);
                    UpdateBillDetails(IDBill);
                    LoadCartDetailsModel(IDBill);
                }
            }
            catch (Exception ex)
            {
                SendMailSSL.SendErrorToAdmin("Error at UpdateCart: " + ex.ToString());
                result = -1;
            }
            return(result);
        }
        public async Task RefundOrderPaypal(string captureOrderId)
        {
            try
            {
                CartDetailsModel cart  = (CartDetailsModel)Session["CartDetails"];
                double           total = cart.Total;
                var request            = new CapturesRefundRequest(captureOrderId);
                request.Prefer("return=representation");
                RefundRequest refundRequest = new RefundRequest()
                {
                    Amount = new PayPalCheckoutSdk.Payments.Money
                    {
                        Value        = "" + total,
                        CurrencyCode = "USD"
                    }
                };
                request.RequestBody(refundRequest);
                var response = await PayPalClient.client().Execute(request);

                var result = response.Result <PayPalCheckoutSdk.Payments.Refund>();
                if (result.Status.Equals("COMPLETED"))
                {
                    TempData["RefundMess"] = "Refund money success !!";
                }
            }
            catch (Exception e)
            {
                SendMailSSL.SendErrorToAdmin("Error at RefundOrderPaypal: " + e.ToString());
            }
        }
        public async Task <HttpResponse> CreateOrderPaypal(string fullname, string address, string street, string city, int IDBill)
        {
            try
            {
                //string strHost = HttpContext.Current.Request.Url.Host.ToString();
                CartDetailsModel cart = LoadCartDetailsModel(IDBill);
                //  string host = Request.Url.Host;

                //  string urlReturn = "https://" + host;
                //  if (host.Equals("localhost"))
                //  {
                //     urlReturn = urlReturn + ":" + HttpContext.Request.Url.Port;
                // }
                // MessageBox.Show(urlReturn);
                var request = new OrdersCreateRequest();
                request.Prefer("return=representation");
                request.RequestBody(CreateOrderSample.BuildRequestBody(fullname, address, street, city, cart));
                var response = await PayPalClient.client().Execute(request);

                var    createOrderResult = response.Result <Order>();
                string orderId           = createOrderResult.Id;
                string url = "https://www.sandbox.paypal.com/checkoutnow?token=" + orderId;
                Response.Redirect(url);
                return(response);
            }
            catch (Exception e)
            {
                SendMailSSL.SendErrorToAdmin("Error at CreateOrderPaypal: " + e.ToString());
            }
            return(null);
        }
Exemple #5
0
        public JsonResult CheckCartIsValidToCheckOut(int IDBill)
        {
            CartDetailsStatusModel       cartDetailsStatus   = new CartDetailsStatusModel();
            IEnumerable <ProductsInBill> listProductsInBills = productsInBillRepo.GetListProductInBill(IDBill);

            try
            {
                foreach (ProductsInBill p in listProductsInBills)
                {
                    //check status
                    string statusProd = productRepo.GetStatus(p.ProductID);
                    if (statusProd.Equals("STOP"))
                    {
                        cartDetailsStatus.ProductID   = p.ProductID;
                        cartDetailsStatus.ProductName = p.ProductName;
                        cartDetailsStatus.Problem     = "STOP";
                        return(Json(cartDetailsStatus, JsonRequestBehavior.AllowGet));
                    }
                    if (statusProd.Equals("OUTS"))
                    {
                        cartDetailsStatus.ProductID   = p.ProductID;
                        cartDetailsStatus.ProductName = p.ProductName;
                        cartDetailsStatus.Problem     = "OUTS";
                        return(Json(cartDetailsStatus, JsonRequestBehavior.AllowGet));
                    }
                    int quantityInCart  = p.Quantity;
                    int quantityInStock = productRepo.GetQuantity(p.ProductID);
                    if (quantityInStock < quantityInCart)
                    {
                        cartDetailsStatus.ProductID       = p.ProductID;
                        cartDetailsStatus.ProductName     = p.ProductName;
                        cartDetailsStatus.Problem         = "HIGHER";
                        cartDetailsStatus.QuantityInStock = quantityInStock;
                        return(Json(cartDetailsStatus, JsonRequestBehavior.AllowGet));
                    }
                    double priceProdInCart  = p.Price;
                    double priceProdInStock = productRepo.GetUnitPrice(p.ProductID);
                    if (priceProdInCart != priceProdInStock)
                    {
                        cartDetailsStatus.ProductID   = p.ProductID;
                        cartDetailsStatus.ProductName = p.ProductName;
                        cartDetailsStatus.Problem     = "CHANGEPRICE";
                        cartDetailsStatus.NewPrice    = priceProdInStock;
                        cartDetailsStatus.OldPrice    = priceProdInCart;
                        return(Json(cartDetailsStatus, JsonRequestBehavior.AllowGet));
                    }
                }
            }catch (Exception ex)
            {
                SendMailSSL.SendErrorToAdmin("Error at CheckCartIsValidToCheckOut: " + ex.ToString());
            }
            return(Json(cartDetailsStatus, JsonRequestBehavior.AllowGet));
        }
Exemple #6
0
        public ActionResult AddToCart(int productID, int quantity = 1)
        {
            CartDetailsModel cartDetailsModel = null;
            Bill             userBill;

            try
            {
                Product product  = GetProduct(productID);
                string  username = HttpContext.User.Identity.Name;
                userBill = billRepo.GetUsersLastBillIsNotPay(username);
                if (userBill == null)
                {
                    userBill = CreateBillUser(username);
                }
                int totalQuantity = quantity;
                if (productsInBillRepo.CheckBillContainProduct(userBill.BillID, productID))
                {
                    totalQuantity = totalQuantity + productsInBillRepo.GetQuantityAProductInBill(userBill.BillID, productID);
                }
                int statusProdInStock = CheckProductIsAvailableWithQuantity(product, totalQuantity);
                if (statusProdInStock == 1)
                {
                    ProductsInBill productsInBill = new ProductsInBill();
                    productsInBill.BillID      = userBill.BillID;
                    productsInBill.ProductID   = productID;
                    productsInBill.ProductName = product.ProductName;
                    productsInBill.Quantity    = quantity;
                    productsInBill.Price       = product.Price;
                    productsInBill.Total       = product.Price * quantity;
                    bool result = productsInBillRepo.InsertProductToBill(productsInBill);
                    if (result)
                    {
                        UpdateBillDetails(userBill.BillID);
                        TempData["MessageAddToCart"] = "Add to cart success!!";
                    }
                    else
                    {
                        TempData["MessageAddToCart"] = "Error to Add to cart!!";
                    }
                }
                else
                {
                    TempData["ErrorAddToCart"] = "Error to Add to cart!!";
                }
                cartDetailsModel = LoadCartDetailsModel(userBill.BillID);
            }catch (Exception ex)
            {
                SendMailSSL.SendErrorToAdmin("Error at AddToCart: " + ex.ToString());
            }
            return(Json(cartDetailsModel, JsonRequestBehavior.AllowGet));
        }
Exemple #7
0
        public ActionResult LoadCartDetails(int IDBill = 0, string returnUrl = null)
        {
            CartDetailsModel cart = (CartDetailsModel)Session["CartDetails"];

            if (!string.IsNullOrEmpty(returnUrl))
            {
                TempData["returnUrl"] = returnUrl;
            }
            if (IDBill == 0 && cart != null)
            {
                IDBill = cart.BillID;
            }
            try
            {
                if (IDBill == 0)
                {
                    ViewBag.CartNull = "You have no product in cart !!";
                }
                else
                {
                    //if session timeout
                    if (cart == null)
                    {
                        LoadCartDetailsModel(IDBill);
                    }
                    if (cart != null)
                    {
                        if (cart.ProductsInCart != null)
                        {
                            CheckCartIsChangeInStock(cart);
                            cart = LoadCartDetailsModel(IDBill);
                        }
                        else
                        {
                            ViewBag.CartNull = "You have no product in cart !!";
                        }
                    }
                    else
                    {
                        ViewBag.CartNull = "You have no product in cart !!";
                    }
                }
            }
            catch (Exception ex)
            {
                SendMailSSL.SendErrorToAdmin("Error at LoadCartDetails: " + ex.ToString());
            }
            return(View("ShoppingCart", cart));
        }
        private CartDetailsModel LoadCartDetailsModel(int IDBill)
        {
            CartDetailsModel cart = new CartDetailsModel();

            try
            {
                Bill bill = billRepo.GetBillDetails(IDBill);
                IEnumerable <ProductsInBill> productsInBills = productsInBillRepo.GetListProductInBill(IDBill);
                cart.BillID         = IDBill;
                cart.SubTotal       = bill.SubTotal;
                cart.Tax            = bill.Tax;
                cart.Total          = bill.Total;
                cart.LastTimeChange = bill.LastTimeChange.ToString("dd/MM/yyyy HH:mm:ss");
                int numProd = 0;
                if (productsInBills != null && productsInBills.Any())
                {
                    foreach (ProductsInBill p in productsInBills)
                    {
                        ProductInCartDetailsModel pInCart = new ProductInCartDetailsModel();
                        pInCart.ProductID        = p.ProductID;
                        pInCart.ProductName      = p.ProductName;
                        pInCart.Category         = GetProductCategoryName(p.ProductID);
                        pInCart.Kind             = GetProductKindName(p.ProductID);
                        pInCart.Quantity         = p.Quantity;
                        pInCart.Price            = p.Price;
                        pInCart.Total            = p.Total;
                        pInCart.ShortDescription = productRepo.GetShortDescription(p.ProductID);
                        pInCart.StatusCode       = productRepo.GetStatus(p.ProductID);
                        if (cart.ProductsInCart == null)
                        {
                            cart.ProductsInCart = new List <ProductInCartDetailsModel>();
                        }
                        cart.ProductsInCart.Add(pInCart);
                        numProd = numProd + p.Quantity;
                    }
                }
                cart.QuantityProduct   = numProd;
                Session["CartDetails"] = cart;
            }
            catch (Exception e)
            {
                SendMailSSL.SendErrorToAdmin("Error at LoadCartDetailsModel: " + e.ToString());
            }
            return(cart);
        }
Exemple #9
0
        public string RemoveAProductInCart(int IDBill, int productID)
        {
            string prodName = null;

            try
            {
                bool result = productsInBillRepo.RemoveAProductInBill(IDBill, productID);
                if (result)
                {
                    prodName = productRepo.GetProductName(productID);
                    UpdateBill(IDBill);
                    LoadCartDetailsModel(IDBill);
                }
            }
            catch (Exception ex)
            {
                SendMailSSL.SendErrorToAdmin("Error at RemoveAProductInCart: " + ex.ToString());
            }
            return(prodName);
        }
        public async Task <ActionResult> AuthorizeCaptureOrderPaypal()
        {
            CartDetailsModel cart = (CartDetailsModel)Session["CartDetails"];

            try
            {
                if (CheckProductInCart(cart))
                {
                    //neu on het thi tien hanh thanh toan
                    string orderId = Request["token"];
                    var    requestAuthorization = new OrdersAuthorizeRequest(orderId);
                    requestAuthorization.Prefer("return=representation");
                    requestAuthorization.RequestBody(new AuthorizeRequest());
                    var responseAuthorization = await PayPalClient.client().Execute(requestAuthorization);

                    var resultAuthorization = responseAuthorization.Result <Order>();
                    if (resultAuthorization.Status.Equals("COMPLETED"))
                    {
                        //if success
                        //get authorizationID
                        string AuthorizationId = resultAuthorization.PurchaseUnits[0].Payments.Authorizations[0].Id;
                        var    requestCapture  = new AuthorizationsCaptureRequest(AuthorizationId);
                        requestCapture.Prefer("return=representation");
                        requestCapture.RequestBody(new CaptureRequest());
                        //execute capture
                        var responseCapture = await PayPalClient.client().Execute(requestCapture);

                        var captureOrderResult = responseCapture.Result <PayPalCheckoutSdk.Payments.Capture>();
                        if (captureOrderResult.Status.Equals("PENDING"))//success
                        {
                            //check lai xem con hang trong kho k sau khi customer da thanh toan xong
                            //neu k thi refund tien r tra ve trang shopping-cart
                            if (!CheckProductInCart(cart))
                            {
                                await RefundOrderPaypal(captureOrderResult.Id);

                                return(RedirectToAction("LoadCartDetails", "Cart"));
                            }
                            else
                            {
                                return(FinishOrderPaypal(cart));
                            }
                        }
                        else
                        {
                            TempData["ErrorOrder"] = "Error to order by PayPal!!";
                            return(RedirectToAction("LoadCartDetails", "Cart"));
                        }
                    }
                    else
                    {
                        TempData["ErrorOrder"] = "Error to order by PayPal!!";
                        return(RedirectToAction("LoadCartDetails", "Cart"));
                    }
                }
                else
                {
                    TempData["ErrorOrder"] = "Error to order by PayPal!!";
                    return(RedirectToAction("LoadCartDetails", "Cart"));
                }
            }
            catch (Exception e)
            {
                SendMailSSL.SendErrorToAdmin("Error at AuthorizeCaptureOrderPaypal: " + e.ToString());
            }
            return(RedirectToAction("LoadCartDetails", "Cart"));
        }