Wednesday, April 9, 2014

C# ATOM Payment Gateway Implementation

  1. Payment Request Page

    protected void Page_Load(object sender, EventArgs e)
    {
    try
    {           
        string ATOMPaymentIP = ATOMPaymentIP // PROVIDED BY ATOM //203.114.240.183--Stage
        string ATOMLogin = ATOMLogin // PROVIDED BY ATOM
        string ATOMPassword = ATOMPassword // PROVIDED BY ATOM
        string ATOMTransactionType = ATOMTransactionType // PROVIDED BY ATOM
        string ATOMProductId = ATOMProductId // PROVIDED BY ATOM

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + ATOMPaymentIP + "/paynetz/epi/fts");

        request.Method = "POST";

        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; CK={CVxk71YSfgiE6+6P6ftT7lWzblrdvMbRqavYf/6OcMIH8wfE6iK7TNkcwFAsxeChX7qRAlQhvPWso3KI6Jthvnvls9scl+OnAEhsgv+tuvs=}; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
        int pos = Request.Url.AbsoluteUri.LastIndexOf('/');
       
        // PAYMENT RESPONSE PAGE
        string strURL = Request.Url.AbsoluteUri.Substring(0, pos + 1) + "PaymentResponseATOM.aspx";
       
        string postData = "custacc=1234567890&login=" + ATOMLogin + "&pass=" + ATOMPassword + "&ttype=" + ATOMTransactionType + "&prodid=" + ATOMProductId + "&amt=" + PAYMENT AMOUNT + "&txncurr=INR&txnscamt=0&clientcode=" + CLIENT CODE(USER DEFINED) + "=&txnid=" + TRANSACTION ID + "&date=" + DateTime.Now.ToString("dd/mm/yyyy hh:mm:ss") + "&ru=" + strURL + "&udf1=" + FIRST NAME + "&udf2=" + EMAIL OF CUSTOMER + "&udf3=" + CUSTOMER MOBILE + "&udf4=" + CUSTOMER ADDRESS + "&udf5=" + EMI OPTION IF REQUIRED(BANK NAME) + "&udf6=" + EMI OPTION IF REQUIRED;

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        request.ContentType = "application/x-www-form-urlencoded";

        request.ContentLength = byteArray.Length;
        request.AllowAutoRedirect = true;

        request.Proxy.Credentials = CredentialCache.DefaultCredentials;

        Stream dataStream = request.GetRequestStream();

        dataStream.Write(byteArray, 0, byteArray.Length);

        dataStream.Close();

        WebResponse response = request.GetResponse();

        XmlDocument objXML = new XmlDocument();

        dataStream = response.GetResponseStream();

        objXML.Load(dataStream);

        string TxnId = objXML.DocumentElement.ChildNodes[0].ChildNodes[0].ChildNodes[2].InnerText;

        string Token = objXML.DocumentElement.ChildNodes[0].ChildNodes[0].ChildNodes[3].InnerText;
        string txnData = "ttype=NBFundTransfer&txnStage=1&tempTxnId=" + TxnId + "&token=" + Token;

        dataStream.Close();
        response.Close();
        Response.Redirect("http://" + ATOMPaymentIP + "/paynetz/epi/fts?" + txnData);
    }
    catch (Exception ex)
    {
        HANDLE ERROR   
    }
    }
  2. Payment Response Page

    protected void Page_Load(object sender, EventArgs e)
    {
    try
    {
        if (!IsPostBack)
        {
        NameValueCollection nvc = Request.Form;
        string mmp_Transaction = nvc["mmp_txn"].ToString();
        string PaymentStatus = nvc["f_code"].ToString();
        string MerchantTransactionId = Request.Form["mer_txn"];

        string Amount = Request.Form["amt"];
        string Product = Request.Form["prod"];
        string TransactionDate = Request.Form["date"];
        string BankTransactionId = Request.Form["bank_txn"];
        string ClientCode = Request.Form["clientcode"]; // Encrypted User Information
        string BankName = Request.Form["bank_name"];

        //User Details
        string Udf1 = Request.Form["udf1"]; //First Name
        string Udf2 = Request.Form["udf2"]; //Email
        string Udf3 = Request.Form["udf3"]; //Mobile
        string Udf4 = Request.Form["udf4"]; //Address
        string Udf5 = Request.Form["udf5"]; //Bank Name
        string Udf6 = Request.Form["udf6"]; //EMI Option

        string strResponceIP = HttpContext.Current.Request.UserHostAddress;
        string strRemarks = "txnId:" + MerchantTransactionId + ", txnStatus:" + PaymentStatus + ", amount:" + Amount + ", pgTxnId:" + mmp_Transaction + ", BankTransactionId:" + BankTransactionId + ", Udf1:" + Udf1 + ", Udf2:" + Udf2 + ", Udf3:" + Udf3 + ", Udf4:" + Udf4 + ", Udf5:" + Udf5 + ", Udf6:" + Udf6;

       
        string ATOMPaymentIP = ConfigurationManager.AppSettings["ATOMPaymentIP"].ToString();//203.114.240.183--Stage
        //Verify the Response Server
        if (Request.UrlReferrer.Host.Trim() != ATOMPaymentIP)
        {
            // REDIRECT TO ERROR PAGE IF RESPONSE IP IS NOT VERIFIED
        }

        // CHECK THE PAYMENT STATUS AND VALIDATE THE TRANSACTION ID
        if (PaymentStatus.ToUpper().Trim() == "OK" && transactionid.Trim()==MerchantTransactionId.Trim())
        {
            //"Transaction proceed successfully."

            // UPDATE THE TRANSACTION STATUS IN DB
            //
            // CODE FOR UPDATE STATUS
            //
            //
           
            // REDIRECT TO SUCCESS PAGE OR SHOW THE PAYMENT RESPONSE ON SAME PAGE   

        }
        else
        {
            // PAYMENT STATUS IS NOT SUCCESS OR TRANSACTION ID IS NOT VERIFIED
            // REDIRECT TO ERROR PAGE
        }
        }
    }
    catch (Exception ex)
    {
        // HANDLE ERROR
    }
    }

