You are here

function commerce_braintree_tr_redirect_form in Commerce Braintree 7.3

Same name and namespace in other branches
  1. 7.2 commerce_braintree.module \commerce_braintree_tr_redirect_form()

Payment method callback: Braintree Transparent Redirect form.

1 string reference to 'commerce_braintree_tr_redirect_form'
commerce_braintree_commerce_payment_method_info in ./commerce_braintree.module
Implements hook_commerce_payment_method_info().

File

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

Code

function commerce_braintree_tr_redirect_form($form, &$form_state, $order, $payment_method) {
  global $user;

  // Initialize the Braintree client.
  commerce_braintree_initialize($payment_method);
  $context = commerce_braintree_payment_session_load($order->order_id);
  list($amount, $customer_name, $first_name, $last_name, $country, $thoroughfare, $locality, $postal_code, $administrative_area, $customer_mail) = _commerce_braintree_get_transaction_informations($order);

  // Retrieve the order balance instead of the order total, this allows you
  // to pay your order with multiple payment methods.
  $balance = commerce_payment_order_balance($order);
  $amount = commerce_braintree_price_amount($balance['amount'], $balance['currency_code']);

  // Depending on the currency, set the correct merchant account.
  $merchant_account_id = commerce_braintree_get_merchant_account_id($payment_method, $balance['currency_code']);

  // Determine if we should settle the transaction immediately.
  if (isset($payment_method['settings']['submit_for_settlement'])) {
    $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;
  }
  if (empty($payment_method['settings']['cardonfile']) || $context === NULL || $context == 'new') {

    // Build the credit card form first.
    $form = commerce_braintree_credit_card_form($payment_method);

    // Create a transaction data string using the Braintree client.
    $trData = Braintree_TransparentRedirect::transactionData(array(
      // Add transaction related data.
      'transaction' => array(
        'channel' => 'CommerceGuys_BT_Vzero',
        'type' => Braintree_Transaction::SALE,
        'amount' => $amount,
        'orderId' => $order->order_id,
        'merchantAccountId' => $merchant_account_id,
        'customer' => array(
          'firstName' => $first_name,
          'lastName' => $last_name,
          'email' => $customer_mail,
        ),
        'billing' => array(
          'countryCodeAlpha2' => $country,
          'streetAddress' => $thoroughfare,
          'firstName' => $customer_name,
          'locality' => $locality,
          'postalCode' => $postal_code,
          'region' => $administrative_area,
        ),
        'options' => array(
          'storeInVault' => TRUE,
          'submitForSettlement' => $submit_for_settlement,
        ),
      ),
      'redirectUrl' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array(
        'absolute' => TRUE,
      )),
    ));
  }
  elseif (module_exists('commerce_cardonfile')) {

    // Load the selected card on file.
    $card = commerce_cardonfile_load(commerce_braintree_payment_session_load($order->order_id));

    // Create a transaction data string using the Braintree client.
    $trData = Braintree_TransparentRedirect::transactionData(array(
      // Add transaction related data.
      'transaction' => array(
        'type' => Braintree_Transaction::SALE,
        'orderId' => $order->order_id,
        'amount' => $amount,
        'paymentMethodToken' => $card->remote_id,
        'options' => array(
          'submitForSettlement' => $submit_for_settlement,
        ),
      ),
      'redirectUrl' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array(
        'absolute' => TRUE,
      )),
    ));
    $form['card']['#markup'] = theme('card_data_overview', array(
      'card_data' => $card,
    ));
  }

  // Store the Transparent Redirect request data in the form.
  $form['tr_data'] = array(
    '#type' => 'hidden',
    '#default_value' => $trData,
    '#name' => 'tr_data',
  );

  // Provide a submit button with a back link that returns to the payment
  // redirect back URL, which sends the customer to the previous page.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Process payment'),
    '#suffix' => l(t('Cancel'), 'checkout/' . $order->order_id . '/payment/back/' . $order->data['payment_redirect_key'], array(
      'absolute' => TRUE,
    )),
  );

  // Set the action URL to submit directly to Braintree's server.
  $form['#action'] = Braintree_TransparentRedirect::url();
  return $form;
}