By: Roger Yonley – Coder Tactics
Authorize.Net is a payment gateway that provides the infrastructure and security needed to safely and easily connect merchants to credit card processing networks. It offers an API for most situations, making it a popular choice among developers and merchants.
Setting Up Recurring Billing for Clients
One need that online businesses commonly have is the ability to setup recurring billing for their clients. Authorize.Net offers an API dedicated to this task called the Automated Recurring Billing (ARB) API. This API is a convenient solution that removes the need to store credit cards on your server.
To set up a subscription, you provide the API with the subscription start date, interval length, interval unit, total occurrences, amount of subscription, and the billing information. Once the subscription has been successfully set up, Authorize.Net handles the rest. Every time the recurring billing system processes a payment using a free credit card processing tool, it will send transaction information back to a URL you are able to specify in the Merchant Interface. You can then save the information in your database and use it to create a new order if needed. This functionality can save you time and money on development and security costs, but it does have a few limitations that I ran into.
Limitations to Authorize.Net’s Automated Recurring Billing API
The first limitation is that the initial transaction will not be processed until the following day. If you want to give your website visitor access to a service immediately, you must also use the Authorize.Net AIM (Advanced Integration Method) API. The Authorize.Net website says “AIM allows merchants to host their own secure payment form on a website” and is commonly used for processing one time transactions in real time. To combine these two, you would first process the user’s payment with the AIM API; once the transaction is successful, you would then perform the ARB setup setting the start date for the time which you want the second payment to be drafted. There is another solution that Authorize.Net offers that will save you from having to work with two API’s, which I will discuss in a moment.
The second limitation involves the Silent Post functionality that sends back the transaction information when the transaction is finished processing. In the documentation, they make it clear that subscriptions are not updated in real time. If you have a subscription member whose transaction is declined, Authorize.Net does not cancel their subscription. To solve this problem, you might want to use the subscription cancellation method provided by the ARB API to cancel their subscription with Authorize.Net immediately after the transaction is declined. However, if you do this, the subscription will be cancelled, their update will go through, and it will be reactivated. You will think that the subscription is cancelled on your end while the user will still be getting billed. This means you would need to schedule a Cron job to perform subscription cancellations at a later time. The ARB API would also not be able to provide you with the ability to update the amount of the subscription or update when the payment is drafted.
Using the Customer Information Manager API for Recurring Billing
It was these limitations that lead me to use their Customer Information Manager (CIM) API as a recurring billing solution. Authorize.Net’s website states the CIM “allows merchants to create customer profiles that are stored on Authorize.Net’s secure servers.” To process a transaction, you provide the API with the customers profile ID and payment profile ID, which tells Authorize which credit card information to use for the transaction. The credit card information is safely stored at Authorize.Net. This gives you complete control over payment timing and amounts, which is great for businesses that “process recurring transactions where the date and/or amount is different each month,” such as utility companies.
This new level of control does, however, give the developer a little more work to do. The merchant’s server will now be responsible for checking when subscriptions are due and processing the payment through the CIM using a Cron Job. Other than that, this solution is very simple and more straight forward than using the ARB in some situations. It saves you from the details and limitations of the Silent Post functionality and prevents you from having to use two API’s to accomplish one goal.
Implementing Recurring Billing Solutions Using Authorize.Net API
The best way to implement this solution is through the use of the software development kit (SDK) provided by Authorize.Net. The code below is intended to give you an overview of the process. It leaves out some very important steps, such as validation. The process to create the subscription is as follows:
1. Set your account codes and require the software development kit classes.
[code language="php"]
define("AUTHORIZENET_API_LOGIN_ID", $login_id);
define("AUTHORIZENET_TRANSACTION_KEY", $transaction_key);
require_once 'anet_php_sdk/AuthorizeNet.php';
[/code]
2. Create a customer profile object.
[code language="php"] $customerProfile = new AuthorizeNetCustomer; [/code]
3. Provide required information to the customer profile object.
[code language="php"] $customerProfile->description = $description; $customerProfile->merchantCustomerId = $merchant_customer_id; $customerProfile->email = $email; [/code]
4. Create a payment profile object.
[code language="php"] $paymentProfile = new AuthorizeNetPaymentProfile; $paymentProfile->customerType = ""; $paymentProfile->payment->creditCard->cardNumber = $card_number; $paymentProfile->payment->creditCard->expirationDate = $expiration_date; $paymentProfile->payment->creditCard->cardCode = $card_code; $paymentProfile->billTo->firstName = $first_name; $paymentProfile->billTo->lastName = $last_name; $paymentProfile->billTo->phoneNumber = $phone_number; [/code]
5. Add the payment profile object to the customer profile object.
[code language="php"] $customerProfile->paymentProfiles[] = $paymentProfile; [/code]
6. Submit the customer profile object to the API using the createCustomerProfile method.
[code language="php"] $request = new AuthorizeNetCIM; $response = $request->createCustomerProfile($customerProfile); [/code]
7. If successful, retrieve the customer profile ID and the payment profile ID and create a transaction object.
[code language="php"] if($response->isOk()): $customerProfileId = $response->getCustomerProfileId(); $paymentProfileId = $response->getCustomerPaymentProfileIds(); [/code]
8. Create the transaction object and supply it with required properties.
[code language="php"] $transaction = new AuthorizeNetTransaction; $transaction->amount = $amount; $transaction->customerProfileId = $customerProfileId; $transaction->customerPaymentProfileId = $paymentProfileId; $transaction->order->invoiceNumber = $order_id; $transaction->order->description = $description; [/code]
9. If you wish to send line item information with the transaction, add them now.
[code language="php"]
foreach($items as $item){
$lineItem = new AuthorizeNetLineItem;
$lineItem->itemId = $item['id'];
$lineItem->name = $item['name'];
$lineItem->description = $item['description'];
$lineItem->quantity = "1";
$lineItem->unitPrice = $item['price'];
$lineItem->taxable = "false";
$transaction->lineItems[] = $lineItem;
}
[/code]
10. Send the transaction object to the API using the createCustomerProfileTransaction method.
[code language="php"]
$t_request = new AuthorizeNetCIM;
$t_request->setSandbox(false);
$t_response = $t_request->createCustomerProfileTransaction("AuthCapture", $transaction);
[/code]
11. Retrieve all of the pertinent response codes and information.
[code language="php"] $transactionResponse = $t_response->getTransactionResponse(); $response_code = $transactionResponse->response_code; $response_subcode = $transactionResponse->response_subcode; $response_reason_code = $transactionResponse->response_reason_code; $response_reason_text = $transactionResponse->response_reason_text; $authorization_code = $transactionResponse->authorization_code; $avs_response = $transactionResponse->avs_response; $cavv_response = $transactionResponse->cavv_response; $transaction_id = $transactionResponse->transaction_id; [/code]
12. Use the transaction status (approved, declined, held, or other) to determine what to do next.
[code language="php"]
if($transactionResponse->approved){
} elseif ($transactionResponse->declined) {
} elseif ($transactionResponse->held) {
} else {
}
//ending the conditional from step 7
endif;
[/code]
Choose a Recurring Billing API to Fit Your Needs
Whether you choose to use the CIM or the ARB API is entirely dependent on what you need to accomplish. The ARB API is nice in some situations where it isn’t necessary to bill the client immediately and there is no need for subscription amounts to fluctuate or dates to change. In situations where this could be necessary, I would recommend the CIM API. It will put the responsibility on the developer to process the payment via an automated script but it offers much more flexibility.