You are here

public function CommercePaymentTransactionEntityController::save in Commerce Core 7

Saves a payment transaction.

When saving a transaction without an ID, this function will create a new transaction at that time. Subsequent transactions that should be saved as new revisions should set $transaction->revision to TRUE and include a log string in $transaction->log.

Parameters

$transaction: The full transaction object to save.

$transaction: An optional transaction object.

Return value

SAVED_NEW or SAVED_UPDATED depending on the operation performed.

Overrides DrupalCommerceEntityController::save

File

modules/payment/includes/commerce_payment_transaction.controller.inc, line 65
The controller for the payment transaction entity containing the CRUD operations.

Class

CommercePaymentTransactionEntityController
The controller class for payment transactions contains methods for the transaction CRUD operations. The load method is inherited from the default controller.

Code

public function save($transaction, DatabaseTransaction $db_transaction = NULL) {
  if (!isset($db_transaction)) {
    $db_transaction = db_transaction();
    $started_transaction = TRUE;
  }
  try {
    global $user;

    // Determine if we will be inserting a new transaction.
    $transaction->is_new = empty($transaction->transaction_id);

    // Set the timestamp fields.
    if (empty($transaction->created)) {
      $transaction->created = REQUEST_TIME;
    }
    else {

      // Otherwise if the payment transaction is not new but comes from an
      // entity_create() or similar function call that initializes the created
      // timestamp to an empty string, unset it to prevent destroying existing
      // data in that property on update.
      if ($transaction->created === '') {
        unset($transaction->created);
      }
    }
    $transaction->changed = REQUEST_TIME;
    $transaction->revision_uid = $user->uid;
    $transaction->revision_timestamp = REQUEST_TIME;

    // Round the amount to ensure it's an integer for storage.
    $transaction->amount = round($transaction->amount);
    if ($transaction->is_new || !empty($transaction->revision)) {

      // When inserting either a new transaction or revision, $transaction->log
      // must be set because {commerce_payment_transaction_revision}.log is a
      // text column and therefore cannot have a default value. However, it
      // might not be set at this point, so we ensure that it is at least an
      // empty string in that case.
      if (!isset($transaction->log)) {
        $transaction->log = '';
      }
    }
    elseif (empty($transaction->log)) {

      // If we are updating an existing transaction without adding a new
      // revision, we need to make sure $transaction->log is unset whenever it
      // is empty.  As long as $transaction->log is unset, drupal_write_record()
      // will not attempt to update the existing database column when re-saving
      // the revision.
      unset($transaction->log);
    }
    return parent::save($transaction, $db_transaction);
  } catch (Exception $e) {
    if (!empty($started_transaction)) {
      $db_transaction
        ->rollback();
      watchdog_exception($this->entityType, $e);
    }
    throw $e;
  }
}