You are here

function commerce_payment_order_transaction_add_form in Commerce Core 7

Allows an administrator to choose a payment method type and add a transaction for a specific order.

Parameters

$order: The order to add the transaction to.

1 string reference to 'commerce_payment_order_transaction_add_form'
commerce_payment_handler_area_totals::render in modules/payment/includes/views/handlers/commerce_payment_handler_area_totals.inc
Render the area.

File

modules/payment/includes/commerce_payment.forms.inc, line 15
Defines forms for creating and administering payment transactions.

Code

function commerce_payment_order_transaction_add_form($form, &$form_state, $order) {

  // Ensure this include file is loaded when the form is rebuilt from the cache.
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'commerce_payment') . '/includes/commerce_payment.forms.inc';

  // Store the initial order in the form state.
  $form_state['order'] = $order;
  $form['#access'] = commerce_payment_transaction_order_access('create', $order);

  // If a payment method has already been selected...
  if (!empty($form_state['payment_method'])) {
    $payment_method = $form_state['payment_method'];
    $form['payment_terminal'] = array(
      '#type' => 'fieldset',
      '#title' => t('Payment terminal: @title', array(
        '@title' => $payment_method['title'],
      )),
      '#attributes' => array(
        'class' => array(
          'payment-terminal',
        ),
      ),
      '#element_validate' => array(
        'commerce_payment_order_transaction_add_form_payment_terminal_validate',
      ),
    );

    // Establish defaults for the amount if possible.
    if ($balance = commerce_payment_order_balance($order)) {
      $default_amount = $balance['amount'] > 0 ? $balance['amount'] : 0;
      $default_currency_code = $balance['currency_code'];

      // Convert the default amount to an acceptable textfield value.
      $default_amount = commerce_currency_amount_to_decimal($default_amount, $default_currency_code);
      $currency = commerce_currency_load($default_currency_code);
      $default_amount = number_format($default_amount, $currency['decimals'], '.', '');
    }
    else {
      $default_amount = '';
      $default_currency_code = commerce_default_currency();
    }
    $form['payment_terminal']['amount'] = array(
      '#type' => 'textfield',
      '#title' => t('Amount'),
      '#default_value' => $default_amount,
      '#required' => TRUE,
      '#size' => 10,
      '#prefix' => '<div class="payment-terminal-amount">',
    );

    // Build a currency options list from all enabled currencies.
    $options = array();
    foreach (commerce_currencies(TRUE) as $currency_code => $currency) {
      $options[$currency_code] = check_plain($currency['code']);
    }
    $form['payment_terminal']['currency_code'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $default_currency_code,
      '#suffix' => '</div>',
    );

    // Find the values already submitted for the payment terminal.
    $terminal_values = !empty($form_state['values']['payment_details']) ? $form_state['values']['payment_details'] : array();
    if ($callback = commerce_payment_method_callback($payment_method, 'submit_form')) {
      $form['payment_terminal']['payment_details'] = $callback($payment_method, $terminal_values, NULL, $order);
    }
    else {
      $form['payment_terminal']['payment_details'] = array();
    }
    $form['payment_terminal']['payment_details']['#tree'] = TRUE;
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
  }
  else {

    // Otherwise present the payment method selection form.
    $event = rules_get_cache('event_commerce_payment_methods');

    // Build an options array of all available payment methods that can setup
    // transactions using the local terminal. If there is more than one instance
    // of any payment method available on site, list them in optgroups using the
    // payment method title.
    $instances = array();
    $options = array();
    $optgroups = FALSE;

    // Only build the options array if payment method Rules are enabled.
    if (!empty($event)) {
      foreach (commerce_payment_methods() as $method_id => $payment_method) {

        // Only check payment methods that should appear on the terminal.
        if ($payment_method['terminal']) {

          // Look for a Rule enabling this payment method.
          foreach ($event
            ->getIterator() as $rule) {
            foreach ($rule
              ->actions() as $action) {

              // If an action is found, add its instance to the options array.
              if ($action
                ->getElementName() == 'commerce_payment_enable_' . $method_id) {
                $instances[check_plain($payment_method['title'])][] = array(
                  'instance_id' => commerce_payment_method_instance_id($method_id, $rule),
                  'label' => check_plain($rule
                    ->label()),
                );

                // If this is the second instance for this payment method, turn
                // on optgroups.
                if (count($instances[check_plain($payment_method['title'])]) > 1) {
                  $optgroups = TRUE;
                }
              }
            }
          }
        }
      }

      // Build an options array based on whether or not optgroups are necessary.
      foreach ($instances as $optgroup => $values) {
        foreach ($values as $value) {
          if ($optgroups) {
            $options[$optgroup][$value['instance_id']] = $value['label'];
          }
          else {
            $options[$value['instance_id']] = $value['label'];
          }
        }
      }
    }
    if (!empty($options)) {
      $form['payment_method'] = array(
        '#type' => 'select',
        '#options' => $options,
        '#prefix' => '<div class="add-payment">',
      );
      $form['add_payment'] = array(
        '#type' => 'submit',
        '#value' => t('Add payment'),
        '#suffix' => '</div>',
        '#ajax' => array(
          'callback' => 'commerce_payment_order_transaction_add_form_add_refresh',
          'wrapper' => 'commerce-payment-order-transaction-add-form',
        ),
      );
    }
    else {
      $form['payment_method'] = array(
        '#markup' => t('No payment methods available to add payments.'),
      );
    }
  }
  return $form;
}