You are here

function commerce_braintree_js_cardonfile_form_submit in Commerce Braintree 7.3

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

Submit handler for commerce_cardonfile form callbacks.

2 string references to 'commerce_braintree_js_cardonfile_form_submit'
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 1317
Integrates Braintree Transparent Redirect with Drupal Commerce.

Code

function commerce_braintree_js_cardonfile_form_submit(&$form, &$form_state) {
  $account = user_load($form_state['card']->uid);
  $cards = commerce_braintree_cardonfile_entities_by_user($form_state['card']->uid);
  commerce_braintree_initialize($form_state['payment_instance']);
  $nonce = !empty($form_state['nonce']) ? $form_state['nonce'] : commerce_braintree_js_get_nonce();

  // Populate the customer variable with data from Braintree.
  if (!empty($form_state['values']['customer_id'])) {

    // Query for the customer record from the customer id in form values.
    try {
      $customer = Braintree_Customer::find($form_state['values']['customer_id']);
    } catch (Exception $ex) {
      watchdog('commerce_braintree', 'Unable to fetch Braintree customer account due to @error', array(
        '@error' => $ex
          ->getMessage(),
      ), WATCHDOG_ERROR);
      drupal_set_message(t('We are unable to create a payment profile for you at this time.'), 'error');
      return FALSE;
    }

    // Create or determine the payment method that was selected
    // during update.
    try {
      $payment_method = Braintree_PaymentMethod::create(array(
        'customerId' => $form_state['values']['customer_id'],
        'paymentMethodNonce' => $nonce,
      ))->paymentMethod;
    } catch (Exception $ex) {
      watchdog('commerce_braintree', 'Unable to load/create a payment method due to @error', array(
        '@error' => $ex
          ->getMessage(),
      ), WATCHDOG_ERROR);
      drupal_set_message(t('We are unable to create a payment profile for you at this time.'), 'error');
      return FALSE;
    }
  }
  else {

    // Create a new customer and payment method at once.
    try {
      $customer = Braintree_Customer::create(array(
        'email' => $account->mail,
        'paymentMethodNonce' => $nonce,
      ))->customer;
      $payment_method = reset($customer->creditCards);
    } catch (Exception $ex) {
      watchdog('commerce_braintree', 'Unable to fetch Braintree customer account due to @error', array(
        '@error' => $ex
          ->getMessage(),
      ), WATCHDOG_ERROR);
      drupal_set_message(t('We are unable to create a payment profile for you at this time.'), 'error');
      return FALSE;
    }
  }

  // Make sure a customer id is loaded or created before proceeding.
  if (empty($customer->id)) {
    watchdog('commerce_braintree', 'Unable to load or create a Braintree customer object', array(), WATCHDOG_ERROR);
    drupal_set_message(t('We are unable to create a payment profile for you at this time.'), 'error');
    return FALSE;
  }
  try {
    $token = !empty($payment_method->token) ? $payment_method->token : $form_state['card']->remote_id;
    Braintree_PaymentMethod::update($token, array(
      'options' => array(
        'makeDefault' => $form_state['values']['instance_default'],
      ),
    ));
  } catch (Exception $ex) {
    watchdog('commerce_braintree', 'Unable to set default payment method due to @error', array(
      '@error' => $ex
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }

  // Update the customer object to make sure we have all payment methods.
  $customer = Braintree_Customer::find($customer->id);

  // Loop over each of the Braintree payment methods and make sure
  // a matching card on file entity exits.
  foreach ($customer->creditCards as $vault_profile) {
    $card = commerce_cardonfile_load_multiple(array(), array(
      'remote_id' => $vault_profile->token,
    ));

    // Create a new card on file entity if we were unable to load one
    // for this vault profile.
    if (empty($card)) {
      $card = commerce_cardonfile_new();
      $card->remote_id = $vault_profile->token;
      $card->status = TRUE;
      $card->uid = $form_state['card']->uid;
    }
    else {
      $card = reset($card);
    }

    // Update the values returned from Braintree.
    $card->card_type = $vault_profile->cardType;
    $card->card_number = $vault_profile->last4;
    $card->card_exp_month = $vault_profile->expirationMonth;
    $card->card_exp_year = $vault_profile->expirationYear;
    $card->instance_default = $vault_profile->default;
    $card->payment_method = !empty($card->payment_method) ? $card->payment_method : $form_state['payment_instance']['method_id'];
    $card->instance_id = !empty($card->instance_id) ? $card->instance_id : $form_state['payment_instance']['instance_id'];
    $card->instance_default = !empty($instance_default) ? $instance_default == $vault_profile->token : $vault_profile->default;
    commerce_cardonfile_save($card);

    // Pop this card off the cards on file array.
    unset($cards[$card->card_id]);
  }

  // Loop over any cards that were't returned from Braintree
  // and make sure they're deleted.
  foreach ($cards as $card) {
    commerce_cardonfile_delete($card->card_id);
  }

  // Store the Braintree customer information on the Drupal user.
  $account->data['braintree_vault']['id'] = $customer->id;
  user_save($account);
  drupal_set_message(t('Payment information updated successfully'));
  $form_state['redirect'] = 'user/' . $form_state['card']->uid . '/cards';
}