You are here

function commerce_payment_pane_checkout_form in Commerce Core 7

Payment pane: form callback.

File

modules/payment/includes/commerce_payment.checkout_pane.inc, line 51
Callback functions for the Payment module's checkout panes.

Code

function commerce_payment_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
  $pane_form = array();

  // Invoke the payment methods event that will populate the order with
  // an array of method IDs for available payment methods.
  $order->payment_methods = array();
  rules_invoke_all('commerce_payment_methods', $order);

  // Sort the payment methods array by the enabling Rules' weight values.
  uasort($order->payment_methods, 'drupal_sort_weight');

  // Generate an array of payment method options for the checkout form.
  $options = array();
  foreach ($order->payment_methods as $instance_id => $method_info) {

    // Ensure we've received a valid payment method that can be used on the
    // checkout form.
    if ($payment_method = commerce_payment_method_load($method_info['method_id'])) {
      if (!empty($payment_method['checkout'])) {
        $options[$instance_id] = $payment_method['display_title'];
      }
    }
  }

  // If no payment methods were found, return the empty form.
  if (empty($options)) {
    if (!variable_get('commerce_payment_pane_require_method', FALSE)) {
      $behavior = variable_get('commerce_payment_pane_no_method_behavior', COMMERCE_PAYMENT_PANE_NO_METHOD_MESSAGE);
      switch ($behavior) {
        case COMMERCE_PAYMENT_PANE_NO_METHOD_MESSAGE:
        case COMMERCE_PAYMENT_PANE_NO_METHOD_MESSAGE_EVENT:
          $pane_form['message'] = array(
            '#markup' => '<div>' . t('Payment is not required to complete your order.') . '</div>',
          );
          break;
        case COMMERCE_PAYMENT_PANE_NO_METHOD_EMPTY:
        case COMMERCE_PAYMENT_PANE_NO_METHOD_EMPTY_EVENT:
        default:
          break;
      }
      return $pane_form;
    }
    else {
      $pane_form['message'] = array(
        '#markup' => '<div>' . t('Unfortunately we could not find any suitable payment methods, and we require a payment method to complete checkout.') . '<br /><strong>' . t('Please contact us to resolve any issues with your order.') . '</strong></div>',
      );
    }
  }

  // Store the payment methods in the form for validation purposes.
  $pane_form['payment_methods'] = array(
    '#type' => 'value',
    '#value' => $order->payment_methods,
  );

  // If at least one payment option is available...
  if (!empty($options)) {

    // Add a radio select widget to specify the payment method.
    $pane_form['payment_method'] = array(
      '#type' => 'radios',
      '#options' => $options,
      '#ajax' => array(
        'callback' => 'commerce_payment_pane_checkout_form_details_refresh',
        'wrapper' => 'payment-details',
      ),
    );

    // Find the default payment method using either the preselected value stored
    // in the order / checkout pane or the first available method.
    $pane_values = !empty($form_state['values'][$checkout_pane['pane_id']]) ? $form_state['values'][$checkout_pane['pane_id']] : array();
    if (isset($pane_values['payment_method']) && isset($options[$pane_values['payment_method']])) {
      $default_value = $pane_values['payment_method'];
    }
    elseif (isset($form_state['input']['commerce_payment']['payment_method'])) {
      $default_value = $form_state['complete form']['commerce_payment']['payment_method']['#default_value'];
    }
    elseif (isset($order->data['payment_method']) && isset($options[$order->data['payment_method']])) {
      $default_value = $order->data['payment_method'];
    }
    else {
      reset($options);
      $default_value = key($options);
    }

    // Set the default value for the payment method radios.
    $pane_form['payment_method']['#default_value'] = $default_value;

    // Add the payment method specific form elements.
    $payment_method = commerce_payment_method_instance_load($pane_form['payment_method']['#default_value']);
    if ($callback = commerce_payment_method_callback($payment_method, 'submit_form')) {
      $pane_form['payment_details'] = $callback($payment_method, $pane_values, $checkout_pane, $order);
    }
    else {
      $pane_form['payment_details'] = array();
    }
    $pane_form['payment_details']['#prefix'] = '<div id="payment-details">';
    $pane_form['payment_details']['#suffix'] = '</div>';
  }
  return $pane_form;
}