You are here

commerce_sagepay.module in Drupal Commerce SagePay Integration 7

File

commerce_sagepay.module
View source
<?php

/**
 * @file
 * Provides a SagePay Direct payment method for Drupal Commerce.
 */
module_load_include('inc', 'commerce_sagepay', 'includes/commerce_sagepay_constants');
module_load_include('inc', 'commerce_sagepay', 'includes/commerce_sagepay_utils');

/**
 * Implements hook_menu().
 */
function commerce_sagepay_menu() {
  $items = array();

  // Define a settings page.
  $items['admin/commerce/config/sagepay'] = array(
    'title' => 'SagePay settings',
    'description' => 'Configure the settings for the SagePay payment gateway integration.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_sagepay_settings_form',
    ),
    'access arguments' => array(
      'administer sagepay',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'includes/commerce_sagepay.admin.inc',
  );

  // Define a path to receive VPS callback (Server only).
  $items['commerce-sagepay-server/vps-callback/%/%'] = array(
    'page callback' => 'commerce_sagepay_server_handle_callback',
    'page arguments' => array(
      2,
      3,
    ),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'file' => 'includes/commerce_sagepay_server.inc',
  );

  // Define a path for a waiting page displayed whilst the iframe is loading.
  $items['commerce_sagepay/sagepay_waiting_page'] = array(
    'page callback' => 'commerce_sagepay_server_waiting_page',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'includes/commerce_sagepay_server.inc',
  );

  // Add a menu item for capturing authorizations.
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-release'] = array(
    'title' => 'Release',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_sagepay_release_form',
      3,
      5,
    ),
    'access callback' => 'commerce_sagepay_release_access',
    'access arguments' => array(
      3,
      5,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'weight' => 2,
    'file' => 'includes/commerce_sagepay_release.inc',
  );

  // Add a menu item for refunding payments.
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-refund'] = array(
    'title' => 'Refund',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_sagepay_refund_form',
      3,
      5,
    ),
    'access callback' => 'commerce_sagepay_refund_access',
    'access arguments' => array(
      3,
      5,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'weight' => 2,
    'file' => 'includes/commerce_sagepay_refund.inc',
  );

  // Add a menu item for aborting payments.
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-abort'] = array(
    'title' => 'Abort',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_sagepay_abort_form',
      3,
      5,
    ),
    'access callback' => 'commerce_sagepay_abort_access',
    'access arguments' => array(
      3,
      5,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'weight' => 2,
    'file' => 'includes/commerce_sagepay_abort.inc',
  );

  // Add a menu item for voiding payments.
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-void'] = array(
    'title' => 'Void',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_sagepay_void_form',
      3,
      5,
    ),
    'access callback' => 'commerce_sagepay_void_access',
    'access arguments' => array(
      3,
      5,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'weight' => 2,
    'file' => 'includes/commerce_sagepay_void.inc',
  );

  // Add a menu item for authorising payments.
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-authorise'] = array(
    'title' => 'Authorise',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_sagepay_authorise_form',
      3,
      5,
    ),
    'access callback' => 'commerce_sagepay_authorise_access',
    'access arguments' => array(
      3,
      5,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'weight' => 2,
    'file' => 'includes/commerce_sagepay_authorise.inc',
  );

  // Add a menu item for cancelling payments.
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-cancel'] = array(
    'title' => 'Cancel',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_sagepay_cancel_form',
      3,
      5,
    ),
    'access callback' => 'commerce_sagepay_cancel_access',
    'access arguments' => array(
      3,
      5,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'weight' => 2,
    'file' => 'includes/commerce_sagepay_cancel.inc',
  );

  // Add a menu item for repeating payments.
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-repeat'] = array(
    'title' => 'Repeat',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_sagepay_repeat_form',
      3,
      5,
    ),
    'access callback' => 'commerce_sagepay_repeat_access',
    'access arguments' => array(
      3,
      5,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'weight' => 2,
    'file' => 'includes/commerce_sagepay_repeat.inc',
  );

  // Add a menu item for deferred repeat payments.
  $items['admin/commerce/orders/%commerce_order/payment/%commerce_payment_transaction/sagepay-repeat-defer'] = array(
    'title' => 'Repeat Deferred',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_sagepay_repeat_deferred_form',
      3,
      5,
    ),
    'access callback' => 'commerce_sagepay_repeat_deferred_access',
    'access arguments' => array(
      3,
      5,
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'weight' => 2,
    'file' => 'includes/commerce_sagepay_repeat_deferred.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function commerce_sagepay_permission() {
  $permissions = array(
    'administer sagepay' => array(
      'title' => t('Administer SagePay'),
      'description' => t('Configure SagePay Settings.'),
      'restrict access' => TRUE,
    ),
    'sagepay abort payment' => array(
      'title' => t('Abort Payment'),
      'description' => t('Allow users to Abort a SagePay Payment'),
      'restrict access' => TRUE,
    ),
    'sagepay authorise payment' => array(
      'title' => t('Authorise Payment'),
      'description' => t('Allow users to Authorise a SagePay Payment'),
      'restrict access' => TRUE,
    ),
    'sagepay cancel payment' => array(
      'title' => t('Cancel Payment'),
      'description' => t('Allow users to Cancel a SagePay Payment'),
      'restrict access' => TRUE,
    ),
    'sagepay refund payment' => array(
      'title' => t('Refund Payment'),
      'description' => t('Allow users to Refund a SagePay Payment'),
      'restrict access' => TRUE,
    ),
    'sagepay release payment' => array(
      'title' => t('Release Payment'),
      'description' => t('Allow users to Release a SagePay Payment'),
      'restrict access' => TRUE,
    ),
    'sagepay repeat payment' => array(
      'title' => t('Repeat Payment'),
      'description' => t('Allow users to Repeat a SagePay Payment'),
      'restrict access' => TRUE,
    ),
    'sagepay repeat deferred payment' => array(
      'title' => t('Repeat Deferred Payment'),
      'description' => t('Allow users to Repeat a Deferred SagePay Payment'),
      'restrict access' => TRUE,
    ),
    'sagepay void payment' => array(
      'title' => t('Void Payment'),
      'description' => t('Allow users to Void a SagePay Payment'),
      'restrict access' => TRUE,
    ),
  );
  return $permissions;
}

