You are here

function commerce_braintree_settle_form_submit in Commerce Braintree 7.2

Same name and namespace in other branches
  1. 7.3 includes/commerce_braintree.admin.inc \commerce_braintree_settle_form_submit()

Submit handler: submit the transaction for settlement.

File

includes/commerce_braintree.admin.inc, line 69
Provides admin forms for commerce_braintree.

Code

function commerce_braintree_settle_form_submit($form, &$form_state) {
  $transaction = $form_state['transaction'];
  $amount = $form_state['values']['amount'];
  $payment_method = commerce_payment_method_instance_load($transaction->instance_id);
  commerce_braintree_initialize($payment_method);

  // Attempt to invoke the Braintree API and process a transaction settlement.
  try {
    $result = Braintree_Transaction::submitForSettlement($transaction->remote_id, $amount);
  } catch (Exception $ex) {
    $result = NULL;
  }

  // Inform the user if there were any issues submitting the settlement.
  if (empty($result) || empty($result->success)) {
    $message = !empty($result->message) ? $result->message : t('No error provided from Braintree');
    drupal_set_message(t('The payment transaction could not be submitted for settlement. @message', array(
      '@message' => $message,
    )), 'error');
  }

  // Inform the user of a successful submission for settlement and update the payment transaction.
  if (!empty($result) && !empty($result->success)) {
    drupal_set_message(t('Transaction submitted for settlement successfully.'));

    // Update the payment transaction values to show the settlement.
    $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
    $transaction->remote_status = $result->transaction->status;
    $transaction->message .= '<br />' . t('Submitted for settlement: @date', array(
      '@date' => format_date(REQUEST_TIME, 'short'),
    ));
    $transaction->amount = commerce_currency_decimal_to_amount($result->transaction->amount, $result->transaction->currencyIsoCode);
    commerce_payment_transaction_save($transaction);
  }
  $form_state['redirect'] = 'admin/commerce/orders/' . $form_state['order']->order_id . '/payment';
}