Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

A Wrapper for Using .NET to Submit Credit Card Transactions via the Authorize Gateway

4.38/5 (9 votes)
11 Apr 2008CPOL1 min read 1   329  
A .NET wrapper for Authorize

Introduction

Recently, I had to write a website that processes credit card transactions, and I was shocked to find that Authorize, one of the larger payment gateways, did not provide any sort of .NET API. They support HTTP Posting of a string that defines the transaction, and they send back a string that describes the result. So, I wrote a wrapper class for use in our website, and decided to offer it here for anyone else that may need it. (It's also a good way to get peer review of code that I'm using....)

Using the Code

This is meant to be used as a library. The solution has two projects in it, one is the Authorize code as a DLL, the other is a console app that simply demonstrates how the library is used. Authorize requires three pieces of information, which are your login to their system. These are provided by static properties, then you build a request object, set its properties, add line items and then send the transaction, getting back a response which will tell you if the process succeeded or failed, and why.

One property which I didn't expose is this:

C#
private bool _testRequest = false;

Setting this will alternate between using the test and live server, so I expect a user would set it to true during testing, then back for release. The Authorize docs specify several URLs for test and live server, the ones in my code were correct at the time of printing, I will obviously become aware if they change it, and endeavour to keep it up to date.

What follows is the code of my sample app, showing how the library is to be used:

C#
    //first set the properties
    AuthorizeRequest.Login = "myLogin";
    AuthorizeRequest.Password = "MyPassword";
    AuthorizeRequest.TransactionKey = "whatever";

    // optionally set other settings
    AuthorizeRequest.Operation = AuthorizeRequest.Type.AUTH_CAPTURE;
    AuthorizeRequest.TransactionType = AuthorizeRequest.Method.CC;

    // now build the request
    AuthorizeRequest request = new AuthorizeRequest();

    request.ClientFirstName = "fred";
    request.ClientLastName = "Jones";
    request.CompanyName = "Acme";
    request.Address = "Address";
    request.City = "city";    
    request.Country = "India";

    request.CardNumber = "number";
    request.ExpDate = "12/2008";
    request.Ccv = "CCV is not used unless you set it up on the Authorize end (" +
                  " this costs extra )";
    
    request.ClientEmail = "this is for a receipt emailed by Authorize";
    request.MerchantEmail = "this is for an email sent to the merchant";

    // Now add the items to be purchased.
    request.LineItems.Add(new AuthorizeRequest.LineItem("001", "product standard", 
                                              "product standard", 1, 19.95, false));
    request.LineItems.Add(new AuthorizeRequest.LineItem("002", "product upgrade", 
                                              "product upgrade", 1, 9.95, false));

    // finally add the price
    request.Freight = 9.95;

    // Note the shipping is specified separately for invoicing purposes, but Amount is 
    // the exact amount that will be charged. This includes tax, you need to calculate 
    // it separately, if need be.
    request.Amount = (Decimal)(19.95 + 9.95 + 9.95); 

    // Now send the transaction
    AuthorizeResponse response = AuthoriseTransaction.SendRequest(request);

    if (response.TransactionResult != AuthorizeResponse.Result.Approved)
    {
        // work out what went wrong
    }
    else
    {
        // provide the transaction Id to the user
    }
}

History

  • 11th April, 2008: Version 1.0 - Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)