From 7fd37fb272f9b1e14ce97a07b996f1b5e694645b Mon Sep 17 00:00:00 2001
From: Chris Oden <wodenx@gmail.com>
Date: Mon, 9 Apr 2012 14:53:20 -0400
Subject: [PATCH 1/2] Fix 'invalid option' on payment method after ajax submission.

---
 payment/uc_payment/uc_payment_checkout_pane.inc |   19 ++++++++++---------
 1 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/payment/uc_payment/uc_payment_checkout_pane.inc b/payment/uc_payment/uc_payment_checkout_pane.inc
index 755203c..2bb1d86 100644
--- a/payment/uc_payment/uc_payment_checkout_pane.inc
+++ b/payment/uc_payment/uc_payment_checkout_pane.inc
@@ -43,16 +43,17 @@ function uc_checkout_pane_payment($op, &$order, $form = NULL, &$form_state = NUL
         drupal_goto('cart');
       }
 
-      if (count($options)) {
-        if (isset($form_state['values'])  &&
-          isset($form_state['values']['panes']['payment']['payment_method']) &&
-          in_array($form_state['values']['panes']['payment']['payment_method'], array_keys($options))) {
-          $default = $form_state['values']['panes']['payment']['payment_method'];
-        }
-        else {
-          $default = (count($options) == 1 || empty($order->payment_method)) ? key($options) : $order->payment_method;
-        }
+      if (isset($options[$order->payment_method])) {
+        $default = $order->payment_method;
+      }
+      else {
+        $default = key($options);
       }
+      // Ensure that the form builder uses #default_value to determine which
+      // button should be selected after an ajax submission. This is
+      // necessary because the previously selected value may have become
+      //unavailable, which would result in an invalid selection.
+      unset($form_state['input']['panes']['payment']['payment_method']);
 
       if (count($options) > 1) {
         $description = t('Select a payment method from the following options.');
-- 
1.7.3.4


From 981a7a55f489f794a1b7fdc6b355845202f8d87d Mon Sep 17 00:00:00 2001
From: Chris Oden <wodenx@gmail.com>
Date: Mon, 9 Apr 2012 15:03:36 -0400
Subject: [PATCH 2/2] Fix ajax error when no methods available after ajax submission.

---
 payment/uc_payment/uc_payment_checkout_pane.inc |  114 +++++++++++++----------
 1 files changed, 66 insertions(+), 48 deletions(-)

diff --git a/payment/uc_payment/uc_payment_checkout_pane.inc b/payment/uc_payment/uc_payment_checkout_pane.inc
index 2bb1d86..6a1b52d 100644
--- a/payment/uc_payment/uc_payment_checkout_pane.inc
+++ b/payment/uc_payment/uc_payment_checkout_pane.inc
@@ -39,66 +39,84 @@ function uc_checkout_pane_payment($op, &$order, $form = NULL, &$form_state = NUL
       }
 
       if (!$options) {
-        drupal_set_message(t('Checkout cannot be completed without any payment methods enabled. Please contact an administrator to resolve the issue.'), 'error');
-        drupal_goto('cart');
-      }
-
-      if (isset($options[$order->payment_method])) {
-        $default = $order->payment_method;
-      }
-      else {
-        $default = key($options);
-      }
-      // Ensure that the form builder uses #default_value to determine which
-      // button should be selected after an ajax submission. This is
-      // necessary because the previously selected value may have become
-      //unavailable, which would result in an invalid selection.
-      unset($form_state['input']['panes']['payment']['payment_method']);
-
-      if (count($options) > 1) {
-        $description = t('Select a payment method from the following options.');
+        // No payment methods available.
+        if (!empty($_POST)) {
+          // When the form is being rebuilt after ajax, place the method in the
+          // payment pane.
+          $contents['payment_method'] = array(
+            '#type' => 'hidden',
+            '#value' => NULL,
+          );
+          $description = t('Checkout cannot be completed without any payment methods enabled. Please contact an administrator to resolve the issue.');
+        }
+        else {
+          // Otherwise redirect to the cart page.
+          drupal_set_message(t('Checkout cannot be completed without any payment methods enabled. Please contact an administrator to resolve the issue.'), 'error');
+          drupal_goto('cart');
+        }
       }
       else {
-        $description = '';
-      }
+        if (isset($options[$order->payment_method])) {
+          $default = $order->payment_method;
+        }
+        else {
+          $default = key($options);
+        }
+        // Ensure that the form builder uses #default_value to determine which
+        // button should be selected after an ajax submission. This is
+        // necessary because the previously selected value may have become
+        //unavailable, which would result in an invalid selection.
+        unset($form_state['input']['panes']['payment']['payment_method']);
+
+        if (count($options) > 1) {
+          $description = t('Select a payment method from the following options.');
+        }
+        else {
+          $description = '';
+        }
 
-      $contents['payment_method'] = array(
-        '#type' => 'radios',
-        '#title' => t('Payment method'),
-        '#title_display' => 'invisible',
-        '#options' => $options,
-        '#default_value' => $default,
-        '#disabled' => count($options) == 1 ? TRUE : FALSE,
-        '#required' => TRUE,
-        '#ajax' => array(
-          'callback' => 'uc_payment_checkout_payment_details',
-          'wrapper' => 'payment-details',
-          'progress' => array(
-            'type' => 'throbber',
+        $contents['payment_method'] = array(
+          '#type' => 'radios',
+          '#title' => t('Payment method'),
+          '#title_display' => 'invisible',
+          '#options' => $options,
+          '#default_value' => $default,
+          '#disabled' => count($options) == 1 ? TRUE : FALSE,
+          '#required' => TRUE,
+          '#ajax' => array(
+            'callback' => 'uc_payment_checkout_payment_details',
+            'wrapper' => 'payment-details',
+            'progress' => array(
+              'type' => 'throbber',
+            ),
           ),
-        ),
-      );
+        );
 
-      $contents['details'] = array(
-        '#prefix' => '<div id="payment-details" class="clearfix payment-details-' . $default . '">',
-        '#suffix' => '</div>',
-      );
+        $contents['details'] = array(
+          '#prefix' => '<div id="payment-details" class="clearfix payment-details-' . $default . '">',
+          '#suffix' => '</div>',
+        );
 
-      $details = FALSE;
-      $func = _uc_payment_method_data($default, 'callback');
-      if (function_exists($func)) {
-        $details = $func('cart-details', $order, $form, $form_state);
-      }
-      if (is_array($details) && !empty($details)) {
-        $contents['details'] += $details;
-      }
-      else {
-        $contents['details']['#markup'] = t('Continue with checkout to complete payment.');
+        $details = FALSE;
+        $func = _uc_payment_method_data($default, 'callback');
+        if (function_exists($func)) {
+          $details = $func('cart-details', $order, $form, $form_state);
+        }
+        if (is_array($details) && !empty($details)) {
+          $contents['details'] += $details;
+        }
+        else {
+          $contents['details']['#markup'] = t('Continue with checkout to complete payment.');
+        }
       }
 
       return array('description' => $description, 'contents' => $contents);
 
     case 'process':
+      if (empty($form_state['values']['panes']['payment']['payment_method'])) {
+        form_set_error('panes][payment][payment_method', t('You cannot check out without selecting a payment method.'));
+        return FALSE;
+      }
       $order->payment_method = $form_state['values']['panes']['payment']['payment_method'];
       $func = _uc_payment_method_data($order->payment_method, 'callback');
       if (function_exists($func)) {
-- 
1.7.3.4

