You are here

function commerce_braintree_js_form_submit in Commerce Braintree 7.2

Same name and namespace in other branches
  1. 7.3 commerce_braintree.module \commerce_braintree_js_form_submit()

Submit callback for the Braintree JS based payment methods.

See also

CALLBACK_commerce_payment_method_submit_form_submit()

2 string references to 'commerce_braintree_js_form_submit'
commerce_braintree_dropin_commerce_payment_method_info in modules/commerce_braintree_dropin/commerce_braintree_dropin.module
Implements hook_commerce_payment_method_info().
commerce_braintree_hostedfields_commerce_payment_method_info in modules/commerce_braintree_hostedfields/commerce_braintree_hostedfields.module
Implements hook_commerce_payment_method_info().

File

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

Code

function commerce_braintree_js_form_submit($payment_method, $pane_form, &$pane_values, $order, $charge) {
  $nonce = commerce_braintree_js_get_nonce();
  commerce_braintree_initialize($payment_method);
  list($amount, $customer_name, $first_name, $last_name, $country, $thoroughfare, $locality, $postal_code, $administrative_area, $customer_mail) = _commerce_braintree_get_transaction_informations($order);
  $merchant_account_id = commerce_braintree_get_merchant_account_id($payment_method, $charge['currency_code']);

  // Determine if we should settle the transaction immediately.
  if (isset($pane_values['submit_for_settlement'])) {

    // Allows the admin payment transaction terminal to override the default.
    $submit_for_settlement = $pane_values['submit_for_settlement'];
  }
  elseif (isset($payment_method['settings']['submit_for_settlement'])) {

    // Use the payment method settings.
    $submit_for_settlement = (bool) $payment_method['settings']['submit_for_settlement'];
  }
  else {

    // Default to TRUE if this setting hasn't been explicitly set by the store admin.
    $submit_for_settlement = TRUE;
  }
  $sale_data = array(
    'amount' => commerce_braintree_price_amount($charge['amount'], $charge['currency_code']),
    'orderId' => $order->order_id,
    'channel' => 'CommerceGuys_BT_Vzero',
    'merchantAccountId' => $merchant_account_id,
    'paymentMethodNonce' => $nonce,
    'customer' => array(
      'firstName' => $first_name,
      'lastName' => $last_name,
      'email' => $customer_mail,
    ),
    'billing' => array(
      'countryCodeAlpha2' => $country,
      'streetAddress' => $thoroughfare,
      'firstName' => $first_name,
      'lastName' => $last_name,
      'locality' => $locality,
      'postalCode' => $postal_code,
      'region' => $administrative_area,
    ),
    'options' => array(
      'storeInVault' => !empty($pane_values['cardonfile']) ? TRUE : FALSE,
      'submitForSettlement' => $submit_for_settlement,
    ),
  );
  if (isset($payment_method['settings']['descriptor_name'])) {
    $descriptor_name = $payment_method['settings']['descriptor_name'];
    if (!empty($descriptor_name)) {
      $sale_data['descriptor'] = array(
        'name' => $descriptor_name,
      );
    }
  }

  // Update the sale data to charge the card on file
  // if it was the selected payment method. Ignore
  // this option for drop-in transactions since
  // they have their own card on file interface.
  if (!empty($pane_values['cardonfile']) && $pane_values['cardonfile'] != 'new' && $payment_method['base'] != 'commerce_braintree_dropin') {
    $cardonfile = commerce_cardonfile_load($pane_values['cardonfile']);

    // Remove the nonce if the transaction is against a card on file.
    unset($sale_data['paymentMethodNonce']);

    // Add the remote id for the transaction to be charged to.
    $sale_data['paymentMethodToken'] = $cardonfile->remote_id;
  }

  // Allow other modules to alter the sale before sending it to Braintree.
  drupal_alter('commerce_braintree_js_sale_data', $sale_data, $order);

  // Also invoke braintree_dropin_sale_data for legacy purposes (Deprecated).
  drupal_alter('commerce_braintree_dropin_sale_data', $sale_data, $order);

  // Execute the API sale method to create a sale object.
  $response = Braintree_Transaction::sale($sale_data);

  // Process the Braintree response and create a payment transaction.
  $transaction = commerce_braintree_js_process_transaction($order, $payment_method, $charge, $response);

  // Set a form error if the payment transaction failed for any reason.
  if (empty($transaction->status) || !in_array($transaction->status, array(
    COMMERCE_PAYMENT_STATUS_SUCCESS,
    COMMERCE_PAYMENT_STATUS_PENDING,
  ))) {
    $error = t('Your payment transaction could not be processed at this time. If an error was provided it was: @response', array(
      '@response' => $response->message,
    ));

    // Allow other modules to customize the error that's displayed ot customers.
    drupal_alter('commerce_braintree_transaction_error', $error, $transaction, $response);
    form_set_error('braintree_dropin', $error);
    return FALSE;
  }

  // Save the Braintree vault information to the customer.
  if (!empty($pane_values['cardonfile'])) {
    commerce_braintree_js_create_cardonfile($order, $payment_method, $response);
  }
  return TRUE;
}