Restrict Payment Methods

Posts: 40
Joined: 10/10/2007

Is it possible to restict payment methods to user groups?

The site i'm working on for a friend will eventually allow business accounts to purchase through the online store. This will require the use of a payment method other than "Credit Card". This other payment method would then need to be accessible to only the users in that particular group.

Any ideas?

Posts: 5367
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']));
    }
  }
}
?>

Posts: 4
Joined: 02/08/2008

howdy,

I'm having trouble setting up conditional payment methods. I'd like to restrict bank transfer and check/money order payment methods to users with a particular second level domain (ie. school.nz).

I'm having trouble grokking Ryan's code:

<?php
function emailcheck_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'])) {
 
   
// I'll have to get some regex together to validate against a users email address variable - but that is another issue...

   
if users email contains school.nz {

    print
all payment methods; //how do I print out this bit?

else {

print
only credit card & paypal; //and this bit?


}
}
}
?>

any clues would be much appreciated!