using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;

public partial class Merchant : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        if (Session["culture"] != null)
        {
            UICulture = Session["culture"].ToString();
            Culture = Session["culture"].ToString();

            Thread.CurrentThread.CurrentCulture =
              CultureInfo.CreateSpecificCulture(Session["culture"].ToString());
            Thread.CurrentThread.CurrentUICulture =
              new CultureInfo(Session["culture"].ToString());
        }
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            // -------------------------- FORMULARZ --------------------------------
            string tORDERDESCRIPTION = (string)Session["ORDERDESCRIPTION"];
            ORDERDESCRIPTION.Text = "<input name='ORDERDESCRIPTION' type='hidden' value='" + tORDERDESCRIPTION + "'/>";
            string tAMOUNT = (string)Session["AMOUNT"];
            AMOUNT.Text = "<input name='AMOUNT' type='hidden' value='" + tAMOUNT + "'/>";
            string tCURRENCY = (string)Session["CURRENCY"];
            CURRENCY.Text = "<input name='CURRENCY' type='hidden' value='" + tCURRENCY + "'/>";
            string tNAME = (string)Session["NAME"];
            NAME.Text = "<input name='NAME' type='hidden' value='" + tNAME + "'/>";
            string tSURNAME = (string)Session["SURNAME"];
            SURNAME.Text = "<input name='SURNAME' type='hidden' value='" + tSURNAME + "'/>";
            string tSESSIONID = (string)Session["SESSIONID"];
            //SESSIONID.Text = "<input name='SESSIONID' type='hidden' value='" + tSESSIONID + "'/>";
            string tLANGUAGE = (string)Session["LANGUAGE"];
            LANGUAGE.Text = "<input name='LANGUAGE' type='hidden' value='" + tLANGUAGE + "'/>";
            string tCHARSET = (string)Session["CHARSET"];
            CHARSET.Text = "<input name='CHARSET' type='hidden' value='" + tCHARSET + "'/>";
            string tCOUNTRY = (string)Session["COUNTRY"];
            COUNTRY.Text = "<input name='COUNTRY' type='hidden' value='" + tCOUNTRY + "'/>";
            string tPAYMENTTYPE = (string)Session["PAYMENTTYPE"];
            PAYMENTTYPE.Text = "<input name='PAYMENTTYPE' type='hidden' value='" + tPAYMENTTYPE + "'/>";

            CheckBrowserCaps();

            string tAUTODEPOSIT = (string)Session["AUTODEPOSIT"];
            AUTODEPOSIT.Text = "<input name='AUTODEPOSIT' type='hidden' value='" + tAUTODEPOSIT + "'/>";

            // ---------------- Pobranie ORDERNUMBER ---------------------
            string connString = ConfigurationManager.ConnectionStrings["BazaReklamConn"].ConnectionString;
            string cmdText = "proc_NowaPlatnoscEcard";
            string nip = (string)Session["nip"];
            string nrZlec = (string)Session["nrZlec"];
            string idFaktury = (string)Session["IDFaktury"];
            string tORDERNUMBER = "";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(cmdText, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ORDERDESCRIPTION", tORDERDESCRIPTION);
                int iidFaktury = Convert.ToInt32(idFaktury);
                cmd.Parameters.AddWithValue("@idFaktury", iidFaktury);
                cmd.Parameters.AddWithValue("@nip", nip);
                cmd.Parameters.AddWithValue("@nrZlecenia", nrZlec);
                int iAMOUNT = Convert.ToInt32(tAMOUNT);
                cmd.Parameters.AddWithValue("@AMOUNT", iAMOUNT);
                cmd.Parameters.AddWithValue("@CURRENCY", tCURRENCY);
                cmd.Parameters.AddWithValue("@SESSIONID", tSESSIONID);
                cmd.Parameters.AddWithValue("@NAME", tNAME);
                cmd.Parameters.AddWithValue("@SURNAME", tSURNAME);
                int iAUTODEPOSIT = Convert.ToInt32(tAUTODEPOSIT);
                cmd.Parameters.AddWithValue("@AUTODEPOSIT", iAUTODEPOSIT);
                cmd.Parameters.AddWithValue("@LANGUAGE", tLANGUAGE);
                cmd.Parameters.AddWithValue("@CHARSET", tCHARSET);
                cmd.Parameters.AddWithValue("@COUNTRY", tCOUNTRY);
                int JavaScript = vJS;
                cmd.Parameters.AddWithValue("@JS", JavaScript);
                cmd.Parameters.AddWithValue("@PAYMENTTYPE", tPAYMENTTYPE);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                reader.Read();
                //reader.NextResult();
                tORDERNUMBER = reader["ID"].ToString();
                reader.Close();
                conn.Close();
            }

            ORDERNUMBER.Text = "<input name='ORDERNUMBER' type='hidden' value='" + tORDERNUMBER + "'/>";

            // ---------------- Pobranie HASHA z eCardu ------------------

            string strValue;
            string strResponse;
            // Utworzenie żądania zwrotnego
            string strReq = "https://pay.ecard.pl/servlet/HS?orderNumber=" + tORDERNUMBER + "&amount=" + tAMOUNT + "&currency=" + tCURRENCY;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strReq);
            // Ustawienie wartości żądania zwrotnego
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            strValue = "&orderDescription=&merchantId=170906000&password=JaYpqfs0";
            req.ContentLength = strValue.Length;
            // Zapisanie łańcucha żądania zwrotnego
            StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
            stOut.Write(strValue);
            stOut.Close();

            // Przesyłanie żądanie do systemu eCard i uzyskanie odpowiedzi
            StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
            strResponse = stIn.ReadToEnd();
            stIn.Close();

            // Sprawdzenie odpowiedzi
            if (strResponse == "zlyHash")
            {
                // błędne parametry - zły numer zamówienia, opis, kwota lub waluta

            }
            else if (strResponse == "wrongPassword")
            {
                // Niepoprawne hasło
            }
            else
            {
                //OK
            }

            string tHASH = strResponse;
            tHASH = tHASH.Trim();
            HASH.Text = "<input name='HASH' type='hidden' value='" + tHASH + "'/>";
            string tMERCHANTID = (string)Session["MERCHANTID"];
            MERCHANTID.Text = "<input name='MERCHANTID' type='hidden' value='" + tMERCHANTID + "'/>";
            LINKOK.Text = "<input name='LINKOK' type='hidden' value='http://platnosci.admoto.pl/info.aspx?status=ok'/>";
            LINKFAIL.Text = "<input name='LINKFAIL' type='hidden' value='http://platnosci.admoto.pl/info.aspx?status=fail'/>";
        }
        else
        {
            FormsAuthentication.SignOut();
            Response.Redirect("login.aspx");
        }
                
    }
    
    void CheckBrowserCaps()
    {
        string jsInfoText = "<h2>Twoja przeglądarka ma wyłączoną obsługę Java Script.</h2><p>Obsługa Java Script nie jest niezbędna, ale przyspiesza i upraszcza proces płatności.</p><p>Aby kontynuować proces naciśnij przycisk Dalej</p>";
        System.Web.HttpBrowserCapabilities myBrowserCaps = Request.Browser;
        if (((System.Web.Configuration.HttpCapabilitiesBase)myBrowserCaps).EcmaScriptVersion.Major > 1)
        {
            jsInfo.Text = jsInfoText;
            JS.Text = "<input name='JS' type='hidden' value='0'/>";
            submit.Text = "<input type='submit' value='Dalej'/>";
            vJS = 0;
        }
        else
        {
            jsInfo.Text = "";
            JS.Text = "<input name='JS' type='hidden' value='1'/>";
            vJS = 1;
        }
                
    }

    private int JavaScript;
    public int vJS
    {
        get
        {
            return JavaScript;
        }
        set
        {
            JavaScript = value;            
        }
    }
}
