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

/// <summary>
/// Summary description for pineLab
/// </summary>
public class pineLab
{
    public string PinelabPaymentStatus(string TxnId, string amount)
    {
     
        string serviceUrl = "https://www.plutuscloudservice.in:8201/API/CloudBasedIntegration/V1/PerformTxnValidation";
        string dataString = "";
        string json = "";


        try
        {

            const System.Security.Authentication.SslProtocols _Tls12 = (System.Security.Authentication.SslProtocols)0x00000C00;
            const SecurityProtocolType Tls12 = (SecurityProtocolType)_Tls12;
            ServicePointManager.SecurityProtocol = Tls12;
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
            httpRequest.Method = "POST";
            httpRequest.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
            httpRequest.ContentType = "application/json";
            string AmountinPaisa = (Convert.ToDecimal(amount) * 100).ToString("f0");

            json = "{\"BillingRefNumber\": \""+TxnId+"\",\"TxnAmtPaise\" : \""+ AmountinPaisa + "\",\"MerchantID\": \"800113\",\"TxnTypeIdentifierString\":\"ALL\",\"SecurityToken\": \"3B327741-5BEE-465F-9F05-47A7B068AD1F\"}";
    
            using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
            {

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }
            var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                dataString = streamReader.ReadToEnd();
            }
           
            
        }
        catch (Exception ex)
        {
            dataString = "{\"CardTxnData\": null,\"RewardTxnData\": null,\"PaybackTxnData\": null,\"Pine360TxnData\": null,\"UPITxnData\": null,\"ResponseMessage\": \""+ex.Message+"\",\"BillingRefNumber\": \""+TxnId+"\",\"Ptrn\": 0,\"HostTransactionNumber\": null}";
        }

        InsertServiceRequestResponseLog("GetPineStatus", json, dataString);
        return dataString;
    }
    public void InsertServiceRequestResponseLog(string MethodName, string RequestLog, string ResponseLog)
    {
        try
        {
            SqlParameter[] warlock = new SqlParameter[]
            {
                new SqlParameter("@MethodName", MethodName),
                new SqlParameter("@RequestLog", RequestLog),
                new SqlParameter("@ResponseLog", ResponseLog)
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_Insert_ServiceRequestResponseLog", warlock);
        }
        catch (Exception ex)
        {
            throw ex;

        }
    }
}

public class PineLabStatusResponse
{
    public CardTxnData CardTxnData { get; set; }
    public object RewardTxnData { get; set; }
    public object PaybackTxnData { get; set; }
    public object Pine360TxnData { get; set; }
    public UPITxnData UPITxnData { get; set; }
    public string ResponseMessage { get; set; }
    public string BillingRefNumber { get; set; }
    public int Ptrn { get; set; }
    public object HostTransactionNumber { get; set; }
}
public class CardTxnData
{
    public string BillReferenceNumber { get; set; }
    public string ResponseMessage { get; set; }
    public string TxnStatus { get; set; }
    public string TxnType { get; set; }
    public string TxnDateTime { get; set; }
    public string RRN { get; set; }
    public string AuthCode { get; set; }
    public string AcquirerName { get; set; }
    public string TxnAmtPaise { get; set; }
    public string MaskedPanNum { get; set; }
    public string CardHolderName { get; set; }
    public string BankMid { get; set; }
    public string BankTid { get; set; }
    public string BatchNum { get; set; }
    public string InvoiceNum { get; set; }
    public string CardType { get; set; }
    public string CardEntryMode { get; set; }
    public string AcquirerCode { get; set; }
    public string MerchantName { get; set; }
    public string MerchantAddress { get; set; }
    public string MerchantCity { get; set; }
    public string BatchStatus { get; set; }
    public string TransactionLogId { get; set; }
    public List<object> AdditionalInfo { get; set; }
}
public class UPITxnData
{
    public string TxnType { get; set; }
    public string UPITxnStatus { get; set; }
    public string UPITxnStatusDesciption { get; set; }
    public int TxnAmtPaise { get; set; }
    public string HostTxnId { get; set; }
    public string HostTID { get; set; }
    public string HostMID { get; set; }
    public string ApprovalCode { get; set; }
    public string RRN { get; set; }
    public string Invoice { get; set; }
    public string MaskedMobileNumber { get; set; }
    public string AcquirerCode { get; set; }
    public string AcquirerName { get; set; }
    public string TxnDateTime { get; set; }
    public int TransactionLogId { get; set; }
    public string CustomerVPA { get; set; }
    public string TxnStatus { get; set; }
    public int HostCategoryID { get; set; }
    public string BillReferenceNumber { get; set; }
}