/**
 * Implements hook_commerce_payment_method_info().
 */
function commerce_sagepay_commerce_payment_method_info() {
  $payment_methods = array();
  $payment_methods['commerce_sagepay_form'] = array(
    'base' => 'commerce_sagepay_form',
    'title' => t('SagePay Form Integration'),
    'display_title' => t('Credit or Debit card (Form)'),
    'short_title' => t('SagePay'),
    'description' => t('Integration with SagePay using Form method.'),
    'active' => FALSE,
    'terminal' => FALSE,
    'offsite' => TRUE,
    'offsite_autoredirect' => TRUE,
    'callbacks' => array(),
    'file' => 'includes/commerce_sagepay_form.inc',
  );
  $payment_methods['commerce_sagepay_server'] = array(
    'title' => t('SagePay Server Integration'),
    'short_title' => t('SagePay Server'),
    'display_title' => t('Credit or Debit card (Server)'),
    'description' => t('Integration with SagePay using Server method.'),
    'active' => TRUE,
    'offsite' => TRUE,
    'terminal' => FALSE,
    'callbacks' => array(),
    'offsite_autoredirect' => FALSE,
    'file' => 'includes/commerce_sagepay_server.inc',
  );
  $payment_methods['commerce_sagepay_direct'] = array(
    'base' => 'commerce_sagepay_direct',
    'title' => t('SagePay Direct Integration'),
    'display_title' => t('Credit or Debit card (Direct)'),
    'short_title' => t('SagePay'),
    'description' => t('Integration with SagePay using Direct method.'),
    'active' => FALSE,
    'terminal' => TRUE,
    'offsite' => FALSE,
    'offsite_autoredirect' => FALSE,
    'callbacks' => array(),
    'file' => 'includes/commerce_sagepay_direct.inc',
  );
  return $payment_methods;
}

/**
 * Determines access to the prior authorization release form for SagePay.
 *
 * @param commerce_order $order
 *   The order the transaction is on.
 *
 * @param commerce_payment_transaction $transaction
 *   The payment transaction object to be released.
 *
 * @return bool
 *   TRUE or FALSE indicating release access.
 */
function commerce_sagepay_release_access($order, $transaction) {
  global $user;

  // Return FALSE if the transaction isn't for SagePay Server or Direct or isn't
  // awaiting capture.
  if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
    return FALSE;
  }
  if ($transaction->remote_status != SAGEPAY_REMOTE_STATUS_REPEAT_DEFERRED && $transaction->remote_status != SAGEPAY_REMOTE_STATUS_DEFERRED) {
    return FALSE;
  }

  // Return FALSE if it is more than 90 days past the original authorization.
  if (time() - $transaction->created > 86400 * 90) {
    return FALSE;
  }

  // check SagePay Payment permission
  if (!user_access('sagepay release payment', $user)) {
    return FALSE;
  }

  // Allow access if the user can update payments on this order.
  return commerce_payment_transaction_access('update', $order, $user);
}

