Reply
Member
Posts: 4
Registered: ‎01-13-2015

GetTransactionDetails XML web service call sample

I have reviewed the XML sample code, and wanted to know if there was any C# sample code to actually make the call (HTTP POST).

 

http://community.developer.authorize.net/t5/News-and-Announcements/New-API-Call-getTransactionDetail...

 

This is the XML that I want to send:

 

<?xml version="1.0" encoding="utf-8"?>
<getTransactionDetailsRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
  <merchantAuthentication>
    <name>YourUserLogin</name>
    <transactionKey>YourTranKey</transactionKey>
  </merchantAuthentication>
  <transId>1111111111</transId>
</getTransactionDetailsRequest>

 

So in other words, how do I package and send this code in my C# web application.

Expert
Posts: 4,525
Registered: ‎03-08-2010

Re: GetTransactionDetails XML web service call sample

You mean how to do xml call in c#?

 

http://developer.authorize.net/downloads/samplecode/

CIM - C#

have example on how to do xml call.

Member
Posts: 4
Registered: ‎01-13-2015

Re: GetTransactionDetails XML web service call sample

It seems to show good examples of how to process the XML response, but not seeing any sample code on how to take a piece of existing XML code, and send it as a request. For example, PostRequest method takes ApiRequest of type Object as one of it's parameters, and then serializes it . I'm new to C# so I'm not sure how serializer works, and whether it really matters in this situation.

 

I'm not sure how to process a XDocument using PostRequest or similar.

 

 

Expert
Posts: 4,525
Registered: ‎03-08-2010

Re: GetTransactionDetails XML web service call sample

Member
Posts: 4
Registered: ‎01-13-2015

Re: GetTransactionDetails XML web service call sample

Thanks much. Just to make sure, would the following be correct:

 

public const String API_URL = "https://apitest.authorize.net/xml/v1/request.api";

public static void Main()
{
    XNamespace ns = "AnetApi/xml/v1/schema/AnetApiSchema.xsd";

            XDocument doc = new XDocument(
                    new XElement(ns + "getTransactionDetailsRequest",
                        new XAttribute("xmlns", "AnetApi/xml/v1/schema/AnetApiSchema.xsd"),
                    new XElement(ns + "merchantAuthentication",
                        new XElement(ns + "name", TEST_NAME),
                        new XElement(ns + "transactionKey", TEST_TRANS_KEY)),
                    new XElement(ns + "transId", TEST_TRANS_ID))
                    );

    System.Xml.XmlDocument response_xml = null;
    PostRequest(doc.ToString(), out response_xml);
}

		public static bool PostRequest(object ApiRequest, out XmlDocument xmldoc)
		{
			bool bResult = false;
			XmlSerializer serializer;

			xmldoc = null;

			try
			{
				HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(API_URL);
				webRequest.Method = "POST";
				webRequest.ContentType = "text/xml";
				webRequest.KeepAlive = true;

				// Serialize the request
				serializer = new XmlSerializer(ApiRequest.GetType());
				XmlWriter writer = new XmlTextWriter(webRequest.GetRequestStream(), Encoding.UTF8);
				serializer.Serialize(writer, ApiRequest);
				writer.Close();

				if (PRINT_POST)
				{
					Console.WriteLine("\r\n\r\n");
					StreamWriter consoleOutput = new StreamWriter(Console.OpenStandardOutput());
					consoleOutput.AutoFlush = true;
					serializer.Serialize(consoleOutput, ApiRequest);
					consoleOutput.Close();
					Console.WriteLine("\r\n\r\n");
				}

				// Get the response
				WebResponse webResponse = webRequest.GetResponse();

				// Load the response from the API server into an XmlDocument.
				xmldoc = new XmlDocument();
				xmldoc.Load(XmlReader.Create(webResponse.GetResponseStream()));

				if (PRINT_POST)
				{
					XmlWriterSettings settings = new XmlWriterSettings();
					settings.Indent = true;
					settings.Encoding = Encoding.ASCII;
					XmlWriter consoleWriter = XmlWriter.Create(Console.Out, settings);
					xmldoc.WriteTo(consoleWriter);
					consoleWriter.Close();
					Console.WriteLine("\r\n\r\n");
				}

				bResult = true;
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.GetType().ToString() + ": " + ex.Message);
				bResult = false;
			}

			return bResult;
		}

 

Expert
Posts: 4,525
Registered: ‎03-08-2010

Re: GetTransactionDetails XML web service call sample

Try it and debug it.

Member
Posts: 4
Registered: ‎01-13-2015

Re: GetTransactionDetails XML web service call sample

Since I'm not familiar with serializer, I modified the PostRequest method in the API to meet my needs. It seems like I am getting the response from Authorize.NET, and can see some XML, but I am having trouble capturing the responseReasonCode node's value (or for that matter -- I can't even get the node as it says it is NULL in the code below).

 

            XNamespace ns = "AnetApi/xml/v1/schema/AnetApiSchema.xsd";

            XDocument doc = new XDocument(
                    new XElement(ns + "getTransactionDetailsRequest",
                        new XAttribute("xmlns", "AnetApi/xml/v1/schema/AnetApiSchema.xsd"),
                    new XElement(ns + "merchantAuthentication",
                        new XElement(ns + "name", TEST_NAME),
                        new XElement(ns + "transactionKey", TEST_TRANS_KEY)),
                    new XElement(ns + "transId", TEST_TRANS_ID))
                    );

            MemoryStream stream = new MemoryStream();
            doc.Save(stream);
            byte[] postBytes = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(postBytes, 0, postBytes.Length);
            

            WebClient WC = new WebClient();
            ServicePointManager.ServerCertificateValidationCallback = TrustAllCertificateCallback;
            HttpWebRequest wrequest = (HttpWebRequest)WebRequest.Create(IsLive ? LIVE_URL : TEST_URL);
            wrequest.Timeout = 300000;
            wrequest.Method = "POST";
            wrequest.ContentType = "text/xml";
            wrequest.ContentLength = stream.Length;

            Stream lilriver = wrequest.GetRequestStream();
            lilriver.Write(postBytes, 0, postBytes.Length);

            WebResponse webResponse = wrequest.GetResponse();

            XDocument xdoc = XDocument.Load(XmlReader.Create(webResponse.GetResponseStream()));
            //var node = xdoc.Descendants().Where(n => n.Name == "responseReasonCode").FirstOrDefault();
            var node = xdoc.Descendants("responseReasonCode").FirstOrDefault();

            if (node != null)
            {
                reason = node.Value;
            }

 

Expert
Posts: 4,525
Registered: ‎03-08-2010

Re: GetTransactionDetails XML web service call sample

Why not just use the soap/web service? then you don't have to deal with xml at all.