Re: Restrict Payment Methods

Posts: 5632
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

See if you can grok this code from the recurring fee module I'm working on... it uses hook_form_alter() to remove improper payment methods from the form. In your case, you'd need to change the logic that determines what to remove:

<?php
function uc_recurring_form_alter($form_id, &$form) {
 
// We may need to alter the checkout form to remove invalid payment methods.
 
if ($form_id == 'uc_cart_checkout_form' && isset($form['panes']['payment'])) {
   
$order->products = uc_cart_get_contents();

   
// Make no changes if no recurring fees are found.
   
if (uc_recurring_find_fees($order) == array()) {
      return;
    }

   
// Remove invalid payment methods from the payment pane.
   
$valid = variable_get('uc_recurring_payment_methods', array());
    foreach (
array_keys($form['panes']['payment']['payment_method']['#options']) as $key) {
      if (!isset(
$valid[$key]) || $valid[$key] === 0) {
        unset(
$form['panes']['payment']['payment_method']['#options'][$key]);
      }
    }

   
$count = count($form['panes']['payment']['payment_method']['#options']);
    if (
$count == 0) {
     
// Display an error message if no payment methods remain.
     
drupal_set_message(t('There are no payment methods configured for orders with recurring fees!'), 'error');
     
drupal_set_message(t('Please contact an administrator to solve the issue.'), 'error');
    }
    elseif (
$count == 1) {
     
// If only one payment method remains, make it the default.
     
$form['panes']['payment']['payment_method']['#default_value'] = array_pop(array_keys($form['panes']['payment']['payment_method']['#options']));
    }
  }
}
?>

Restrict Payment Methods By: daniel.s (2 replies) Thu, 01/17/2008 - 23:25