You are here

function _commerce_braintree_default_process_transaction in Commerce Braintree 7.2

Same name and namespace in other branches
  1. 7.3 commerce_braintree.module \_commerce_braintree_default_process_transaction()
  2. 7 commerce_braintree.module \_commerce_braintree_default_process_transaction()

Process the actual Braintree transaction.

Parameters

$result:

$order:

array $payment_method:

$redirect:

1 call to _commerce_braintree_default_process_transaction()
commerce_braintree_tr_process_transaction in ./commerce_braintree.module
Processes a Transparent Redirect transaction after the customer has returned.

File

./commerce_braintree.module, line 1072
Integrates Braintree Transparent Redirect with Drupal Commerce.

Code

function _commerce_braintree_default_process_transaction($result, $order, $payment_method, $redirect) {

  // Get the braintree transaction object.
  $transaction = $result->transaction;

  // Check if we already have this transaction stores in Commerce.
  $transaction_id = isset($transaction->id) ? commerce_braintree_get_payment_transaction($transaction->id) : NULL;
  if (!$transaction_id) {
    $commerce_transaction = commerce_payment_transaction_new($payment_method['method_id'], $order->order_id);
  }
  else {
    $commerce_transaction = commerce_payment_transaction_load($transaction_id);
  }

  // Show any errors to the customer if the transaction failed.
  if (empty($result->success)) {
    $error = t('There was an error: %error.', array(
      '%error' => $result->message,
    ));

    // Allow other modules to alter the error message displayed to customers.
    drupal_alter('commerce_braintree_transaction_error', $error, $commerce_transaction, $result);
    drupal_set_message($error, 'error');
  }

  // Prepare the data to be recorded in Commerce.
  $commerce_transaction->instance_id = $payment_method['instance_id'];
  $commerce_transaction->message = commerce_braintree_build_payment_transaction_message($result);
  if ($redirect) {
    if (!$result->success) {
      $commerce_transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
      commerce_payment_transaction_save($commerce_transaction);

      // There was an error, go back to the previous pane.
      commerce_payment_redirect_pane_previous_page($order);
    }
    else {
      $commerce_transaction->remote_id = $transaction->id;
      $commerce_transaction->remote_status = $transaction->status;

      // Commerce don't store amount in decimal, convert it.
      $commerce_transaction->amount = commerce_currency_decimal_to_amount($transaction->amount, $transaction->currencyIsoCode);
      $commerce_transaction->currency_code = $transaction->currencyIsoCode;
      $commerce_transaction->status = commerce_braintree_transaction_status($transaction->status);
      commerce_payment_transaction_save($commerce_transaction);

      // Transaction succeded. Go to next pane.
      commerce_payment_redirect_pane_next_page($order);
    }
  }
}