How to do Credit Card Billing With Authorize.net and C#

Discussion in 'ASP.NET / ASP.NET Core' started by Takeshi Eto, Jul 30, 2005.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Takeshi Eto

    Takeshi Eto DiscountASP.NET Staff

    To clarify: IPWorks deprecated the CCICharge component and they folded this into their new iBiz component suites. Our product team was unable to work out a partnership for the iBiz components and the older CC ICharge component is no longer available for download off of the IPWorks website.

    DiscountASP.NET
    http://www.DiscountASP.NET
     
  2. Hi all,

    It turns out that the DiscountASP admins weren't able to reach an agreement with IPWorks about a server license for the "CCharge" credit card billing component. As a result, some of the newer hosting servers do not have CCharge installed, forcing us to do billing manually or buy their $99 license. After some researching, I discovered some C# code which will perform credit card billingwith Authorize.net. Irewrote the code into a class, and hopefully this will help you get up to speed quickly. Be sure to supply your Username, Password and Transaction key in the private variables below before attempting transactions.

    I am going to work on a similar class for PayPal over the next couple of weeks. The SDK offered by PayPal is confusing (if you ask me) and it would be nice to have a well-written object-oriented interface for PayPal.

    Reply here or email me at [email protected] if you find any bugs. Enjoy!




    using System;


    using System.Text;


    using System.Net;


    using System.Web;


    using System.Collections.Specialized;





    namespace MyNamespace


    {


    /// <summary>


    /// Summary description for AuthorizeNet.


    /// </summary>


    public sealed class AuthorizeNet


    {


    #region Private Variables


    // Enables or disables test mode. When enabled, no credit cards are actually


    // billed.


    #if TESTMODE


    private bool pIsTestModeEnabled = false;


    #else


    private bool pIsTestModeEnabled = true;


    #endif


    // Authentication information for Authorize.net


    private string pUsername = "";


    private string pPassword = "";


    private string pTransactionKey = "";


    private string pVersion = "3.1";


    private string pCurrency = "USD";


    private string pOrderDescription = "MyCompany Software Purchase [MyPhoneNumber]";


    // Ordering information. These parameters are set before performing a billing


    // operation using the Authorize() method.


    private string pFirstName;


    private string pLastName;


    private string pAddress;


    private string pCity;


    private string pState;


    private string pZip;


    private string pCountry;


    private float pAmount;


    private string pCardNumber;


    private DateTime pCardExpirationDate;


    private string pCardCvvCode;


    private string pAuthorizationCode;


    private int pTransactionId;


    #endregion


    #region Constructors


    public AuthorizeNet()


    {


    }


    #endregion


    #region Public Properties


    public string Version


    {


    get


    {


    return pVersion;


    }


    }


    public bool IsTestModeEnabled


    {


    get


    {


    return pIsTestModeEnabled;


    }


    set


    {


    pIsTestModeEnabled = value;


    }


    }


    public string FirstName


    {


    get


    {


    return pFirstName;


    }


    set


    {


    pFirstName = value;


    }


    }


    public string LastName


    {


    get


    {


    return pLastName;


    }


    set


    {


    pLastName = value;


    }


    }





    public string Address


    {


    get


    {


    return pAddress;


    }


    set


    {


    pAddress = value;


    }


    }





    public string City


    {


    get


    {


    return pCity;


    }


    set


    {


    pCity = value;


    }


    }


    public string State


    {


    get


    {


    return pState;


    }


    set


    {


    pState = value;


    }


    }


    public string Zip


    {


    get


    {


    return pZip;


    }


    set


    {


    pZip = value;


    }


    }





    public string Country


    {


    get


    {


    return pCountry;


    }


    set


    {


    pCountry = value;


    }


    }


    public float Amount


    {


    get


    {


    return pAmount;


    }


    set


    {


    pAmount = value;


    }


    }


    public string CardNumber


    {


    get


    {


    return pCardNumber;


    }


    set


    {


    pCardNumber = value;


    }


    }





    public DateTime CardExpirationDate


    {


    get


    {


    return pCardExpirationDate;


    }


    set


    {


    pCardExpirationDate = value;


    }


    }





    public string CardExpirationString


    {


    get


    {


    // Get a two-digit year


    int pExpirationMonth = pCardExpirationDate.Month;


    int pExpirationYear = pCardExpirationDate.Year - 2000;


    return pExpirationMonth.ToString().PadLeft(2, '0') + "/"


    + pExpirationYear.ToString().PadLeft(2, '0');


    }


    }


    public string CardCvvCode


    {


    get


    {


    return pCardCvvCode;


    }


    set


    {


    pCardCvvCode = value;


    }


    }


    public string OrderDescription


    {


    get


    {


    return pOrderDescription;


    }


    set


    {


    pOrderDescription = value;


    }


    }





    public string AuthorizationCode


    {


    get


    {


    return pAuthorizationCode;


    }


    }


    public int TransactionId


    {


    get


    {


    return pTransactionId;


    }


    }


    #endregion


    #region Public Methods


    public bool ChargeCard()


    {


    // Create a client to service the request


    WebClient AuthorizationRequest = new WebClient();


    try


    {


    // Create a collection to store parameters to send off to Authorize.net


    NameValueCollection RequestParameters = new NameValueCollection(24);





    // Set up basic configuration information for the request


    RequestParameters.Add("x_version", pVersion);


    RequestParameters.Add("x_delim_data", "True");


    RequestParameters.Add("x_login", pUsername);


    RequestParameters.Add("x_password", pPassword);


    RequestParameters.Add("x_tran_key", pTransactionKey);


    RequestParameters.Add("x_relay_response", "False");


    RequestParameters.Add("x_delim_char", ",");


    RequestParameters.Add("x_encap_char", "|");


    RequestParameters.Add("x_method", "CC");


    RequestParameters.Add("x_type", "AUTH_CAPTURE");


    RequestParameters.Add("x_currency_code", pCurrency);


    // Billing Address


    RequestParameters.Add("x_first_name", pFirstName);


    RequestParameters.Add("x_last_name", pLastName);


    RequestParameters.Add("x_address", pAddress);


    RequestParameters.Add("x_city", pCity);


    RequestParameters.Add("x_state", pState);


    RequestParameters.Add("x_zip", pZip);


    RequestParameters.Add("x_country", pCountry);


    RequestParameters.Add("x_description", pOrderDescription);


    // Are we using the test server?


    if(pIsTestModeEnabled)


    {


    // Yes. No actual billing will take place.


    AuthorizationRequest.BaseAddress = "https://certification.authorize.net/gateway/transact.dll";


    RequestParameters.Add("x_card_num", "4111111111111111");


    RequestParameters.Add("x_exp_date", "01/06");


    RequestParameters.Add("x_card_code", "123");


    RequestParameters.Add("x_test_request", "True");


    }


    else


    {


    // No. The credit card will be billed!


    AuthorizationRequest.BaseAddress = "https://secure.authorize.net/gateway/transact.dll";


    RequestParameters.Add("x_card_num", pCardNumber);


    RequestParameters.Add("x_exp_date", CardExpirationString);


    RequestParameters.Add("x_card_code", pCardCvvCode);


    RequestParameters.Add("x_test_request", "False");


    }





    // Set the amount to charge to the credit card


    RequestParameters.Add("x_amount", pAmount.ToString());


    // Send the request out


    byte[] objRetBytes = AuthorizationRequest.UploadValues(AuthorizationRequest.BaseAddress, "POST", RequestParameters);


    // And read the response, splitting variables by commas


    string[] objRetVals = Encoding.ASCII.GetString(objRetBytes).Split(',');





    // Was the transaction approved?


    if (objRetVals[0].Trim('|') == "1")


    {


    // Yes! Get the Authorisation Code


    pAuthorizationCode = objRetVals[4].Trim('|');


    // and the Transaction ID


    pTransactionId = Convert.ToInt32(objRetVals[6].Trim('|'));


    // And return success


    return true;


    }


    else


    {


    // Which error occurred?


    if (objRetVals[2].Trim('|') == "44")


    {


    // CCV transaction decline


    switch (objRetVals[38].Trim('|'))


    {


    case "N":


    throw new CreditCardException("The card verification code is incorrect.");


    case "P":


    throw new CreditCardException("The card verification code was not processed.");


    case "S":


    throw new CreditCardException("The card verification code should have been specified but was not.");


    case "U":


    throw new CreditCardException("The card issuer has not been certified to use card codes.");


    }


    }


    else if (objRetVals[2].Trim('|') == "45")


    {


    switch (objRetVals[5].Trim('|'))


    {


    case "A":


    throw new CreditCardException("The zip code entered does not match the card billing address.");


    case "B":


    throw new CreditCardException("No address verification information was specified.");


    case "E":


    throw new CreditCardException("The address verification system reported an internal error, possibly due to maintenance in progress.");


    case "G":


    throw new CreditCardException("Address verification was not performed because the card is for a bank outside the United States.");


    case "N":


    throw new CreditCardException("The zip code and address both failed an address verification check.");


    case "P":


    throw new CreditCardException("Address verification is not applicable for this kind of transaction.");


    case "R":


    throw new CreditCardException("The address verification system did not respond in a timely manner. The server could be temporarily unavailable. Please try your transaction again in a moment.");


    case "S":


    throw new CreditCardException("Address verification is not supported by the credit card issuer.");


    case "U":


    throw new CreditCardException("Address verification is not available for this credit card.");


    case "W":


    throw new CreditCardException("The nine-digit zip code matches, but the billing address does not match the address on file for the credit card. Please check the address and try again.");


    case "Z":


    throw new CreditCardException("The zip code matches, but the billing address does not match the address on file for the credit card. Please check the address and try again.");


    }


    }


    // Return failure


    return false;


    }


    }


    catch


    {


    throw;


    }


    finally


    {


    // Shut down the WebClient


    if(AuthorizationRequest != null)


    AuthorizationRequest.Dispose();


    }


    }


    #endregion


    }


    public class CreditCardException : Exception


    {


    public CreditCardException(string message) : base(message)


    {


    }


    }


    }
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page