Saturday 2 May 2015

Set up Authorize.Net ARB to work with Drupal commerce

Authorize.net is a popular alternative to Paypal for eCommerce websites especially its recurring transaction product - Authorize.net ARB (Automated Recurring Billing). Drupal support for Authnet ARB is available through Ubercart but it is not available for Drupal_commerce. However there is a workaround to setup Authorizet.net ARB to work with Drupal commerce. Read on to know more.

There is no Authorize.net ARB module for Drupal commerce but there is a sandbox module - Authnet ARB This module does not work straight out of box but you need to make a few changes. But before that lets get familiar with the module
Drupal authnet_arb sandbox module uses authorize.net SDK for ARB API request, so we have to install the sdk for using this module. First of all we can look on what else features the authnet_arb module provide and then we can look on how to integrate it with drupal commerce.

Features provided by authnet_arb module.

  1. Payment method for ARB:
    The sandbox module provides an additional payment method named 'Recurring payments with Authorize.Net ARB (with credit card)' along with default drupal commerce payement methods. Enabling this payment method, we get an option for choosing 'Recurring payments' method of payment in drupal commerce payment form.
            
    /**
     * Implements hook_commerce_payment_method_info().
     */
    function authnet_arb_commerce_payment_method_info() {
     $payment_methods = array();
     $payment_methods['authnet_arb'] = array(
      'base' =>  'authnet_arb',
      'title' => t('Recurring payments with Authorize.Net ARB (with credit card)'),
      'short_title' => t('Authorize.Net ARB CC'),
      'display_title' => t('Recurring payments'),
      'description' => t('Integrates Authorize.Net ARB for transactions.'),
      'callbacks' => array(
        'settings_form' => 'authnet_arb_commerce_settings_pane',
        'submit_form' => 'authet_arb_payment_pane',
        'submit_form_submit' => 'authet_arb_payment_pane_submit',
        'submit_form_validate' => 'authet_arb_payment_pane_validate',
       ),
       'file' => 'commerce.inc',
     );
    
     return $payment_methods;
    }
    
  2. Silent post URL:
    Silent post is the feature provided by Authorize.Net, which will post a http request to our domain silent url (which we need to configure in Authorize.Net account settings page) with the transaction details of ARB subscriptions payement as xml. Using this we can listen to the post request coming to our drupal site and do necessary business logic according to transaction status details.
    
    /**
     * Implements hook_menu().
     */
    function authnet_arb_menu () {
      $items = array();
      $items['authnet-arb-silentpost'] = array(
        'type' => MENU_CALLBACK,
        'page callback' => 'authnet_arb_silentpost',
        'access callback' => TRUE,
      );
    
      return $items;
    }
    
  3. Authnet ARB settings:
    Admin config page for setting the API login Id and transaction key. This form also provide an option for sandbox transactions, which will be more useful while testing.
    Authnet_ARB_part_1_img1.png
  4. ARB Subscription cancellation form:
    Using this subscribers can cancel their ARB subscription any time they want.
Since we are now familiar with the module, lets get to work on those changes to make this module production ready.

Making production ready

Before integration we have make some hack on the sandbox module to make it effectively work with drupal_commerce and bug free.
In Drupal organization hacking module and generating a patch is more oftenly done by all drupal contributors.
Basically to be a good drupal contributor before hacking a set of code we need to have clear idea of what is the cause of problem and why we need to fix it. Hence we can look on the problem, whenever creating ARB subscription or an API request is been to happen SDK will check for sandbox boolean value is true or false. Since authnet_arb is a sandbox module its not checking whether the payment method is been set as sandbox and then set the SDK sandbox boolean as proper. So we have to hack the sandbox module for this to work properly before each API request is to created or generated. Here come the sample code needed to set sandbox boolean of SDK.
    $request = new AuthorizeNetARB($settings['login'], $settings['tran_key']);
    if ($settings['sandbox']) {
      $request->setSandbox(TRUE);
    } else {
      $request->setSandbox(FALSE);
    }
  
Another feature we needed while implementing Authorize.Net ARB is the option for update subscription details such as credit card. authnet_arb sandbox module doesn't provide this update feature, instead they says it need to be implemented. Here I havehack that also as follow:
    function authnet_arb_update_subscription ($update_values) {

      $settings = authnet_arb_settings();
      require_once authnet_arb_sdk_path().'/AuthorizeNet.php';

      $subscription = new AuthorizeNet_Subscription;
      $subscription->billToFirstName = $update_values['first_name'];
      $subscription->billToLastName = $update_values['last_name'];
      $subscription->billToAddress = $update_values['thoroughfare'] . ' ' . $update_values['premise'];
      $subscription->billToCity = $update_values['locality'];
      $subscription->billToState = $update_values['administrative_area'];
      $subscription->billToZip = $update_values['postal_code'];
      $subscription->billToCountry = 'US';

      $subscription->creditCardCardNumber = $update_values['card_number'];
      $subscription->creditCardExpirationDate = $update_values['expiration_date'];

      $request = new AuthorizeNetARB($settings['login'], $settings['tran_key']);
      if ($settings['sandbox']) {
        $request->setSandbox(TRUE);
      } else {
        $request->setSandbox(FALSE);
      }
      $response = $request->updateSubscription($update_values['subscription_id'], $subscription);
      return $response;
    }
  
There is one more feature need to implement delete subscription. That you can try. That's all enjoy coding and hacking.

No comments:

Post a Comment