Only in /uc_sagepay/uc_sagepay_token: .gitignore
diff -crB /uc_protx_vsp_direct/uc_sagepay_token/uc_sagepay_token.admin.inc /uc_sagepay/uc_sagepay_token/uc_sagepay_token.admin.inc
*** /uc_protx_vsp_direct/uc_sagepay_token/uc_sagepay_token.admin.inc	2011-09-30 23:28:28.000000000 +0100
--- /uc_sagepay/uc_sagepay_token/uc_sagepay_token.admin.inc	2013-01-31 17:04:37.000000000 +0000
***************
*** 6,14 ****
   * @see uc_credit_terminal()
   */
  function uc_sagepay_token_terminal($order) {
!   $output = l(t('Return to order view screen.'), 'admin/store/orders/'. $order->order_id);
  
!   $output .= '<p>'. t('Use this terminal to process payments against stored credit cards.') .'</p>';
  
    $context = array(
      'revision' => 'formatted-original',
--- 6,14 ----
   * @see uc_credit_terminal()
   */
  function uc_sagepay_token_terminal($order) {
!   $output = l(t('Return to order view screen.'), 'admin/store/orders/' . $order->order_id);
  
!   $output .= '<p>' . t('Use this terminal to process payments against stored credit cards.') . '</p>';
  
    $context = array(
      'revision' => 'formatted-original',
***************
*** 17,24 ****
        'order' => $order,
      ),
    );
!   $output .= '<div><strong>'. t('Order total: @total', array('@total' => uc_price($order->order_total, $context))) .'</strong></div>'
!             .'<div><strong>'. t('Balance: @balance', array('@balance' => uc_price(uc_payment_balance($order), $context))) .'</strong></div>';
  
    $output .= drupal_get_form('uc_sagepay_token_terminal_form', $order);
  
--- 17,24 ----
        'order' => $order,
      ),
    );
!   $output .= '<div><strong>' . t('Order total: @total', array('@total' => uc_price($order->order_total, $context))) . '</strong></div>'
!             . '<div><strong>' . t('Balance: @balance', array('@balance' => uc_price(uc_payment_balance($order), $context))) . '</strong></div>';
  
    $output .= drupal_get_form('uc_sagepay_token_terminal_form', $order);
  
***************
*** 30,36 ****
   *
   * @see uc_credit_terminal_form()
   */
! function uc_sagepay_token_terminal_form($form_state, $order) {
    // Use the credit card terminal form as a base.
    module_load_include('inc', 'uc_credit', 'uc_credit.admin');
    $form = uc_credit_terminal_form($form_state, $order);
--- 30,36 ----
   *
   * @see uc_credit_terminal_form()
   */
! function uc_sagepay_token_terminal_form($form, $form_state, $order) {
    // Use the credit card terminal form as a base.
    module_load_include('inc', 'uc_credit', 'uc_credit.admin');
    $form = uc_credit_terminal_form($form_state, $order);
***************
*** 100,106 ****
      $crypt = new uc_encryption_class;
  
      // Load up the existing data array.
!     $data = db_result(db_query("SELECT data FROM {uc_orders} WHERE order_id = %d", $form_state['values']['order_id']));
      $data = unserialize($data);
  
      $cache = uc_credit_cache('load');
--- 100,106 ----
      $crypt = new uc_encryption_class;
  
      // Load up the existing data array.
!     $data = db_query("SELECT data FROM {uc_orders} WHERE order_id = :order_id", array(':order_id' => $form_state['values']['order_id']))->fetchField();
      $data = unserialize($data);
  
      $cache = uc_credit_cache('load');
***************
*** 112,118 ****
      uc_store_encryption_errors($crypt, 'uc_credit');
  
      // Save it again.
!     db_query("UPDATE {uc_orders} SET data = '%s' WHERE order_id = %d", serialize($data), $form_state['values']['order_id']);
  
      drupal_set_message(t('The credit card was processed successfully. See the admin comments for more details.'));
    }
--- 112,125 ----
      uc_store_encryption_errors($crypt, 'uc_credit');
  
      // Save it again.
!     // TODO Please review the conversion of this statement to the D7 database API syntax.
!     /* db_query("UPDATE {uc_orders} SET data = '%s' WHERE order_id = %d", serialize($data), $form_state['values']['order_id']) */
!     db_update('uc_orders')
!       ->fields(array(
!         'data' => serialize($data),
!       ))
!     ->condition('order_id', $form_state['values']['order_id'])
!     ->execute();
  
      drupal_set_message(t('The credit card was processed successfully. See the admin comments for more details.'));
    }
***************
*** 124,136 ****
      drupal_set_message(t('There was an error processing the credit card.  See the admin comments for details.'), 'error');
    }
  
!   $form_state['redirect'] = 'admin/store/orders/'. $form_state['values']['order_id'];
  }
  
  /**
   * Theme function to display a list of saved cards.
   */
