using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Agent_Aeps_Pay : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["LOGINID"] == null && Session["USERTYPE"] == null)
        {
            Response.Redirect("../login.aspx");
        }
        Session["AuthID"] = "10000";
        Session["AuthKey"] = "a58b8ef332beea11daa1369c271725d17fc3443ee837a6d286aa1fff3c744ef0";
        Session["VenusAEPSAGID"] = GetPayworld_Agent();
        Session["PostUrl"]= "http://aepsid.in/aeps.aspx?AuthID="+Convert.ToString(Session["AuthID"]) + "&AGID=" + Convert.ToString(Session["VenusAEPSAGID"]) + "&Key=" + Convert.ToString(Session["AuthKey"]) ;
        
    }
    public string KindaEncrypt(string RequestMsg, string Key)
    {
        string result = "";
        string strUrl = "http://dmr.venusrecharge.com/venus/resources/encryption/encrypt";

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(strUrl);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = new JavaScriptSerializer().Serialize(new
            {
                data = RequestMsg,
                key = Key
            });
            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            result = streamReader.ReadToEnd();
        }
        JObject jsonResponse = JObject.Parse(result);
        JValue ResponseStatus = (JValue)jsonResponse["data"];
        return ResponseStatus.ToString();
    }

    public string GetPayworld_Agent()
    {
        string payworldAgID = "";
        SqlParameter[] checktransaction = new SqlParameter[2]
        {
            new SqlParameter ("@AgentId",Session["USERNAME"].ToString()),
            new SqlParameter("@Flag",1),
        };
        SqlDataReader  dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetPayworldKeyDetails", checktransaction);
        if(dr.Read())
        {
            payworldAgID = Convert.ToString(dr["BCId"]);
        }
        return payworldAgID;
    }
 
}