From f3d705c3ac8dce1ed87189b4a5184b08aebfc4af Mon Sep 17 00:00:00 2001
From: Dave Long <dave@longwaveconsulting.com>
Date: Fri, 10 Jun 2011 10:43:50 +0100
Subject: [PATCH 1/2] Issue #1168280: Change hook_uc_payment_method() to be keyed by method ID.

---
 payment/uc_2checkout/uc_2checkout.module           |    3 +-
 payment/uc_credit/uc_credit.module                 |    3 +-
 .../uc_google_checkout/uc_google_checkout.module   |    3 +-
 payment/uc_payment/uc_payment.admin.inc            |   34 ++++++++++----------
 payment/uc_payment/uc_payment.api.php              |    3 +-
 payment/uc_payment/uc_payment.module               |   26 +++++++++------
 payment/uc_payment/uc_payment_checkout_pane.inc    |    4 +-
 payment/uc_payment/uc_payment_order_pane.inc       |    8 ++--
 payment/uc_payment_pack/uc_payment_pack.module     |    9 ++---
 9 files changed, 45 insertions(+), 48 deletions(-)

diff --git a/payment/uc_2checkout/uc_2checkout.module b/payment/uc_2checkout/uc_2checkout.module
index 4a20c68..a992e2c 100644
--- a/payment/uc_2checkout/uc_2checkout.module
+++ b/payment/uc_2checkout/uc_2checkout.module
@@ -53,8 +53,7 @@ function uc_2checkout_uc_payment_method() {
   $title = variable_get('uc_2checkout_method_title', t('Credit card on a secure server:'));
   $title .= '<br /><img src="' . $path . '/2co_logo.jpg" style="position: relative; left: 2.5em;" />';
 
-  $methods[] = array(
-    'id' => '2checkout',
+  $methods['2checkout'] = array(
     'name' => t('2Checkout'),
     'title' => $title,
     'review' => variable_get('uc_2checkout_check', FALSE) ? t('Credit card/eCheck') : t('Credit card'),
diff --git a/payment/uc_credit/uc_credit.module b/payment/uc_credit/uc_credit.module
index 3fd12e2..fcf92f4 100644
--- a/payment/uc_credit/uc_credit.module
+++ b/payment/uc_credit/uc_credit.module
@@ -409,8 +409,7 @@ function uc_credit_uc_payment_method() {
       $title .= ' <img class="uc-credit-cctype uc-credit-cctype-' . $type . '" src="' . $path . '/images/' . $type . '.gif" style="position: relative; top: 5px;" />';
     }
   }
-  $methods[] = array(
-    'id' => 'credit',
+  $methods['credit'] = array(
     'name' => t('Credit card'),
     'title' => $title,
     'desc' => t('Pay by credit card.'),
diff --git a/payment/uc_google_checkout/uc_google_checkout.module b/payment/uc_google_checkout/uc_google_checkout.module
index 7703798..a644a3a 100644
--- a/payment/uc_google_checkout/uc_google_checkout.module
+++ b/payment/uc_google_checkout/uc_google_checkout.module
@@ -354,8 +354,7 @@ function uc_google_checkout_uc_order($op, &$order, $arg2) {
  * Implements hook_uc_payment_method().
  */
 function uc_google_checkout_uc_payment_method() {
-  $methods[] = array(
-    'id' => 'google_checkout',
+  $methods['google_checkout'] = array(
     'name' => t('Google Checkout'),
     'title' => t('Google Checkout'),
     'desc' => t('Express payment with Google Checkout.'),
diff --git a/payment/uc_payment/uc_payment.admin.inc b/payment/uc_payment/uc_payment.admin.inc
index 05c5d1b..be024fa 100644
--- a/payment/uc_payment/uc_payment.admin.inc
+++ b/payment/uc_payment/uc_payment.admin.inc
@@ -32,34 +32,34 @@ function uc_payment_methods_form($form, &$form_state) {
   );
 
   if (is_array($methods) && count($methods) > 0) {
-    foreach ($methods as $method) {
+    foreach ($methods as $id => $method) {
 
-      $form['pmtable'][$method['id']]['#summary callback'] = 'uc_summarize_form';
+      $form['pmtable'][$id]['#summary callback'] = 'uc_summarize_form';
 
-      $form['pmtable'][$method['id']]['uc_payment_method_' . $method['id'] . '_checkout'] = array(
+      $form['pmtable'][$id]['uc_payment_method_' . $id . '_checkout'] = array(
         '#type' => 'checkbox',
         '#summary callback' => 'uc_summarize_checkbox',
         '#summary arguments' => array(
           t('@payment is enabled for checkout.', array('@payment' => $method['name'])),
           t('@payment is disabled for checkout.', array('@payment' => $method['name'])),
         ),
-        '#default_value' => variable_get('uc_payment_method_' . $method['id'] . '_checkout', $method['checkout']),
+        '#default_value' => variable_get('uc_payment_method_' . $id . '_checkout', $method['checkout']),
       );
-      $form['pmtable'][$method['id']]['name'] = array(
+      $form['pmtable'][$id]['name'] = array(
         '#markup' => $method['name'],
       );
-      $form['pmtable'][$method['id']]['uc_payment_method_' . $method['id'] . '_weight'] = array(
+      $form['pmtable'][$id]['uc_payment_method_' . $id . '_weight'] = array(
         '#type' => 'weight',
-        '#default_value' => variable_get('uc_payment_method_' . $method['id'] . '_weight', $method['weight']),
+        '#default_value' => variable_get('uc_payment_method_' . $id . '_weight', $method['weight']),
       );
 
       if (isset($method['no_gateway']) && $method['no_gateway'] === TRUE) {
-        $form['pmtable'][$method['id']]['uc_payment_' . $method['id'] . '_gateway'] = array(
+        $form['pmtable'][$id]['uc_payment_' . $id . '_gateway'] = array(
           '#markup' => '-',
         );
       }
       else {
-        $gateways = _uc_payment_gateway_list($method['id'], TRUE);
+        $gateways = _uc_payment_gateway_list($id, TRUE);
         $options = array();
         $default = FALSE;
         if (is_array($gateways)) {
@@ -73,25 +73,25 @@ function uc_payment_methods_form($form, &$form_state) {
         if (!$default) {
           $options = array('none' => t('None available.'));
         }
-        $form['pmtable'][$method['id']]['uc_payment_' . $method['id'] . '_gateway'] = array(
+        $form['pmtable'][$id]['uc_payment_' . $id . '_gateway'] = array(
           '#type' => 'select',
           '#options' => $options,
           '#summary callback' => 'uc_summarize_null',
-          '#default_value' => variable_get('uc_payment_' . $method['id'] . '_gateway', 'none'),
+          '#default_value' => variable_get('uc_payment_' . $id . '_gateway', 'none'),
         );
       }
 
       $null = NULL;
       $method_settings = $method['callback']('settings', $null, array(), $form_state);
       if (is_array($method_settings)) {
-        $form['method_' . $method['id']] = array(
+        $form['method_' . $id] = array(
           '#type' => 'fieldset',
           '#summary callback' => 'uc_summarize_null',
           '#title' => t('!method settings', array('!method' => $method['name'], '!weight' => $method['weight'])),
           '#collapsible' => TRUE,
           '#collapsed' => TRUE,
         );
-        $form['method_' . $method['id']] = array_merge($form['method_' . $method['id']], $method_settings);
+        $form['method_' . $id] = array_merge($form['method_' . $id], $method_settings);
       }
     }
   }
@@ -149,8 +149,8 @@ function uc_payment_gateways_form($form, &$form_state) {
         '#collapsed' => TRUE,
       );
       $supported_methods = array();
-      foreach ($methods as $method) {
-        if (isset($gateway[$method['id']]) && function_exists($gateway[$method['id']])) {
+      foreach ($methods as $method_id => $method) {
+        if (isset($gateway[$method_id]) && function_exists($gateway[$method_id])) {
           $supported_methods[] = $method['name'];
         }
       }
@@ -334,8 +334,8 @@ function uc_payment_by_order_form($form, &$form_state, $order) {
       '#markup' => '-',
     );
     $methods = _uc_payment_method_list();
-    foreach ($methods as $method) {
-      $options[$method['id']] = $method['name'];
+    foreach ($methods as $id => $method) {
+      $options[$id] = $method['name'];
     }
     $form['payments']['new']['method'] = array(
       '#type' => 'select',
diff --git a/payment/uc_payment/uc_payment.api.php b/payment/uc_payment/uc_payment.api.php
index 3413424..f0772ec 100644
--- a/payment/uc_payment/uc_payment.api.php
+++ b/payment/uc_payment/uc_payment.api.php
@@ -99,8 +99,7 @@ function hook_uc_payment_gateway_alter(&$gateways) {
  *   An array of payment methods.
  */
 function hook_uc_payment_method() {
-  $methods[] = array(
-    'id' => 'check',
+  $methods['check'] = array(
     'name' => t('Check'),
     'title' => t('Check or Money Order'),
     'desc' => t('Pay by mailing a check or money order.'),
diff --git a/payment/uc_payment/uc_payment.module b/payment/uc_payment/uc_payment.module
index 26c0c3c..fba364f 100644
--- a/payment/uc_payment/uc_payment.module
+++ b/payment/uc_payment/uc_payment.module
@@ -619,18 +619,26 @@ function uc_payment_load_payments($order_id, $action = NULL) {
  * Build a list of payment methods defined in the enabled modules.
  */
 function _uc_payment_method_list($action = NULL) {
-  static $methods;
+  static $methods = array();
 
   if (count($methods) > 0 && $action !== 'rebuild') {
     return $methods;
   }
 
-  $methods = module_invoke_all('uc_payment_method');
-  foreach ($methods as $i => $value) {
-    $methods[$i]['checkout'] = variable_get('uc_payment_method_' . $methods[$i]['id'] . '_checkout', $methods[$i]['checkout']);
-    $methods[$i]['weight'] = variable_get('uc_payment_method_' . $methods[$i]['id'] . '_weight', $methods[$i]['weight']);
+  foreach (module_invoke_all('uc_payment_method') as $id => $method) {
+    // Preserve backward compatibility for panes with no key specified.
+    if (is_numeric($id)) {
+      $id = $method['id'];
+    }
+
+    $methods[$id] = array_merge($method, array(
+      'id' => $id,
+      'checkout' => variable_get('uc_payment_method_' . $id . '_checkout', $method['checkout']),
+      'weight' => variable_get('uc_payment_method_' . $id . '_weight', $method['weight']),
+    ));
   }
-  usort($methods, 'uc_weight_sort');
+
+  uasort($methods, 'uc_weight_sort');
 
   return $methods;
 }
@@ -640,11 +648,7 @@ function _uc_payment_method_list($action = NULL) {
  */
 function _uc_payment_method_data($method_id, $key) {
   $methods = _uc_payment_method_list();
-  foreach ($methods as $method) {
-    if ($method['id'] == $method_id) {
-      return isset($method[$key]) ? $method[$key] : NULL;
-    }
-  }
+  return $methods[$method_id][$key];
 }
 
 /**
diff --git a/payment/uc_payment/uc_payment_checkout_pane.inc b/payment/uc_payment/uc_payment_checkout_pane.inc
index 88d5e97..4c4591f 100644
--- a/payment/uc_payment/uc_payment_checkout_pane.inc
+++ b/payment/uc_payment/uc_payment_checkout_pane.inc
@@ -40,9 +40,9 @@ function uc_checkout_pane_payment($op, &$order, $form = NULL, &$form_state = NUL
       $options = array();
       $default = NULL;
 
-      foreach ($methods as $method) {
+      foreach ($methods as $id => $method) {
         if ($method['checkout']) {
-          $options[$method['id']] = $method['title'];
+          $options[$id] = $method['title'];
         }
       }
 
diff --git a/payment/uc_payment/uc_payment_order_pane.inc b/payment/uc_payment/uc_payment_order_pane.inc
index 94d7429..0513453 100644
--- a/payment/uc_payment/uc_payment_order_pane.inc
+++ b/payment/uc_payment/uc_payment_order_pane.inc
@@ -71,8 +71,8 @@ function uc_order_pane_payment($op, $order, &$form = NULL, &$form_state = NULL)
 
       $methods = _uc_payment_method_list();
       $options = array();
-      foreach ($methods as $method) {
-        $options[$method['id']] = $method['name'];
+      foreach ($methods as $id => $method) {
+        $options[$id] = $method['name'];
       }
       $form['payment']['payment_method'] = array(
         '#type' => 'select',
@@ -90,8 +90,8 @@ function uc_order_pane_payment($op, $order, &$form = NULL, &$form_state = NULL)
       // build a JS array of our payment methods
       $methods = _uc_payment_method_list();
       $method_array_js = array();
-      foreach ($methods as $method) {
-        $method_array_js[$method['name']] = $method['id'];
+      foreach ($methods as $id => $method) {
+        $method_array_js[$method['name']] = $id;
       }
       drupal_add_js(array('methods' => $method_array_js), 'setting');
 
diff --git a/payment/uc_payment_pack/uc_payment_pack.module b/payment/uc_payment_pack/uc_payment_pack.module
index 4607e4f..ed88507 100644
--- a/payment/uc_payment_pack/uc_payment_pack.module
+++ b/payment/uc_payment_pack/uc_payment_pack.module
@@ -52,8 +52,7 @@ function uc_payment_pack_init() {
  * Implements hook_uc_payment_method().
  */
 function uc_payment_pack_uc_payment_method() {
-  $methods[] = array(
-    'id' => 'check',
+  $methods['check'] = array(
     'name' => t('Check', array(), array('context' => 'cheque')),
     'title' => t('Check or money order'),
     'desc' => t('Pay by mailing a check or money order.'),
@@ -62,8 +61,7 @@ function uc_payment_pack_uc_payment_method() {
     'checkout' => TRUE,
     'no_gateway' => TRUE,
   );
-  $methods[] = array(
-    'id' => 'cod',
+  $methods['cod'] = array(
     'name' => t('COD'),
     'title' => t('Cash on delivery'),
     'desc' => t('Pay cash on delivery on pick-up.'),
@@ -72,8 +70,7 @@ function uc_payment_pack_uc_payment_method() {
     'checkout' => FALSE,
     'no_gateway' => TRUE,
   );
-  $methods[] = array(
-    'id' => 'other',
+  $methods['other'] = array(
     'name' => t('Other'),
     'title' => t('Other'),
     'desc' => t('A generic payment method type.'),
-- 
1.7.1


From bd1312ebc53c96675618f5205f0cec78818110ff Mon Sep 17 00:00:00 2001
From: Dave Long <dave@longwaveconsulting.com>
Date: Sat, 18 Jun 2011 00:02:35 +0100
Subject: [PATCH 2/2] Issue #1168280: Change hook_uc_payment_gateway() to be keyed by gateway ID.

---
 payment/uc_authorizenet/uc_authorizenet.module     |    3 +-
 payment/uc_credit/test_gateway.module              |    3 +-
 payment/uc_credit/uc_credit.module                 |   42 +------
 payment/uc_cybersource/uc_cybersource.module       |    3 +-
 .../uc_google_checkout/uc_google_checkout.module   |    3 +-
 payment/uc_payment/uc_payment.admin.inc            |   28 ++---
 payment/uc_payment/uc_payment.api.php              |   20 +--
 payment/uc_payment/uc_payment.module               |  132 ++++++++------------
 payment/uc_paypal/uc_paypal.module                 |    3 +-
 9 files changed, 82 insertions(+), 155 deletions(-)

diff --git a/payment/uc_authorizenet/uc_authorizenet.module b/payment/uc_authorizenet/uc_authorizenet.module
index 4782600..7a511c4 100644
--- a/payment/uc_authorizenet/uc_authorizenet.module
+++ b/payment/uc_authorizenet/uc_authorizenet.module
@@ -70,8 +70,7 @@ function uc_authorizenet_menu() {
  * Implements hook_uc_payment_gateway().
  */
 function uc_authorizenet_uc_payment_gateway() {
-  $gateways[] = array(
-    'id' => 'authorizenet',
+  $gateways['authorizenet'] = array(
     'title' => t('Authorize.net'),
     'description' => t('Process credit card payments using the AIM service of Authorize.net.'),
     'settings' => 'uc_authorizenet_settings_form',
diff --git a/payment/uc_credit/test_gateway.module b/payment/uc_credit/test_gateway.module
index 62cb750..9332cea 100644
--- a/payment/uc_credit/test_gateway.module
+++ b/payment/uc_credit/test_gateway.module
@@ -12,8 +12,7 @@
  ******************************************************************************/
 
 function test_gateway_uc_payment_gateway() {
-  $gateways[] = array(
-    'id' => 'test_gateway',
+  $gateways['test_gateway'] = array(
     'title' => t('Test Gateway'),
     'description' => t('Process credit card payments through the Test Gateway.'),
     'credit' => 'test_gateway_charge',
diff --git a/payment/uc_credit/uc_credit.module b/payment/uc_credit/uc_credit.module
index fcf92f4..a7abbc0 100644
--- a/payment/uc_credit/uc_credit.module
+++ b/payment/uc_credit/uc_credit.module
@@ -1527,44 +1527,12 @@ function uc_credit_default_gateway() {
     return FALSE;
   }
 
-  // If we only found one gateway for this payment method...
-  if (count($gateways) == 1) {
-    // Get the payment gateway array and store its ID.
-    $gateway = array_shift($gateways);
-    $gateway_id = $gateway['id'];
-
-    // Store the callback for this gateway.
-    $callback = $gateway['credit'];
-  }
-  else {
-    // Otherwise attempt to find the appropriate gateway function in the array.
-    $callback = FALSE;
-
-    // Loop through each gateway.
-    foreach ($gateways as $gateway) {
-      // Store the callback if this is the specified default.
-      if ($gateway['id'] == variable_get('uc_payment_credit_gateway', '')) {
-        $callback = $gateway['credit'];
-        $gateway_id = $gateway['id'];
-      }
-    }
-
-    // If we didn't find a default callback...
-    if ($callback === FALSE) {
-      // Get the key for the first payment gateway in the array.
-      $gateway_id = array_shift(array_keys($gateways));
-
-      // Store the callback for this gateway.
-      $callback = $gateways[$gateway_id]['credit'];
-    }
-  }
-
-  // Return FALSE if the specified callback does not exist.
-  if (!function_exists($callback)) {
-    return FALSE;
-  }
+  // Find the default gateway, or otherwise choose the first available.
+  $default = variable_get('uc_payment_credit_gateway', 'none');
+  $gateway = isset($gateways[$default]) ? $gateways[$default] : reset($gateways);
 
-  return $gateway_id;
+  // Return FALSE if the credit callback does not exist.
+  return function_exists($gateway['credit']) ? $gateway['id'] : FALSE;
 }
 
 /**
diff --git a/payment/uc_cybersource/uc_cybersource.module b/payment/uc_cybersource/uc_cybersource.module
index 257da9b..6f76e85 100644
--- a/payment/uc_cybersource/uc_cybersource.module
+++ b/payment/uc_cybersource/uc_cybersource.module
@@ -99,8 +99,7 @@ function uc_cybersource_uc_payment_gateway() {
   if (!module_exists('uc_credit')) {
     return;
   }
-  $gateways[] = array(
-    'id' => 'cybersource',
+  $gateways['cybersource'] = array(
     'title' => t('CyberSource Silent Order POST'),
     'description' => t('Process credit card payments using the Silent Order POST service of CyberSource.'),
     'settings' => 'uc_cybersource_settings_form',
diff --git a/payment/uc_google_checkout/uc_google_checkout.module b/payment/uc_google_checkout/uc_google_checkout.module
index a644a3a..16fda78 100644
--- a/payment/uc_google_checkout/uc_google_checkout.module
+++ b/payment/uc_google_checkout/uc_google_checkout.module
@@ -370,8 +370,7 @@ function uc_google_checkout_uc_payment_method() {
  * Implements hook_uc_payment_gateway().
  */
 function uc_google_checkout_uc_payment_gateway() {
-  $gateways[] = array(
-    'id' => 'google_checkout',
+  $gateways['google_checkout'] = array(
     'title' => t('Google Checkout'),
     'description' => t('Express payment with Google Checkout.'),
     'google_checkout' => 'uc_google_checkout_charge',
diff --git a/payment/uc_payment/uc_payment.admin.inc b/payment/uc_payment/uc_payment.admin.inc
index be024fa..7f2f8ca 100644
--- a/payment/uc_payment/uc_payment.admin.inc
+++ b/payment/uc_payment/uc_payment.admin.inc
@@ -61,16 +61,10 @@ function uc_payment_methods_form($form, &$form_state) {
       else {
         $gateways = _uc_payment_gateway_list($id, TRUE);
         $options = array();
-        $default = FALSE;
-        if (is_array($gateways)) {
-          foreach ($gateways as $gateway) {
-            if (!$default) {
-              $default = $gateway['id'];
-            }
-            $options[$gateway['id']] = $gateway['title'];
-          }
+        foreach ($gateways as $gateway_id => $gateway) {
+          $options[$gateway_id] = $gateway['title'];
         }
-        if (!$default) {
+        if (empty($options)) {
           $options = array('none' => t('None available.'));
         }
         $form['pmtable'][$id]['uc_payment_' . $id . '_gateway'] = array(
@@ -141,8 +135,8 @@ function uc_payment_gateways_form($form, &$form_state) {
       '#weight' => -10,
     );
 
-    foreach ($gateways as $gateway) {
-      $form['gateways'][$gateway['id']] = array(
+    foreach ($gateways as $id => $gateway) {
+      $form['gateways'][$id] = array(
         '#type' => 'fieldset',
         '#title' => t('@gateway_name settings', array('@gateway_name' => $gateway['title'])),
         '#collapsible' => TRUE,
@@ -155,22 +149,22 @@ function uc_payment_gateways_form($form, &$form_state) {
         }
       }
 
-      $form['gateways'][$gateway['id']]['supported_methods'] = array(
+      $form['gateways'][$id]['supported_methods'] = array(
         '#markup' => '<div>' . t('This gateway supports the following payment methods:')
                    . '<br />' . implode(',', $supported_methods) . '</div>',
         '#weight' => -10,
       );
-      $form['gateways'][$gateway['id']]['uc_pg_' . $gateway['id'] . '_enabled'] = array(
+      $form['gateways'][$id]['uc_pg_' . $id . '_enabled'] = array(
         '#type' => 'checkbox',
         '#title' => t('Enable this payment gateway for use.'),
-        '#default_value' => variable_get('uc_pg_' . $gateway['id'] . '_enabled', TRUE),
+        '#default_value' => variable_get('uc_pg_' . $id . '_enabled', TRUE),
       );
 
       // Find any additional settings defined in the payment gateway callback.
       if (isset($gateway['settings']) && function_exists($gateway['settings'])) {
         $gateway_settings = $gateway['settings'](array(), $form_state);
         if (is_array($gateway_settings)) {
-          $form['gateways'][$gateway['id']] = array_merge($form['gateways'][$gateway['id']], $gateway_settings);
+          $form['gateways'][$id] = array_merge($form['gateways'][$id], $gateway_settings);
         }
       }
     }
@@ -209,8 +203,8 @@ function _uc_payment_gateways_summarize($form, $gateways) {
  */
 function uc_payment_gateway_select() {
   $gateways = _uc_payment_gateway_list($_SESSION['uc_payment_method'], TRUE);
-  foreach ($gateways as $gateway) {
-    $options[$gateway['id']] = $gateway['title'];
+  foreach ($gateways as $id => $gateway) {
+    $options[$id] = $gateway['title'];
   }
 
   $build['instructions'] = array('#markup' => t('Please choose a payment gateway to use for that payment.'));
diff --git a/payment/uc_payment/uc_payment.api.php b/payment/uc_payment/uc_payment.api.php
index f0772ec..88777b4 100644
--- a/payment/uc_payment/uc_payment.api.php
+++ b/payment/uc_payment/uc_payment.api.php
@@ -45,12 +45,8 @@ function hook_uc_payment_entered($order, $method, $amount, $account, $data, $com
  * @see http://www.ubercart.org/docs/api/hook_payment_gateway
  *
  * @return
- *   Returns an array of payment gateways, which are arrays with the
- *   following keys:
- *   - "id"
- *     - type: string
- *     - value: The internal ID of the payment gateway, using a-z, 0-9, and - or
- *         _.
+ *   Returns an array of payment gateways, keyed by the gateway ID, and with
+ *   the following members:
  *   - "title"
  *     - type: string
  *     - value: The name of the payment gateway displayed to the user. Use t().
@@ -61,10 +57,11 @@ function hook_uc_payment_entered($order, $method, $amount, $account, $data, $com
  *     - type: string
  *     - value: The name of a function that returns an array of settings form
  *         elements for the gateway.
+ *   - Other keys are payment method IDs, with the value as the name of the
+ *     gateway charge function.
  */
 function hook_uc_payment_gateway() {
-  $gateways[] = array(
-    'id' => 'test_gateway',
+  $gateways['test_gateway'] = array(
     'title' => t('Test Gateway'),
     'description' => t('Process credit card payments through the Test Gateway.'),
     'credit' => 'test_gateway_charge',
@@ -79,11 +76,8 @@ function hook_uc_payment_gateway() {
  *   Payment gateways passed by reference.
  */
 function hook_uc_payment_gateway_alter(&$gateways) {
-  // Change the title of all gateways.
-  foreach ($gateways as &$gateway) {
-    // $gateway was passed by reference.
-    $gateway['title'] = t('Altered gateway @original', array('@original' => $gateway['title']));
-  }
+  // Change the title of the test gateway.
+  $gateways['test_gateway']['title'] = t('Altered test gateway title.');
 }
 
 /**
diff --git a/payment/uc_payment/uc_payment.module b/payment/uc_payment/uc_payment.module
index fba364f..6cc69ce 100644
--- a/payment/uc_payment/uc_payment.module
+++ b/payment/uc_payment/uc_payment.module
@@ -413,67 +413,44 @@ function uc_payment_process_payment($method, $order_id, $amount, $data = NULL, $
     return FALSE;
   }
 
+  // Find the default gateway if requested.
+  if ($default) {
+    $default = variable_get('uc_payment_' . $method . '_gateway', '');
+  }
+
   // If we only found one gateway for this payment method...
   if (count($gateways) == 1) {
-    // Get the right key for the payment gateway in the array.
-    $key = array_shift(array_keys($gateways));
-
-    // If we can find a callback in the gateway for the payment method...
-    if (function_exists($gateways[$key][$method])) {
-      // Pass the payment data onto the callback and store the result.
-      $result = $gateways[$key][$method]($order_id, $amount, $data);
-    }
-    else {
-      // Otherwise display a failure message to administrators.
-      if (user_access('administer store')) {
-        drupal_set_message(t("Attempted to process a %type payment but the gateway's function was not found."));
-      }
-
-      $result['success'] = FALSE;
-    }
+    $gateway = reset($gateways);
+  }
+  elseif ($default && isset($gateways[$default])) {
+    // The default gateway was forced.
+    $gateway = $gateways[$default];
+  }
+  elseif ($selected && isset($gateways[$selected])) {
+    // A specific gateway was selected.
+    $gateway = $gateways[$selected];
   }
   else {
-    // Otherwise attempt to find the appropriate gateway function in the array.
-    $callback = FALSE;
-
-    foreach ($gateways as $gateway) {
-      // If we want the default gateway and this is it, store the callback
-      // and continue.
-      if ($default && $gateway['id'] == variable_get('uc_payment_' . $method . '_gateway', '')) {
-        $callback = $gateway[$method];
-        continue;
-      }
-
-      // If we want to use a specific gateway and this is it, store the callback.
-      if (!empty($selected) && $gateway['id'] == $selected) {
-        $callback = $gateway[$method];
-      }
-    }
-
-    // If we found a callback...
-    if ($callback !== FALSE) {
-      // Check to see if the function exists and process the payment.
-      if (function_exists($callback)) {
-        $result = $callback($order_id, $amount, $data);
-      }
-      else {
-        // Otherwise display an error message to administrators.
-        if (user_access('administer store')) {
-          drupal_set_message(t('An error has occurred with your payment gateway.  The charge function could not be found.'));
-        }
+    // Otherwise store the info that was passed to us in the session and
+    // redirect to a form where we can choose a payment gateway.
+    $_SESSION['uc_payment_method'] = $method;
+    $_SESSION['uc_payment_order_id'] = $order_id;
+    $_SESSION['uc_payment_amount'] = $amount;
+    $_SESSION['uc_payment_data'] = serialize($data);
+
+    drupal_goto('admin/store/orders/' . $order_id . '/payments/select/' . $method);
+  }
 
-        $result['success'] = FALSE;
-      }
-    }
-    else {
-      // Otherwise store the info that was passed to us in the session and
-      // redirect to a form where we can choose a payment gateway.
-      $_SESSION['uc_payment_method'] = $method;
-      $_SESSION['uc_payment_order_id'] = $order_id;
-      $_SESSION['uc_payment_amount'] = $amount;
-      $_SESSION['uc_payment_data'] = serialize($data);
-
-      drupal_goto('admin/store/orders/' . $order_id . '/payments/select/' . $method);
+  // Check to see if the function exists and process the payment.
+  if (function_exists($gateway[$method])) {
+    $result = $gateway[$method]($order_id, $amount, $data);
+  }
+  else {
+    // Otherwise display an error message to administrators.
+    $result['success'] = FALSE;
+    $result['message'] = t('An error has occurred with your payment gateway.  The charge function could not be found.');
+    if (user_access('administer store')) {
+      drupal_set_message($result['message']);
     }
   }
 
@@ -626,7 +603,7 @@ function _uc_payment_method_list($action = NULL) {
   }
 
   foreach (module_invoke_all('uc_payment_method') as $id => $method) {
-    // Preserve backward compatibility for panes with no key specified.
+    // Preserve backward compatibility for methods with no key specified.
     if (is_numeric($id)) {
       $id = $method['id'];
     }
@@ -655,23 +632,30 @@ function _uc_payment_method_data($method_id, $key) {
  * Build a list of payment gateways defined in the enabled modules.
  */
 function _uc_payment_gateway_list($filter = NULL, $enabled_only = FALSE) {
-  $gateways = module_invoke_all('uc_payment_gateway');
+  $gateways = array();
+
+  foreach (module_invoke_all('uc_payment_gateway') as $id => $gateway) {
+    // Preserve backward compatibility for gateways with no key specified.
+    if (is_numeric($id)) {
+      $id = $method['id'];
+    }
+
+    $gateways[$id] = array_merge($gateway, array(
+      'id' => $id,
+      'enabled' => variable_get('uc_pg_' . $id . '_enabled', TRUE),
+    ));
+  }
 
   // Allow other modules to alter the payment gateways.
   drupal_alter('uc_payment_gateway', $gateways);
 
-  foreach ($gateways as $i => $value) {
-    $gateways[$i]['enabled'] = variable_get('uc_pg_' . $gateways[$i]['id'] . '_enabled', TRUE);
-    if ($filter != NULL) {
-      if (!isset($gateways[$i][$filter]) || !function_exists($gateways[$i][$filter])) {
-        unset($gateways[$i]);
-        continue;
-      }
+  foreach ($gateways as $id => $gateway) {
+    if ($filter && (!isset($gateway[$filter]) || !function_exists($gateway[$filter]))) {
+      unset($gateways[$id]);
+      continue;
     }
-    if ($enabled_only) {
-      if (!variable_get('uc_pg_' . $gateways[$i]['id'] . '_enabled', TRUE)) {
-        unset($gateways[$i]);
-      }
+    if ($enabled_only && !$gateway['enabled']) {
+      unset($gateways[$id]);
     }
   }
 
@@ -690,14 +674,6 @@ function _uc_payment_gateway_list($filter = NULL, $enabled_only = FALSE) {
  *   The requested data.
  */
 function _uc_payment_gateway_data($gateway_id, $key) {
-  // Load all the payment gateways.
   $gateways = _uc_payment_gateway_list();
-
-  // Loop through the array to find the matching gateway.
-  foreach ($gateways as $gateway) {
-    // If this is it, return the requested data.
-    if ($gateway['id'] == $gateway_id) {
-      return isset($gateway[$key]) ? $gateway[$key] : NULL;
-    }
-  }
+  return isset($gateways[$gateway_id][$key]) ? $gateways[$gateway_id][$key] : NULL;
 }
diff --git a/payment/uc_paypal/uc_paypal.module b/payment/uc_paypal/uc_paypal.module
index be3f404..c65e756 100644
--- a/payment/uc_paypal/uc_paypal.module
+++ b/payment/uc_paypal/uc_paypal.module
@@ -133,8 +133,7 @@ function uc_paypal_uc_payment_gateway() {
     return;
   }
 
-  $gateways[] = array(
-    'id' => 'paypal_wpp',
+  $gateways['paypal_wpp'] = array(
     'title' => t('PayPal Website Payments Pro'),
     'description' => t('Process credit card payments using Website Payments Pro.'),
     'settings' => 'uc_paypal_wpp_settings_form',
-- 
1.7.1