/**
 * Determines access to the refund form for SagePay.
 *
 * @param commerce_order $order
 *   The order the transaction is on.
 *
 * @param commerce_payment_transaction $transaction
 *   The payment transaction object to be captured.
 *
 * @return bool
 *   TRUE or FALSE indicating refund access.
 */
function commerce_sagepay_refund_access($order, $transaction) {
  global $user;

  // Return FALSE if the transaction isn't for SagePay Server or Direct.
  if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
    return FALSE;
  }

  // Return FALSE is the remote status is FAIL or INVALID.
  if (in_array($transaction->remote_status, array(
    SAGEPAY_REMOTE_STATUS_FAIL,
    SAGEPAY_REMOTE_STATUS_INVALID,
    SAGEPAY_REMOTE_STATUS_STARTED,
    SAGEPAY_REMOTE_STATUS_UNKNOWN,
    SAGEPAY_REMOTE_STATUS_REFUNDED,
    SAGEPAY_REMOTE_STATUS_REGISTERED,
    SAGEPAY_REMOTE_STATUS_REPEAT_DEFERRED,
  ))) {
    return FALSE;
  }
  if ($transaction->status == COMMERCE_PAYMENT_STATUS_FAILURE) {
    return FALSE;
  }

  // check SagePay Payment permission
  if (!user_access('sagepay refund payment', $user)) {
    return FALSE;
  }

  // Allow access if the user can update payments on this order.
  return commerce_payment_transaction_access('update', $order, $user);
}

/**
 * Determines access to the abort form for SagePay.
 *
 * @param commerce_order $order
 *   The order the transaction is on.
 *
 * @param commerce_payment_transaction $transaction
 *   The payment transaction object to be aborted.
 *
 * @return bool
 *   TRUE or FALSE indicating abort access.
 */
function commerce_sagepay_abort_access($order, $transaction) {
  global $user;

  // Return FALSE if the transaction isn't for SagePay Server or Direct.
  if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
    return FALSE;
  }
  if ($transaction->remote_status != SAGEPAY_REMOTE_STATUS_DEFERRED && $transaction->remote_status != SAGEPAY_REMOTE_STATUS_REPEAT_DEFERRED) {
    return FALSE;
  }

  // check SagePay Payment permission
  if (!user_access('sagepay abort payment', $user)) {
    return FALSE;
  }

  // Allow access if the user can update payments on this order.
  return commerce_payment_transaction_access('update', $order, $user);
}

/**
 * Determines access to the void form for SagePay.
 *
 * @param commerce_order $order
 *   The order the transaction is on.
 *
 * @param commerce_payment_transaction $transaction
 *   The payment transaction object to be aborted.
 *
 * @return bool
 *   TRUE or FALSE indicating void access.
 */
function commerce_sagepay_void_access($order, $transaction) {
  global $user;

  // Return FALSE if the transaction isn't for SagePay Server or Direct.
  if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
    return FALSE;
  }
  if ($transaction->remote_status != 'PAYMENT' && $transaction->remote_status != 'RELEASED' && $transaction->remote_status != 'DEFERRED') {
    return FALSE;
  }

  // Check if transaction status is not already cancelled.
  if ($transaction->status == COMMERCE_PAYMENT_STATUS_FAILURE) {
    return FALSE;
  }

  // check SagePay Payment permission
  if (!user_access('sagepay void payment', $user)) {
    return FALSE;
  }

  // Allow access if the user can update payments on this order.
  return commerce_payment_transaction_access('update', $order, $user);
}

/**
 * Determines access to the cancel form for SagePay.
 *
 * @param commerce_order $order
 *   The order the transaction is on.
 *
 * @param commerce_payment_transaction $transaction
 *   The payment transaction object to be aborted.
 *
 * @return bool
 *   TRUE or FALSE indicating void access.
 */
