예제 #1
0
 public ActionResult Register(Customer cust)
 {
     if (ModelState.IsValid)
     {
         var check = db.Customers.FirstOrDefault(c => c.UserName == cust.UserName);
         if (check == null)
         {
             cust.ConfirmEmail = false;
             db.Customers.Add(cust);
             db.SaveChanges();
             TempShpData.UserID = GetUser(cust.UserName).CustomerID;
             SendMail.GuiEmail("Đăng ký tài khoản", cust.Email, string.Format("Dear {0} <br/> Thank you for your registration, please click on the  below link to complete your registration: <a href =\"{1}\"  title =\"User Email Confirm\">{1}</a>", cust.First_Name + cust.Last_Name, Url.Action("ConfirmEmail", "Account", new { Token = cust.CustomerID, Email = cust.Email }, Request.Url.Scheme)));
             return(RedirectToAction("Confirm", "Account", new { Email = cust.Email }));
         }
         ModelState.AddModelError("Register", "Tài khoản đã tồn tại");
         return(View("Login", cust));
     }
     return(View("Login", cust));
 }
예제 #2
0
        //PLACE ORDER--LAST STEP
        public ActionResult PlaceOrder(FormCollection getCheckoutDetails)
        {
            if (Convert.ToInt32(getCheckoutDetails["PayMethod"]) == 1)
            {
                int shpID = 1;
                if (db.ShippingDetails.Count() > 0)
                {
                    shpID = db.ShippingDetails.Max(x => x.ShippingID) + 1;
                }
                int payID = 1;
                if (db.Payments.Count() > 0)
                {
                    payID = db.Payments.Max(x => x.PaymentID) + 1;
                }
                int orderID = 1;
                if (db.Orders.Count() > 0)
                {
                    orderID = db.Orders.Max(x => x.OrderID) + 1;
                }



                ShippingDetail shpDetails = new ShippingDetail();
                shpDetails.ShippingID = shpID;
                shpDetails.FirstName  = getCheckoutDetails["FirstName"];
                shpDetails.LastName   = getCheckoutDetails["LastName"];
                shpDetails.Email      = getCheckoutDetails["Email"];
                shpDetails.Mobile     = getCheckoutDetails["Mobile"];
                shpDetails.Address    = getCheckoutDetails["Address"];
                shpDetails.Province   = getCheckoutDetails["Province"];
                shpDetails.City       = getCheckoutDetails["City"];
                shpDetails.PostCode   = getCheckoutDetails["PostCode"];
                db.ShippingDetails.Add(shpDetails);
                db.SaveChanges();

                Payment pay = new Payment();
                pay.PaymentID = payID;
                pay.Type      = Convert.ToInt32(getCheckoutDetails["PayMethod"]);
                db.Payments.Add(pay);
                db.SaveChanges();

                Order o = new Order();
                o.OrderID     = orderID;
                o.CustomerID  = TempShpData.UserID;
                o.PaymentID   = payID;
                o.ShippingID  = shpID;
                o.Discount    = Convert.ToInt32(getCheckoutDetails["discount"]);
                o.TotalAmount = Convert.ToInt32(getCheckoutDetails["totalAmount"]);
                o.isCompleted = true;
                o.OrderDate   = DateTime.Now;
                db.Orders.Add(o);
                db.SaveChanges();

                foreach (var OD in TempShpData.items)
                {
                    OD.OrderID = orderID;
                    OD.Order   = db.Orders.Find(orderID);
                    OD.Product = db.Products.Find(OD.ProductID);
                    db.OrderDetails.Add(OD);
                    db.SaveChanges();
                }
                SendMail.GuiEmail("Xác nhận đặt hàng thành công", getCheckoutDetails["Email"], "Cảm ơn bạn đã đặt hàng, mã đơn hàng của bạn là: " + o.OrderID);

                return(RedirectToAction("Index", "ThankYou"));
            }
            //Paypal
            else
            {
                PayPal.Api.APIContext apiContext = Configuration.GetAPIContext();
                try
                {
                    string payerId = Request.Params["PayerID"];
                    if (string.IsNullOrEmpty(payerId))
                    {
                        int shpID = 1;
                        if (db.ShippingDetails.Count() > 0)
                        {
                            shpID = db.ShippingDetails.Max(x => x.ShippingID) + 1;
                        }
                        int payID = 1;
                        if (db.Payments.Count() > 0)
                        {
                            payID = db.Payments.Max(x => x.PaymentID) + 1;
                        }
                        int orderID = 1;
                        if (db.Orders.Count() > 0)
                        {
                            orderID = db.Orders.Max(x => x.OrderID) + 1;
                        }



                        ShippingDetail shpDetails = new ShippingDetail();
                        shpDetails.ShippingID = shpID;
                        shpDetails.FirstName  = getCheckoutDetails["FirstName"];
                        shpDetails.LastName   = getCheckoutDetails["LastName"];
                        shpDetails.Email      = getCheckoutDetails["Email"];
                        shpDetails.Mobile     = getCheckoutDetails["Mobile"];
                        shpDetails.Address    = getCheckoutDetails["Address"];
                        shpDetails.Province   = getCheckoutDetails["Province"];
                        shpDetails.City       = getCheckoutDetails["City"];
                        shpDetails.PostCode   = getCheckoutDetails["PostCode"];
                        db.ShippingDetails.Add(shpDetails);
                        db.SaveChanges();

                        Payment pay = new Payment();
                        pay.PaymentID = payID;
                        pay.Type      = Convert.ToInt32(getCheckoutDetails["PayMethod"]);
                        db.Payments.Add(pay);
                        db.SaveChanges();

                        Order o = new Order();
                        o.OrderID     = orderID;
                        o.CustomerID  = TempShpData.UserID;
                        o.PaymentID   = payID;
                        o.ShippingID  = shpID;
                        o.Discount    = Convert.ToInt32(getCheckoutDetails["discount"]);
                        o.TotalAmount = Convert.ToInt32(getCheckoutDetails["totalAmount"]);
                        o.isCompleted = true;
                        o.OrderDate   = DateTime.Now;
                        db.Orders.Add(o);
                        db.SaveChanges();

                        string tenSp = "";
                        foreach (var OD in TempShpData.items)
                        {
                            OD.OrderID = orderID;
                            OD.Order   = db.Orders.Find(orderID);
                            OD.Product = db.Products.Find(OD.ProductID);
                            tenSp     += db.Products.Find(OD.ProductID).Name + ", ";
                            db.OrderDetails.Add(OD);
                            db.SaveChanges();
                        }
                        SendMail.GuiEmail("Xác nhận đặt hàng thành công", getCheckoutDetails["Email"], "Cảm ơn bạn đã đặt hàng, mã đơn hàng của bạn là: " + o.OrderID);

                        //this section will be executed first because PayerID doesn't exist
                        //it is returned by the create function call of the payment class
                        // Creating a payment
                        // baseURL is the url on which paypal sendsback the data.
                        // So we have provided URL of this controller only
                        string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/CheckOut/PlaceOrder?";
                        //guid we are generating for storing the paymentID received in  session
                        //after calling the create function and it is used in the payment execution
                        var guid = Convert.ToString((new Random()).Next(100000));
                        //CreatePayment function gives us the payment approval url
                        //on which payer is redirected for paypal account payment
                        var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid, tenSp, o.TotalAmount);
                        //get links returned from paypal in response to Create function  call
                        var    links             = createdPayment.links.GetEnumerator();
                        string paypalRedirectUrl = null;
                        while (links.MoveNext())
                        {
                            PayPal.Api.Links lnk = links.Current;
                            if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                            {
                                //saving the payapalredirect URL to which user will be redirected for payment
                                paypalRedirectUrl = lnk.href;
                            }
                        }
                        // saving the paymentID in the key guid
                        Session.Add(guid, createdPayment.id);
                        return(Redirect(paypalRedirectUrl));
                    }
                    else
                    {
                        // This section is executed when we have received all the payments parameters
                        // from the previous call to the function Create
                        // Executing a payment
                        var guid            = Request.Params["guid"];
                        var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);
                        if (executedPayment.state.ToLower() != "approved")
                        {
                            return(View("FailureView"));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log("Error" + ex.Message);
                    return(View("FailureView"));
                }

                return(RedirectToAction("Index", "ThankYou"));
            }
        }