You are here

function commerce_payment_order_balance in Commerce Core 7

Calculates the balance of an order by subtracting the total of all successful transactions from the total of all the line items on the order.

Parameters

$order: The fully loaded order object whose balance should be calculated.

$totals: Optionally submit an array of transaction totals keyed by currency code with the amount as the value.

Return value

An array containing the amount and currency code representing the balance of the order or FALSE if it is impossible to calculate.

8 calls to commerce_payment_order_balance()
CommercePaymentUITest::testCommercePaymentAdministration in modules/payment/tests/commerce_payment_ui.test
Test the adding payments using administration pages.
commerce_payment_commerce_payment_totals_row_info in modules/payment/commerce_payment.module
Implements hook_commerce_payment_totals_row_info().
commerce_payment_commerce_payment_transaction_insert in modules/payment/commerce_payment.module
Implements hook_commerce_payment_transaction_insert().
commerce_payment_handler_field_balance::render in modules/payment/includes/views/handlers/commerce_payment_handler_field_balance.inc
Render the field.
commerce_payment_order_transaction_add_form in modules/payment/includes/commerce_payment.forms.inc
Allows an administrator to choose a payment method type and add a transaction for a specific order.

... See full list

File

modules/payment/commerce_payment.module, line 1103
Defines the payment system and checkout integration.

Code

function commerce_payment_order_balance($order, $totals = array()) {
  $wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_total = $wrapper->commerce_order_total
    ->value();

  // Calculate the transaction totals if not supplied.
  if (empty($totals)) {
    $transaction_statuses = commerce_payment_transaction_statuses();
    foreach (commerce_payment_transaction_load_multiple(array(), array(
      'order_id' => $order->order_id,
    )) as $transaction) {

      // If the payment transaction status indicates it should include the
      // current transaction in the total...
      if ($transaction_statuses[$transaction->status]['total']) {

        // Add the transaction to its currency's running total if it exists...
        if (isset($totals[$transaction->currency_code])) {
          $totals[$transaction->currency_code] += $transaction->amount;
        }
        else {

          // Or begin a new running total for the currency.
          $totals[$transaction->currency_code] = $transaction->amount;
        }
      }
    }
  }

  // Only return a balance if the totals array contains a single matching currency.
  if (count($totals) == 1 && isset($totals[$order_total['currency_code']])) {
    return array(
      'amount' => $order_total['amount'] - $totals[$order_total['currency_code']],
      'currency_code' => $order_total['currency_code'],
    );
  }
  elseif (empty($totals)) {
    return array(
      'amount' => $order_total['amount'],
      'currency_code' => $order_total['currency_code'],
    );
  }
  else {
    return FALSE;
  }
}