示例#1
0
        public ActionResult PaymentWithPaypal(string cancel = null)
        {
            var apiContext = PaypalConfiguration.GetApiContext();

            try
            {
                string payerId = Request.Params["PayerID"];
                if (string.IsNullOrEmpty(payerId))
                {
                    var baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + "Paypal/PaymentWithPaypal?";

                    var guid = Convert.ToString(new Random().Next(100000));

                    var createdPayment = CreatePayment(apiContext, baseUrl + "guid=" + guid);

                    var links = createdPayment.links.GetEnumerator();

                    string paypalRedirectUrl = null;
                    while (links.MoveNext())
                    {
                        var link = links.Current;
                        if (link.rel.ToLower().Trim().Equals("approval_url"))
                        {
                            paypalRedirectUrl = link.href;
                        }
                    }

                    Session.Add(guid, createdPayment.id);
                    return(Redirect(paypalRedirectUrl));
                }
                else
                {
                    var guid            = Request.Params["guid"];
                    var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);
                    if (executedPayment.state.ToLower() != "approved")
                    {
                        return(View("failed"));
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return(View("success"));
        }