You are here

function commerce_braintree_redirect_form in Commerce Braintree 7

Payment method callback: redirect form.

Building the request that is going to be sent to braintree after the necessary checks.

File

./commerce_braintree.commerce_braintree.inc, line 22
Include payment method settings callback, redirect form callbacks...

Code

function commerce_braintree_redirect_form($form, &$form_state, $order, $payment_method) {
  $settings = _commerce_braintree_set_feedback_url($order, $payment_method);
  global $user;
  list($amount, $customer_name, $country, $thoroughfare, $locality, $postal_code, $administrative_area, $customer_mail) = _commerce_braintree_get_transaction_informations($order);
  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
  _commerce_braintree_init_credentials($payment_method);
  $form = commerce_braintree_credit_card_form($payment_method);

  // 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_currency_amount_to_decimal($balance['amount'], commerce_default_currency());
  $trData = Braintree_TransparentRedirect::transactionData(array(
    // Add transaction related data.
    'transaction' => array(
      'type' => Braintree_Transaction::SALE,
      'amount' => $amount,
      'customer' => array(
        'firstName' => $customer_name,
        'email' => $customer_mail,
      ),
      'billing' => array(
        'countryCodeAlpha2' => $country,
        'streetAddress' => $thoroughfare,
        'firstName' => $customer_name,
        'locality' => $locality,
        'postalCode' => $postal_code,
        'region' => $administrative_area,
      ),
      'options' => array(
        'submitForSettlement' => TRUE,
      ),
    ),
    'redirectUrl' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array(
      'absolute' => TRUE,
    )),
  ));
  $form['tr_data']['#type'] = 'hidden';
  $form['tr_data']['#name'] = 'tr_data';
  $form['tr_data']['#default_value'] = $trData;

  // Change the action. The form will be submitted directly to braintree's
  // servers.
  $form['#action'] = Braintree_TransparentRedirect::url();
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('submit'),
  );
  return $form;
}