Exemple #1
0
        public ActionResult PaymentWithPaypal()
        {
            Reservation reservation  = (Reservation)Session[strReservation];
            Reservation reservation1 = new Reservation
            {
                ArrivalDate   = reservation.ArrivalDate,
                DepartureDate = reservation.DepartureDate,
                RoomId        = reservation.RoomId,
                UserId        = reservation.UserId,
            };

            APIContext apiContext = PaypalConfiguration.GetAPIContext();

            try
            {
                string payerId = Request.Params["PayerID"];
                if (string.IsNullOrEmpty(payerId))
                {
                    string baseURI        = Request.Url.Scheme + "://" + Request.Url.Authority + "/reservations/paymentwithpaypal?";
                    var    guid           = Convert.ToString((new Random()).Next(100000));
                    var    createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid);

                    var    links             = createdPayment.links.GetEnumerator();
                    string paypalRedirectUrl = string.Empty;
                    while (links.MoveNext())
                    {
                        Links lnk = links.Current;
                        if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                        {
                            paypalRedirectUrl = lnk.href;
                        }
                    }
                    Session.Add(guid, createdPayment.id);
                    return(Redirect(paypalRedirectUrl));
                }
                else
                {
                    //this one will be executed when we have received all the payment params from previous call
                    var guid           = Request.Params["guid"];
                    var executePayment = ExecutePayment(apiContext, payerId, Session[guid] as string);
                    if (executePayment.state.ToLower() != "approved")
                    {
                        return(View("Failure"));
                    }
                }
            }
            catch (Exception ex)
            {
                PaypalLogger.Log("Error: " + ex.Message);
                return(View("Failure"));
            }
            _reservationsRepository.AddReservation(reservation1);
            return(View("Success"));
        }