using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net;
using System.IO;
using QuintoCaminoV2.Data_objects;

namespace QuintoCaminoV2.Buy
{
    public partial class PaySusses : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string authToken = ConfigurationManager.AppSettings["PDTToken"];

                //read in txn token from querystring
               string txToken = Request.QueryString.Get("tx");


                string query = string.Format("cmd=_notify-synch&tx={0}&at={1}",
                                      txToken, authToken);

                // Create the request back
                string url = ConfigurationManager.AppSettings["PayPalSubmitUrl"];
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

                // Set values for the request back
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = query.Length;

                // Write the request back IPN strings
                StreamWriter stOut = new StreamWriter(req.GetRequestStream(),
                                         System.Text.Encoding.ASCII);
                stOut.Write(query);
                stOut.Close();

                // Do the request to PayPal and get the response
                StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
                string strResponse = stIn.ReadToEnd();
                stIn.Close();

                // sanity check
                //Label2.Text = strResponse;

                // If response was SUCCESS, parse response string and output details
                if (strResponse.StartsWith("SUCCESS"))
                {
                    PDTHolder pdt = PDTHolder.Parse(strResponse);
                    lblFolioPaypal.Text = pdt.TransactionId;

                    Pagos oPago = new Pagos();
                  
                    oPago.vchUsuario = "";
                    oPago.intUsuarioId = Convert.ToInt32(pdt.Custom); ;
                    oPago.vchFolioPayPal = pdt.TransactionId;
                    oPago.decMontoPago = Convert.ToDecimal(pdt.GrossTotal);
                    oPago.intCursoId = Convert.ToInt32(pdt.item_number);
                    oPago.bitConfirmado = false;
                    oPago.datFechaPago = DateTime.Today.ToString();
                    oPago.datFechaAlta = DateTime.Today.ToString();
                    oPago.vchEstatusPago = pdt.PaymentStatus.ToString();
                    oPago.bitActivo = true;
                    oPago.vchMetodoPago = "PAYPAL";
                    oPago.vchCurso = pdt.ItemName;
                    SavePagos(oPago);
                    //Label1.Text =
                    //    string.Format("Thank you {0} {1} [{2}] for your payment of {3} {4}!",
                    //    pdt.PayerFirstName, pdt.PayerLastName,
                    //    pdt.PayerEmail, pdt.GrossTotal, pdt.Currency);

                    


                }
                else
                {
                    //Label1.Text = "Oooops, something went wrong...";
                }
            }
        }


        public void SavePagos(Pagos oPago)
        {
            List<Data_objects.Pagos> lstPagos = new List<Data_objects.Pagos>();
            //Data_objects.Pagos oPago = new Data_objects.Pagos();
            DataLayer oDataLayer = new DataLayer();
            Errores oError = new Errores();


            //oPago.vchUsuario = "makrack";
            //oPago.intUsuarioId = 1;
            //oPago.vchFolioPayPal = "123456";
            //oPago.decMontoPago = 2000;
            //oPago.intCursoId = 1;
            //oPago.bitConfirmado = false;
            //oPago.datFechaPago = DateTime.Today.ToString();
            //oPago.datFechaAlta = DateTime.Today.ToString();
            //oPago.vchEstatusPago = "CONFIRMADO";
            //oPago.bitActivo = true;
            //oPago.vchMetodoPago = "PAYPAL";
            //oPago.vchCurso = "Curso 1";

            lstPagos.Add(oPago);

            oError = oDataLayer.insertPago(lstPagos);

            if (!(oError.bitError))
            {

            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
        }

    }
}