Authorize.Net Integration using C# / ASP.NET

I was doing payment integration for one of the site. The client uses authorize.net and I could find a wonderful code implemented by Mikhail Esteves

The code goes like this.

private bool AuthorizePayment(string FirstName, string LastName, string Address,
    string City, string State, string ZIP, string Country, double Amount)
  string AuthNetVersion = "3.1"; // Contains CCV support
  string AuthNetLoginID = "YourLoginID";
  string AuthNetPassword = "YourPassword";
  // Get this from your authorize.net merchant interface
  string AuthNetTransKey = "YourTransactionKey";

  WebClient objRequest = new WebClient();
  System.Collections.Specialized.NameValueCollection objInf =
    new System.Collections.Specialized.NameValueCollection(30);
  System.Collections.Specialized.NameValueCollection objRetInf =
    new System.Collections.Specialized.NameValueCollection(30);
  byte[] objRetBytes;
  string[] objRetVals;
  string strError;

  objInf.Add("x_version", AuthNetVersion);
  objInf.Add("x_delim_data", "True");
  objInf.Add("x_login", AuthNetLoginID);
  objInf.Add("x_password", AuthNetPassword);
  objInf.Add("x_tran_key", AuthNetTransKey);
  objInf.Add("x_relay_response", "False");

  // Switch this to False once you go live
  objInf.Add("x_test_request", "True");

  objInf.Add("x_delim_char", ",");
  objInf.Add("x_encap_char", "|");

  // Billing Address
  objInf.Add("x_first_name", FirstName);
  objInf.Add("x_last_name", LastName);
  objInf.Add("x_address", Address);
  objInf.Add("x_city", City);
  objInf.Add("x_state", State);
  objInf.Add("x_zip", ZIP);
  objInf.Add("x_country", Country);

  objInf.Add("x_description", "Description of Order");

  // Card Details
  objInf.Add("x_card_num", "4111111111111111");
  objInf.Add("x_exp_date", "01/06");

  // Authorisation code of the card (CCV)
  objInf.Add("x_card_code", "123");

  objInf.Add("x_method", "CC");
  objInf.Add("x_type", "AUTH_CAPTURE");
  objInf.Add("x_amount", Amount.ToString());

  // Currency setting. Check the guide for other supported currencies
  objInf.Add("x_currency_code", "USD");

    // Pure Test Server
    objRequest.BaseAddress =

    // Actual Server
    //(uncomment the following line and also set above Testmode=off to go live)
    //objRequest.BaseAddress =
    //  "https://secure.authorize.net/gateway/transact.dll";

    objRetBytes =
      objRequest.UploadValues(objRequest.BaseAddress, "POST", objInf);
    objRetVals =

    if (objRetVals[0].Trim(char.Parse("|")) == "1")
      // Returned Authorisation Code
      this.lblAuthNetCode.Text = objRetVals[4].Trim(char.Parse("|"));
      // Returned Transaction ID
      this.lblAuthNetTransID.Text = objRetVals[6].Trim(char.Parse("|"));

      return true;
      // Error!
      strError = objRetVals[3].Trim(char.Parse("|")) + " (" +
        objRetVals[2].Trim(char.Parse("|")) + ")";

      if (objRetVals[2].Trim(char.Parse("|")) == "44")
        // CCV transaction decline
        strError += "Our Card Code Verification (CCV) returned " +
          "the following error: ";

        switch (objRetVals[38].Trim(char.Parse("|")))
          case "N":
            strError += "Card Code does not match.";
          case "P":
            strError += "Card Code was not processed.";
          case "S":
            strError += "Card Code should be on card but was not indicated.";
          case "U":
            strError += "Issuer was not certified for Card Code.";

      if (objRetVals[2].Trim(char.Parse("|")) == "45")
        if (strError.Length>1)
          strError += "<br />n";

        // AVS transaction decline
        strError += "Our Address Verification System (AVS) " +
          "returned the following error: ";

        switch (objRetVals[5].Trim(char.Parse("|")))
          case "A":
            strError += " the zip code entered does not match " +
              "the billing address.";
          case "B":
            strError += " no information was provided for the AVS check.";
          case "E":
            strError += " a general error occurred in the AVS system.";
          case "G":
            strError += " the credit card was issued by a non-US bank.";
          case "N":
            strError += " neither the entered street address nor zip " +
              "code matches the billing address.";
          case "P":
            strError += " AVS is not applicable for this transaction.";
          case "R":
            strError += " please retry the transaction; the AVS system " +
              "was unavailable or timed out.";
          case "S":
            strError += " the AVS service is not supported by your " +
              "credit card issuer.";
          case "U":
            strError += " address information is unavailable for the " +
              "credit card.";
          case "W":
            strError += " the 9 digit zip code matches, but the " +
              "street address does not.";
          case "Z":
            strError += " the zip code matches, but the address does not.";

      // strError contains the actual error
      lblMsg1.Text = strError;
      return false;
  catch (Exception ex)
    lblMsg1.Text = ex.Message;
    return false;

But I was getting this error everytime. “The merchant login ID or password is invalid or the account is inactive. (13)”
The account was in test mode and the credentials I provided was correct. After some googling I found to change the url to
https://test.authorize.net/gateway/transact.dll&#8221;. That also didn’t work. Atlast I have changed the url to "https://secure.authorize.net/gateway/transact.dll"
and it got worked. The thing is the account is already in test mode and we don’t need to change the url to any other.
So simple isn’t it. 🙂 . Anyway it worked for me at last.


2 thoughts on “Authorize.Net Integration using C# / ASP.NET

  1. Hi,

    I am using the CIM integration.Please can you give me any code to validate the customer credit cardnumber,cvv code and expiary date..Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s