Wednesday, 3 July 2013

How to implement the First Data Global Gateway E4 in asp.net

How to implement the First Data Global Gateway E4 in asp.net:


Some people say the first data's support is not good, so to help others i am posting this blog, First of all why we go for First Data Global Gateway E4 payment, because the first data has don't support for 64 bit windows server. so utilize the first data's service along with the windows 64 bit server we need to move for global E4 payment gateway.

Here is whole code to implement or utilize the Web API.

 
protected void Button1_Click(object sender, EventArgs e)

    {

        StringBuilder string_builder = new StringBuilder();

        using (StringWriter string_writer = new StringWriter(string_builder))

        {

            using (XmlTextWriter xml_writer = new XmlTextWriter(string_writer))

            {     //build XML string

                xml_writer.Formatting = Formatting.Indented;

                xml_writer.WriteStartElement("Transaction");

                xml_writer.WriteElementString("ExactID", "***********");//Gateway ID

                xml_writer.WriteElementString("Password", "***********");//Password

                xml_writer.WriteElementString("Transaction_Type", "00");

                xml_writer.WriteElementString("DollarAmount", "22.66");

                xml_writer.WriteElementString("Expiry_Date", "1214");

               // xml_writer.WriteElementString("CardType", "Gift");

                xml_writer.WriteElementString("CardHoldersName", "Visions Test");

                xml_writer.WriteElementString("Card_Number", "340000000000009");

                xml_writer.WriteElementString("VerificationStr2", "123");

                xml_writer.WriteEndElement();

                String xml_string = string_builder.ToString();

                //SHA1 hash on XML string

                ASCIIEncoding encoder = new ASCIIEncoding();

                byte[] xml_byte = encoder.GetBytes(xml_string);

                SHA1CryptoServiceProvider sha1_crypto = new SHA1CryptoServiceProvider();

                string hash = BitConverter.ToString(sha1_crypto.ComputeHash(xml_byte)).Replace("-", "");

                string hashed_content = hash.ToLower();

                //assign values to hashing and header variables

                string method = "POST\n";

                string type = "application/xml\n";//REST XML

                string time = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");

                string url = "/transaction/v12";

                string keyID = "***********";//key ID

                string key = "***********";//Hmac key

                string hash_data = method + type + hashed_content + "\n" + time + "\n" + url;

                //hmac sha1 hash with key + hash_data

                HMAC hmac_sha1 = new HMACSHA1(Encoding.UTF8.GetBytes(key)); //key

                byte[] hmac_data = hmac_sha1.ComputeHash(Encoding.UTF8.GetBytes(hash_data)); //data

                //base64 encode on hmac_data

                string base64_hash = Convert.ToBase64String(hmac_data);

                //begin HttpWebRequest // use https://api.globalgatewaye4.firstdata.com/transaction/v12 for production

                HttpWebRequest web_request = (HttpWebRequest)WebRequest.Create("https://api.demo.globalgatewaye4.firstdata.com/transaction/v12");

                web_request.Method = "POST";

                web_request.Accept = "application/xml";

                web_request.Headers.Add("x-gge4-date", time);

                web_request.Headers.Add("x-gge4-content-sha1", hashed_content);

                web_request.ContentLength = xml_string.Length;

                web_request.ContentType = "application/xml";

                web_request.Headers["Authorization"] = "GGE4_API " + keyID + ":" + base64_hash;

                // send request as stream

                StreamWriter xml = null;

                xml = new StreamWriter(web_request.GetRequestStream());

                xml.Write(xml_string);

                xml.Close();

                //get response and read into string

                string response_string;

                try

                {

                    HttpWebResponse web_response = (HttpWebResponse)web_request.GetResponse();

                    using (StreamReader response_stream = new StreamReader(web_response.GetResponseStream()))

                    {

                        response_string = response_stream.ReadToEnd();

                        response_stream.Close();

                    }

                    //load xml

                    XmlDocument xmldoc = new XmlDocument();

                    xmldoc.LoadXml(response_string);

                    XmlNodeList nodelist = xmldoc.SelectNodes("TransactionResult");

                    //bind XML source DataList control

                    DataList1.DataSource = nodelist;

                    DataList1.DataBind();

                    //output raw XML for debugging

                   

                    request_label.Text = web_request.Headers.ToString() + System.Web.HttpUtility.HtmlEncode(xml_string);

                    response_label.Text = web_response.Headers.ToString() + System.Web.HttpUtility.HtmlEncode(response_string);

                }

                catch (System.Exception ex)

                {

                    error.Text = ex.ToString();

                }

            }

        }

    }
 
 
Html(.aspx) page is:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>
</body>
</html>