You are here

function commerce_payment_commerce_payment_transaction_insert in Commerce Core 7

Implements hook_commerce_payment_transaction_insert().

When a new payment transaction is inserted that is already completed, check the order balance and invoke a Rules event if the order is paid in full.

1 call to commerce_payment_commerce_payment_transaction_insert()
commerce_payment_commerce_payment_transaction_update in modules/payment/commerce_payment.module
Implements hook_commerce_payment_transaction_update().

File

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

Code

function commerce_payment_commerce_payment_transaction_insert($transaction) {

  // If the inserted transaction was marked as a success and placed against a
  // valid order...
  $transaction_statuses = commerce_payment_transaction_statuses();
  if (!empty($transaction_statuses[$transaction->status]['total']) && ($order = commerce_order_load($transaction->order_id))) {

    // Then check to make sure the event hasn't been invoked for this order.
    if (empty($order->data['commerce_payment_order_paid_in_full_invoked'])) {

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

        // If automatic order revisions are enabled site-wide, create a new
        // revision now with a log message identifying the payment event.
        if (variable_get('commerce_order_auto_revision', TRUE)) {
          $order->revision = TRUE;
          $order->log = t('Order updated to reflect the order has just been paid in full.');
        }

        // Invoke the event including a hook of the same name.
        rules_invoke_all('commerce_payment_order_paid_in_full', $order, $transaction);

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

        // Save the updated order.
        commerce_order_save($order);
      }
    }
  }
}