You are here

function uc_recurring_form_alter in Ubercart 5

Implementation of hook_form_alter().

File

payment/uc_recurring/uc_recurring.module, line 108
Allows you to add a recurring fee to a product/SKU to handle subscription type services.

Code

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;
    }

    // If configured, display a message about the recurring fees.
    if ($message = variable_get('uc_recurring_checkout_message', '')) {
      drupal_set_message(check_markup($message));
    }

    // 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']));
    }
  }

  // Wipe any existing recurring fees on the review form load to prevent
  // duplicate or unexpected fees.
  if ($form_id == 'uc_cart_checkout_review_form') {
    db_query("DELETE FROM {uc_recurring_users} WHERE order_id = %d", $_SESSION['cart_order']);
  }
  if ($form_id == 'uc_order_view_update_form') {

    // Load the order object based on the form value for the order ID.
    $order = uc_order_load($form['order_id']['#value']);

    // Load up the valid payment methods.
    $methods = variable_get('uc_recurring_payment_methods', array());

    // Check to make sure the payment method is good and we're in CC debug mode.
    if ($methods[$order->payment_method] === $order->payment_method && variable_get('uc_credit_debug', FALSE)) {

      // Look for recurring fees on this order.
      $fees = uc_recurring_find_fees($order);

      // If we have fees, check to see if they've already been added to the order.
      if (count($fees)) {
        $result = db_result(db_query("SELECT COUNT(*) FROM {uc_recurring_users} WHERE order_id = %d AND fee_handler = '%s'", $order->order_id, variable_get('uc_recurring_handler', 'uc_recurring')));

        // If they haven't been added, display the checkbox to make it so.
        if ($result == 0) {
          $form['process_fees'] = array(
            '#type' => 'checkbox',
            '#title' => t('Process the recurring fees associated with products on this order.', array(
              '@count' => count($fees),
            )),
            '#description' => t('This action will not be available after any fees are successfully processed.<br /><b>Important:</b> You must verify that the credit card information is correct before processing the fees!'),
            '#weight' => 5,
          );
          $form['#submit']['uc_recurring_order_view_update_form_submit'] = array();
        }
      }
    }
  }
}