using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Text;
using System.Configuration;
using System.Net;
using System.IO;

namespace QuintoCaminoV2.Buy
{
    public partial class BuyPayPal : System.Web.UI.Page
    {
        public string vchProducto;
        public decimal Costo;
        protected void Page_Load(object sender, EventArgs e)
        {

            vchProducto = Request.QueryString["Producto"];
            Costo = Convert.ToDecimal(Request.QueryString["Costo"]);

            redirect();
           // RedirectAndPOST(this);

        }


        public void redirect()
        {
            //string url = ConfigurationManager.AppSettings["PayPalSubmitUrl"];

            //StringBuilder postData = new StringBuilder();


            //postData.Append("cmd=" + HttpUtility.UrlEncode("_xclick") + "&");
            //postData.Append("business=" + HttpUtility.UrlEncode(ConfigurationManager.AppSettings["PayPalUsername"]) + "&");





            //postData.Append("item_name=" + HttpUtility.UrlEncode("Curso1") + "&");
            //postData.Append("amount=" + HttpUtility.UrlEncode("10.00") + "&");
            //postData.Append("return=" + HttpUtility.UrlEncode("http:\\/\\/localhost:17911/PDT/PDTSuccess.aspx") + "&");
            //postData.Append("custom=" + HttpUtility.UrlEncode(DateTime.Now.ToString()));

            ////ETC for all Form Elements

            //// Now to Send Data.
            //StreamWriter writer = null;

            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            //request.Method = "POST";
            //request.ContentType = "application/x-www-form-urlencoded";
            //request.ContentLength = postData.ToString().Length;
            //try
            //{
            //    writer = new StreamWriter(request.GetRequestStream());
            //    writer.Write(postData.ToString());
            //}
            //finally
            //{
            //    if (writer != null)
            //        writer.Close();
            //}

            //Response.Redirect(url);


            string redirecturl = "";
            redirecturl += "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" + ConfigurationManager.AppSettings["PayPalUsername"].ToString();
            redirecturl += "&first_name=Liton";
            redirecturl += "&city=Dhaka";
            redirecturl += "&state=Baridhara";
            redirecturl += "&item_name=" + vchProducto;
            redirecturl += "&amount=" + Costo.ToString();
            redirecturl += "&shipping=0";
            redirecturl += "&handling=0";
            redirecturl += "&tax=0";
            redirecturl += "&quantity=1";
            redirecturl += "&currency=MXN";
            redirecturl += "&return=" + "www.ok.com";
            redirecturl += "&cancel_return=" + "www.ok.com";
            Response.Redirect(redirecturl);

        }


        //private static String PreparePOSTForm(string url)
        //{
        //    //Set a name for the form
        //    string formID = "PostForm";


        //    //Build the form using the specified data to be posted.
        //    StringBuilder strForm = new StringBuilder();
        //    strForm.Append("<form id='" + formID + "' name='" + formID + "' action='" + ConfigurationManager.AppSettings["PayPalSubmitUrl"] + "' method='POST'>");
        //    strForm.Append("<input type='hidden' name='cmd' value='_xclick' />");
        //    strForm.Append("<input type='hidden' name='business' value='" + ConfigurationManager.AppSettings["PayPalUsername"] + "'/>");
        //    strForm.Append("<input type='hidden' name='item_name' value='" + "Curso1" + "' />");
        //    strForm.Append("<input type='hidden' name='amount' value='" + "10.00" + "' />");
        //    strForm.Append("<input type='hidden' name='return' value='" + "http:\\/\\/localhost:17911/PDT/PDTSuccess.aspx" + "' />");
        //    strForm.Append("<input type='hidden' name='custom' value='" + DateTime.Now.ToString() + "' />");

        //    strForm.Append("</form>");

        //    //Build the JavaScript which will do the Posting operation.
        //    StringBuilder strScript = new StringBuilder();
        //    strScript.Append("<script language='javascript'>");
        //    strScript.Append("var v" + formID + " = document." + formID + ";");
        //    strScript.Append("v" + formID + ".submit();");
        //    strScript.Append("</script>");

        //    //Build the JavaScript which will do the Posting operation.
        //    //StringBuilder strScript = new StringBuilder();
        //    //strScript.Append("<script language='javascript'>");
        //    //strScript.Append(" window.open('../TestHelper.aspx','popup','width=300,height=400'; ");
        //    //strScript.Append("</script>");

        //    //Return the form and the script concatenated. (The order is important, Form then JavaScript)
        //    return strScript.ToString();
        //}


        //public void RedirectAndPOST(Page page)
        //{
        //    string url = ConfigurationManager.AppSettings["PayPalSubmitUrl"];
        //    //Prepare the Posting form
        //    string strForm = PreparePOSTForm(url);

        //    //Add a literal control the specified page holding the Post Form, this is to submit the Posting form with the request.
        //    //page.Controls.Add(new LiteralControl(strForm));

        //    //page.Controls.Add(new LiteralControl(strForm));
        //    page.Controls.Add(new LiteralControl(strForm));

        //}
    }
}