// Creamos el metodo Payment con Pay pal
        public ActionResult PaymentWithPaypal()
        {
            //Contexto de gettings de las bases de paypal en clientId y clientSecret del pago
            APIContext apiContext = PaypalConfiguration.GetAPIContext();

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


                    //se obtiene los enlaces devueltos de la respuesta de PayPal para crear la función de las llamadas
                    var    links             = createdPayment.links.GetEnumerator();
                    string paypalRedirectUrl = string.Empty;

                    while (links.MoveNext())
                    {
                        Links link = links.Current;
                        if (link.rel.ToLower().Trim().Equals("approval_url"))
                        {
                            paypalRedirectUrl = link.href;
                        }
                    }
                    Session.Add(guid, createdPayment.id);
                    return(Redirect(paypalRedirectUrl));
                }
                else
                {
                    //este se ejecutará cuando hayamos recibido todos los pagos de la llamada anterior
                    var guid           = Request.Params["guid"];
                    var executePayment = ExecutePayment(apiContext, payerId, Session[guid] as string);

                    if (executePayment.state.ToLower() != "approved")
                    {
                        return(View("Fracaso"));
                    }
                }
            }
            catch (Exception ex)
            {
                PayPalLogger.Log("Error: " + ex.Message);
                return(View("Fracaso"));
            }

            return(View("satisfactori"));
        }
Beispiel #2
0
        public ActionResult PaymentWithPaypal() // OM: add order
        {
            // OM: get order from POST:/Checkout, if null show error view
            Models.Order order = TempData["Order"] as Models.Order;
            if (order == null)
            {
                return(View("FailureView"));
            }

            // getting the apiContext as earlier
            APIContext apiContext = Configuration.GetAPIContext();

            try
            {
                string payerId = Request.Params["PayerID"];

                if (string.IsNullOrEmpty(payerId))
                {
                    // 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 + "/Paypal/PaymentWithPayPal?";

                    // 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, order); // OM: add order

                    // get links returned from paypal in response to Create function call

                    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;
                        }
                    }

                    // 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)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                PayPalLogger.Log("Error" + ex.Message);
                return(View("FailureView"));
            }

            // OM: Update database Orders with current after payment is complete
            SaveOrder(order);

            TempData["RedirectedFromPayment"] = true; // Flag to check that user was redirected to Checkout/Complete. Prevents ability to refresh said page and send duplicate emails

            return(RedirectToAction("Complete", "Checkout", new { order.ID }));
        }
Beispiel #3
0
        public ActionResult PaymentWithPaypal(Receipt receipt, string Cancel = null)
        {
            //getting the apiContext
            APIContext apiContext = PaypalConfiguration.GetAPIContext();

            try
            {
                //A resource representing a Payer that funds a payment Payment Method as paypal
                //Payer Id will be returned when payment proceeds or click to pay
                string payerId = Request.Params["PayerID"];
                if (string.IsNullOrEmpty(payerId))
                {
                    //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.
                    string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority + "/ShoppingCart/PaymentWithPayPal?";
                    //here we are generating guid for storing the paymentID received in session
                    //which will be 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);
                    //get links returned from paypal in response to Create function call
                    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;
                        }
                    }
                    // saving the paymentID in the key guid
                    Session.Add(guid, createdPayment.id);
                    return(Redirect(paypalRedirectUrl));
                }
                else
                {
                    // This function exectues after receving all parameters for the payment
                    var guid            = Request.Params["guid"];
                    var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);
                    //If executed payment failed then we will show payment failure message to user
                    if (executedPayment.state.ToLower() != "approved")
                    {
                        return(View("FailureView"));
                    }
                    else
                    {
                        List <ShoppingCart> cart = (List <ShoppingCart>)Session["Cart"];
                        // gets the username of the current user
                        receipt.Email = System.Web.HttpContext.Current.User.Identity.GetUserName();
                        //gets and store the real time date.
                        receipt.Receipt_Date = DateTime.Now;
                        //get the infor of the current user using its email
                        var userInfo = (from user in db.Users
                                        where user.Email.Equals(receipt.Email)
                                        select new
                                        { user.FirstName, user.LastName, user.PhoneNumber, user.Address }).ToList();
                        //setting name equals to firstname and lastname
                        foreach (var user in userInfo)
                        {
                            receipt.FirstName   = user.FirstName;
                            receipt.LastName    = user.LastName;
                            receipt.Address     = user.Address;
                            receipt.PhoneNumber = user.PhoneNumber;
                        }
                        db.Receipts.Add(receipt);
                        db.SaveChanges();
                        foreach (ShoppingCart shopcart in cart)
                        {
                            OrderDetail orderDetail = new OrderDetail()
                            {
                                Receipt_Id  = receipt.Receipt_Id,
                                Picture_Id  = shopcart.Picture.Picture_Id,
                                Picture     = shopcart.Picture.Picture,
                                Title       = shopcart.Picture.Title,
                                Price       = shopcart.Picture.Price,
                                Description = shopcart.Picture.Description
                            };
                            db.OrderDetails.Add(orderDetail);
                            db.SaveChanges();
                        }
                        //remove all in the cart session
                        Session.Remove("Cart");
                    }
                }
            }
            catch (PayPal.PaymentsException ex)
            {
                PayPalLogger.Log("Error" + ex.Message);
                return(View("Failure"));
            }
            //on successful payment, show success page to user.
            return(View("Success"));
        }