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

Using Our Class

Now that we have a functional class let's use it to establish recurring billing subscriptions. As previously mentioned there are two scenarios in which a recurring billing subscription may occur: after a payment has been successfully processed or without an initial payment. Below we will examine each in greater detail and show our new code in action.

Scenario 1: Instant Payment And Recurring Billing

As mentioned previously the most common way a recurring billing subscription is created is after a user is charged an instantly. The most common use for recurring billing is for subscription-based websites where you will want to grant the user immediate access to your content. Naturally you will want to verify that your new user has paid before granting access to restricted content.

Through the AIM API their initial payment can be processed. If this payment is successful you can grant your user immediate access to your restricted content. You also can be 100% certain that the credit card you have received is valid as if there was a problem with the credit card it would have been declined by the processing bank. This means we do not have to do any further work with this transaction before establishing a recurring billing account for it.

Valid does not mean safe

Although a payment has been approved by the processing bank it doesn't mean that you are free and clear. The card may be a "real" credit card but that does not mean you are safe from chargebacks or fraud. For the sake of our article an approved transaction will suffice. However, it is still recommended you do your own fraud screening as any responsible business would do.

Here is the code to establish a recurring billing account after processing a successful transaction using our Authnet and AuthnetARB classes:

// Instantiate our class $payment = new Authnet();
// Set transaction variables // ...
// Set our basic transaction information $payment->setTransaction($creditcard, $expiration, $total, $cvv, $invoice, $tax);
// Set other transaction parameters $payment->setParameter("x_duplicate_window", 180); $payment->setParameter("x_first_name", $firstname); // ...
// Process the initial payment $payment->process();
if ($payment->isApproved()) { // Setup recurring billing here
// Instanciate our ARB class $arb = new AuthnetARB();
// Set recurring billing variables // ...
// Set recurring billing parameters $arb->setParameter('amount', $total); $arb->setParameter('cardNumber', $creditcard); $arb->setParameter('expirationDate', $expiration); $arb->setParameter('firstName', $firstname); $arb->setParameter('firstName', $firstname); $arb->setParameter('address', $address); $arb->setParameter('city', $city); $arb->setParameter('state', $state); $arb->setParameter('zip', $zip); $arb->setParameter('email', $email); $arb->setParameter('subscrName', $userid);
// Create the recurring billing subscription $arb->createAccount();
// If successful let's get the subscription ID if ($arb->isSuccessful()) { $arb_id = $arb->getSubscriberID(); } }

As you can see we have our transaction broken down into two parts. The first part processes the initial transaction which not only serves to make the first payment for the user but also verifies their credit card is valid. If that payment is successful we can continue to the recurring billing portion of our code where we attempt to establish a subscription for the user.

We start by instantiating our object:

$arb = new AuthnetARB();

After assigning our transaction information variables we set the parameters necessary for Authorize.Net to establish our recurring billing subscription:

$arb->setParameter('amount', $total); $arb->setParameter('cardNumber', $creditcard); $arb->setParameter('expirationDate', $expiration); $arb->setParameter('firstName', $firstname); $arb->setParameter('lastName', $lastname); $arb->setParameter('address', $address); $arb->setParameter('city', $city); $arb->setParameter('state', $state); $arb->setParameter('zip', $zip); $arb->setParameter('email', $email); $arb->setParameter('subscrName', $userid);

You will notice we do not send over the CVV information in our recurring billing subscription. This is because it is not necessary for recurring payments. Once the initial payment is successful it may be omitted.

Now that our parameters are set we can send the subscription to Authorize.Net to be processed. We do that in one line of code:

$arb->createAccount();

Lastly we check for a successful reply from Authorize.Net. If we get one we grab the subscription ID assigned to the user so we can go back and edit their account in the future if need be.

As you can see once we have a successful transaction processed through the AIM API we do not need to do any further validation. All we need to do is set the appropriate parameters and then make the call to our process() method to establish the subscription.

Methods | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Delayed Payment