Example #1
0
        public ActionResult PaymentWithPaypal()
        {
            APIContext apiContext = PaypalConfiguration.GetAPIContext();

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

                if (string.IsNullOrEmpty(payerId))
                {
                    string baseURI           = Request.Url.Scheme + "://" + Request.Url.Authority + "/Checkout/PaymentWithPayPal?";
                    var    guid              = Convert.ToString((new Random()).Next(100000));
                    var    model             = _checkOutService.GetSessionOrder(User.Identity.GetUserId(), Langs.English, EShop.Common.Currency.Dollar);
                    var    createdPayment    = _paypalService.CreatePayment(apiContext, baseURI + "guid=" + guid, model);
                    var    links             = createdPayment.links.GetEnumerator();
                    string paypalRedirectUrl = null;
                    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
                {
                    var guid            = Request.Params["guid"];
                    var executedPayment = _paypalService.ExecutePayment(apiContext, payerId, Session[guid] as string, ref payment);
                    if (executedPayment.state.ToLower() != "approved")
                    {
                        return(View("FailureView"));
                    }
                    else
                    {
                        //var model = Session["SubscriptionModel"] as PaypalTransactionViewModel;
                        //// SUCCESSFUL TRANSACTION
                        //SubscriptionInvoice inv = new SubscriptionInvoice()
                        //{
                        //    Amount = model.Amount,
                        //    CycleStartDate = DateTime.Now,
                        //    CycleEndDate = DateTime.Now.AddMonths(1),
                        //    Description = model.Description,
                        //    InvoiceDate = model.InvoiceDate,
                        //    InvoiceNo = model.InvoiceNo,
                        //    ItemCurrency = model.Items[0].Currency,
                        //    ItemDescription = model.Items[0].Description,
                        //    ItemName = model.Items[0].Name,
                        //    ItemPrice = model.Items[0].Price,
                        //    ItemQuantity = model.Items[0].Quantity,
                        //    Paid = true,
                        //    PayerID = executedPayment.payer.payer_info.payer_id,
                        //    PaymentId = executedPayment.id,
                        //    PaymentToken = executedPayment.token,
                        //    TransactionId = executedPayment.transactions[0].related_resources[0].sale.id,
                        //    UserId = model.AppUser.Id,
                        //    PayPalJSON = executedPayment.ConvertToJson()
                        //};
                        //_unitOfWork.SubscriptionInvoiceRepository.Insert(inv);
                        //_unitOfWork.Save();
                        //var user = UserManager.FindById(User.Identity.GetUserId());
                        //user.IsPremium = true;
                        //user.PremiumExpiryDate = inv.CycleEndDate;
                        //UserManager.Update(user);
                        //return View("SuccessView", inv);
                        return(View("SuccessView"));
                    }
                }
            }
            catch (PayPal.PaymentsException ex)
            {
                PaypalLogger.Log("Error" + ex.Details.ConvertToJson());
                return(View("FailureView"));
            }
            catch (Exception ex)
            {
                PaypalLogger.Log("Error" + ex.Message);
                return(View("FailureView"));
            }
            //return View("SuccessView");
        }