! function theme_uc_sagepay_token_list_cards($tokens) {
    if (!empty($tokens)) {
      $header = array(
        array('data' => t('Card')),
--- 131,144 ----
      drupal_set_message(t('There was an error processing the credit card.  See the admin comments for details.'), 'error');
    }
  
!   $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'];
  }
  
  /**
   * Theme function to display a list of saved cards.
   */
! function theme_uc_sagepay_token_list_cards($variables) {
!   $tokens = $variables['tokens'];
    if (!empty($tokens)) {
      $header = array(
        array('data' => t('Card')),
***************
*** 140,151 ****
  
      foreach ($tokens as $token) {
        $rows[] = array(
!         array('data' => theme('uc_sagepay_token_description', $token)),
          array('data' => l(t('delete'), 'user/' . $token['uid'] . '/delete_card/' . $token['token_id'])),
        );
      }
  
!     return theme('table', $header, $rows);
    }
    else {
      return t('No cards have been saved to your account.');
--- 148,159 ----
  
      foreach ($tokens as $token) {
        $rows[] = array(
!         array('data' => theme('uc_sagepay_token_description', array('token' => $token))),
          array('data' => l(t('delete'), 'user/' . $token['uid'] . '/delete_card/' . $token['token_id'])),
        );
      }
  
!     return theme('table', array('header' => $header, 'rows' => $rows));
    }
    else {
      return t('No cards have been saved to your account.');
***************
*** 155,161 ****
  /**
   * Confirmation form to delete a saved card.
   */
! function uc_sagepay_token_delete_confirm_form($form_state, $account, $token) {
    $form['uid'] = array(
      '#type' => 'value',
      '#value' => $account->uid,
--- 163,169 ----
  /**
   * Confirmation form to delete a saved card.
   */
! function uc_sagepay_token_delete_confirm_form($form, $form_state, $account, $token) {
    $form['uid'] = array(
      '#type' => 'value',
      '#value' => $account->uid,
***************
*** 165,172 ****
      '#type' => 'value',
      '#value' => $token['token_id'],
    );
!   
!   return confirm_form($form, t('Are you sure you want to delete the saved @card?', array('@card' => theme('uc_sagepay_token_description', $token))), 'user/' . $account->uid, NULL, t('Delete'));
  }
  
  /**
--- 173,180 ----
      '#type' => 'value',
      '#value' => $token['token_id'],
    );
! 
!   return confirm_form($form, t('Are you sure you want to delete the saved @card?', array('@card' => theme('uc_sagepay_token_description', array('token' => $token)))), 'user/' . $account->uid, NULL, t('Delete'));
  }
  
  /**
diff -crB /uc_protx_vsp_direct/uc_sagepay_token/uc_sagepay_token.info /uc_sagepay/uc_sagepay_token/uc_sagepay_token.info
*** /uc_protx_vsp_direct/uc_sagepay_token/uc_sagepay_token.info	2011-10-01 01:51:23.000000000 +0100
--- /uc_sagepay/uc_sagepay_token/uc_sagepay_token.info	2013-01-31 17:04:37.000000000 +0000
***************
*** 1,12 ****
  name = Sage Pay Direct token system
  description = Allows customers to store credit card details at Sage Pay for future purchases. Supports recurring payments if uc_recurring is also installed.
! dependencies[] = uc_protx_vsp_direct
  package = Ubercart - payment
! core = 6.x
! 
! ; Information added by drupal.org packaging script on 2011-10-01
! version = "6.x-2.x-dev"
! core = "6.x"
! project = "uc_protx_vsp_direct"
! datestamp = "1317430283"
! 
--- 1,5 ----
  name = Sage Pay Direct token system
  description = Allows customers to store credit card details at Sage Pay for future purchases. Supports recurring payments if uc_recurring is also installed.
! dependencies[] = uc_sagepay
  package = Ubercart - payment
! core = 7.x
diff -crB /uc_protx_vsp_direct/uc_sagepay_token/uc_sagepay_token.install /uc_sagepay/uc_sagepay_token/uc_sagepay_token.install
*** /uc_protx_vsp_direct/uc_sagepay_token/uc_sagepay_token.install	2011-09-30 23:28:28.000000000 +0100
--- /uc_sagepay/uc_sagepay_token/uc_sagepay_token.install	2013-01-31 17:04:37.000000000 +0000
***************
*** 1,21 ****
  <?php
  
  /**
!  * Implementation of hook_install().
   */
  function uc_sagepay_token_install() {
!   drupal_install_schema('uc_sagepay_token');
  }
  
  /**
!  * Implementation of hook_uninstall().
   */
  function uc_sagepay_token_uninstall() {
!   drupal_uninstall_schema('uc_sagepay_token');
  }
  
  /**
!  * Implementation of hook_schema().
   */
  function uc_sagepay_token_schema() {
    $schema = array();
--- 1,29 ----
  <?php
+ /**
+  * @file
+  * Install, update and uninstall functions for the uc_sagepay_token module.
+  *
+  */
+ 
  
  /**
!  * Implements hook_install().
   */
  function uc_sagepay_token_install() {
!   // TODO The drupal_(un)install_schema functions are called automatically in D7.
!   // drupal_install_schema('uc_sagepay_token')
  }
  
  /**
!  * Implements hook_uninstall().
   */
  function uc_sagepay_token_uninstall() {
!   // TODO The drupal_(un)install_schema functions are called automatically in D7.
!   // drupal_uninstall_schema('uc_sagepay_token')
  }
  
  /**
!  * Implements hook_schema().
   */
  function uc_sagepay_token_schema() {
    $schema = array();
***************
*** 80,91 ****
  
    return $schema;
  }
- 
- // Store the order id where the token was created, so recurring orders can find it later.
- function uc_sagepay_token_update_6200() {
-   $ret = array();
- 
-   db_add_field($ret, 'uc_sagepay_tokens', 'order_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
- 
-   return $ret;
- }
--- 88,90 ----
diff -crB /uc_protx_vsp_direct/uc_sagepay_token/uc_sagepay_token.module /uc_sagepay/uc_sagepay_token/uc_sagepay_token.module
*** /uc_protx_vsp_direct/uc_sagepay_token/uc_sagepay_token.module	2011-09-30 23:28:28.000000000 +0100
--- /uc_sagepay/uc_sagepay_token/uc_sagepay_token.module	2013-02-05 14:04:30.000000000 +0000
***************
*** 1,16 ****
  <?php
  
  /**
!  * Implementation of hook_init().
   */
  function uc_sagepay_token_init() {
    if (module_exists('uc_recurring')) {
!     require_once('uc_sagepay_token.recurring.inc');
    }
  }
  
  /**
!  * Implementation of hook_menu().
   */
  function uc_sagepay_token_menu() {
    $items = array();
--- 1,16 ----
  <?php
  
  /**
!  * Implements hook_init().
   */
  function uc_sagepay_token_init() {
    if (module_exists('uc_recurring')) {
!     require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'uc_sagepay_token') . '/' . 'uc_sagepay_token.recurring.inc';
    }
  }
  
  /**
!  * Implements hook_menu().
   */
  function uc_sagepay_token_menu() {
    $items = array();
***************
*** 48,57 ****
  }
  
  /**
!  * Implementation of hook_perm().
   */
! function uc_sagepay_token_perm() {
!   return array('use stored credit cards');
  }
  
  /**
--- 48,62 ----
  }
  
  /**
!  * Implements hook_permission().
   */
! function uc_sagepay_token_permission() {
!   return array(
!     'use stored credit cards' => array(
!       'title' => t('use stored credit cards'),
!       'description' => t('TODO Add a description for \'use stored credit cards\''),
!     ),
!   );
  }
  
  /**
***************
*** 71,99 ****
  }
  
  /**
!  * Implementation of hook_user().
   */
! function uc_sagepay_token_user($op, &$edit, &$account, $category = NULL) {
    global $user;
!   switch ($op) {
!     case 'view':
!       if ($user->uid && (($user->uid == $account->uid && user_access('use stored credit cards')) || user_access('administer credit cards'))) {
!         $account->content['sagepay_token'] = array(
!           '#type' => 'user_profile_category',
!           '#title' => t('Saved credit/debit cards'),
!           'cards' => array(
!             '#type' => 'user_profile_item',
!             '#value' => theme('uc_sagepay_token_list_cards', uc_sagepay_token_load_tokens($account->uid)),
!           ),
!         );
!       }
! 
!       break;
    }
  }
  
  /**
!  * Implementation of hook_payment_method().
   */
  function uc_sagepay_token_payment_method() {
    if (arg(0) == 'cart') {
--- 76,99 ----
  }
  
  /**
!  * Implements hook_user_view().
   */
! function uc_sagepay_token_user_view($account, $view_mode) {
    global $user;
!   if ($user->uid && (($user->uid == $account->uid && user_access('use stored credit cards')) || user_access('administer credit cards'))) {
!     $account->content['sagepay_token'] = array(
!       '#type' => 'user_profile_category',
!       '#title' => t('Saved credit/debit cards'),
!       'cards' => array(
!         '#type' => 'user_profile_item',
!         '#value' => theme('uc_sagepay_token_list_cards', array('tokens' => uc_sagepay_token_load_tokens($account->uid))),
!       ),
!     );
    }
  }
  
  /**
!  * Implements hook_payment_method().
   */
  function uc_sagepay_token_payment_method() {
    if (arg(0) == 'cart') {
***************
*** 105,157 ****
    }
  
    return array(array(
!     'id' => 'sagepay_token',
!     'name' => t('Sage Pay stored credit card'),
!     'title' => t('Use my saved card details'),
!     'review' => t('Stored credit card'),
!     'callback' => 'uc_payment_method_sagepay_token',
!     'weight' => 1,
!     'checkout' => TRUE,
!     'no_gateway' => TRUE,
!   ));
  }
  
  /**
!  * Implementation of hook_theme().
   */
  function uc_sagepay_token_theme() {
    return array(
      'uc_sagepay_token_description' => array(
!       'arguments' => array('token' => NULL),
      ),
      'uc_sagepay_token_list_cards' => array(
!       'arguments' => array('tokens' => NULL),
        'file' => 'uc_sagepay_token.admin.inc',
      ),
    );
  }
  
  /**
!  * Implementation of hook_theme_registry_alter().
   *
   * Override theme_uc_payment_method_credit_form to add our checkbox.
   */
  function uc_sagepay_token_theme_registry_alter(&$theme_registry) {
    global $theme;
  
    // Back up the existing theme function.
    $themes = variable_get('uc_sagepay_token_theme', array());
!   $themes[$theme] = array(
!     'uc_payment_method_credit_form' => $theme_registry['uc_payment_method_credit_form']['function'],
!   );
!   variable_set('uc_sagepay_token_theme', $themes);
  
!   // Replace with our own.
!   $theme_registry['uc_payment_method_credit_form']['function'] = 'uc_sagepay_token_theme_uc_payment_method_credit_form';
  }
  
  /**
!  * Implementation of hook_form_alter().
   *
   * Add a new checkbox to the credit card payment form.
   */
--- 105,162 ----
    }
  
    return array(array(
!       'id' => 'sagepay_token',
!       'name' => t('Sage Pay stored credit card'),
!       'title' => t('Use my saved card details'),
!       'review' => t('Stored credit card'),
!       'callback' => 'uc_payment_method_sagepay_token',
!       'weight' => 1,
!       'checkout' => TRUE,
!       'no_gateway' => TRUE,
!     ));
  }
  
  /**
!  * Implements hook_theme().
   */
  function uc_sagepay_token_theme() {
    return array(
      'uc_sagepay_token_description' => array(
!       'variables' => array('token' => NULL),
      ),
      'uc_sagepay_token_list_cards' => array(
!       'variables' => array('tokens' => NULL),
        'file' => 'uc_sagepay_token.admin.inc',
      ),
    );
  }
  
  /**
!  * Implements hook_theme_registry_alter().
   *
   * Override theme_uc_payment_method_credit_form to add our checkbox.
   */
  function uc_sagepay_token_theme_registry_alter(&$theme_registry) {
    global $theme;
  
+   dfb($theme_registry);
+ 
    // Back up the existing theme function.
    $themes = variable_get('uc_sagepay_token_theme', array());
!   
!   if(isset($theme_registry['uc_payment_method_credit_form'])) {
!     $themes[$theme] = array(
!       'uc_payment_method_credit_form' => $theme_registry['uc_payment_method_credit_form']['function'],
!     );
!     variable_set('uc_sagepay_token_theme', $themes);
  
!     // Replace with our own.
!     $theme_registry['uc_payment_method_credit_form']['function'] = 'uc_sagepay_token_theme_uc_payment_method_credit_form';
!   }
  }
  
  /**
!  * Implements hook_form_alter().
   *
   * Add a new checkbox to the credit card payment form.
   */
***************
*** 194,206 ****
  }
  
  /**
!  * Implementation of hook_form_alter().
   */
  function uc_sagepay_token_form_uc_cart_checkout_form_alter(&$form, $form_state) {
    if (user_access('use stored credit cards')) {
      // Modify the standard "credit card" payment method title so it makes more sense.
      if (isset($form['panes']['payment']['payment_method']['#options']['sagepay_token'])
!         && isset($form['panes']['payment']['payment_method']['#options']['credit'])) {
        $form['panes']['payment']['payment_method']['#options']['credit'] = t('Enter details for a different card');
      }
    }
--- 199,211 ----
  }
  
  /**
!  * Implements hook_form_alter().
   */
  function uc_sagepay_token_form_uc_cart_checkout_form_alter(&$form, $form_state) {
    if (user_access('use stored credit cards')) {
      // Modify the standard "credit card" payment method title so it makes more sense.
      if (isset($form['panes']['payment']['payment_method']['#options']['sagepay_token'])
!          && isset($form['panes']['payment']['payment_method']['#options']['credit'])) {
        $form['panes']['payment']['payment_method']['#options']['credit'] = t('Enter details for a different card');
      }
    }
***************
*** 208,213 ****
--- 213,222 ----
    $form['#submit'][] = 'uc_sagepay_token_checkout_form_submit';
  }
  
+ /**
+  * @todo Please document this function.
+  * @see http://drupal.org/node/1354
+  */
  function uc_sagepay_token_checkout_form_submit($form, &$form_state) {
    $_SESSION['create_sagepay_token'] = isset($_POST['create_sagepay_token']) && $_POST['create_sagepay_token'];
  
***************
*** 222,247 ****
  }
  
  /**
!  * Implementation of hook_uc_protx_vsp_direct_transaction_alter().
   *
   * If the user selected to create a token, register their card with SagePay and use it for the transaction.
   * If this order is being processed with a stored card, load the token and the customer-supplied CVV.
   */
! function uc_sagepay_token_uc_protx_vsp_direct_transaction_alter(&$transaction, $order) {
    global $user;
  
    // Transaction fields that need to be removed when using tokens.
    $fields = array('CardHolder', 'CardNumber', 'StartDate', 'ExpiryDate', 'IssueNumber', 'CV2', 'CardType');
  
    if ($order->payment_method == 'credit' && isset($_SESSION['create_sagepay_token']) && $_SESSION['create_sagepay_token']) {
!     if (variable_get('uc_protx_vsp_direct_server', 2) == 0) {
        drupal_set_message(t('The SagePay token system is not implemented in the simulator, use the test server instead.'), 'error');
        return;
      }
!  
      $registration = array(
        'VPSProtocol' => '2.23',
!       'Vendor' => variable_get('uc_protx_vsp_direct_vendor', ''),
        'TxType' => 'TOKEN',
        'Currency' => $transaction['Currency'],
      );
--- 231,256 ----
  }
  
  /**
!  * Implements hook_uc_sagepay_transaction_alter().
   *
   * If the user selected to create a token, register their card with SagePay and use it for the transaction.
   * If this order is being processed with a stored card, load the token and the customer-supplied CVV.
   */
! function uc_sagepay_token_uc_sagepay_transaction_alter(&$transaction, $order) {
    global $user;
  
    // Transaction fields that need to be removed when using tokens.
    $fields = array('CardHolder', 'CardNumber', 'StartDate', 'ExpiryDate', 'IssueNumber', 'CV2', 'CardType');
  
    if ($order->payment_method == 'credit' && isset($_SESSION['create_sagepay_token']) && $_SESSION['create_sagepay_token']) {
!     if (variable_get('uc_sagepay_server', 2) == 0) {
        drupal_set_message(t('The SagePay token system is not implemented in the simulator, use the test server instead.'), 'error');
        return;
      }
! 
      $registration = array(
        'VPSProtocol' => '2.23',
!       'Vendor' => variable_get('uc_sagepay_vendor', ''),
        'TxType' => 'TOKEN',
        'Currency' => $transaction['Currency'],
      );
***************
*** 255,261 ****
  
      // Send token registration to SagePay.
      uc_order_comment_save($order->order_id, $user->uid, t('Token registration sent.'));
!     $result = uc_protx_vsp_direct_transaction('TOKEN', $registration, $order);
  
      if ($result['success']) {
        // Save the token for later, as the user may not have an account to attach it to just yet.
--- 264,270 ----
  
      // Send token registration to SagePay.
      uc_order_comment_save($order->order_id, $user->uid, t('Token registration sent.'));
!     $result = uc_sagepay_transaction('TOKEN', $registration, $order);
  
      if ($result['success']) {
        // Save the token for later, as the user may not have an account to attach it to just yet.
***************
*** 308,316 ****
  }
  
  /**
!  * Implementation of hook_uc_protx_vsp_direct_url_alter().
   */
! function uc_sagepay_token_uc_protx_vsp_direct_url_alter(&$servers) {
    $servers['TOKEN'] = array(
      1 => 'https://test.sagepay.com/gateway/service/directtoken.vsp',
      2 => 'https://live.sagepay.com/gateway/service/directtoken.vsp',
--- 317,325 ----
  }
  
  /**
!  * Implements hook_uc_sagepay_url_alter().
   */
! function uc_sagepay_token_uc_sagepay_url_alter(&$servers) {
    $servers['TOKEN'] = array(
      1 => 'https://test.sagepay.com/gateway/service/directtoken.vsp',
      2 => 'https://live.sagepay.com/gateway/service/directtoken.vsp',
***************
*** 322,328 ****
  }
  
  /**
!  * Implementation of hook_uc_checkout_complete().
   *
   * Save a created token to the database, now we have the user's account details.
   */
--- 331,337 ----
  }
  
  /**
!  * Implements hook_uc_checkout_complete().
   *
   * Save a created token to the database, now we have the user's account details.
   */
***************
*** 332,338 ****
      $token = $_SESSION['sagepay_token'];
      $token['uid'] = $order->uid;
      $token['order_id'] = $order->order_id;
!     $token['server'] = variable_get('uc_protx_vsp_direct_server', 2);
      drupal_write_record('uc_sagepay_tokens', $token);
    }
  
--- 341,347 ----
      $token = $_SESSION['sagepay_token'];
      $token['uid'] = $order->uid;
      $token['order_id'] = $order->order_id;
!     $token['server'] = variable_get('uc_sagepay_server', 2);
      drupal_write_record('uc_sagepay_tokens', $token);
    }
  
***************
*** 343,350 ****
  /**
   * Load a single saved card.
   */
! function uc_sagepay_token_load($token_id) {
!   return db_fetch_array(db_query("SELECT * FROM {uc_sagepay_tokens} WHERE token_id = %d", $token_id));
  }
  
  /**
--- 352,364 ----
  /**
   * Load a single saved card.
   */
! function uc_sagepay_token_load($token_ids) {
!   foreach ($token_ids as $nid => &$token_id) {
!     $node_additions = db_fetch_array(db_query("SELECT * FROM {uc_sagepay_tokens} WHERE token_id = :token_id", array(':token_id' => $token_id)));
!     foreach ($node_additions as $property => &$value) {
!       $token_id->$property = $value;
!     }
!   }
  }
  
  /**
***************
*** 354,367 ****
    if ($token = uc_sagepay_token_load($token_id)) {
      $transaction = array(
        'VPSProtocol' => '2.23',
!       'Vendor' => variable_get('uc_protx_vsp_direct_vendor', ''),
        'TxType' => 'REMOVETOKEN',
        'Token' => $token['token'],
      );
!     $result = uc_protx_vsp_direct_transaction('REMOVETOKEN', $transaction);
  
      if ($result['success'] || $force) {
!       db_query("DELETE FROM {uc_sagepay_tokens} WHERE token_id = %d", $token_id);
        return TRUE;
      }
    }
--- 368,385 ----
    if ($token = uc_sagepay_token_load($token_id)) {
      $transaction = array(
        'VPSProtocol' => '2.23',
!       'Vendor' => variable_get('uc_sagepay_vendor', ''),
        'TxType' => 'REMOVETOKEN',
        'Token' => $token['token'],
      );
!     $result = uc_sagepay_transaction('REMOVETOKEN', $transaction);
  
      if ($result['success'] || $force) {
!       // TODO Please review the conversion of this statement to the D7 database API syntax.
!       /* db_query("DELETE FROM {uc_sagepay_tokens} WHERE token_id = %d", $token_id) */
!       db_delete('uc_sagepay_tokens')
!         ->condition('token_id', $token_id)
!         ->execute();
        return TRUE;
      }
    }
***************
*** 378,391 ****
    if (is_null($uid)) {
      $uid = $user->uid;
    }
!   $server = variable_get('uc_protx_vsp_direct_server', 2);
  
    $tokens = array();
!   $result = db_query("SELECT * FROM {uc_sagepay_tokens} WHERE uid = %d AND server = %d ORDER BY token_id", $uid, $server);
!   while ($token = db_fetch_array($result)) {
      // Don't return expired cards.
!     if (_valid_card_expiration(substr($token['expiry'], 0, 2), 2000 + substr($token['expiry'], 2))) {
!       $tokens[$token['token_id']] = $token;
      }
    }
  
--- 396,409 ----
    if (is_null($uid)) {
      $uid = $user->uid;
    }
!   $server = variable_get('uc_sagepay_server', 2);
  
    $tokens = array();
!   $result = db_query("SELECT * FROM {uc_sagepay_tokens} WHERE uid = :uid AND server = :server ORDER BY token_id", array(':uid' => $uid, ':server' => $server));
!   foreach ($result as $token) {
      // Don't return expired cards.
!     if (_valid_card_expiration(substr($token->expiry, 0, 2), 2000 + substr($token->expiry, 2))) {
!       $tokens[$token->token_id] = $token;
      }
    }
  
***************
*** 393,399 ****
  }
  
  /**
!  * Implementation of hook_order().
   */
  function uc_sagepay_token_order($op, &$arg1, $arg2) {
    switch ($op) {
--- 411,417 ----
  }
  
  /**
!  * Implements hook_order().
   */
  function uc_sagepay_token_order($op, &$arg1, $arg2) {
    switch ($op) {
***************
*** 513,534 ****
  
        $review = array();
        if (variable_get('uc_credit_type_enabled', FALSE)) {
!         $review[] = array('title' => t('Card Type'), 'data' => check_plain($token['card_type']));
        }
!       $review[] = array('title' => t('Card Number'), 'data' => str_repeat('-', 12) . check_plain($token['last_4']));
!       $review[] = array('title' => t('Expiration'), 'data' => substr($token['expiry'], 0, 2) . '/20' . substr($token['expiry'], 2));
        if (variable_get('uc_credit_cvv_enabled', TRUE)) {
!         $review[] = array('title' => t('CVV'), 'data' => user_access('view cc numbers') ? $arg1->payment_details['cc_cvv'] : str_repeat('-', strlen($arg1->payment_details['cc_cvv'])));
        }
        return $review;
  
      case 'order-view':
        if (user_access('view cc details') && $token = uc_sagepay_token_load($arg1->payment_details['cc_token'])) {
!         $output = '<a href="#" onclick="$(this).hide().next().show(); return false;">'. t('View card details') .'</a>';
          $output .= '<table style="width: auto; display: none;">';
!         $output .= '<tr><td>'. t('Card Type:') . ' </td><td>' . check_plain($token['card_type']) . '</td></tr>';
!         $output .= '<tr><td>'. t('Card Number:') . ' </td><td>' . t('(Last 4) ') . check_plain($token['last_4']) . '</td></tr>';
!         $output .= '<tr><td>'. t('Expiration:') . ' </td><td>' . substr($token['expiry'], 0, 2) . '/20' . substr($token['expiry'], 2) . '</td></tr>';
          $output .= '</table>';
  
          if (user_access('process credit cards')) {
--- 531,564 ----
  
        $review = array();
        if (variable_get('uc_credit_type_enabled', FALSE)) {
!         $review[] = array(
!           'title' => t('Card Type'),
!           'data' => check_plain($token['card_type']),
!         );
        }
!       $review[] = array(
!         'title' => t('Card Number'),
!         'data' => str_repeat('-', 12) . check_plain($token['last_4']),
!       );
!       $review[] = array(
!         'title' => t('Expiration'),
!         'data' => substr($token['expiry'], 0, 2) . '/20' . substr($token['expiry'], 2),
!       );
        if (variable_get('uc_credit_cvv_enabled', TRUE)) {
!         $review[] = array(
!           'title' => t('CVV'),
!           'data' => user_access('view cc numbers') ? $arg1->payment_details['cc_cvv'] : str_repeat('-', strlen($arg1->payment_details['cc_cvv'])),
!         );
        }
        return $review;
  
      case 'order-view':
        if (user_access('view cc details') && $token = uc_sagepay_token_load($arg1->payment_details['cc_token'])) {
!         $output = '<a href="#" onclick="$(this).hide().next().show(); return false;">' . t('View card details') . '</a>';
          $output .= '<table style="width: auto; display: none;">';
!         $output .= '<tr><td>' . t('Card Type:') . ' </td><td>' . check_plain($token['card_type']) . '</td></tr>';
!         $output .= '<tr><td>' . t('Card Number:') . ' </td><td>' . t('(Last 4) ') . check_plain($token['last_4']) . '</td></tr>';
!         $output .= '<tr><td>' . t('Expiration:') . ' </td><td>' . substr($token['expiry'], 0, 2) . '/20' . substr($token['expiry'], 2) . '</td></tr>';
          $output .= '</table>';
  
          if (user_access('process credit cards')) {
***************
*** 551,557 ****
   *
   * @see uc_credit_order_view_form()
   */
! function uc_sagepay_token_order_view_form($form_state, $order_id) {
    $form['#attributes'] = array('style' => 'margin: 0.5em 0 0 0;');
    $form['order_id'] = array(
      '#type' => 'hidden',
--- 581,587 ----
   *
   * @see uc_credit_order_view_form()
   */
! function uc_sagepay_token_order_view_form($form, $form_state, $order_id) {
    $form['#attributes'] = array('style' => 'margin: 0.5em 0 0 0;');
    $form['order_id'] = array(
      '#type' => 'hidden',
***************
*** 565,572 ****
    return $form;
  }
  
  function uc_sagepay_token_order_view_form_submit($form, &$form_state) {
!   $form_state['redirect'] = 'admin/store/orders/'. $form_state['values']['order_id'] .'/sagepay_token';
  }
  
  /**
--- 595,606 ----
    return $form;
  }
  
+ /**
+  * @todo Please document this function.
+  * @see http://drupal.org/node/1354
+  */
  function uc_sagepay_token_order_view_form_submit($form, &$form_state) {
!   $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'] . '/sagepay_token';
  }
  
  /**
***************
*** 574,580 ****
   *
   * @see uc_payment_method_credit_form()
   */
! function uc_payment_method_sagepay_token_form($form_state, $order) {
    // Normally the CC data is posted in via AJAX.
    if (!empty($_POST['payment-details-data']) && arg(0) == 'cart') {
      $order->payment_details = uc_credit_cache('save', $_POST['payment-details-data']);
--- 608,614 ----
   *
   * @see uc_payment_method_credit_form()
   */
! function uc_payment_method_sagepay_token_form($form, $form_state, $order) {
    // Normally the CC data is posted in via AJAX.
    if (!empty($_POST['payment-details-data']) && arg(0) == 'cart') {
      $order->payment_details = uc_credit_cache('save', $_POST['payment-details-data']);
***************
*** 589,595 ****
    $tokens = uc_sagepay_token_load_tokens($order->uid);
    $options = array();
    foreach ($tokens as $token) {
!     $options[$token['token_id']] = theme('uc_sagepay_token_description', $token);
    }
  
    $form['cc_policy'] = array('#value' => variable_get('uc_credit_policy', t('Your billing information must match the billing address for the credit card entered below or we will be unable to process your payment.')));
--- 623,629 ----
    $tokens = uc_sagepay_token_load_tokens($order->uid);
    $options = array();
    foreach ($tokens as $token) {
!     $options[$token['token_id']] = theme('uc_sagepay_token_description', array('token' => $token));
    }
  
    $form['cc_policy'] = array('#value' => variable_get('uc_credit_policy', t('Your billing information must match the billing address for the credit card entered below or we will be unable to process your payment.')));
***************
*** 621,627 ****
        '#type' => 'textfield',
        '#title' => t('CVV'),
        '#description' => t('To protect our customers, we do not store the CVV number.'),
!       '#field_suffix' => '<img src="' . base_path() . drupal_get_path('module', 'uc_credit') . '/images/info.png" onclick="cvv_info_popup();" style="cursor: pointer; position: relative; top: 3px;" /> <a style="cursor: pointer; font-weight: normal;" onclick="cvv_info_popup();">' . t("What's the CVV?") .'</a>',
        '#default_value' => $_SESSION['clear_cc'] ? '' : $default_cvv,
        '#attributes' => array('autocomplete' => 'off'),
        '#size' => variable_get('uc_credit_amex', TRUE) ? 4 : 3,
--- 655,661 ----
        '#type' => 'textfield',
        '#title' => t('CVV'),
        '#description' => t('To protect our customers, we do not store the CVV number.'),
!       '#field_suffix' => '<img src="' . base_path() . drupal_get_path('module', 'uc_credit') . '/images/info.png" onclick="cvv_info_popup();" style="cursor: pointer; position: relative; top: 3px;" /> <a style="cursor: pointer; font-weight: normal;" onclick="cvv_info_popup();">' . t("What's the CVV?") . '</a>',
        '#default_value' => $_SESSION['clear_cc'] ? '' : $default_cvv,
        '#attributes' => array('autocomplete' => 'off'),
        '#size' => variable_get('uc_credit_amex', TRUE) ? 4 : 3,
***************
*** 637,643 ****
  /**
   * Theme description of a saved card.
   */
! function theme_uc_sagepay_token_description($token) {
    $expiry = substr($token['expiry'], 0, 2) . '/20' . substr($token['expiry'], 2);
    return t('!type, ending with !last-4, expires !expiry', array('!type' => $token['card_type'], '!last-4' => $token['last_4'], '!expiry' => $expiry));
  }
--- 671,678 ----
  /**
   * Theme description of a saved card.
   */
! function theme_uc_sagepay_token_description($variables) {
!   $token = $variables['token'];
    $expiry = substr($token['expiry'], 0, 2) . '/20' . substr($token['expiry'], 2);
    return t('!type, ending with !last-4, expires !expiry', array('!type' => $token['card_type'], '!last-4' => $token['last_4'], '!expiry' => $expiry));
  }
diff -crB /uc_protx_vsp_direct/uc_sagepay_token/uc_sagepay_token.recurring.inc /uc_sagepay/uc_sagepay_token/uc_sagepay_token.recurring.inc
*** /uc_protx_vsp_direct/uc_sagepay_token/uc_sagepay_token.recurring.inc	2011-09-30 23:28:28.000000000 +0100
--- /uc_sagepay/uc_sagepay_token/uc_sagepay_token.recurring.inc	2013-02-04 16:08:18.000000000 +0000
***************
*** 1,32 ****
  <?php
  
  /**
!  * Implementation of hook_recurring_info().
   */
  function uc_sagepay_token_recurring_info() {
!   $items['protx_vsp_direct'] = array(
      'name' => t('Sage Pay Direct'),
-     'module' => 'uc_sagepay_token',
-     'fee handler' => 'sagepay_token',
      'payment method' => 'credit',
      'process callback' => 'uc_sagepay_recurring_process',
      'menu' => array(
        'charge' => UC_RECURRING_MENU_DEFAULT,
!       'edit'   => UC_RECURRING_MENU_DEFAULT,
        'cancel' => UC_RECURRING_MENU_DEFAULT,
      ),
    );
  
    $items['sagepay_token'] = array(
      'name' => t('Sage Pay stored credit card'),
-     'module' => 'uc_sagepay_token',
-     'fee handler' => 'sagepay_token',
      'payment method' => 'sagepay_token',
      'process callback' => 'uc_sagepay_token_recurring_process',
      'renew callback' => 'uc_sagepay_token_recurring_renew',
      'menu' => array(
        'charge' => UC_RECURRING_MENU_DEFAULT,
!       'edit'   => UC_RECURRING_MENU_DEFAULT,
        'cancel' => UC_RECURRING_MENU_DEFAULT,
      ),
    );
--- 1,34 ----
  <?php
  
  /**
!  * Implements hook_recurring_info().
   */
  function uc_sagepay_token_recurring_info() {
!   $items = array();
! 
!   $items['uc_sagepay'] = array(
      'name' => t('Sage Pay Direct'),
      'payment method' => 'credit',
+     'fee handler' => 'sagepay_token',
+     'module' => 'uc_sagepay_token',
      'process callback' => 'uc_sagepay_recurring_process',
      'menu' => array(
        'charge' => UC_RECURRING_MENU_DEFAULT,
!       'edit' => UC_RECURRING_MENU_DEFAULT,
        'cancel' => UC_RECURRING_MENU_DEFAULT,
      ),
    );
  
    $items['sagepay_token'] = array(
      'name' => t('Sage Pay stored credit card'),
      'payment method' => 'sagepay_token',
+     'fee handler' => 'sagepay_token',
+     'module' => 'uc_sagepay_token',
      'process callback' => 'uc_sagepay_token_recurring_process',
      'renew callback' => 'uc_sagepay_token_recurring_renew',
      'menu' => array(
        'charge' => UC_RECURRING_MENU_DEFAULT,
!       'edit' => UC_RECURRING_MENU_DEFAULT,
        'cancel' => UC_RECURRING_MENU_DEFAULT,
      ),
    );
***************
*** 55,67 ****
  }
  
  /**
!  * Implementation of hook_recurring_renewal_pending().
   */
  function uc_sagepay_token_recurring_renewal_pending(&$order, $fee) {
    // If the original order was made by credit card, switch this order to the
    // token method, using the token that was created against the original.
    if ($fee->fee_handler == 'sagepay_token' && $order->payment_method == 'credit') {
!     $token = db_result(db_query("SELECT token_id FROM {uc_sagepay_tokens} WHERE order_id = %d", $order->data['old_order_id']));
      $order->payment_method = 'sagepay_token';
      $order->payment_details['cc_token'] = $token;
  
--- 57,69 ----
  }
  
  /**
!  * Implements hook_recurring_renewal_pending().
   */
  function uc_sagepay_token_recurring_renewal_pending(&$order, $fee) {
    // If the original order was made by credit card, switch this order to the
    // token method, using the token that was created against the original.
    if ($fee->fee_handler == 'sagepay_token' && $order->payment_method == 'credit') {
!     $token = db_query("SELECT token_id FROM {uc_sagepay_tokens} WHERE order_id = :order_id", array(':order_id' => $order->data['old_order_id']))->fetchField();
      $order->payment_method = 'sagepay_token';
      $order->payment_details['cc_token'] = $token;
  