function commerce_sagepay_cancel_access($order, $transaction) {
  global $user;

  // Return FALSE if the transaction isn't for SagePay Server or Direct.
  if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
    return FALSE;
  }
  if (!in_array($transaction->remote_status, array(
    SAGEPAY_REMOTE_STATUS_AUTHENTICATE,
    SAGEPAY_REMOTE_STATUS_REGISTERED,
  ))) {
    return FALSE;
  }
  if ($transaction->remote_status != 'AUTHENTICATE' && $transaction->remote_status != 'REGISTERED') {
    return FALSE;
  }

  // check SagePay Payment permission
  if (!user_access('sagepay cancel payment', $user)) {
    return FALSE;
  }

  // Allow access if the user can update payments on this order.
  return commerce_payment_transaction_access('update', $order, $user);
}

/**
 * Determines access to the authorise form for SagePay.
 *
 * @param commerce_order $order
 *   The order the transaction is on.
 *
 * @param commerce_payment_transaction $transaction
 *   The payment transaction object to be authorised.
 *
 * @return bool
 *   TRUE or FALSE indicating void access.
 */
function commerce_sagepay_authorise_access($order, $transaction) {
  global $user;

  // Return FALSE if the transaction isn't for SagePay Server or Direct.
  if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
    return FALSE;
  }
  if ($transaction->remote_status != 'AUTHENTICATE' && $transaction->remote_status != 'REGISTERED') {
    return FALSE;
  }

  // Return FALSE if it is more than 90 days past the original authorization.
  if (time() - $transaction->created > 86400 * 90) {
    return FALSE;
  }

  // check SagePay Payment permission
  if (!user_access('sagepay authorise payment', $user)) {
    return FALSE;
  }

  // Allow access if the user can update payments on this order.
  return commerce_payment_transaction_access('update', $order, $user);
}

/**
 * Determines access to the repeat form for SagePay.
 *
 * @param commerce_order $order
 *   The order the transaction is on.
 *
 * @param commerce_payment_transaction $transaction
 *   The payment transaction object to be repeated.
 *
 * @return bool
 *   TRUE or FALSE indicating void access.
 */
function commerce_sagepay_repeat_access($order, $transaction) {
  global $user;

  // Return FALSE if the transaction isn't for SagePay Server or Direct.
  if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
    return FALSE;
  }
  if (in_array($transaction->remote_status, array(
    SAGEPAY_REMOTE_STATUS_FAIL,
    SAGEPAY_REMOTE_STATUS_INVALID,
    SAGEPAY_REMOTE_STATUS_STARTED,
    SAGEPAY_REMOTE_STATUS_UNKNOWN,
    SAGEPAY_REMOTE_STATUS_REFUNDED,
    SAGEPAY_REMOTE_STATUS_REGISTERED,
  ))) {
    return FALSE;
  }

  // Check if this is a successful transaction.
  if ($transaction->status != COMMERCE_PAYMENT_STATUS_SUCCESS) {
    return FALSE;
  }

  // check SagePay Payment permission
  if (!user_access('sagepay repeat payment', $user)) {
    return FALSE;
  }

  // Allow access if the user can update payments on this order.
  return commerce_payment_transaction_access('update', $order, $user);
}

/**
 * Determines access to the repeat form for SagePay.
 *
 * @param commerce_order $order
 *   The order the transaction is on.
 *
 * @param commerce_payment_transaction $transaction
 *   The payment transaction object to be repeated.
 *
 * @return bool
 *   TRUE or FALSE indicating void access.
 */
function commerce_sagepay_repeat_deferred_access($order, $transaction) {
  global $user;

  // Return FALSE if the transaction isn't for SagePay Server or Direct.
  if ($transaction->payment_method != 'commerce_sagepay_server' && $transaction->payment_method != 'commerce_sagepay_direct') {
    return FALSE;
  }
  if (in_array($transaction->remote_status, array(
    SAGEPAY_REMOTE_STATUS_FAIL,
    SAGEPAY_REMOTE_STATUS_INVALID,
    SAGEPAY_REMOTE_STATUS_STARTED,
    SAGEPAY_REMOTE_STATUS_UNKNOWN,
    SAGEPAY_REMOTE_STATUS_REFUNDED,
    SAGEPAY_REMOTE_STATUS_REGISTERED,
  ))) {
    return FALSE;
  }

  // Check if this is a successful transaction.
  if ($transaction->status != COMMERCE_PAYMENT_STATUS_SUCCESS) {
    return FALSE;
  }

  // check SagePay Payment permission
  if (!user_access('sagepay repeat deferred payment', $user)) {
    return FALSE;
  }

  // Allow access if the user can update payments on this order.
  return commerce_payment_transaction_access('update', $order, $user);
}

/**
 * Implements hook_page_build().
 */
