You are here

function commerce_authnet_acceptjs_cardonfile_create in Commerce Authorize.Net 7

Commerce Card on File create callback.

Parameters

array $form: The card on file create form.

array $form_state: The card on file create form state.

array $payment_method: The payment method for the card on file request.

object $card_data: The commerce_cardonfile entity.

Return value

object|bool A new commerce_cardonfile entity or FALSE if there was an error.

1 string reference to 'commerce_authnet_acceptjs_cardonfile_create'
commerce_authnet_commerce_payment_method_info in ./commerce_authnet.module
Implements hook_commerce_payment_method_info().

File

./commerce_authnet.module, line 1143
Implements Authorize.Net payment services for use in Drupal Commerce.

Code

function commerce_authnet_acceptjs_cardonfile_create($form, &$form_state, $payment_method, $card_data) {
  $account = user_load($card_data->uid);

  // Submit the profile to the field attach handlers.
  $profile = $form_state['commerce_customer_profile'];
  field_attach_submit('commerce_customer_profile', $profile, $form['commerce_customer_profile'], $form_state);
  commerce_customer_profile_save($profile);

  // Format the address into the Auth.net schema.
  $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
  $billto = commerce_authnet_cim_billto_array(NULL, $profile_wrapper->commerce_customer_address
    ->value());

  // Build the card data to submit to Auth.net.
  $data_descriptor = $form_state['values']['data_descriptor'];
  $data_value = $form_state['values']['data_value'];
  $last4 = $form_state['values']['last4'];
  $card_data->card_exp_month = $form_state['values']['expiration_month'];
  $card_data->card_exp_year = substr(date('Y'), 0, 2) . $form_state['values']['expiration_year'];
  $card_type = $form_state['values']['type'];
  $card_data->card_number = $last4;

  // Attempt to load and existing profile id from the customers card data.
  $existing_cards = commerce_cardonfile_load_multiple_by_uid($account->uid, $payment_method['instance_id']);

  // Check for a remote ID.
  $remote_id = NULL;
  if (!empty($existing_cards)) {
    $existing_card = reset($existing_cards);
    $remote_id = $existing_card->remote_id;
  }
  if ($remote_id) {

    // Extract the profile id from the remote id.
    list($customer_profile_id, $cim_customer_payment_id) = explode('|', $remote_id);

    // Build a request to add a new payment method to an existing profile.
    $api_request_data = array(
      'customerProfileId' => $customer_profile_id,
      'paymentProfile' => array(
        'billTo' => $billto,
        'payment' => array(
          'opaqueData' => array(
            'dataDescriptor' => $data_descriptor,
            'dataValue' => $data_value,
          ),
        ),
      ),
    );
    $xml_response = commerce_authnet_cim_request($payment_method, 'createCustomerPaymentProfileRequest', $api_request_data);
    if ((string) $xml_response->messages->resultCode == 'Ok') {
      $customer_payment_id = (string) $xml_response->customerPaymentProfileId;
    }
    elseif ((string) $xml_response->messages->message->code == 'E00039') {
      $customer_payment_id = (string) $xml_response->customerPaymentProfileId;
    }
    else {
      return FALSE;
    }
  }
  else {

    // Build a request to create a profile and add payment method to it.
    $api_request_data = array(
      'profile' => array(
        'merchantCustomerId' => $account->uid,
        'description' => $billto['firstName'] . ' ' . $billto['lastName'],
        'email' => $account->mail,
      ),
    );
    $xml_response = commerce_authnet_cim_request($payment_method, 'createCustomerProfileRequest', $api_request_data);
    if ((string) $xml_response->messages->resultCode == 'Ok') {
      $customer_profile_id = (string) $xml_response->customerProfileId;
    }
    elseif ((string) $xml_response->messages->message->code == 'E00039') {
      $result = array_filter(explode(' ', (string) $xml_response->messages->message->text), 'is_numeric');
      $customer_profile_id = reset($result);
    }
    else {
      return FALSE;
    }

    // Build a request to add a new payment method to an existing profile.
    $api_request_data = array(
      'customerProfileId' => $customer_profile_id,
      'paymentProfile' => array(
        'billTo' => $billto,
        'payment' => array(
          'opaqueData' => array(
            'dataDescriptor' => $data_descriptor,
            'dataValue' => $data_value,
          ),
        ),
      ),
    );
    $xml_response = commerce_authnet_cim_request($payment_method, 'createCustomerPaymentProfileRequest', $api_request_data);
    if ((string) $xml_response->messages->resultCode == 'Ok') {
      $customer_payment_id = (string) $xml_response->customerPaymentProfileId;
    }
    elseif ((string) $xml_response->messages->message->code == 'E00039') {
      $customer_payment_id = (string) $xml_response->customerPaymentProfileId;
    }
    else {
      return FALSE;
    }
  }
  $remote_id = $customer_profile_id . '|' . $customer_payment_id;
  $card_data_wrapper = entity_metadata_wrapper('commerce_cardonfile', $card_data);
  $card_data->uid = $account->uid;
  $card_data->remote_id = $remote_id;
  $card_data->card_type = $card_type;
  $card_data->card_name = $billto['firstName'] . ' ' . $billto['lastName'];
  $card_data->card_number = $last4;
  $card_data->status = 1;
  $card_data_wrapper->commerce_cardonfile_profile = $profile;
  return $card_data;
}