Ejemplo n.º 1
0
        public ActionResult PaymentWithPaypal()
        {
            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 +
                                     "/Carts/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 productList    = (List <Cart>)Session["CartItems"];
                    var createdPayment = PaymentDataService.CreatePayment(apiContext, baseURI + "guid=" + guid, productList);

                    //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 = PaymentDataService.ExecutePayment(apiContext, payerId, Session[guid] as string);

                    //Save Customer
                    PaymentDataService.SaveCustomerInfo(executedPayment, _userID);

                    //Mark cartitems as paid
                    var cartItems = _cartDataService.GetCartItems(_userID);
                    _cartDataService.UpdateCartItems(cartItems);

                    //Save Successful Order
                    PaymentDataService.SaveOrder(executedPayment, _userID);

                    if (executedPayment.state.ToLower() != "approved")
                    {
                        return(View("Not_Approved"));
                    }
                }
            }
            catch (Exception ex)
            {
                var error = ex;// Logger.log("Error" + ex.Message);
                return(View("FailureView"));
            }

            return(View("Successful_Payment"));
        }
Ejemplo n.º 2
0
 public PaymentController(PaymentDataService dataService)
 {
     _paymentDS = dataService;
 }