You are here

function commerce_braintree_dropin_cardonfile_form_submit in Commerce Braintree 7.3

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

Submit handler for commerce_cardonfile form callbacks.

File

modules/commerce_braintree_dropin/commerce_braintree_dropin.module, line 343
Provides integration with Braintree Drop-in UI.

Code

function commerce_braintree_dropin_cardonfile_form_submit(&$form, &$form_state) {
  $account = user_load($form_state['card']->uid);
  $cards = commerce_cardonfile_load_multiple_by_uid($form_state['card']->uid, $form_state['payment_instance']['instance_id']);
  commerce_braintree_initialize($form_state['payment_instance']);
  $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_dropin', 'Unable to fetch Braintree customer account due to @error', array(
        '@error' => $ex
          ->getMessage(),
      ), WATCHDOG_ERROR);
      return;
    }

    // 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_dropin', 'Unable to load/create a payment method due to @error', array(
        '@error' => $ex
          ->getMessage(),
      ), WATCHDOG_ERROR);
      return;
    }
  }
  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_dropin', 'Unable to fetch Braintree customer account due to @error', array(
        '@error' => $ex
          ->getMessage(),
      ), WATCHDOG_ERROR);
      return;
    }
  }
  try {
    if (!empty($payment_method->token)) {

      // Set this payment method as the default.
      Braintree_PaymentMethod::update($payment_method->token, array(
        'options' => array(
          'makeDefault' => TRUE,
        ),
      ));
      $instance_default = $payment_method->token;
    }
  } catch (Exception $ex) {
    watchdog('commerce_braintree_dropin', 'Unable to set default payment method due to @error', array(
      '@error' => $ex
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }

  // 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 = $form_state['payment_instance']['method_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';
}