public string MakePaymentByPaypal(List <Product> products)
        {
            var paypalUrlRedirect = _payPalPayments.RedirectToPayPal(products);

            return(paypalUrlRedirect);
        }
Пример #2
0
        public ActionResult PostToPaypal(FormCollection forms)
        {
            var emailRegEx = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";

            if (forms["buyerEmail"] == null || !Regex.IsMatch(forms["buyerEmail"], emailRegEx))
            {
                ModelState.AddModelError("", "Your email is not in correct format");
            }
            if (System.Web.HttpContext.Current.Session["ShoppingBasket"] == null)
            {
                return(RedirectToAction("Basket", "Shopping"));
            }
            var shoppingBasket = (IList <SHOP_PRODS>)System.Web.HttpContext.Current.Session["ShoppingBasket"];

            var upaProducts = shoppingBasket.Select(item => new { ProductPrice = item.prodPrice, ProductDescription = item.prodDesc, ProductName = item.prodName }).ToList();
            var totalAmount = shoppingBasket.Sum(item => item.prodPrice);

            if (!(totalAmount > 0))
            {
                ModelState.AddModelError("", "Gross total should be more than $0.00");
            }

            if (!ModelState.IsValid)
            {
                return(View("Payment"));
            }
            var order = new Order();

            order.email       = forms["email"];
            order.order_date  = DateTime.Now;
            order.username    = User.Identity.Name;
            order.status      = "Unpaid";
            order.order_gross = totalAmount;

            _repositoryServices.SaveOrUpdateOrders(order);

            System.Web.HttpContext.Current.Session["InvoiceNo"]   = order.orderId;
            System.Web.HttpContext.Current.Session["ProductsUPA"] = upaProducts.Select(p => new UPAEventsPayPal.Product {
                ProductDescription = p.ProductDescription, ProductName = p.ProductName, Ammount = p.ProductPrice, Quantity = 1
            }).ToList();
            System.Web.HttpContext.Current.Session["buyerEmail"] = order.email;

            foreach (var product in shoppingBasket)
            {
                _repositoryServices.SaveOrUpdateItemOrders(new ItemOrder {
                    numberOrdered = 1, order_id_fk = order.orderId, product_name = product.prodName, username = User.Identity.Name
                });
            }
            var context = HttpContext;
            //Process Payment
            var paypal = new PayPalHandler(context.ApplicationInstance.Context.Session, System.Configuration.ConfigurationManager.AppSettings["PaypalBaseUrl"],
                                           System.Configuration.ConfigurationManager.AppSettings["BusinessEmail"],
                                           System.Configuration.ConfigurationManager.AppSettings["SuccessUrl"],
                                           System.Configuration.ConfigurationManager.AppSettings["CancelUrl"],
                                           System.Configuration.ConfigurationManager.AppSettings["NotifyUrl"]);

            paypal.Response = context.ApplicationInstance.Context.Response;


            paypal.RedirectToPayPal();
            return(View("PaymentMade"));
        }
        public ActionResult BookTickets(TicketViewModel model)
        {
            ViewBag.Title = "Book Tickets";
            PopulateUIEventsView();

            int eventId = -1;

            if (!int.TryParse(model.EventName, out eventId) || model.NumberOfTickets < 1 || model.Price <= (decimal)0.0 || model.TotalPrice <= (decimal)0.00)
            {
                ModelState.AddModelError("transactionVoid", "Price, Number of tickets required");
            }
            try
            {
                if (ModelState.IsValid)
                {
                    eventId = -1;
                    if (int.TryParse(model.EventName, out eventId))
                    {
                        var evnt = _repositoryTicketServices.GetEventById(eventId);
                        var user = _repositoryTicketServices.GetUserByName(User.Identity.Name);
                        if (evnt == null || eventId == 1)
                        {
                            ModelState.AddModelError("chooseEvent", "Event needs to be chosen");
                            return(View("BookTickets", model));
                        }
                        var ticket = new Ticket
                        {
                            EventId    = eventId,
                            Price      = model.Price,
                            TicketGUID = Guid.NewGuid()
                        };

                        var bookingId     = _repositoryTicketServices.BookTickets(ticket, model.NumberOfTickets, user.UserId);
                        var paypalBaseUrl = ConfigurationManager.AppSettings["PaypalBaseUrl"];
                        var cancelUrl     = ConfigurationManager.AppSettings["CancelUrl"];
                        var successUrl    = ConfigurationManager.AppSettings["SuccessUrl"];
                        var notifyUrl     = ConfigurationManager.AppSettings["NotifyUrl"];
                        var businessEmail = ConfigurationManager.AppSettings["BusinessEmail"];
                        var customer      = _repositoryTicketServices.GetUserByName(User.Identity.Name);

                        var buyerEmail = customer.Email;
                        var product    = new Product
                        {
                            Ammount            = model.Price,
                            ProductDescription = evnt.EventDescription,
                            ProductName        = evnt.EventName,
                            Quantity           = model.NumberOfTickets,
                            VATAmmount         = 0
                        };
                        var products = new List <Product> {
                            product
                        };
                        Session["ShoppingBasket"] = products;
                        var upaProducts = products;

                        Session["InvoiceNo"]   = bookingId;
                        Session["ProductsUPA"] = upaProducts;
                        Session["buyerEmail"]  = buyerEmail;

                        var context = HttpContext;
                        //Process Payment
                        var paypal = new PayPalHandler(context.ApplicationInstance.Context.Session,
                                                       paypalBaseUrl, businessEmail, successUrl, cancelUrl, notifyUrl);

                        paypal.Response = context.ApplicationInstance.Context.Response;


                        paypal.RedirectToPayPal();
                        return(View("BookedSuccess"));
                    }
                }
                else
                {
                    return(View(model));
                }
            }
            catch (Exception e)
            {
            }
            return(View("BookTickets", model));
        }