Click here to Skip to main content
15,884,472 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
4.38/5 (9 votes)
11 Apr 2008CPOL1 min read 41.5K   328   44   10
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)


Written By
Software Developer (Senior)
Australia Australia
Programming computers ( self taught ) since about 1984 when I bought my first Apple ][. Was working on a GUI library to interface Win32 to Python, and writing graphics filters in my spare time, and then building n-tiered apps using asp, atl and asp.net in my job at Dytech. After 4 years there, I've started working from home, at first for Code Project and now for a vet telemedicine company. I owned part of a company that sells client education software in the vet market, but we sold that and I worked for the owners for five years before leaving to get away from the travel, and spend more time with my family. I now work for a company here in Hobart, doing all sorts of Microsoft based stuff in C++ and C#, with a lot of T-SQL in the mix.

Comments and Discussions

 
QuestionHow to add Payment Gateway in Windows application Pin
Member 1103375815-Dec-15 23:53
Member 1103375815-Dec-15 23:53 
QuestionIs it susceptible to man in the middle attack? Pin
zyxeasy23-Jul-10 2:00
zyxeasy23-Jul-10 2:00 
GeneralThanks - and a little help please. Pin
Glen Harvy18-Jun-08 12:59
Glen Harvy18-Jun-08 12:59 
GeneralConsumers Beware Pin
terphi19-May-08 12:29
terphi19-May-08 12:29 
The AuthorizeRequest sends the email "christian.graus@gmail.com" as the merchant email in the request if it is not specifically overridden. At best this is annoying, at worst it is a security threat since this email will be associated with live transactions.
Questionreal account?? Pin
SuperEric17-May-08 0:57
SuperEric17-May-08 0:57 
GeneralLooks Good, but you mispelled Authorize Pin
terphi13-May-08 10:56
terphi13-May-08 10:56 
GeneralCool Pin
leppie11-Apr-08 22:47
leppie11-Apr-08 22:47 
GeneralNice! Pin
Ravi Bhavnani11-Apr-08 19:02
professionalRavi Bhavnani11-Apr-08 19:02 
GeneralRe: Nice! Pin
Christian Graus11-Apr-08 22:18
protectorChristian Graus11-Apr-08 22:18 
GeneralNice and clean Pin
Axel Rietschin11-Apr-08 15:12
professionalAxel Rietschin11-Apr-08 15:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.