I'm using ASP.NET 4.0, C#, Web Forms, and Master Pages.
I've looked all over and can't figure out why this isn't working. I have a test page (Silent_Test) with button that posts to another page (Silent). The problem seems to be that the Silent page only captures the information through POST if the Silent page loads. It makes sense since the information is in the Page Load event, but I understand that Authorize.Net's Silent Post will not load the page so how can I capture their POST information if the page is never loaded? Is my understanding off or am I going about this the wrong way?...Can anyone provide tips or sample code?
Silent_Test.ASPX
<%@ Page Title="Silent Test" Language="C#" MasterPageFile="~/MasterPages/Site.master" AutoEventWireup="true" CodeFile="Silent_Test.aspx.cs" Inherits="_Silent_Test" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server"> <form action="https://www.test.com/silent.aspx" method="post"> <input type="hidden" name="x_response_code" value="9"/> <input type="hidden" name="x_cust_id" value="99999999-9999-9999-9999-999999999999"/> <input type="submit"/> </form> </asp:Content>
Silent_Test.ASPX.CS
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Silent_Test : BasePage { protected void Page_Load(object sender, EventArgs e) { } }
Silent.ASPX
<%@ Page Title="Silent" Language="C#" MasterPageFile="~/MasterPages/Site.master" AutoEventWireup="true" CodeFile="Silent.aspx.cs" Inherits="_Silent" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server"> <div> <%-- There is no need to put anything in the .ASPX file since Authorize.net's server does not care about the response from the POST call.--%> <p> You have reached this page in error. Please verify you have the correct web page address. </p> </div> </asp:Content>
Silent.ASPX.CS
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data.SqlClient; public partial class _Silent : BasePage { protected void Page_Load(object sender, EventArgs e) { string connectionString = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ConnectionString; string insertSql = "INSERT INTO Silent(x_cust_id, x_response_code) VALUES(@cust_id,@response_code)"; using (SqlConnection myConnection = new SqlConnection(connectionString)) { myConnection.Open(); SqlCommand myCommand = new SqlCommand(insertSql, myConnection); myCommand.Parameters.AddWithValue("@cust_id", this.Request.Form["x_cust_id"]); myCommand.Parameters.AddWithValue("@response_code", this.Request.Form["x_response_code"]); myCommand.ExecuteNonQuery(); myConnection.Close(); } } }
12-09-2011 02:18 PM
Making some progress here, it appears that i can only capture a post back (so far) when i void a transaction. I wrote a short function to log everything in the form as well as see what my SQL query would look like (two birds):
private void WriteToLog()
{
string data = "Started to Write " + DateTime.Now.ToString() + "\n\r";
data = data +
"*************************************************************************\n\r";
int icount = 0;
string destPath = System.Web.HttpContext.Current.Server.MapPath("~/bin");
File.WriteAllText(destPath + "\\SilentPost.txt", data);
data =
"";
while (icount < Request.Form.Count)
{
data = data + Request.Form[icount].ToString() +
"\n\r";
icount++;
}
File.AppendAllText(destPath + "\\SilentPost.txt", data);
data =
"Starting SQL Query \n\r *************************************************************************\n\r";
File.AppendAllText(destPath + "\\SilentPost.txt", data);
string status = "";
switch (Request.Form["x_response_code"].ToString())
{
case"1":
status =
"Approved";
break;
case"2":
status =
"Declined";
break;
case"3":
status =
"Error";
break;
case"4":
status =
"Held for Review";
break;
}
StringBuilder OrdersCMD = newStringBuilder();
OrdersCMD.Append(
"UPDATE orders set ");
OrdersCMD.Append(
"First_Name = '" + Request.Form["x_first_name"].ToString() + "', Last_Name = '" + Request.Form["x_last_name"].ToString() + "', Address = '" + Request.Form["x_address"].ToString() + "', City = '");
OrdersCMD.Append(Request.Form[
"x_city"].ToString() + "', State = '" + Request.Form["x_state"].ToString() + "', Zip = '" + Request.Form["x_zip"].ToString() + "', Email = '" + Request.Form["x_email"].ToString() + "', AuthCode = '");
OrdersCMD.Append(Request.Form[
"x_auth_code"].ToString() + "', TransactionID = '" + Request.Form["x_trans_id"].ToString() + "', Status = '" + status + "' WHERE OrderNumber = '" + Request.Form["x_invoice_num"].ToString() + "'");
File.AppendAllText(destPath + "\\SilentPost.txt", OrdersCMD.ToString());
data =
"End Write\n\r";
data = data +
"*************************************************************************\n\r";
File.AppendAllText(destPath + "\\SilentPost.txt", data);
}
To test i hit the page via a browser and the log is created. I then deleted the log and submitted a transaction via my site with no issues. I waited 5 minutes (timestamp on the emailed reciept) and got no log file. Next i void the transaction and my log is created (timestamp shows it created 5 minutes later then transaction). containing only partial data. Any ideas?
01-04-2012 02:09 PM
What's your log file output look like? I don't know C#, but here's something I found in Google that might work for neatly converting the form data to a string:
http://ruuddottech.blogspot.com/2009/07/php-vardump-method-for-c.html
Note that File.WriteAllText overwrites whatever's there already, so if you do multiple calls, you probably want to use a call that appends to the file instead.
01-04-2012 08:48 PM
@TJPride - I only use the File.WriteAllText on the first call to generate a new log or over write an old one, the rest of the File calls are appends. At this point I don't care about the format or if it's appending the point is it's never being called. The example i posted captures data as expected when a VOID takes place so i know the code works but i see NOTHING generated (no logs at all) when a standard transaction happens. I can only assume authorize.net never calls my page because at the very least i should get a log file with the date in it. (i commit that action before moving on to anything that could fail because there is no data). Do you know if there is any way to check on the authorize.net side to see if the kick off the post as expected??
01-05-2012 04:30 AM
Doesn't make any sense. Anything that would stop regular transactions from coming through would also stop voids, unless somehow you're specifying a different URL, or perhaps Authorize.net isn't triggering the Page_Load event. Have you tried setting up a PHP page that does nothing but log output to see if this is specific to C#?
01-05-2012 10:15 AM
I don't know anything about PHP but i did find an ASP sample Authorize provides, i'm going to test it today.
01-06-2012 12:59 PM
Hi John,
I am having a similar issue. Could you please explain what you did....if possible with a sample code(thanks a lot)...
You now have a Authorize.NET hosted payment form and whose receipt page are you using?
Could you say what are all values you set to work....
Appreciate very much for the help..... Got struck....:(
Thanks again..
04-20-2012 06:15 PM