You are here

function commerce_braintree_js_cardonfile_form in Commerce Braintree 7.3

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

Form callback for commerce_cardonfile entities.

2 string references to 'commerce_braintree_js_cardonfile_form'
commerce_braintree_dropin_commerce_payment_method_info in modules/commerce_braintree_dropin/commerce_braintree_dropin.module
Implements hook_commerce_payment_method_info().
commerce_braintree_hostedfields_commerce_payment_method_info in modules/commerce_braintree_hostedfields/commerce_braintree_hostedfields.module
Implements hook_commerce_payment_method_info().

File

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

Code

function commerce_braintree_js_cardonfile_form($form, &$form_state, $op, $card) {
  $form_state['card'] = $card;
  $account = user_load($card->uid);
  $arguments = array();
  $payment_instance = commerce_payment_method_instance_load($card->instance_id);
  $form_state['payment_instance'] = $payment_instance;
  commerce_braintree_initialize($payment_instance);
  if (!empty($card->remote_id)) {

    // Query Braintree for the payment method matching this card on file.
    try {
      $payment_method = \Braintree_PaymentMethod::find($card->remote_id);
    } catch (Exception $ex) {

      // If Braintree doesn't return the payment method, we cannot proceed.
      drupal_set_message(t('We are unable to locate your stored payment method'), 'error');
      watchdog('commerce_braintree', 'Unable to fetch Braintree payment method due to @error', array(
        '@error' => $ex
          ->getMessage(),
      ), WATCHDOG_ERROR);
      return array();
    }
  }

  // Determine the Braintree customer id and append it to the API request.
  if (!empty($account->data) && !empty($account->data['braintree_vault'])) {

    // Set the Braintree Customer ID if stored on the user object.
    $arguments['customerId'] = $account->data['braintree_vault']['id'];
  }
  else {
    if (!empty($payment_method->customerId)) {

      // Set the Braintree Customer ID from the loaded payment method.
      $arguments['customerId'] = $payment_method->customerId;
    }
  }

  // Append common the appropriate fields form elements to the form array.
  if ($payment_instance['method_id'] == 'braintree_dropin') {
    $form += (array) commerce_braintree_dropin_submit_form_elements($payment_instance, $arguments);
  }
  else {
    if ($payment_instance['method_id'] == 'braintree_hostedfields') {

      // Only provide the Hosted Fields interface for adding a new card.
      // Otherwise, instruct the user to delete this card and add a new one.
      if (!empty($card->remote_id)) {
        $delete_link = l(t('delete it'), '/user/' . $card->uid . '/cards/' . $card->card_id . '/delete');
        $form['description'] = array(
          '#markup' => t('<p>If the card you are editing is no longer valid, please !delete and add a new card.</p>', array(
            '!delete' => $delete_link,
          )),
        );
      }
      else {
        $form += (array) commerce_braintree_hostedfields_submit_form_elements($payment_instance, $arguments);
      }
    }
  }

  // Remove the card on file options since we're always saving these.
  unset($form['cardonfile']);
  $form['instance_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Default'),
    '#description' => t('Set this card as your default payment method'),
    '#default_value' => !empty($card->instance_default) ? TRUE : FALSE,
  );
  $form['customer_id'] = array(
    '#type' => 'value',
    '#value' => !empty($arguments['customerId']) ? $arguments['customerId'] : FALSE,
  );
  $form['actions'] = array(
    '#type' => 'container',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Payment Method'),
  );
  $form['actions']['cancel'] = array(
    '#markup' => l(t('Cancel'), '/user/' . $card->uid . '/cards'),
  );
  return $form;
}