You are here

function commerce_payment_pane_checkout_form_submit in Commerce Core 7

Payment pane: submit callback.

File

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

Code

function commerce_payment_pane_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {

  // Check to make sure there are no validation issues with other form elements
  // before executing payment method callbacks.
  if (form_get_errors()) {
    drupal_set_message(t('Your payment will not be processed until all errors on the page have been addressed.'), 'warning');
    return FALSE;
  }
  $pane_id = $checkout_pane['pane_id'];

  // Only submit if we actually had payment methods on the form.
  if (!empty($form[$pane_id]) && !empty($form_state['values'][$pane_id])) {
    $pane_form = $form[$pane_id];
    $pane_values = $form_state['values'][$pane_id];

    // Only process if there were payment methods available.
    if ($pane_values['payment_methods']) {
      $order->data['payment_method'] = $pane_values['payment_method'];

      // If we can calculate a single order total for the order...
      if ($balance = commerce_payment_order_balance($order)) {

        // Delegate submit to the payment method callback.
        $payment_method = commerce_payment_method_instance_load($pane_values['payment_method']);
        if ($callback = commerce_payment_method_callback($payment_method, 'submit_form_submit')) {

          // Initialize the payment details array to accommodate payment methods
          // that don't add any additional details to the checkout pane form.
          if (empty($pane_values['payment_details'])) {
            $pane_values['payment_details'] = array();
          }

          // If payment fails, rebuild the checkout form without progressing.
          if ($callback($payment_method, $pane_form['payment_details'], $pane_values['payment_details'], $order, $balance) === FALSE) {
            $form_state['rebuild'] = TRUE;
          }
        }
      }
    }
  }
  else {

    // If there were no payment methods on the form, check to see if the pane is
    // configured to trigger "When an order is first paid in full" on submission
    // for free orders.
    $behavior = variable_get('commerce_payment_pane_no_method_behavior', COMMERCE_PAYMENT_PANE_NO_METHOD_MESSAGE);
    if (in_array($behavior, array(
      COMMERCE_PAYMENT_PANE_NO_METHOD_EMPTY_EVENT,
      COMMERCE_PAYMENT_PANE_NO_METHOD_MESSAGE_EVENT,
    ))) {

      // Check the balance of the order.
      $balance = commerce_payment_order_balance($order);
      if (!empty($balance) && $balance['amount'] <= 0) {

        // Trigger the event now for free orders, simulating payment being
        // submitted on pane submission that brings the balance to 0. Use an
        // empty transaction, as we wouldn't typically save a transaction where
        // a financial transaction has not actually occurred.
        rules_invoke_all('commerce_payment_order_paid_in_full', $order, commerce_payment_transaction_new('', $order->order_id));

        // Update the order's data array to indicate this just happened.
        $order->data['commerce_payment_order_paid_in_full_invoked'] = TRUE;
      }
    }
  }
}