REST example XML in CSharp

Created by Richard Moore, Modified on Wed, 12 Apr 2023 at 12:02 PM by Richard Moore

//Example created VS2010 using 4.0 framework but shouldn't matter.  Nothing exotic here.


//Disclaimer:  This worked for me but I don't profess to be a coding guru.  Not optimised and I don't work for E-xact.


//don't forget to include XML


using System.XML


public void request CC(object sender, EventArgs e)


{


 


 


String post_url = "https://api-demo.e-xact.com/transaction";  //<----"https://api.e-xact.com/transaction" for non demo
            String strFirstName = "";
            String strLastName = "";
            String StrCreditCardNumber = "";
            String strAmount = "";
            String encoded="";
            String redirectUrl = "";
            strFirstName = TextBoxFirstName.Text;
            strLastName = TextBoxLastName.Text;
            StrCreditCardNumber = TextBoxCCNum.Text;
            strAmount = TBAmount.Text;


            //Create the request Transaction.  Form(nodes)will depend on transaction type
            XmlDocument requestXML = new XmlDocument();
            XmlNode docNode = requestXML.CreateXmlDeclaration("1.0", "UTF-8", null);
            requestXML.AppendChild(docNode);


            XmlNode transactionNode = requestXML.CreateElement("Transaction");
            requestXML.AppendChild(transactionNode);
           
            XmlNode node = requestXML.CreateElement("ExactID");
            node.InnerText = "ADXXXX-01";
            transactionNode.AppendChild(node);
            node = requestXML.CreateElement("Password");
            node.InnerText = "password";
            transactionNode.AppendChild(node);
           
           
            //Request Type.  Set to blank which is default for CC request and capture
            node = requestXML.CreateElement("Transaction_Type");
            node.InnerText = "00";
            transactionNode.AppendChild(node);
            //Amount
            node = requestXML.CreateElement("DollarAmount");
            node.InnerText = HttpUtility.UrlEncode(strAmount); //DD.CC
            transactionNode.AppendChild(node);
            //Card Number
            node = requestXML.CreateElement("Card_Number");
            node.InnerText = StrCreditCardNumber; //4111111111111111
            transactionNode.AppendChild(node);
            //Expiry
            node = requestXML.CreateElement("Expiry_Date");
            node.InnerText = ListBox1.SelectedValue + ListBox2.SelectedValue; //MMYY
            transactionNode.AppendChild(node);
            //Cardholders Name
            node = requestXML.CreateElement("CardHoldersName");
            node.InnerText = strFirstName+" "+ strLastName;  //Go Canucks
            transactionNode.AppendChild(node);
            /*
            //CVV
            node = requestXML.CreateElement("VerificationStr2");
            node.InnerText = TextBoxCCV.Text;
            transactionNode.AppendChild(node);
            //CVV
            node = requestXML.CreateElement("CVD_Presence_Ind");
            node.InnerText = "1";
            transactionNode.AppendChild(node);
            */
            //convert to XML string
            string post_string = requestXML.OuterXml;
           
  //if you want basic authentication instead of sending ID and Pass in message body
            // create an HttpWebRequest object to communicate with E-xact
            byte[] encbuff = System.Text.Encoding.UTF8.GetBytes("ADXXXX-01:password");
            encoded = Convert.ToBase64String(encbuff);


            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(post_url);
            objRequest.Method = "POST";
            //objRequest.Accept = "application/xml; charset=utf-8";
            objRequest.ContentLength = post_string.Length;
            objRequest.ContentType = "application/xml";
            //objRequest.Headers["Authorization"] = "Basic " + encoded; //if you want basic authentication  //don't do both Authenticate OR xml body


            // post data is sent as a stream
            StreamWriter myWriter = null;
            myWriter = new StreamWriter(objRequest.GetRequestStream());
            myWriter.Write(post_string);
            myWriter.Close();


          
            // returned values are returned as a stream, then read into a string
            String post_response;
            try
            {
                HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
                using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
                {
                    post_response = responseStream.ReadToEnd();
                    responseStream.Close();
                }
                //if post response is XML
                System.Xml.XmlDocument doc = new XmlDocument();
                doc.LoadXml(post_response);
               
  //get data for response
                XmlNode root = doc.DocumentElement;


                XmlNode responseParser = root.SelectSingleNode("Transaction_Approved");
                if (responseParser.InnerText.Equals("true"))
                {
                    responseParser = root.SelectSingleNode("Authorization_Num");
                    string strAuthNum = responseParser.InnerText;
                    responseParser = root.SelectSingleNode("DollarAmount");
                    string amount = responseParser.InnerText;
                   
                }
                else
                {
                    responseParser = root.SelectSingleNode("Bank_Message");
                    string serverMessage = responseParser.InnerText;
                }
               
            }


}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article