17 comments:

  1. The underlying connection was closed: An unexpected error occurred on a send.
    pls give me solution

    ReplyDelete
  2. what is parmeter ......is it parameter :)

    ReplyDelete
  3. how to redirect to localhost to test or debug after payment success or cancel

    ReplyDelete
    Replies
    1. Please create a sample page with temp data and post them to the action url

      Delete
  4. how to receive and store payment response into database after TransactionCompletion ?

    ReplyDelete
    Replies
    1. On the Payment Response page, capture the data from Request.Form object and store them in DB.

      For ex.
      string Amount = Request.Form["amt"];

      by this way you can get amount, check the Requst.Form for other values.

      Thanks

      Delete
  5. creativity of writer is purely impressive. It has touched to the level of expertise with his writing. Everything is up to the mark. Written perfectly and I can use such information for my coming assignment.
    cursus website design amsterdam

    ReplyDelete
  6. This blog is really helpful regarding all educational knowledge I earned. It covered a great area of subject which can assist a lot of needy people. Everything mentioned here is clear and very useful.
    opleiding paragnost

    ReplyDelete
  7. At some point couple of instructive web journals turn out to be exceptionally useful while getting significant and new data identified with your focused on region. As I discovered this blog and welcome the data conveyed to my database. zona plus

    ReplyDelete
  8. Graceful written content on this blog is really useful for everyone same as I got to know. Difficult to locate relevant and useful informative blog as I found this one to get more knowledge but this is really a nice one.cursus website bouwen amsterdam

    ReplyDelete
  9. Web customers locate the Online Payments framework profoundly useful. They can buy various types of products online to encourage their necessities and wants.

    ReplyDelete
  10. This blog is really helpful to pass on redesignd enlightening endeavors over web which is genuinely examination. I found one productive instance of this truth through this blog. Trustpay app

    ReplyDelete
  11. We offer Online payment services in Saudi Arabia. Our service enables schools to accept fees and housing societies' to accept their payments and credit card payments via mobile. Saudi arabia payment app

    ReplyDelete
  12. Your enthusiasm leads you beyond the limits. whilst you feel yourself enthusiastic that’s the time you can pass any restrict. you're searching out to get perfection through the usage of the functionality of exertions. study such motivational article and truely it'll permit you to recognize new data. Trustpay app

    ReplyDelete
  13. someplace the content material of the weblog surrounded through little arguments. yes it's far wholesome for readers. they are capable of encompass this form of language in their writing talent similarly to on the same time as group dialogue in university. Saudi arabia payment app

    ReplyDelete
  14. This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine. White Label Payment Service Provider

    ReplyDelete
  15. Nice knowledge gaining article. This post is really the best on this valuable topic. North American Bancard Agent Program

    ReplyDelete