Authorize.Net Code Release
Back in February I posted the beginnings of a project to wrap Authorize.Net credit card transactions in C# .NET code. I have been working on this project off and on, in conjunction with a new website we are developing, and have been meaning to post the production version for some time.
Today, I am publishing the current version of the code, DevelopingForDotNet.AuthorizeNet, along with a few supporting updates. I’d like to thank everyone who posted comments on that entry and the First Foray into Unit Testing entry. Most of those suggestions made it into the final version and I learned a lot about Unit Testing along the way.
This version is slightly different than the original post. Here are the major differences:
Validity Checking
This version incorporates validity checking on the following TransactionRequest class properties:
- Zip
- SecurityCode
- CardNumber
- ExpDate
EMail, CardNumber, and ExpDate validation have been completely rewritten.
ExpDate now accepts the following formats:
- MMYY
- MM/YY
- MM-YY
- MMYYYY
- MM/YYYY
- MM-YYYY
The Validity Checking uses a set of Regular Expression patterns that I have put into another namespace, DevelopingForDotNet.RegexSupport. It will be available on the Free Code page as well.
Right now, all the failures throw an ArgumentException, which is very heavy handed but I haven’t had a need to improve it yet.
INotifyPropertyChanged
Each of the three classes implements INotifyPropertyChanged so you can use them for data binding if you wish. If you have never implemented this interface before, it is very easy. Add a reference to System.ComponentModel to your code. Then add the inheritance statement to your class:
public class TransactionRequestInfo : INotifyPropertyChanged { ... }
Then implement the PropertyChangedEventHandler and add a method to fire the event:
public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
Then call the method whenever a property changes:
public string FirstName { get { return _first; } set { _first = value; OnPropertyChanged("FirstName"); } }
Easy as can be!
Transaction.ProcessPayment
Transaction is a static class with ProcessPayment as its single static method. In reviewing the project, I realized that this was a perfect case for an Extension Method, so I added this before the first keyword, and now calling the method is even nicer than before:
// Account is built above... TransactionResponseInfo pmtResponse = pmtInfo.ProcessPayment(Account);
Unit Testing
I really got my feet wet with Unit Testing on this project. I followed the advice I got from FreekShow and implemented the testing of Exceptions in a much cleaner fashion. The whole experience got me thinking about why Unit Testing is so beneficial, and as I began rewriting the code I started by writing tests that fail first and then coding my way into success. Just for grins, the testing project for this solution is included in the download.
How to tie this class to the asp.net form?
I am not an ASP.NET Web Forms developer, so I can’t say for sure. In my case, this was entirely server side code implemented in an ASP.NET MVC application. The Controller class instantiates and executes the Authorize.Net functionality, and then drives the View to the appropriate page based on the results. I assume something similar could be done with ASP.NET.
Hey Joel,thanks for provide wonderful guideline.
I tried to open the link https://developingfor.net/wp-content/uploads/OpenSource/DevelopingForDotNet.AuthorizeNet.zip but say “Sorry, no posts matched your criteria. ”
Will you please check this link ?
Sorry, none of the download links are currently working.
I’m new to all of this. I’m wondering if you know much about some of the latest Payment Processing Software ?
No, I’m not familiar with that.