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

public partial class GetVenusResp : System.Web.UI.Page
{
    static string url, resp;
    string RefNo, con;
    string transaction_id, status, operator_id, res, txn, RefNumber, TxnID, Description;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                //ResponseStatus=FAILURE&OperatorTxnID=&OrderNo=&MerTxnID=12172843975147&AccountNo=1284581160";

                // url = Request.Form["ResponseXML"];
                url = Request.RawUrl;
                resp = url;
                RefNo = TID_GEN();
                SqlParameter[] param = new SqlParameter[] 
                {         
                   new SqlParameter("@RefNo",RefNo),
                   new SqlParameter("@URL",resp),      
                };
                int a = SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "Udp_Saveresult", param);
                txn = resp;
                
                if(1==1)
                {
                   
                    transaction_id = Convert.ToString(Request.QueryString["OperatorTxnID"]);// Convert.ToString(ds.Tables["Response"].Rows[0]["OperatorTxnID"]);
                    status = Convert.ToString(Request.QueryString["ResponseStatus"]);// Convert.ToString(ds.Tables["Response"].Rows[0]["ResponseStatus"]);
                    RefNumber = Convert.ToString(Request.QueryString["MerTxnID"]);// Convert.ToString(ds.Tables["Response"].Rows[0]["MerTxnID"]);
                    
                    con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                   
                   SqlParameter[] TxnResponce = new SqlParameter[]
                    {
                        new SqlParameter("@TXNRESPONCE",txn),
                        new SqlParameter("@REFRENCENO",RefNumber)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                    res = txn;
                    if (!(res.Contains("Error") || res.Contains("ERROR")))
                    {

                        if (status.Equals("SUCCESS") || status.Equals("Success") || status.Equals("success"))
                        {
                            SqlParameter[] inputrequest = new SqlParameter[]
                            {
                                new SqlParameter("@refrenceno",RefNumber),
                                new SqlParameter("@OperatorTxnID",transaction_id),
                                new SqlParameter("@Status",status)
                            };
                           SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatestatus_success_for_Venus", inputrequest);
                        }
                        else if (status.Equals("FAILED") || status.Equals("Failed") || status.Equals("failed")|| status.Equals("FAILURE"))
                        {
                            SqlParameter[] inputrequest = new SqlParameter[]
                            {
                                new SqlParameter("@refrenceno",RefNumber),
                                new SqlParameter("@OperatorTxnID",transaction_id),
                                new SqlParameter("@Status",status)
                            };
                            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_updatestatus_fail_for_Venus", inputrequest);
                        }
                        else if (status.Equals("Pending") || status.Equals("PENDING") || status.Equals("pending"))
                        {
                           
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
           // throw (ex);
        SqlParameter[] param = new SqlParameter[] 
                {         
                   new SqlParameter("@RefNo","ResponseError"),
                   new SqlParameter("@URL",Convert.ToString(ex.InnerException)),      
                };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "Udp_Saveresult", param);
        }
    }
    public static string TID_GEN()
    {
        string new_tid = System.Guid.NewGuid().ToString();
        new_tid = new_tid.Replace("-", string.Empty).ToLower();
        new_tid = new_tid.Substring(0, 20).Replace("a", "1");
        new_tid = new_tid.Substring(0, 20).Replace("b", "2");
        new_tid = new_tid.Substring(0, 20).Replace("c", "3");
        new_tid = new_tid.Substring(0, 20).Replace("d", "4");
        new_tid = new_tid.Substring(0, 20).Replace("e", "5");
        new_tid = new_tid.Substring(0, 20).Replace("f", "6");
        new_tid = new_tid.Substring(0, 20).Replace("g", "7");
        new_tid = new_tid.Substring(0, 20).Replace("h", "8");
        new_tid = new_tid.Substring(0, 20).Replace("i", "9");
        new_tid = new_tid.Substring(0, 20).Replace("j", "0");
        new_tid = new_tid.Substring(0, 20).Replace("k", "1");
        new_tid = new_tid.Substring(0, 20).Replace("l", "2");
        new_tid = new_tid.Substring(0, 20).Replace("m", "3");
        new_tid = new_tid.Substring(0, 20).Replace("n", "4");
        new_tid = new_tid.Substring(0, 20).Replace("o", "5");
        new_tid = new_tid.Substring(0, 20).Replace("p", "0");
        new_tid = new_tid.Substring(0, 20).Replace("q", "9");
        new_tid = new_tid.Substring(0, 20).Replace("r", "8");
        new_tid = new_tid.Substring(0, 20).Replace("s", "7");
        new_tid = new_tid.Substring(0, 20).Replace("t", "6");
        new_tid = new_tid.Substring(0, 20).Replace("u", "5");
        new_tid = new_tid.Substring(0, 20).Replace("v", "4");
        new_tid = new_tid.Substring(0, 20).Replace("w", "3");
        new_tid = new_tid.Substring(0, 20).Replace("x", "2");
        new_tid = new_tid.Substring(0, 20).Replace("y", "1");
        new_tid = new_tid.Substring(0, 20).Replace("z", "0");
        return new_tid;
    }
}