You are here

function commerce_braintree_hostedfields_submit_form_elements in Commerce Braintree 7.2

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

Return the form elements for the hosted fields form.

Parameters

$payment_method: The commerce payment method.

array $arguments: An optional array of arguments to pass to Braintree.

Return value

array An array of form elements.

2 calls to commerce_braintree_hostedfields_submit_form_elements()
commerce_braintree_hostedfields_submit_form in modules/commerce_braintree_hostedfields/commerce_braintree_hostedfields.module
Form callback for Braintree Drop-in payment method.
commerce_braintree_js_cardonfile_form in ./commerce_braintree.module
Form callback for commerce_cardonfile entities.

File

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

Code

function commerce_braintree_hostedfields_submit_form_elements($payment_method, $arguments = array()) {
  global $user;
  $form = array();

  // Setting the customer id loads Braintree vault saved payment methods.
  if (!empty($user->data['braintree_vault']) && empty($arguments['customerId'])) {
    $arguments['customerId'] = $user->data['braintree_vault']['id'];
  }

  // Initialize Braintree and create a token.
  commerce_braintree_initialize($payment_method);
  $form['#attached']['library'][] = array(
    'commerce_braintree_hostedfields',
    'braintree.hostedfields',
  );
  $js_settings = array(
    'environment' => $payment_method['settings']['environment'],
    'advancedFraudTools' => (bool) $payment_method['settings']['advanced_fraud_detect'],
    'clientToken' => Braintree_ClientToken::generate($arguments),
    'hostedFields' => array(
      'styles' => array(),
      'number' => array(
        'selector' => '#card-number',
      ),
      'cvv' => array(
        'selector' => '#cvv',
      ),
      'expirationMonth' => array(
        'selector' => '#expiration-month',
      ),
      'expirationYear' => array(
        'selector' => '#expiration-year',
      ),
    ),
  );

  // Add required settings for PayPal.
  if (!empty($payment_method['settings']['paypal_button'])) {
    $js_settings['paypal'] = array(
      'container' => 'paypal-container',
    );
  }

  // Allow other modules to alter the JS settings.
  drupal_alter('commerce_braintree_hostedfields_js', $js_settings, $payment_method);
  $form['#attached']['js'][] = array(
    'data' => array(
      'commerceBraintreeHostedFields' => $js_settings,
    ),
    'type' => 'setting',
  );
  $form['braintree'] = array(
    '#type' => 'container',
  );
  $form['braintree']['#attributes']['class'][] = 'braintree-form';
  $form['braintree']['errors'] = array(
    '#type' => 'hidden',
  );
  $form['braintree']['device_data'] = array(
    '#type' => 'hidden',
  );
  $form['braintree']['cc'] = array(
    '#type' => 'container',
  );
  $form['braintree']['cc']['method_wrapper'] = array(
    '#prefix' => '<div class="payment-details-method-title">',
    '#markup' => t('Pay with Credit Card'),
    '#suffix' => '</div>',
    '#access' => !empty($payment_method['settings']['paypal_button']),
  );
  $form['braintree']['cc']['number'] = array(
    '#type' => 'item',
    '#title' => t('Card number'),
    '#markup' => '<div id="card-number" class="braintree-hosted-field"></div>',
  );
  $form['braintree']['cc']['expiration'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'clearfix',
      ),
    ),
  );
  $form['braintree']['cc']['expiration']['month'] = array(
    '#type' => 'item',
    '#title' => t('Expiry Month'),
    '#markup' => '<div id="expiration-month" class="braintree-hosted-field"></div>',
  );
  $form['braintree']['cc']['expiration']['divider'] = array(
    '#markup' => '<span class="commerce-month-year-divider">/</span>',
  );
  $form['braintree']['cc']['expiration']['year'] = array(
    '#type' => 'item',
    '#title' => t('Year'),
    '#markup' => '<div id="expiration-year" class="braintree-hosted-field"></div>',
  );
  $form['braintree']['cc']['cvv'] = array(
    '#type' => 'item',
    '#title' => t('CVV'),
    '#markup' => '<div id="cvv" class="braintree-hosted-field"></div>',
  );

  // Add form elements required to add PayPal button.
  if (!empty($payment_method['settings']['paypal_button'])) {
    $form['braintree']['paypal'] = array(
      '#type' => 'container',
    );
    $form['braintree']['paypal']['method_title'] = array(
      '#prefix' => '<div class="paypal-method-title">',
      '#markup' => t('Or pay with PayPal'),
      '#suffix' => '</div>',
    );
    $form['braintree']['paypal']['button'] = array(
      '#type' => 'item',
      '#markup' => '<div id="paypal-container"></div>',
    );
  }

  // Add option to save card on file for authenticated users.
  if (!empty($payment_method['settings']['cardonfile']) && !empty($user->uid)) {
    $storage = variable_get('commerce_cardonfile_storage', 'opt-in');
    if ($storage !== 'required') {
      $form['cardonfile'] = array(
        '#type' => 'checkbox',
        '#title' => t('Securely save this payment method for next time.'),
        '#default_value' => $storage == 'opt-in' ? FALSE : TRUE,
      );
    }
    else {
      $form['cardonfile'] = array(
        '#type' => 'value',
        '#value' => TRUE,
      );
    }
  }
  return $form;
}