function commerce_sagepay_page_build($page) {
  $url = explode('/', current_path());
  if (count($url) > 2) {
    if ($url[0] == 'checkout') {
      drupal_add_js(drupal_get_path('module', 'commerce_sagepay') . '/js/commerce_sagepay_server_iframe.js', array(
        'type' => 'file',
        'scope' => 'header',
        'weight' => -100,
      ));
    }
  }
}

/**
 * Implements hook_form_alter().
 *
 * Adjust the credit card area of the payment form to include the card
 * logos of active card types.
 */
function commerce_sagepay_form_alter(&$form, &$form_state, $form_id) {

  // If the current form ID is for a checkout form...
  if (strpos($form_id, 'commerce_checkout_form_') === 0) {

    // Check if the payment pane is on this page - return if not.
    if (!isset($form['commerce_payment'])) {
      return;
    }

    // Check if this is the direct payment method.
    // This is the only one that needs card logos.
    $instance_id = $form['commerce_payment']['payment_method']['#default_value'];
    $payment_method = commerce_payment_method_instance_load($instance_id);
    if (strpos($payment_method['instance_id'], 'sagepay_direct') > 0) {
      drupal_add_css(drupal_get_path('module', 'commerce_sagepay') . '/css/sagepay_direct.css');

      // And it specifies a valid checkout page...
      if (commerce_checkout_page_load(substr($form_id, 23))) {

        // And the current page's form includes the payment checkout pane...
        if (!empty($form['commerce_payment'])) {

          // get a list of the currently enabled cards
          $enabled_cards = _commerce_sagepay_populate_card_names(variable_get(SAGEPAY_SETTING_ENABLED_CARDS));

          // if there are nio enabled cards, then get all available cards
          if (empty($enabled_cards)) {
            $enabled_cards = _commerce_sagepay_all_card_names();
          }
          $card_images = theme('commerce_sagepay_card_logos', array(
            'enabled_cards' => $enabled_cards,
          ));
          $payment_details = $form['commerce_payment']['payment_details'];
          if (array_key_exists('credit_card', $payment_details)) {

            // Add credit card logos about the card dropdown.
            array_unshift($form['commerce_payment']['payment_details']['credit_card'], array(
              'logos' => array(
                '#markup' => $card_images,
              ),
            ));

            // Replace the card selection dropdown with one based on our
            // available cards.
            $form['commerce_payment']['payment_details']['credit_card']['type']['#options'] = $enabled_cards;

            // Disable compulsory start date field as not every card has a
            // start date.
            $empty_option = array(
              0 => '-',
            );
            $start_months = $form['commerce_payment']['payment_details']['credit_card']['start_month']['#options'];
            $start_years = $form['commerce_payment']['payment_details']['credit_card']['start_year']['#options'];
            $form['commerce_payment']['payment_details']['credit_card']['start_month']['#options'] = $empty_option + $start_months;
            $form['commerce_payment']['payment_details']['credit_card']['start_month']['#default_value'] = 0;
            $form['commerce_payment']['payment_details']['credit_card']['start_month']['#required'] = FALSE;
            $form['commerce_payment']['payment_details']['credit_card']['start_year']['#options'] = $empty_option + $start_years;
            $form['commerce_payment']['payment_details']['credit_card']['start_year']['#default_value'] = 0;
            $form['commerce_payment']['payment_details']['credit_card']['start_year']['#required'] = FALSE;
          }
        }
      }
    }
  }
}

/**
 * Implements hook_theme().
 *
 * Add a theme function for how card logos will be displayed.
 */
function commerce_sagepay_theme($existing, $type, $theme, $path) {
  return array(
    'commerce_sagepay_card_logos' => array(
      'variables' => array(
        'enabled_cards' => array(),
      ),
    ),
    'commerce_sagepay_card_logo' => array(
      'variables' => array(
        'card' => NULL,
        'name' => NULL,
        'img' => NULL,
        'href' => NULL,
      ),
    ),
  );
}

/**
 * Theme function determining how card logos are displayed.
 *
 * This is invoked by a form alter and adds the card logos as a Suffix
 * to the card type dropdown.
 *
 * @param array $enabled_cards
 *   An array of currently enabled cards.
 *
 * @return string
 *   A themed output of card logos.
 */
function theme_commerce_sagepay_card_logos($variables) {
  if (!isset($variables['enabled_cards'])) {
    return '<div />';
  }
  $output = '<div class="card_type_images">';
  foreach ($variables['enabled_cards'] as $card => $name) {
    if ($name != '0') {
      $output .= theme('commerce_sagepay_card_logo', array(
        'card' => $card,
      ));
      $output .= '&nbsp;';
    }
  }
  $output .= '</div>';
  return $output;
}

