You are here

function commerce_braintree_cof_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_cof.inc, line 22
Include payment method settings callback, redirect form callbacks...

Code

function commerce_braintree_cof_redirect_form($form, &$form_state, $order, $payment_method) {
  global $user;
  $context = commerce_braintree_payment_session_load($order->order_id);
  $settings = _commerce_braintree_set_feedback_url($order, $payment_method);
  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);

  // 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());
  if ($context === NULL || $context == 'new') {
    $form = commerce_braintree_credit_card_form($payment_method);
    $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(
          'storeInVault' => TRUE,
          'submitForSettlement' => TRUE,
        ),
      ),
      'redirectUrl' => url('checkout/' . $order->order_id . '/payment/return/' . $order->data['payment_redirect_key'], array(
        'absolute' => TRUE,
      )),
    ));
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('submit'),
      '#suffix' => l(t('Cancel'), 'checkout/' . $order->order_id . '/payment/back/' . $order->data['payment_redirect_key'], array(
        'absolute' => TRUE,
      )),
    );
  }
  else {
    global $user;
    $stored_cards = commerce_cardonfile_data_load_multiple($user->uid, $payment_method['instance_id']);
    $element = variable_get('commerce_cardonfile_selector', 'radios');
    $options = commerce_cardonfile_options_list($stored_cards, $element);
    $card = commerce_braintree_load_cardonfile_data($user->uid, commerce_braintree_payment_session_load($order->order_id));
    $settings = _commerce_braintree_set_feedback_url($order, $payment_method);
    $trData = Braintree_TransparentRedirect::transactionData(array(
      // Add transaction related data.
      'transaction' => array(
        'type' => Braintree_Transaction::SALE,
        'amount' => $amount,
        'paymentMethodToken' => $card['remote_id'],
      ),
      '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' => commerce_cardonfile_data_load($card),
    ));
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Proceed to payment'),
      '#suffix' => l(t('Cancel'), 'checkout/' . $order->order_id . '/payment/back/' . $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();
  return $form;
}