protected void btnPaypalExpress_Click(object sender, EventArgs e)
        {
            if ((NopContext.Current.User == null) || (NopContext.Current.User.IsGuest && !CustomerManager.AnonymousCheckoutAllowed))
            {
                string loginURL = SEOHelper.GetLoginPageUrl(true);
                Response.Redirect(loginURL);
            }

            var payPalExpress = new PayPalExpressPaymentProcessor();
            var ppePaymentMethod = PaymentMethodManager.GetPaymentMethodBySystemKeyword("PayPalExpress");
            if (ppePaymentMethod != null && ppePaymentMethod.IsActive)
            {
                //apply reward points
                CheckoutPaymentMethodControl checkoutPaymentMethodControl = CommonHelper.FindControlRecursive<CheckoutPaymentMethodControl>(this.Page.Controls);
                if (checkoutPaymentMethodControl != null)
                    checkoutPaymentMethodControl.ApplyRewardPoints();

                //payment
                var cart = ShoppingCartManager.GetCurrentShoppingCart(ShoppingCartTypeEnum.ShoppingCart);
                decimal? cartTotal = ShoppingCartManager.GetShoppingCartTotal(cart,
                    ppePaymentMethod.PaymentMethodId, NopContext.Current.User);
                if (cartTotal.HasValue && cartTotal.Value > decimal.Zero)
                {
                    string expressCheckoutURL = payPalExpress.SetExpressCheckout(cartTotal.Value,
                        CommonHelper.GetStoreLocation(false) + "paypalexpressreturn.aspx",
                        CommonHelper.GetStoreLocation(false));
                    Response.Redirect(expressCheckoutURL);
                }
            }
        }
        protected void btnNextStep_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    PayPalExpressPaymentProcessor payPalExpress = new PayPalExpressPaymentProcessor();
                    string token = CommonHelper.QueryString("token");
                    PaypalPayer payer = payPalExpress.GetExpressCheckout(token);
                    if (string.IsNullOrEmpty(payer.PayerID))
                        throw new NopException("Payer ID is not set");

                    PaymentInfo paymentInfo = new PaymentInfo();

                    PaymentMethod paypalExpressPaymentMethod = PaymentMethodManager.GetPaymentMethodBySystemKeyword("PayPalExpress");

                    paymentInfo.PaymentMethodId = paypalExpressPaymentMethod.PaymentMethodId;
                    paymentInfo.BillingAddress = NopContext.Current.User.BillingAddress;
                    paymentInfo.ShippingAddress = NopContext.Current.User.ShippingAddress;
                    paymentInfo.PaypalPayerId = payer.PayerID;
                    paymentInfo.PaypalToken = token;
                    paymentInfo.CustomerLanguage = NopContext.Current.WorkingLanguage;
                    paymentInfo.CustomerCurrency = NopContext.Current.WorkingCurrency;

                    int orderId = 0;
                    string result = OrderManager.PlaceOrder(paymentInfo, NopContext.Current.User, out orderId);

                    Order order = OrderManager.GetOrderById(orderId);
                    if (!String.IsNullOrEmpty(result))
                    {
                        lConfirmOrderError.Text = Server.HtmlEncode(result);
                        btnNextStep.Visible = false;
                        return;
                    }
                    else
                        PaymentManager.PostProcessPayment(order);
                    Response.Redirect("~/checkoutcompleted.aspx");
                }
                catch (Exception exc)
                {
                    LogManager.InsertLog(LogTypeEnum.OrderError, exc.Message, exc);
                    lConfirmOrderError.Text = Server.HtmlEncode(exc.ToString());
                    btnNextStep.Visible = false;
                }
            }
        }
        protected void btnPaypalExpress_Click(object sender, EventArgs e)
        {
            if ((NopContext.Current.User == null) || (NopContext.Current.User.IsGuest && !CustomerManager.AnonymousCheckoutAllowed))
            {
                string loginURL = CommonHelper.GetLoginPageURL(true);
                Response.Redirect(loginURL);
            }

            PayPalExpressPaymentProcessor payPalExpress = new PayPalExpressPaymentProcessor();

            //todo pass payment method identifier if paypal express requires additional payment fee
            decimal? cartTotal = ShoppingCartManager.GetShoppingCartTotal(Cart, NopContext.Current.User);
            if (cartTotal.HasValue)
            {
                string expressCheckoutURL = payPalExpress.SetExpressCheckout(cartTotal.Value,
                    CommonHelper.GetStoreLocation(false) + "PaypalExpressReturn.aspx",
                    CommonHelper.GetStoreLocation(false) + "Default.aspx");
                Response.Redirect(expressCheckoutURL);
            }
        }
        protected override void OnInit(EventArgs e)
        {
            PayPalExpressPaymentProcessor payPalExpress = new PayPalExpressPaymentProcessor();
            try
            {
                token = CommonHelper.QueryString("token");
                payer = payPalExpress.GetExpressCheckout(token);
                if (string.IsNullOrEmpty(payer.PayerID))
                    throw new NopException("Payer ID is not set");

                OrderSummaryControl.VendorId = int.Parse(payer.VendorId);
                OrderSummaryControl.BindData();
            }
            catch (Exception Ex)
            {
                LogService.InsertLog(LogTypeEnum.OrderError, Ex.Message, String.Empty);
            }
            finally
            {
                base.OnInit(e);
            }
        }
        protected void btnNextStep_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {

                var payPalExpress = new PayPalExpressPaymentProcessor();
                var ppePaymentMethod = this.PaymentService.GetPaymentMethodBySystemKeyword("PayPalExpress");
                if (ppePaymentMethod != null && ppePaymentMethod.IsActive)
                {
                    //apply reward points
                    //CheckoutPaymentMethodControl checkoutPaymentMethodControl = CommonHelper.FindControlRecursive<CheckoutPaymentMethodControl>(this.Page.Controls);
                    //if (checkoutPaymentMethodControl != null)
                    //    checkoutPaymentMethodControl.ApplyRewardPoints();

                    //payment
                    var cart = this.ShoppingCartService.GetCurrentShoppingCart(ShoppingCartTypeEnum.ShoppingCart, vendorId);
                    decimal? cartTotal = this.ShoppingCartService.GetShoppingCartTotal(cart,
                        ppePaymentMethod.PaymentMethodId, NopContext.Current.User);

                    //TODO: If some of these are blank something is very wrong and needs to be addressed.
                    if (cartTotal.HasValue && cartTotal.Value > decimal.Zero)
                    {
                        string expressCheckoutURL = payPalExpress.SetExpressCheckout(cart, cartTotal.Value,
                            this.SettingManager.GetSettingValue("PaymentMethod.PaypalExpress.ReturnURL"),
                            this.SettingManager.GetSettingValue("PaymentMethod.PaypalExpress.CancelURL"),
                            this.SettingManager.GetSettingValue("PaymentMethod.PaypalExpress.NotifyURL"),
                            this.txtNoteToSeller.InnerText.Trim());
                        Response.Redirect(expressCheckoutURL);
                    }
                }
            }
            else
            {
                foreach (System.Web.UI.IValidator poo in Page.Validators)
                {
                    if (!poo.IsValid)
                    {
                        if (lConfirmOrderError.Text == String.Empty)
                        {
                            lConfirmOrderError.Text = poo.ErrorMessage;
                        }
                        else
                        {
                            lConfirmOrderError.Text += poo.ErrorMessage;
                        }
                    }
                }
            }
        }