Tutorial: Integrate the Authorize.Net ARB API with PHP

This tutorial has been deprecated. You should read the new tutorial, “Tutorial: Integrate All Authorize.Net XML APIs With One Universal PHP Class (AIM, ARB, CIM, Transaction Details)” which offers new code for working with Authorize.Net’s XML APIs including sample code for every API call here.

Just like Authorize.Net’s Advanced Integration Method (AIM) API and Customer Information Manager (CIM) API, working with their Automated Recurring Billing (ARB) API isn’t difficult if you have suitable code to work with. The sample code offered by Authorize.Net will demonstrate how to work with their API but is not production ready. As with their AIM API and CIM API I have created a class that abstracts their ARB API into something easier to use and maintain.

To follow this tutorial and integrate ARB API into your PHP website follow these steps:

  1. If you don’t already have one sign up for a Authorize.Net Developer Account. This will allow you to test your code without having an actual Authorize.Net account or incurring any processing fees.
  2. Download the ARB Integration Guide (for reference)
  3. Download the PHP code for ARB

To begin you will need to include the class using PHP’s require() function:

  1. require('AuthnetARB.class.php');

As with any class/object the first thing we need to do is instantiate it. Up to three parameters are accepted in the constructor with the first two being required. myapilogin is the API login for the account. mYtRaNsaCTiOnKEy is the transaction key generated in the Authorize.Net control panel (or assigned to you with your test account information). If you have an Authorize.Net developer account you can use it with this script by setting the third parameters to TRUE (you may omit the third parameter if you are using this with the live server):

  1. // Set up the subscription. If you are using a live account remove the third parameter.
  2. $subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy', 
  3.                                               AuthnetARB::USE_DEVELOPMENT_SERVER);

Creating a subscription is very straight forward. We only need to set a few parameters before placing our API call. You can see the full list of parameters in the ARB Guide mentioned above. Naturally you will change these to be specific to your subscription:

  1. $subscription->setParameter('amount', 29.99);
  2. $subscription->setParameter('cardNumber', '4111111111111111');
  3. $subscription->setParameter('expirationDate', '2016-12');
  4. $subscription->setParameter('firstName', 'John');
  5. $subscription->setParameter('lastName', 'Conde');
  6. $subscription->setParameter('address', '123 Main Street');
  7. $subscription->setParameter('city', 'Townsville');
  8. $subscription->setParameter('state', 'NJ');
  9. $subscription->setParameter('zip', '12345');
  10. $subscription->setParameter('email', 'fakemail@example.com');
  11.  
  12. // Create the subscription
  13. $subscription->createAccount();

After making our API call we’ll want to verify it was successful and, if so, capture the subscription ID:

  1. // Check the results of our API call
  2. if ($subscription->isSuccessful())
  3. {
  4.     // Get the subscription ID
  5.     $subscription_id = $subscription->getSubscriberID();
  6. }
  7. else
  8. {
  9.     // The subscription was not created!
  10. }

Make sure you save the subscription ID as you will need to refer to it if you wish to update the subscription or delete it later.

By default the class assumes the subscription will be monthly. To change the frequency and length of the subscription you can use code similar to the following examples:

  1. // Put this before $subscription->createAccount(); 
  2. // if you want the subscription to be every three months.
  3. $subscription->setParameter('interval_length', 3);
  4. $subscription->setParameter('startDate', date("Y-m-d", strtotime("+ 3 months")));
  5.  
  6. // Put this before $subscription->createAccount();
  7. // if you want the subscription to be every year.
  8. $subscription->setParameter('interval_unit', 'years');
  9. $subscription->setParameter('startDate', date("Y-m-d", strtotime("+ 1 year")));

interval_length is how many units (i.e. weeks, months, years) you wish the subscription to wait before processing another payment. In the first example we set it to three which means the payment will process every three months. interval_unit sets the unit (i.e. weeks, months, years) you wish to use. In the second we changed it to “years” which means this subscription will be charged annually. startDate sets the date of the first subscription payment. We use PHP’s built in functions strtotime() and date() to make creating the date very easy. All we need to do is pass strtotime() the start date relative to today’s date in plain English. In our first example we want to set the first subscription payment to start in three months so we pass “+ 3 months” to strtotime(). In our second example we want the subscription to start in one year so we pass “+1 year” to strtotime().

By default the class assumes no trial period will occur. To create your subscription with a trial period just use the following snippet of code:

  1. // Put this before $subscription->createAccount(); 
  2. // if you want a trial period of three months for $10.00.
  3. $subscription->setParameter('trialOccurrences', 3);
  4. $subscription->setParameter('trialAmount', 10.00);

A common question concerning subscriptions is how do you handle delayed subscriptions where no money is charged up front? How do you verify the credit card is valid before making the subscription? Naturally you want to make sure the credit card is valid before making the subscription but if you’re not actually charging the first subscription payment up front (i.e. immediately) you won’t know if the credit card is valid. Fortunately verifying the credit card is easy to do. Just use the AIM API to do an Authorization Only. This will verify that the credit card is indeed valid before you create your subscription.

  1. $authorization = new AuthnetAIM('myapilogin', 'mYtRaNsaCTiOnKEy');
  2. $authorization->setTransaction($creditcard, $expiration, 0.00);
  3. $authorization->setTransactionType('AUTH_ONLY');
  4. $authorization->process();
  5.  
  6. if ($authorization->isApproved())
  7. {
  8.     // Set up ARB subscription
  9. }
  10. else if ($authorization->isDeclined())
  11. {
  12.     // The card is not valid
  13. }

