Merchant Account Services

Integrate the Authorize.Net Recurring Billing API with PHP

Establish subscriptions and recurring payments for your website


Author: Jim Conners

Rating: 10.0

Pages: 1|2|3|4|5|6|7|8|9

Building Our Class

Now that we have an understanding of what we need to accomplish let's write the code that will do it. The natural place to start is with the class that will handle the bulk of the work.

What not put everything in one class?

While it might seem like a good idea to combine the ARB API and AIM API into one class it isn't quite as easy as it sounds. Unfortunately each API is very different. The AIM API uses POST data just like a regular form would use. The ARB API uses XML to send and receive data. This makes how we handle that data a very different process.

Naming The Class

In our previous article we named our class after Authorize.Net's nickname 'Authnet'. We will follow that standard again here but with a twist. Since we can't have two classes with the same name we will call our new class 'AuthnetARB'. This should help to clarify that this script is for the ARB API.

class AuthnetARB { // ... }

Class Properties

Now that we have a shell for our class we should declare the properties will need to store the information we will use to facilitate our transaction as well as store the results of that transaction. We will start by declaring our login and password

private $login = "ertdev6345"; private $transkey = "SR2dsf54dEn7vFLQ";

Your login and transaction key will be different. You can get your API credentials from your Authorize.Net control panel under the 'Settings > Transaction Key' menu options.

PHP 4 compatibility

To make your class PHP 4 compatible simply change the 'private' declaration to be 'var'. That's it!

private $params = array();

This property will store all of the information that we will be sending to Authorize.Net. We will store it in an array so it is easy to set and retrieve information when we need to.

private $success = false; private $error = true;

These two parameters allow us to store the state of our transaction which we can access through methods from our code. The $success parameter stores whether we successfully communicated with the ARB API. The $error parameter stores whether there was an error in our transaction. This is set to 'true' so if for some reason our code fails we do not have to worry about setting it to 'true' since it already is.

private $xml;

This parameter will store the XML that we will send to Authorize.Net. This XML will store the parameters of our transaction and present it in a format that the ARB API is expecting. It will be set by a method that determines what kind of transaction we will be running (create account, edit account, or delete account).

private $response; private $resultCode; private $code; private $text; private $subscrId;

These parameters each store different values returned to us by Authorize.Net. $response holds the response sent to use by Authorize.Net. The rest hold values we derive from that response.

$resultCode will contain a value of 'Ok' if your transaction was successful. This is the parameter we will check to see if our transaction was successful.

$code will store the error code if our transaction fails.

$text will store a description of any errors we incur. In the case of a successful transaction this will say, 'Successful'.

$subscrId holds the subscription ID assigned to this recurring billing subscription. It is unique for each recurring billing customer. If you plan to create an application to edit or delete subscriptions you will need to store this ID.

private $test = true;

This parameter serves only one purpose: to make our life a little bit easier. This parameter allows us to switch back and forth between our test account and our live account. This way we do not need to alter our code each time we want to switch back and forth. We need only change this value and our switch is made.

Assumptions | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Constructor