//Complete Pay Pal Order
    protected void ibCompletePaypalOrder_Click(object sender, ImageClickEventArgs e)
    {
        //GetExpressCheckoutDetails - required if shipping goods or if we need the billing address

        /*
        APIWrapper ppGetDetails = new APIWrapper();
        APIWrapper.PayerInfo ppInfo = new APIWrapper.PayerInfo();
        ppInfo = ppGetDetails.GetExpressCheckout(
        */

        string token = Request.QueryString["token"];
        string payerId = Request.QueryString["PayerID"];

        //DoExpressCheckoutPayment
        PayPalManager payPal = new PayPalManager();
        PayPalInformation payPalInformation = new PayPalInformation();
        payPalInformation.Order = this.LiveFreeRangePage.CurrentOrder;

        // #HACK: WJ: sub null for items array below to get it working
        PayPalManager.OrderInfo ppOrderInfo = new PayPalManager.OrderInfo();

        //this call returns an order info object
        //double orderTotal = Convert.ToDouble(this.CommercePage.Basket.GetBaseTotalAfterDiscount());

        this.LiveFreeRangePage.CurrentOrder = new Orders();
        double orderTotal = Convert.ToDouble(this.LiveFreeRangePage.CurrentOrder.SubTotal);

        // this call actually processes the payment
        ppOrderInfo =  payPal.DoExpressCheckout(token, payPalInformation);

        string ack = ppOrderInfo.Ack; // says "Success" or "ERROR:..."
        string transactionId = ppOrderInfo.TransactionID;
        string receiptId = ppOrderInfo.ReceiptID;

        // handle Paypal web service response
        if (ack.IndexOf("ERROR") != -1)
        {
            //find error placeholder, show and add error message
            ContentPlaceHolder cph = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");

            Panel pnlOrderDeclined = (Panel)cph.FindControl("pnlOrderDeclined");
            pnlOrderDeclined.Visible = true;

            Label lblTransactionFailureMessage = (Label)cph.FindControl("lblTransactionFailureMessage");
            lblTransactionFailureMessage.Text = ack;

            // also need to show a credit card payment option - to do.
        }
        else
        {
            // Transaction successful: complete order!
            ProcessAddOrder addOrder = new ProcessAddOrder();
            addOrder.Orders = this.LiveFreeRangePage.CurrentOrder;

            try
            {
                addOrder.Invoke();
            }
            catch (Exception ex)
            {
                Response.Redirect("ErrorPage.aspx");
            }

            this.UpdateStockLevel();
            this.DeleteShoppingCart();

            // now forward to order completed page
            Response.Redirect("~/Web/CheckOutReceipt.aspx");
        }
    }
    protected void ibCompletePaypalOrder_Click(object sender, ImageClickEventArgs e)
    {
        Product[] products = new Product[gvShoppingCart.Rows.Count];
        foreach (GridViewRow row in gvShoppingCart.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                Product prod = new Product();

                DataKey data = gvShoppingCart.DataKeys[row.DataItemIndex];

                prod.ProductId = int.Parse(data.Values["ProductId"].ToString());

                Literal litProductName = (Literal)row.FindControl("litProductName");
                prod.Name = litProductName.Text;

                Literal litQuantity = (Literal)row.FindControl("litQuantity");
                prod.Quantity = int.Parse(litQuantity.Text);

                Literal litUnitPrice = (Literal)row.FindControl("litUnitPrice");
                litUnitPrice.Text = litUnitPrice.Text.Replace("£", "");
                prod.Price = Convert.ToDecimal(litUnitPrice.Text);

                products.SetValue(prod, row.DataItemIndex);
            }
        }

        CurrentOrder.OrderDetails.Products = products;

        //order total
        this.litTotal.Text = this.litTotal.Text.Replace("£", "");
        this.CurrentOrder.OrderTotal = Convert.ToDecimal(this.litTotal.Text);
        this.CurrentOrder.EndUserId = this.CurrentEndUser.EndUserId;

        string token = Request.QueryString["token"];
        string payerId = Request.QueryString["PayerID"];

        //DoExpressCheckoutPayment
        PayPalManager payPal = new PayPalManager();
        PayPalInformation payPalInformation = new PayPalInformation();
        payPalInformation.Order = this.CurrentOrder;
        payPalInformation.Order.PayerId = payerId;
        payPalInformation.Order.SubTotal = this.CurrentOrder.OrderTotal;

        // #HACK: WJ: sub null for items array below to get it working
        PayPalManager.OrderInfo ppOrderInfo = new PayPalManager.OrderInfo();

        //this call returns an order info object
        //double orderTotal = Convert.ToDouble(this.CommercePage.Basket.GetBaseTotalAfterDiscount());

        double orderTotal = Math.Round(Convert.ToDouble(base.CurrentOrder.SubTotal), 2);

        // this call actually processes the payment
        ppOrderInfo = payPal.DoExpressCheckout(token, payPalInformation);

        string ack = ppOrderInfo.Ack; // says "Success" or "ERROR:..."
        string transactionId = ppOrderInfo.TransactionID;
        string receiptId = ppOrderInfo.ReceiptID;
        this.CurrentOrder.TransactionId = transactionId;
        // handle Paypal web service response
        if (ack.IndexOf("ERROR") != -1)
        {
            //find error placeholder, show and add error message
            ContentPlaceHolder cph = (ContentPlaceHolder)Page.Master.FindControl("ContentPlaceHolder1");

            this.pnlOrderDeclined.Visible = true;

            this.lblTransactionFailureMessage.Text = ack;

            // also need to show a credit card payment option - to do.
        }
        else
        {
            // Transaction successful: complete order!
            ProcessAddOrder addOrder = new ProcessAddOrder();
            addOrder.Orders = this.CurrentOrder;

            try
            {
                addOrder.Invoke();
            }
            catch (Exception ex)
            {
                Response.Redirect("ErrorPage.aspx");
            }

            this.UpdateStockLevel();
            this.DeleteShoppingCart();

            // now forward to order completed page
            Response.Redirect("~/CheckOut/CheckOutReceipt.aspx?ec=true");
        }
    }
    private void SubmitOrder()
    {
        PayPalManager payPal = new PayPalManager();
        PayPalInformation _payPalInformation = new PayPalInformation();

        _payPalInformation.Order = this.CurrentOrder;
        payPal.ProcessDirectPayment(_payPalInformation);

        //if payment successful - add Order to database and display
        if (payPal.IsSubmissionSuccess)
        {
            this.pnlSuccess.Visible = true;
            this.litOrderTotal.Text = string.Format("{0:c}", _payPalInformation.Order.OrderTotal);
            this.litTransactionId.Text = this.CurrentOrder.TransactionId;

            ProcessAddOrder addOrder = new ProcessAddOrder();
            addOrder.Orders = this.CurrentOrder;

            //when the order is added to the db must update stock level.
            //and also delete the corresponding baskets.

            try
            {
                addOrder.Invoke();
            }
            catch(Exception ex)
            {
                Response.Redirect("ErrorPage.aspx");
            }

            this.UpdateStockLevel();
            this.DeleteShoppingCart();
        }
        else
        {
            this.pnlFailure.Visible = true;
            this.litErrorMessage.Text = payPal.SubmissionError;
        }
    }