The AuthnetARB class takes advantage of exceptions which are new in PHP5. These are only generated when an “exceptional” event occurs. The only exceptional event that might occur when using this class is cURL may fail. This may be due to server, network, or user errors. To catch these exceptions and handle them use code similar to this:

  1. try
  2. {
  3.     $cim = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy');
  4.     // Do more ARB stuff here
  5. }
  6. catch (AuthnetARBException $e)
  7. {
  8.     echo 'There was an error processing the transaction. Here is the error message: ';
  9.     echo $e->__toString();
  10. }

Now that we have an idea of how to create subscriptions using the ARB API let’s see a practical example that sets the billing cycle to be every three months and has a trial subscription for three months:

  1. < ?php
  2. // Include AuthnetCIM class. Nothing works without it!
  3. require('AuthnetARB.class.php');
  4.  
  5. // Use try/catch so if an exception is thrown we can catch it 
  6. // and figure out what happened
  7. try
  8. {
  9.     // Set up the subscription. Use the developer account for testing..
  10.     $subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy', 
  11.                                               AuthnetARB::USE_DEVELOPMENT_SERVER);
  12.  
  13.     // Set subscription information
  14.     $subscription->setParameter('amount', 29.99);
  15.     $subscription->setParameter('cardNumber', '4111111111111111');
  16.     $subscription->setParameter('expirationDate', '2016-16');
  17.     $subscription->setParameter('firstName', 'John');
  18.     $subscription->setParameter('lastName', 'Conde');
  19.     $subscription->setParameter('address', '123 Main Street');
  20.     $subscription->setParameter('city', 'Townsville');
  21.     $subscription->setParameter('state', 'NJ');
  22.     $subscription->setParameter('zip', '12345');
  23.     $subscription->setParameter('email', 'fakemail@example.com');
  24.  
  25.     // Set the billing cycle for every three months
  26.     $subscription->setParameter('interval_length', 3);
  27.     $subscription->setParameter('startDate', date("Y-m-d", strtotime("+ 3 months")));
  28.  
  29.     // Set up a trial subscription for three months at a reduced price
  30.     $subscription->setParameter('trialOccurrences', 3);
  31.     $subscription->setParameter('trialAmount', 10.00);
  32.  
  33.     // Create the subscription
  34.     $subscription->createAccount();
  35.  
  36.     // Check the results of our API call
  37.     if ($subscription->isSuccessful())
  38.     {
  39.         // Get the subscription ID
  40.         $subscription_id = $subscription->getSubscriberID();
  41.     }
  42.     else
  43.     {
  44.         // The subscription was not created!
  45.     }
  46. }
  47. catch (AuthnetARBException $e)
  48. {
  49.     echo $e;
  50.     echo $subscription;
  51. }
  52. ?>

This class also supports updating and deleting subscriptions. Updating a subscription might look like this:

  1. $subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy');
  2. $subscription->setParameter('subscriptionId', $subscription_id);
  3. $subscription->setParameter('cardNumber', '4111111111111111');
  4. $subscription->setParameter('expirationDate', '2016-16');
  5. $subscription->setParameter('firstName', 'John');
  6. $subscription->setParameter('lastName', 'Conde');
  7. $subscription->setParameter('address', '123 Main Street');
  8. $subscription->setParameter('city', 'Townsville');
  9. $subscription->setParameter('state', 'NJ');
  10. $subscription->setParameter('zip', '12345');
  11. $subscription->setParameter('email', 'fakemail@example.com');
  12. $subscription->updateAccount();

Deleting a subscription would look like this:

  1. $subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy');
  2. $subscription->setParameter('subscriptionId', $subscription_id);
  3. $subscription->deleteAccount();

There you have it. Working with Authorize.Net’s ARB API is pretty easy once you hide their API behind some easy to use code. Have fun creating subscriptions!

60 thoughts on “Tutorial: Integrate the Authorize.Net ARB API with PHP

  1. You should add a function for checking status:

    public function getAccountStatus()
    {
    $this->xml = ”

    ” . $this->login . ”
    ” . $this->transkey . ”

    ” . $this->params[‘subscrId’] . ”
    “;
    $this->process();
    }

    Then under parseResults() add a status map: $this->status = (string) $xml->status;

    then for the heck of it I threw in this: public function getStatus()
    {
    return $this->status;
    }

    Now you can check the status with:

    $update->setParameter(‘subscrId’, $subscription_id);
    $update->getAccountStatus();

    if ($update->isSuccessful())
    {
    $status = $update->getStatus();
    }
    else
    {
    $status = false;
    }

    echo $status;

    This was a simple approach without the refID or anything but those can be added easy.

  2. Thanks so much for this– works great.
    One question: how do you name the subscription. I tried adding
    $subscription->setParameter(‘name’, $product);
    but it didn’t work.

  3. please help me that when i set arb with aim and arb and i set aim with auth only transaction and set my arb then it no give silent post of arb

  4. I was stuck on trying to cancel a subscription and kept getting an error that my subscription ID was blank. I looked at the ARB class and found that contrary to the code posted above:

    $subscription->setParameter(‘subscriptionId’, $subscription_id);

    The class actually uses “subscrId” instead of “subscriptionId”.

    This is just a heads up for anyone who used the code directly from the blog post.

  5. I used same code as above mention and also i created test account and add transaction key and login key but when i run this code its showing blank page . i checked the code and found ($subscription->isSuccessful()) condition is false .

  6. Pingback: Integrating Authorize.internet ARB cost gateway in php | Mentalist Nuno

  7. is it possible to use this (yes I know it’s technically deprecated) with a customer profile created via CIM integration?

Leave a Reply

Your email address will not be published. Required fields are marked *