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

Constructor

Our constructor initializes our class and we will use it to set some default variables. Some of these will never change others are just so common we might as well set them here and we can always change them to a more appropriate value later on. Let's look at the constructor and then break it down into pieces.

public function __construct() { $subdomain = ($this->test) ? 'apitest' : 'api'; $this->url = "https://" . $subdomain . ".authorize.net/xml/v1/request.api";
$this->params['interval_unit'] = 'months'; $this->params['interval_length'] = 1; $this->params['startDate'] = date("Y-m-d", strtotime("+ 1 month")); $this->params['totalOccurrences'] = 9999; $this->params['trialOccurrences'] = 0; $this->params['trialAmount'] = 0.00; }

PHP 4 compatability

To make your constructor PHP 4 compatible simply change public function __construct() to be function AuthnetARB(). That's it!

Normally we would start with the constructor but since we do not pass it any parameters it is very straight forward. Instead we will start with how we determine which URL to connect to:

$subdomain = ($this->test) ? 'apitest' : 'api'; $this->url = "https://" . $subdomain . ".authorize.net/xml/v1/request.api";

We start off by checking what the value of our $test property is set to. If it is set to true, we will send our transactions to the test server associated with our test account. If it is set to false we will use the live server.

$this->params['interval_unit'] = 'months'; $this->params['interval_length'] = 1; $this->params['startDate'] = date("Y-m-d", strtotime("+ 1 month")); $this->params['totalOccurrences'] = 9999;

These four parameters tell Authorize.Net the important details about our subscription. This includes how often to charge the user and when to start charging them.

$this->params['interval_unit'] = 'months'; tells Authorize.Net that we wish to use 'months' as our interval unit. (The other valid value here is 'days'). That means our recurring periods will always be referred to in terms of months. This affects the next variable which sets this period for us.

$this->params['interval_length'] = 1; tells Authorize.Net how many "interval_units" must pass before charging the user again. In our case, since $this->params['interval_unit'] is set to 'months' we will be charging our users every one month.

$this->params['startDate'] = date("Y-m-d", strtotime("+ 1 month")); tells Authorize.Net when to start the subscription. This must be the exact date you want the first payment to be charged. If you set it for the same day the transaction is run it will be charged that evening, not one month from that date. That's why we set ours to one month from when the recurring billing subscription is set. You may set this to a later date if you wish to delay the start of the subscription.

$this->params['totalOccurrences'] = 9999; tells Authorize.Net how many times to charge the user. Authorize.Net does allow for subscriptions with no end date. To accomplish this you need to set your parameter to 9999. If you want the subscription to expire after 12 months, set this value to be 12.

$this->params['trialOccurrences'] = 0; $this->params['trialAmount'] = 0.00;

The ARB API allows for special pricing to be set for a variable amount of recurring transactions before regular pricing kicks in. They call it a trial period and you can control how long the trial period lasts and how much to charge during this time. For our example there will be no trial period so we have set ours to be zero for both parameters. If you wish to have a trial period you can either change this later with a special setter method (which we will discuss shortly) or by hard coding it here if it will never change.

Building Our Class | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Methods