Tutorial: Integrate the Authorize.Net ARB API with PHP
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:
- 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.
- Download the ARB Integration Guide (for reference)
- 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 2 | // Set up the subscription. If you are using a live account remove the third parameter. $subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy', 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 2 3 4 5 6 7 8 9 10 11 12 13 | $subscription->setParameter('amount', 29.99); $subscription->setParameter('cardNumber', '4111111111111111'); $subscription->setParameter('expirationDate', '2016-16'); $subscription->setParameter('firstName', 'John'); $subscription->setParameter('lastName', 'Conde'); $subscription->setParameter('address', '123 Main Street'); $subscription->setParameter('city', 'Townsville'); $subscription->setParameter('state', 'NJ'); $subscription->setParameter('zip', '12345'); $subscription->setParameter('email', 'fakemail@example.com'); // Create the subscription $subscription->createAccount(); |
After making our API call we’ll want to verify it was successful and, if so, capture the subscription ID:
1 2 3 4 5 6 7 8 9 10 | // Check the results of our API call if ($subscription->isSuccessful()) { // Get the subscription ID $subscription_id = $subscription->getSubscriberID(); } else { // The subscription was not created! } |
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 2 3 4 5 6 7 8 9 | // Put this before $subscription->createAccount(); // if you want the subscription to be every three months. $subscription->setParameter('interval_length', 3); $subscription->setParameter('startDate', date("Y-m-d", strtotime("+ 3 months"))); // Put this before $subscription->createAccount(); // if you want the subscription to be every year. $subscription->setParameter('interval_unit', 'years'); $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 2 3 4 | // Put this before $subscription->createAccount(); // if you want a trial period of three months for $10.00. $subscription->setParameter('trialOccurrences', 3); $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 2 3 4 5 6 7 8 9 10 11 12 13 | $authorization = new AuthnetAIM('myapilogin', 'mYtRaNsaCTiOnKEy'); $authorization->setTransaction($creditcard, $expiration, 0.00); $authorization->setTransactionType('AUTH_ONLY'); $authorization->process(); if ($authorization->isApproved()) { // Set up ARB subscription } else if ($authorization->isDeclined()) { // The card is not valid } |
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 2 3 4 5 6 7 8 9 10 | try { $cim = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy'); // Do more ARB stuff here } catch (AuthnetARBException $e) { echo 'There was an error processing the transaction. Here is the error message: '; echo $e->__toString(); } |
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | < ?php // Include AuthnetCIM class. Nothing works without it! require('AuthnetARB.class.php'); // Use try/catch so if an exception is thrown we can catch it and figure out what happened try { // Set up the subscription. Use the developer account for testing.. $subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy', AuthnetARB::USE_DEVELOPMENT_SERVER); // Set subscription information $subscription->setParameter('amount', 29.99); $subscription->setParameter('cardNumber', '4111111111111111'); $subscription->setParameter('expirationDate', '2016-16'); $subscription->setParameter('firstName', 'John'); $subscription->setParameter('lastName', 'Conde'); $subscription->setParameter('address', '123 Main Street'); $subscription->setParameter('city', 'Townsville'); $subscription->setParameter('state', 'NJ'); $subscription->setParameter('zip', '12345'); $subscription->setParameter('email', 'fakemail@example.com'); // Set the billing cycle for every three months $subscription->setParameter('interval_length', 3); $subscription->setParameter('startDate', date("Y-m-d", strtotime("+ 3 months"))); // Set up a trial subscription for three months at a reduced price $subscription->setParameter('trialOccurrences', 3); $subscription->setParameter('trialAmount', 10.00); // Create the subscription $subscription->createAccount(); // Check the results of our API call if ($subscription->isSuccessful()) { // Get the subscription ID $subscription_id = $subscription->getSubscriberID(); } else { // The subscription was not created! } } catch (AuthnetARBException $e) { echo $e; echo $subscription; } ?> |
This class also supports updating and deleting subscriptions. Updating a subscription might look like this:
1 2 3 4 5 6 7 8 9 10 11 12 | $subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy'); $subscription->setParameter('subscriptionId', $subscription_id); $subscription->setParameter('cardNumber', '4111111111111111'); $subscription->setParameter('expirationDate', '2016-16'); $subscription->setParameter('firstName', 'John'); $subscription->setParameter('lastName', 'Conde'); $subscription->setParameter('address', '123 Main Street'); $subscription->setParameter('city', 'Townsville'); $subscription->setParameter('state', 'NJ'); $subscription->setParameter('zip', '12345'); $subscription->setParameter('email', 'fakemail@example.com'); $subscription->updateAccount(); |
Deleting a subscription would look like this:
1 2 3 | $subscription = new AuthnetARB('myapilogin', 'mYtRaNsaCTiOnKEy'); $subscription->setParameter('subscriptionId', $subscription_id); $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!
Related Posts:
Tags: Authorize.Net, PHP
February 8th, 2010 at 12:18 pm | Posted in Programming
April 27th, 2010 at 8:10 pm
You use strtotime(“+3 months”). Would I use that even if I wanted to start the subscription today.
So if a user signs up today and I want the subscription to start today would I use:
$subscription->setParameter(‘interval_length’, 3);
$subscription->setParameter(‘startDate’, date(“Y-m-d”));
?
April 27th, 2010 at 8:35 pm
Yes. But that’s not the best way to do it. If you want to start the subscription today you should charge their first payment via the AIM API. That way you get an immediate response and if there is a problem you can have the user try again while they are still there. If you use ARB to collect that first payment it won’t be charged until later that night. If it fails then you can’t collect from the user because they are long gone.
May 6th, 2010 at 9:40 am
Hi,
My requirement is such that I want to charge first time setup fee & then set recurring billing.
like this–
Local Value – $149.00 set up fee/$99.00 monthly fee
Since It is first time I’m using authorize.net. How to proceed?
May 6th, 2010 at 9:47 am
Alok,
Use the AIM API to charge the initial $149 setup fee. The use ARB to create the subscription for $99 a month.
May 6th, 2010 at 9:56 am
Thnx for ur reply.
Could u provide me with more help.
Like how to merge both AIM & ARB?
Can u refer to any URL for help where it’s already done.
Thnx again.
May 6th, 2010 at 10:21 am
This is a good overview:
Integrate the Authorize.Net ARB API with PHP
May 7th, 2010 at 1:27 am
Thnx a lot.
Your site is wonderful & it’s helpful as well.
Keep up the good work.
May 27th, 2010 at 8:20 pm
Thank you for this very helpful walk-through.
I think I may have spotted a typo in your updateAccount() example. The parameter subscriptionId is actually named subscrId in the AuthnetARB.class.phps. Once I changed that, my updates began working.
June 17th, 2010 at 12:17 pm
I also ran into that bug, and after some careful debugging found out about the subscriptionId thing. If the example could be updated, it may save some people some hair. Thanks for the great tutorial though John!
One more question: Do users have to re-enter their credit card and billing info even if all that needs to be changed is the interval_length and amount?
Sincerely,
Leighton Whiting
July 13th, 2010 at 12:05 pm
Hi,
After creating a automated recurring billing, is it possible for me to charge the user using the subscription id , and the amount to be charged one time , say for example, an add-on payment apart from the recurring monthly payment ?
Thanks,
Manoj
July 13th, 2010 at 1:15 pm
@Manoj, You cannot charge a credit card using an ARB subscription. You’ll need to use CIM for that.
@Leighton, I’m not sure if they do need to re-enter that information or not. I think they do but I’m not 100% sure. You should hit the Authorize.Net developer forums and ask there.
August 7th, 2010 at 9:32 am
Hi,
I set ARB for customer monthly say at 2010-Aug-07. my next recurring date will be 2010-Sep-07 and if there are not enough fund in customer’s creditcard or it get expired (means transaction not getting) what will happen in that case? is customer’s subscription get expired? how to i check programatically payment is done or not?
August 7th, 2010 at 12:04 pm
If you use Silent Post you will be notified that the payment failed when the subscription is charged.
August 9th, 2010 at 3:22 am
@johny
Thanx for reply.
But what i want is i want to check it pogramatically and send email through my website.
for ex:- I store the subscription_id for each user when they subscribed through my site when next payment date will come at that date i want to check payment is happen or not using that subscription_id and if it not done i want to expired his membership of my website and send an email to notify him/fer so.
August 9th, 2010 at 7:49 am
That is exactly what Silent Post is for.
August 10th, 2010 at 12:52 am
@johny:- thanx johny thanx a million!!!!
Sorry but last question i know how to set URL (Home > Account > Settings > Transaction Format Settings > Transaction Response Settings > Silent Post URL) I also write a code for that the only thing i don’t know is how will icheck the code is working or not?
August 10th, 2010 at 7:42 am
Check out my blog post on Silent Post. It explains how.