/**
 * Theme function to display a single card logo
 *
 * @param string $card
 *   Name of a card as per _commerce_sagepay_all_card_names
 *
 * @param string $name
 *    Optionally override the name
 *
 * @param string $img
 *    Optionally override the img url
 *
 * @param string $href
 *    Optional href to link the image too
 *
 * @return string
 *   A themed output of card logo.
 */
function theme_commerce_sagepay_card_logo($variables) {
  $output = '';
  if (!empty($variables['card'])) {
    if (!empty($variables['img'])) {
      $src = $variables['img'];
    }
    else {
      $path = '/' . drupal_get_path('module', 'commerce_sagepay');
      $src = $path . '/img/' . $variables['card'] . '.png';
    }
    $img = '<img src="' . $src . '" />';
    if (!empty($variables['href'])) {
      $output .= l($img, $variables['href'], array(
        'html' => TRUE,
      ));
    }
    else {
      $output .= $img;
    }
  }
  return $output;
}

/**
 * Implements hook_enable
 */
function commerce_sagepay_enable() {
  drupal_set_message(t('Sage Pay installation complete, please configure your account in the <a href="/admin/commerce/config/sagepay">settings page</a>'));
}

/**
 * Implements hook_sagepay_order_data_alter().
 *
 * Make specific changes to order data for countries that have no City
 * requirement in their address format. SagePay expects a City and therefore
 * will fail the transaction if the address does not contain one.
 */
function commerce_sagepay_sagepay_order_data_alter(&$query, $order) {
  switch ($query['BillingCountry']) {
    case 'SG':
      $query['BillingCity'] = 'Singapore';
      break;
    case 'GI':
      $query['BillingCity'] = 'Gibraltar';
      break;
    case 'HK':
      $query['BillingCity'] = 'Hong Kong';
      break;
    case 'NR':
      $query['BillingCity'] = 'Nauru';
      break;
    case 'SM':
      $query['BillingCity'] = 'San Marino';
      break;
    case 'AE':
      $query['BillingCity'] = 'United Arab Emirates';
      break;
  }
  switch ($query['DeliveryCountry']) {
    case 'SG':
      $query['DeliveryCity'] = 'Singapore';
      break;
    case 'GI':
      $query['DeliveryCity'] = 'Gibraltar';
      break;
    case 'HK':
      $query['DeliveryCity'] = 'Hong Kong';
      break;
    case 'NR':
      $query['DeliveryCity'] = 'Nauru';
      break;
    case 'SM':
      $query['DeliveryCity'] = 'San Marino';
      break;
    case 'AE':
      $query['DeliveryCity'] = 'United Arab Emirates';
      break;
  }

  // Check if the user has entered a first and last name as required.
  // If not, add dummy data so that the transaction still works.
  if (empty($query['BillingSurname'])) {
    $query['BillingSurname'] = '.';
  }
  if (empty($query['DeliverySurname'])) {
    $query['DeliverySurname'] = '.';
  }
}

Functions

Namesort descending Description
commerce_sagepay_abort_access Determines access to the abort form for SagePay.
commerce_sagepay_authorise_access Determines access to the authorise form for SagePay.
commerce_sagepay_cancel_access Determines access to the cancel form for SagePay.
commerce_sagepay_commerce_payment_method_info Implements hook_commerce_payment_method_info().
commerce_sagepay_enable Implements hook_enable
commerce_sagepay_form_alter Implements hook_form_alter().
commerce_sagepay_menu Implements hook_menu().
commerce_sagepay_page_build Implements hook_page_build().
commerce_sagepay_permission Implements hook_permission().
commerce_sagepay_refund_access Determines access to the refund form for SagePay.
commerce_sagepay_release_access Determines access to the prior authorization release form for SagePay.
commerce_sagepay_repeat_access Determines access to the repeat form for SagePay.
commerce_sagepay_repeat_deferred_access Determines access to the repeat form for SagePay.
commerce_sagepay_sagepay_order_data_alter Implements hook_sagepay_order_data_alter().
commerce_sagepay_theme Implements hook_theme().
commerce_sagepay_void_access Determines access to the void form for SagePay.
theme_commerce_sagepay_card_logo Theme function to display a single card logo
theme_commerce_sagepay_card_logos Theme function determining how card logos are displayed.