You are here

function commerce_braintree_hostedfields_commerce_cardonfile_checkout_pane_form_alter in Commerce Braintree 7.3

Same name and namespace in other branches
  1. 7.2 modules/commerce_braintree_hostedfields/commerce_braintree_hostedfields.module \commerce_braintree_hostedfields_commerce_cardonfile_checkout_pane_form_alter()

Implements hook_commerce_cardonfile_checkout_pane_form_alter().

File

modules/commerce_braintree_hostedfields/commerce_braintree_hostedfields.module, line 153
Provides integration with Braintree Hosted Fields.

Code

function commerce_braintree_hostedfields_commerce_cardonfile_checkout_pane_form_alter(&$pane, &$form, $form_state) {

  // Only continue when the selected payment method is Braintree Hosted Fields.
  if (empty($form['commerce_payment']['payment_method']['#default_value']) || strpos($form['commerce_payment']['payment_method']['#default_value'], 'braintree_hostedfields') !== 0) {
    return;
  }

  // Only continue when card on file options are available.
  if ($pane['cardonfile']['#type'] == 'value' && $pane['cardonfile']['#value'] == 'new') {
    return;
  }
  $pane['cardonfile']['#required'] = TRUE;

  // Remove the Braintree form elements and JavaScript if an existing
  // card on file was selected.
  if (empty($form_state['triggering_element']) || $form_state['triggering_element']['#name'] == 'commerce_payment[payment_method]' && $pane['cardonfile']['#default_value'] != 'new' || (empty($form_state['values']['commerce_payment']['payment_details']['cardonfile']) || $form_state['values']['commerce_payment']['payment_details']['cardonfile'] !== 'new')) {
    commerce_braintree_hosted_fields_remove_hosted_fields_form($pane, $form_state);
  }
}