Click here to Skip to main content
15,886,701 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii There !! i have been in this situation i have an asp.net webpage in which i have hidden fields for payment gateway like txnid and hash value fields now when i click on my submit button it supposed to provide the txnid and hash code but its not providing anything i used to see the output by having alert boxes in asp.net but what i found is on first click of button these fields were empty and on second click of button these fields have values and everything is working fine..please help !!!

aspx code:-
<form class="RegForm"  runat="server" method="post" name="Registration.aspx" id="form1" accept-charset="utf-8" >
        <div id ="frmError" runat="server">
     <span style="color:red">Please fill all mandatory fields.</span>
     <br/>
     <br/>
     </div>


     <!---important field dont remove -->
       <input type="hidden" runat="server" id="key" name="key"  />
     <input type="hidden" runat="server" id="hash" name="hash"   />
           <input type="hidden" runat="server" id="txnid" name="txnid" />
            <input type="hidden" runat="server" id="enforce_paymethod" name="enforce_paymethod" />
            <!---important field dont remove --><pre></form>

.cs code

public partial class Registration : System.Web.UI.Page
   {
       public string action1 = string.Empty;
       public string hash1 = string.Empty;
       public string txnid1 = string.Empty;
       public string url = string.Empty;
       static string constring = ConfigurationManager.ConnectionStrings["PalpConString"].ConnectionString;


       protected void Page_Load(object sender, EventArgs e)
       {


           try
           {

               //set merchant key from web.config or AppSettings
               key.Value = ConfigurationManager.AppSettings["MERCHANT_KEY"];
               //DisplayRadioButton();
               if (!IsPostBack)
               {


                   frmError.Visible = false; // error form

               }
               else
               {
                   //frmError.Visible = true;
               }
               if (string.IsNullOrEmpty(Request.Form["hash"]))
               {
                   btnSubmit.Visible = true;
               }
               else
               {
                   btnSubmit.Visible = false;
               }

           }
           catch (Exception ex)
           {
               Response.Write("<span style='color:red'>" + ex.Message + "</span>");

           }
       }
         // Generating Hash Code for PayuMoney
       public string Generatehash512(string text)
       {

           byte[] message = Encoding.UTF8.GetBytes(text);

           UnicodeEncoding UE = new UnicodeEncoding();
           byte[] hashValue;
           SHA512Managed hashString = new SHA512Managed();
           string hex = "";
           hashValue = hashString.ComputeHash(message);
           foreach (byte x in hashValue)
           {
               hex += String.Format("{0:x2}", x);
           }
           return hex; ;

       }

       protected void btnSubmit_Click1(object sender, EventArgs e)
       {
                   RecordInsertion();
           try
           {
                   string[] hashVarsSeq;
                   string hash_string = string.Empty;
               if (string.IsNullOrEmpty(Request.Form["txnid"])) // generating txnid
               {
                   Random rnd = new Random();
                   string strHash = Generatehash512(rnd.ToString() + DateTime.Now);
                   txnid1 = strHash.ToString().Substring(0, 20);
               }
               else
               {
                   txnid1 = Request.Form["txnid"];
               }
               if (string.IsNullOrEmpty(Request.Form["hash"])) // generating hash value
               {
               if (
                   string.IsNullOrEmpty(ConfigurationManager.AppSettings["MERCHANT_KEY"]) ||
                   string.IsNullOrEmpty(txnid1) ||
                   string.IsNullOrEmpty(Request.Form["amount"]) ||
                   string.IsNullOrEmpty(Request.Form["firstname"]) ||
                   string.IsNullOrEmpty(Request.Form["email"]) ||
                   string.IsNullOrEmpty(Request.Form["phone"]) ||
                   string.IsNullOrEmpty(Request.Form["productinfo"]) ||
                   string.IsNullOrEmpty(Request.Form["surl"]) ||
                   string.IsNullOrEmpty(Request.Form["furl"])

                   )
               {  //remove later
                   frmError.Visible = true;
                   return;
               }

               else
               {
                   //remove later
           frmError.Visible = false;
           hashVarsSeq = ConfigurationManager.AppSettings["hashSequence"].Split('|'); // spliting hash sequence from config
           hash_string = "";
           foreach (string hash_var in hashVarsSeq)
           {
               if (hash_var == "key")
               {
                   hash_string = hash_string + ConfigurationManager.AppSettings["MERCHANT_KEY"];
                   hash_string = hash_string + '|';
               }
               else if (hash_var == "txnid")
               {
                   hash_string = hash_string + txnid1;
                   hash_string = hash_string + '|';
               }
               else if (hash_var == "amount")
               {
                   hash_string = hash_string + Convert.ToDecimal(Request.Form[hash_var]).ToString("g29");
                   hash_string = hash_string + '|';
               }
               else
               {

                   hash_string = hash_string + (Request.Form[hash_var] != null ? Request.Form[hash_var] : "");// isset if else
                   hash_string = hash_string + '|';
                           }
                   }

                   hash_string += ConfigurationManager.AppSettings["SALT"];// appending SALT

                   hash1 = Generatehash512(hash_string).ToLower();         //generating hash
                   action1 = ConfigurationManager.AppSettings["PAYU_BASE_URL"] + "/_payment";// setting URL

                   }
               }

               else if (!string.IsNullOrEmpty(Request.Form["hash"]))
               {
                   hash1 = Request.Form["hash"];
                   action1 = ConfigurationManager.AppSettings["PAYU_BASE_URL"] + "/_payment";

               }

               if (!string.IsNullOrEmpty(hash1))
               {
                   hash.Value = hash1;
                   txnid.Value = txnid1;

                   System.Collections.Hashtable data = new System.Collections.Hashtable(); // adding values in gash table for data post
                   data.Add("hash", hash.Value);
                   data.Add("txnid", txnid.Value);
                   data.Add("key", key.Value);
                   string AmountForm = Convert.ToDecimal(amount.Text.Trim()).ToString("g29");// eliminating trailing zeros
                   amount.Text = AmountForm;
                   data.Add("amount", AmountForm);
                   data.Add("firstname", firstname.Text.Trim());
                   data.Add("email", email.Text.Trim());
                   data.Add("phone", phone.Text.Trim());
                   data.Add("productinfo", productinfo.Text.Trim());
                   data.Add("surl", surl.Text.Trim());
                   data.Add("furl", furl.Text.Trim());
                   data.Add("lastname", lastname.Text.Trim());
                   data.Add("curl", "");
                   data.Add("address1", address1.Text.Trim());
                   data.Add("address2", address2.Text.Trim());
                   data.Add("city", city.Text.Trim());
                   data.Add("state", state.Text.Trim());
                   data.Add("country", "");
                   data.Add("zipcode", zipcode.Text.Trim());
                   data.Add("udf1", "");
                   data.Add("udf2", "");
                   data.Add("udf3", "");
                   data.Add("udf4", "");
                   data.Add("udf5", "");
                   data.Add("pg", "");
                   data.Add("service_provider","");
                   string strForm = PreparePOSTForm(action1, data);
                   Page.Controls.Add(new LiteralControl(strForm));

               }

               else
               {
                   //no hash

               }

           }

           catch (Exception ex)
           {
                   Response.Write("<span style='color:red'>" + ex.Message + "</span>");
           }
       }

       private string PreparePOSTForm(string url, System.Collections.Hashtable data)      // post form
       {
                   //Set a name for the form
                   string formID = "PostForm";
                   //Build the form using the specified data to be posted.
                   StringBuilder strForm = new StringBuilder();
                   strForm.Append("<form id=\"" + formID + "\" name=\"" +
                                  formID + "\" action=\"" + url +
                                  "\" method=\"POST\">");

           foreach (System.Collections.DictionaryEntry key in data)
           {

                   strForm.Append("<input type=\"hidden\" name=\"" + key.Key +
                                  "\" value=\"" + key.Value + "\">");
           }


                   strForm.Append("</form>");
                   //Build the JavaScript which will do the Posting operation.
                   StringBuilder strScript = new StringBuilder();
                   strScript.Append("<script language='javascript'>");
                   strScript.Append("var v" + formID + " = document." +
                                    formID + ";");
                   strScript.Append("v" + formID + ".submit();");
                   strScript.Append("</script>");
                   //Return the form and the script concatenated.
                   //(The order is important, Form then JavaScript)
                   return strForm.ToString() + strScript.ToString();
       }


What I have tried:

i tried to have these fields inside of update panel but its again not working please help
Posted
Updated 30-Jan-23 23:57pm
v2
Comments
Karthik_Mahalingam 18-Jul-17 7:23am    
post the code.
Er. Puneet Goel 18-Jul-17 7:48am    
Please check if you are doing some client side work before posting. Sharing code the only way we could dig in.
CyberSaint 18-Jul-17 8:58am    
here is the code i just posted the hidden field code as in other code im taking value direct from user

Hello CyberSaint,

In the posted Hidden fields declaration (aspx code), these have no set value:

<input type="hidden" runat="server" id="txnid" name="txnid" ¿Value="InitialValue"? />


That's why they are empty when you first click the action button.

You must make sure you initialize their values before you attempt to read them. If these values are something you cannot post due to secrecy, then you could set them in you Page_Load event like this:

protected void Page_Load(object sender, EventArgs e)
{

// Your code...

if (HidDv.Value == string.Empty) //Do this for each hidden field
            { 
                //Set initial value here.
            }

// Your code...

}


Either way, your hidden controls will have set values by the time you click on the corresponding button.

Cheers!
 
Share this answer
 
Comments
CyberSaint 18-Aug-17 4:46am    
thank you
I Figure out problem thank alot
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900