예제 #1
0
 public ActionResult Payment(Data.Models.Order orderInput, string YourRadioButton)
 {
     if (YourRadioButton == "cod")
     {
         return(RedirectToAction("PaymentWithCOD", orderInput));
     }
     else
     {
         return(RedirectToAction("PaymentWithPaypal", orderInput));
     }
 }
예제 #2
0
 public ActionResult PaymentWithCOD(Data.Models.Order orderInput)
 {
     try
     {
         var order = new Data.Models.Order()
         {
             CreatedDate     = DateTime.Now,
             PaymentMethod   = "COD",
             ShipAddress     = orderInput.ShipAddress,
             ShipEmail       = orderInput.ShipEmail,
             ShipMobile      = orderInput.ShipMobile,
             ShipDescription = orderInput.ShipDescription,
             Status          = 0,
             ShipName        = orderInput.ShipName
         };
         var insert     = new OrdersDao().Insert(order);
         var detailsDao = new OrderDetailsDao();
         if (insert)
         {
             foreach (var cart in CommonConstants.listCart)
             {
                 var orderDetail = new OrderDetails
                 {
                     ProductID = cart.Product.ID,
                     OrderID   = order.ID,
                     Price     = cart.Product.Price - cart.Product.Price * cart.Product.Discount / 100,
                     Quantity  = cart.Quantity
                 };
                 detailsDao.Insert(orderDetail);
             }
         }
         CommonConstants.listCart.Clear();
         Session.Remove("CartSession");
         return(RedirectToAction("Success", "Cart"));
     }
     catch
     {
         return(RedirectToAction("Index", "Cart"));
     }
 }
예제 #3
0
        public ActionResult PaymentWithPaypal(Data.Models.Order orderInput)
        {
            APIContext apiContext = Configuration.GetAPIContext();

            try
            {
                string payerId = Request.Params["PayerID"];
                if (String.IsNullOrEmpty(payerId))
                {
                    var profile = new WebProfile
                    {
                        name         = Guid.NewGuid().ToString(),
                        presentation = new Presentation
                        {
                            brand_name  = "ELEX Store",
                            locale_code = "US"
                        },
                        input_fields = new InputFields
                        {
                            no_shipping = 1
                        }
                    };
                    var createdProfile = profile.Create(apiContext);

                    string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority +
                                     "/Paypal/PaymentWithPayPal?";
                    var guid = Convert.ToString((new Random()).Next(100000));
                    baseURI += "guid=" + guid + "&webProfileId=" + createdProfile.id;
                    Session["webProfileId"] = createdProfile.id;
                    var    createdPayment    = this.CreatePayment(apiContext, baseURI + "guid=" + guid, createdProfile);
                    var    links             = createdPayment.links.GetEnumerator();
                    string paypalRedirectUrl = null;

                    while (links.MoveNext())
                    {
                        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;
                        }
                    }
                    Session.Add(guid, createdPayment.id);
                    Session["OrderInput"] = orderInput;
                    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 webProfileId             = Session["webProfileId"].ToString();
                    Data.Models.Order orderModel = (Data.Models.Order)Session["OrderInput"];
                    try
                    {
                        var order = new Data.Models.Order()
                        {
                            CreatedDate     = DateTime.Now,
                            PaymentMethod   = "PayPal",
                            ShipAddress     = orderModel.ShipAddress,
                            ShipEmail       = orderModel.ShipEmail,
                            ShipMobile      = orderModel.ShipMobile,
                            ShipDescription = orderModel.ShipDescription,
                            Status          = 0,
                            ShipName        = orderModel.ShipName
                        };
                        var insert     = new OrdersDao().Insert(order);
                        var detailsDao = new OrderDetailsDao();
                        if (insert)
                        {
                            foreach (var cart in CommonConstants.listCart)
                            {
                                var orderDetail = new OrderDetails
                                {
                                    ProductID = cart.Product.ID,
                                    OrderID   = order.ID,
                                    Price     = cart.Product.Price - cart.Product.Price * cart.Product.Discount / 100,
                                    Quantity  = cart.Quantity
                                };
                                detailsDao.Insert(orderDetail);
                            }
                        }
                        var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);

                        WebProfile.Delete(apiContext, webProfileId);
                        Session.Remove("webProfileId");
                        CommonConstants.listCart.Clear();
                        Session["CartSession"] = CommonConstants.listCart;
                        if (executedPayment.state.ToLower() != "approved")
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                    catch (Exception)
                    {
                        return(RedirectToAction("Index", "Cart"));
                    }
                }
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Index", "Home"));
            }


            return(RedirectToAction("Success", "Cart"));
        }