From 2d7fd9632cc137d37433aff3a673a398fb63dee7 Mon Sep 17 00:00:00 2001
From: Melissa Bent <melissa@clikfocus.com>
Date: Sun, 23 Aug 2015 18:23:26 -0600
Subject: [PATCH] Reapplying the changes to Bank on File.

---
 commerce_bank_account_onfile.info                  |   8 +
 commerce_bank_account_onfile.info.inc              |  23 +-
 commerce_bank_account_onfile.install               |  37 +-
 commerce_bank_account_onfile.module                | 502 ++++++++++++++-------
 commerce_bank_account_onfile.rules.inc             |   8 +-
 includes/commerce_bank_account_onfile.admin.inc    |  11 +-
 includes/commerce_bank_account_onfile.entity.inc   |  24 +-
 ...bank_account_onfile.extra_fields_controller.inc |   2 +-
 includes/commerce_bank_account_onfile.pages.inc    | 168 +++++--
 .../views/commerce_bank_account_onfile.views.inc   |  48 ++
 .../commerce_bank_account_onfile.views_default.inc | 157 +++++++
 ..._bank_account_onfile_handler_field_aba_code.inc |  42 ++
 ...ank_account_onfile_handler_field_acct_label.inc |  16 +
 ...nt_onfile_handler_field_bank_account_number.inc |  42 ++
 ...ank_account_onfile_handler_field_operations.inc |  59 +++
 ...count_onfile_plugin_argument_validate_owner.inc |  34 ++
 theme/commerce_bank_account_onfile.view.css        |  13 +
 17 files changed, 943 insertions(+), 251 deletions(-)
 create mode 100644 sites/all/modules/patched/commerce_bankonfile/includes/views/commerce_bank_account_onfile.views.inc
 create mode 100644 sites/all/modules/patched/commerce_bankonfile/includes/views/commerce_bank_account_onfile.views_default.inc
 create mode 100644 sites/all/modules/patched/commerce_bankonfile/includes/views/handlers/commerce_bank_account_onfile_handler_field_aba_code.inc
 create mode 100644 sites/all/modules/patched/commerce_bankonfile/includes/views/handlers/commerce_bank_account_onfile_handler_field_acct_label.inc
 create mode 100644 sites/all/modules/patched/commerce_bankonfile/includes/views/handlers/commerce_bank_account_onfile_handler_field_bank_account_number.inc
 create mode 100644 sites/all/modules/patched/commerce_bankonfile/includes/views/handlers/commerce_bank_account_onfile_handler_field_operations.inc
 create mode 100644 sites/all/modules/patched/commerce_bankonfile/includes/views/handlers/commerce_bank_account_onfile_plugin_argument_validate_owner.inc
 create mode 100644 sites/all/modules/patched/commerce_bankonfile/theme/commerce_bank_account_onfile.view.css

diff --git a/commerce_bank_account_onfile.info b/commerce_bank_account_onfile.info
index 3330f00..2ff866b 100644
--- a/commerce_bank_account_onfile.info
+++ b/commerce_bank_account_onfile.info
@@ -7,6 +7,7 @@ dependencies[] = commerce_payment
 dependencies[] = commerce_order
 dependencies[] = commerce_customer
 dependencies[] = entity
+dependencies[] = views
 core = 7.x
 
 ; Entity class
@@ -15,6 +16,13 @@ files[] = includes/commerce_bank_account_onfile.entity.inc
 ; Extra fields controller
 files[] = includes/commerce_bank_account_onfile.extra_fields_controller.inc
 
+; Views handlers
+files[] = includes/views/handlers/commerce_bank_account_onfile_handler_field_aba_code.inc
+files[] = includes/views/handlers/commerce_bank_account_onfile_handler_field_acct_label.inc
+files[] = includes/views/handlers/commerce_bank_account_onfile_handler_field_bank_account_number.inc
+files[] = includes/views/handlers/commerce_bank_account_onfile_handler_field_operations.inc
+files[] = includes/views/handlers/commerce_bank_account_onfile_plugin_argument_validate_owner.inc
+
 ; Information added by Drupal.org packaging script on 2015-01-02
 version = "7.x-1.x-dev"
 core = "7.x"
diff --git a/commerce_bank_account_onfile.info.inc b/commerce_bank_account_onfile.info.inc
index 81d6e74..180d2be 100644
--- a/commerce_bank_account_onfile.info.inc
+++ b/commerce_bank_account_onfile.info.inc
@@ -11,7 +11,7 @@
 function commerce_bank_account_onfile_entity_property_info() {
   $info = array();
 
-  $properties = &$info['commerce_bank_account_file']['properties'];
+  $properties = &$info['commerce_bank_account_onfile']['properties'];
   $properties['bank_account_id'] = array(
     'label' => t('Bank account ID'),
     'description' => t('The internal numeric ID of the bank account.'),
@@ -78,27 +78,6 @@ function commerce_bank_account_onfile_entity_property_info() {
     'setter callback' => 'entity_property_verbatim_set',
     'schema field' => 'bank_name',
   );
-  // $properties['card_exp_month'] = array(
-  //   'label' => t('Card expiration month'),
-  //   'description' => t('Expiration month.'),
-  //   'type' => 'integer',
-  //   'setter callback' => 'entity_property_verbatim_set',
-  //   'schema field' => 'card_exp_month',
-  // );
-  // $properties['card_exp_year'] = array(
-  //   'label' => t('Card expiration year'),
-  //   'description' => t('Expiration year.'),
-  //   'type' => 'integer',
-  //   'setter callback' => 'entity_property_verbatim_set',
-  //   'schema field' => 'card_exp_year',
-  // );
-  // $properties['card_exp'] = array(
-  //   'label' => t('Card expiration'),
-  //   'description' => t('Expiration month and year.'),
-  //   'type' => 'text',
-  //   'getter callback' => 'commerce_cardonfile_get_properties',
-  //   'computed' => TRUE,
-  // );
   $properties['instance_default'] = array(
     'label' => t('Instance default'),
     'description' => t('Whether this is the default bank account for this payment method instance.'),
diff --git a/commerce_bank_account_onfile.install b/commerce_bank_account_onfile.install
index a761987..a76860f 100644
--- a/commerce_bank_account_onfile.install
+++ b/commerce_bank_account_onfile.install
@@ -1,12 +1,22 @@
 <?php
 
 /**
+ * Implements hook_uninstall().
+ */
+function commerce_bank_account_onfile_uninstall() {
+  field_delete_field('commerce_bankonfile_profile');
+  // Purge field data now to allow cardonfile module to be uninstalled
+  // if this is the only field remaining.
+  field_purge_batch(10);
+}
+
+/**
  * Implements hook_schema().
  */
 function commerce_bank_account_onfile_schema() {
   $schema = array();
-  $schema['commerce_bank_account_file'] = array(
-    'description' => 'The base table for the commerce_bank_account_file entity type.',
+  $schema['commerce_bank_account_onfile'] = array(
+    'description' => 'The base table for the commerce_bank_account_onfile entity type.',
     'fields' => array(
       'bank_account_id' => array(
         'description' => 'Primary key: numeric bank account id.',
@@ -74,20 +84,6 @@ function commerce_bank_account_onfile_schema() {
         'not null' => TRUE,
         'default' => '',
       ),
-      // 'card_exp_month' => array(
-      //   'description' => 'Expiration month.',
-      //   'type' => 'int',
-      //   'size' => 'tiny',
-      //   'not null' => TRUE,
-      //   'default' => 0,
-      // ),
-      // 'card_exp_year' => array(
-      //   'description' => 'Expiration year.',
-      //   'type' => 'int',
-      //   'size' => 'small',
-      //   'not null' => TRUE,
-      //   'default' => 0,
-      // ),
       'instance_default' => array(
         'description' => 'Whether this is the default bank account for this payment method instance.',
         'type' => 'int',
@@ -130,3 +126,12 @@ function commerce_bank_account_onfile_schema() {
 
   return $schema;
 }
+
+/**
+ * Tables and functions weren't name consistently with the name of the module.
+ * Renaming the table to follow Drupal best practices.
+ */
+function commerce_bank_account_onfile_update_7100() {
+  db_rename_table('commerce_bank_account_file', 'commerce_bank_account_onfile');
+  return t('The table name for Commerce Bank Account on File has been updated.');
+}
\ No newline at end of file
diff --git a/commerce_bank_account_onfile.module b/commerce_bank_account_onfile.module
index c3e78a0..1f02196 100644
--- a/commerce_bank_account_onfile.module
+++ b/commerce_bank_account_onfile.module
@@ -67,7 +67,6 @@ function commerce_bank_account_onfile_menu() {
     'weight' => -10,
   );
 
-  /*
   $items['user/%user/bank-accounts/add'] = array(
     'title' => 'Add a bank account',
     'page callback' => 'commerce_bank_account_onfile_add_page',
@@ -76,67 +75,92 @@ function commerce_bank_account_onfile_menu() {
     'type' => MENU_LOCAL_ACTION,
     'file' => 'includes/commerce_bank_account_onfile.pages.inc',
   );
-  $create_implements = commerce_bank_account_file_payment_method_implements('create callback');
+  $create_implements = commerce_bank_account_onfile_payment_method_implements('create callback');
   foreach ($create_implements as $method_id => $method_function) {
     $payment_method_instances = _commerce_bank_account_onfile_payment_method_instances($method_id);
     foreach ($payment_method_instances as $instance_id => $payment_method) {
-      $new_card = commerce_cardonfile_new(array(
+      $new_account = commerce_bank_account_onfile_new(array(
         'instance_id' => $instance_id,
         'payment_method' => $payment_method['method_id'],
       ));
       $items['user/%user/bank-accounts/add/' . drupal_hash_base64($instance_id)] = array(
         'title' => 'Add a bank account for payments with !name',
         'title arguments' => array('!name' => $payment_method['display_title']),
-        'page callback' => 'commerce_cardonfile_card_form_page',
-        'page arguments' => array('create', $new_card, 1),
-        'access callback' => 'commerce_cardonfile_access',
-        'access arguments' => array('create', $new_card, 1),
-        'file' => 'includes/commerce_cardonfile.pages.inc',
+        'page callback' => 'commerce_bank_account_onfile_bank_account_form_page',
+        'page arguments' => array('create', $new_account, 1),
+        'access callback' => 'commerce_bank_account_onfile_access',
+        'access arguments' => array('create', $new_account, 1),
+        'file' => 'includes/commerce_bank_account_onfile.pages.inc',
       );
     }
   }
 
-  $items['user/%user/cards/%commerce_cardonfile'] = array(
+  $items['user/%user/bank-accounts/%commerce_bank_account_onfile'] = array(
     'title callback' => 'commerce_cardonfile_card_title',
     'title arguments' => array(3),
-    'page callback' => 'commerce_cardonfile_card_form_page',
+    'page callback' => 'commerce_bank_account_onfile_bank_account_form_page',
     'page arguments' => array('update', 3, 1),
-    'access callback' => 'commerce_cardonfile_access',
+    'access callback' => 'commerce_bank_account_onfile_access',
     'access arguments' => array('update', 3),
-    'file' => 'includes/commerce_cardonfile.pages.inc',
+    'file' => 'includes/commerce_bank_account_onfile.pages.inc',
     'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
   );
-  $items['user/%user/cards/%commerce_cardonfile/edit'] = array(
+  $items['user/%user/bank-accounts/%commerce_bank_account_onfile/edit'] = array(
     'title' => 'Edit',
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => -10,
     'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
   );
-  $items['user/%user/cards/%commerce_cardonfile/delete'] = array(
+  $items['user/%user/bank-accounts/%commerce_bank_account_onfile/delete'] = array(
     'title' => 'Delete',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('commerce_cardonfile_delete_form', 3),
-    'access callback' => 'commerce_cardonfile_access',
+    'page arguments' => array('commerce_bank_account_onfile_delete_form', 3),
+    'access callback' => 'commerce_bank_account_onfile_access',
     'access arguments' => array('delete', 3),
     'type' => MENU_LOCAL_TASK,
     'context' => MENU_CONTEXT_INLINE,
     'weight' => 5,
-    'file' => 'includes/commerce_cardonfile.pages.inc',
-  );*/
+    'file' => 'includes/commerce_bank_account_onfile.pages.inc',
+  );
 
   return $items;
 }
 
 /**
+ * Implements hook_admin_paths().
+ */
+function commerce_bank_account_onfile_admin_paths() {
+  // The user module defines user/*/edit as an admin path, so we must explicitly
+  // declare user/*/bank-accounts/* as non-admin paths.
+  $paths = array(
+    'user/*/bank-accounts/*' => FALSE,
+  );
+
+  return $paths;
+}
+
+/**
+ * Implements hook_theme().
+ */
+function commerce_bank_account_onfile_theme() {
+  return array(
+    'bank_account_add_list' => array(
+      'variables' => array('content' => array()),
+      'file' => 'includes/commerce_bank_account_onfile.pages.inc',
+    ),
+  );
+}
+
+/**
  * Implements of hook_entity_info().
  */
 function commerce_bank_account_onfile_entity_info() {
   $data = array();
-  $data['commerce_bank_account_file'] = array(
+  $data['commerce_bank_account_onfile'] = array(
     'label' => t('Commerce Bank Account File'),
     'entity class' => 'CommerceBankAccountFile',
     'controller class' => 'EntityAPIController',
-    'base table' => 'commerce_bank_account_file',
+    'base table' => 'commerce_bank_account_onfile',
     'fieldable' => TRUE,
     'module' => 'commerce_bank_account_onfile',
     'entity keys' => array(
@@ -144,7 +168,7 @@ function commerce_bank_account_onfile_entity_info() {
     ),
     'label callback' => 'entity_class_label',
     'bundles' => array(
-      'commerce_bank_account_file' => array(
+      'commerce_bank_account_onfile' => array(
         'label' => t('Bank Account File'),
         'admin' => array(
           'path' => 'admin/commerce/config/bank-account-file',
@@ -152,7 +176,7 @@ function commerce_bank_account_onfile_entity_info() {
         ),
       ),
     ),
-    'load hook' => 'commerce_bank_account_file_load',
+    'load hook' => 'commerce_bank_account_onfile_load',
     'view modes' => array(
       'administrator' => array(
         'label' => t('Administrator'),
@@ -163,7 +187,7 @@ function commerce_bank_account_onfile_entity_info() {
         'custom settings' => FALSE,
       ),
     ),
-    'access callback' => 'commerce_bank_account_file_access',
+    'access callback' => 'commerce_bank_account_onfile_access',
     'token type' => 'commerce-bank-account-file',
     'metadata controller class' => '',
     'extra fields controller class' => 'CommerceBankAccountFileExtraFieldsController',
@@ -173,6 +197,54 @@ function commerce_bank_account_onfile_entity_info() {
 }
 
 /**
+ * Implements hook_flush_caches().
+ *
+ * Creates the commerce_cardonfile_profile field on the commerce_cardonfile
+ * entity type if missing.
+ */
+function commerce_bank_account_onfile_flush_caches() {
+  $field = field_info_field('commerce_bankonfile_profile');
+  if (!$field) {
+    $field = array(
+      'field_name' => 'commerce_bankonfile_profile',
+      'type' => 'commerce_customer_profile_reference',
+      'cardinality' => 1,
+      'entity_types' => array('commerce_bank_account_onfile'),
+      'translatable' => FALSE,
+      'settings' => array(
+        'profile_type' => 'billing',
+      ),
+    );
+    $field = field_create_field($field);
+  }
+
+  $instance = field_info_instance('commerce_bank_account_onfile', 'commerce_bankonfile_profile', 'commerce_bank_account_onfile');
+  if (!$instance) {
+    $instance = array(
+      'field_name' => 'commerce_bankonfile_profile',
+      'entity_type' => 'commerce_bank_account_onfile',
+      'bundle' => 'commerce_bank_account_onfile',
+      'label' => 'Billing Profile',
+      'widget' => array(
+        'type' => 'commerce_customer_profile_manager',
+        'weight' => -5,
+      ),
+      'display' => array(),
+    );
+    // Set the default display formatters for various view modes.
+    foreach (array('default', 'customer', 'administrator') as $view_mode) {
+      $instance['display'][$view_mode] = array(
+        'label' => 'above',
+        'type' => 'commerce_customer_profile_reference_display',
+        'weight' => -5,
+      );
+    }
+
+    field_create_instance($instance);
+  }
+}
+
+/**
  * Returns an initialized bank account file entity.
  *
  * @param $values
@@ -181,8 +253,8 @@ function commerce_bank_account_onfile_entity_info() {
  * @return
  *   A bank account file entity with all default fields initialized.
  */
-function commerce_bank_account_file_new(array $values = array()) {
-  return entity_create('commerce_bank_account_file', $values);
+function commerce_bank_account_onfile_new(array $values = array()) {
+  return entity_create('commerce_bank_account_onfile', $values);
 }
 
 /**
@@ -194,8 +266,8 @@ function commerce_bank_account_file_new(array $values = array()) {
  * @return
  *   The loaded bank account file entity or FALSE if not found.
  */
-function commerce_bank_account_file_load($bank_account_id) {
-  return entity_load_single('commerce_bank_account_file', $bank_account_id);
+function commerce_bank_account_onfile_load($bank_account_id) {
+  return entity_load_single('commerce_bank_account_onfile', $bank_account_id);
 }
 
 /**
@@ -206,7 +278,7 @@ function commerce_bank_account_file_load($bank_account_id) {
  * @param $bank_account_ids
  *   An array of bank account IDs.
  * @param $conditions
- *   An array of conditions on the {commerce_bank_account_file} table in the form
+ *   An array of conditions on the {commerce_bank_account_onfile} table in the form
  *     'field' => $value.
  * @param $reset
  *   Whether to reset the internal cache.
@@ -214,8 +286,8 @@ function commerce_bank_account_file_load($bank_account_id) {
  * @return
  *   An array of card entities indexed by bank_account_id.
  */
-function commerce_bank_account_file_load_multiple($bank_account_ids = array(), $conditions = array(), $reset = FALSE) {
-  return entity_load('commerce_bank_account_file', $bank_account_ids, $conditions, $reset);
+function commerce_bank_account_onfile_load_multiple($bank_account_ids = array(), $conditions = array(), $reset = FALSE) {
+  return entity_load('commerce_bank_account_onfile', $bank_account_ids, $conditions, $reset);
 }
 
 /**
@@ -227,8 +299,8 @@ function commerce_bank_account_file_load_multiple($bank_account_ids = array(), $
  * @return
  *   SAVED_NEW or SAVED_UPDATED depending on the operation performed.
  */
-function commerce_bank_account_file_save($bank_account) {
-  return entity_save('commerce_bank_account_file', $bank_account);
+function commerce_bank_account_onfile_save($bank_account) {
+  return entity_save('commerce_bank_account_onfile', $bank_account);
 }
 
 /**
@@ -240,8 +312,8 @@ function commerce_bank_account_file_save($bank_account) {
  * @return
  *   TRUE on success, FALSE otherwise.
  */
-function commerce_bank_account_file_delete($bank_account_id) {
-  return entity_delete('commerce_bank_account_file', $bank_account_id);
+function commerce_bank_account_onfile_delete($bank_account_id) {
+  return entity_delete('commerce_bank_account_onfile', $bank_account_id);
 }
 
 /**
@@ -253,15 +325,15 @@ function commerce_bank_account_file_delete($bank_account_id) {
  * @return
  *   TRUE on success, FALSE otherwise.
  */
-function commerce_bank_account_file_delete_multiple($bank_account_ids) {
-  return entity_delete_multiple('commerce_bank_account_file', $bank_account_ids);
+function commerce_bank_account_onfile_delete_multiple($bank_account_ids) {
+  return entity_delete_multiple('commerce_bank_account_onfile', $bank_account_ids);
 }
 
 /**
  * Implements hook_entity_view().
  */
-function commerce_bank_account_file_entity_view($entity, $entity_type, $view_mode, $langcode) {
-  if ($entity_type == 'commerce_bank_account_file') {
+function commerce_bank_account_onfile_entity_view($entity, $entity_type, $view_mode, $langcode) {
+  if ($entity_type == 'commerce_bank_account_onfile') {
     // Hide the labels of properties rendered as extra fields.
     foreach ($entity->content as &$render_array) {
       if (isset($render_array['#theme']) && $render_array['#theme'] == 'entity_property') {
@@ -279,8 +351,8 @@ function commerce_bank_account_file_entity_view($entity, $entity_type, $view_mod
 function commerce_bank_account_onfile_bank_account_types() {
   // TODO: load helpers similar to commerce_cardonfile_credit_card_types() ?
 
-  module_load_include('inc', 'commerce_authnet', 'includes/commerce_authnet.payment');
-  return commerce_payment_bank_account_types();
+  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.echeck');
+  return commerce_payment_echeck_types();
 }
 
 /**
@@ -294,14 +366,13 @@ function commerce_bank_account_onfile_bank_account_types() {
  * file for future usage.
  */
 function commerce_bank_account_onfile_form_alter(&$form, &$form_state, $form_id) {
-  // return;
   // Exit if the current form ID is for a checkout page form...
   if (strpos($form_id, 'commerce_checkout_form_') !== 0 ||
       !commerce_checkout_page_load(substr($form_id, 23))) {
     return;
   }
 
-  // Exit if the current page's form does no include the payment checkout pane...
+  // Exit if the current page's form does not include the payment checkout pane...
   if (empty($form['commerce_payment'])) {
     return;
   }
@@ -317,7 +388,7 @@ function commerce_bank_account_onfile_form_alter(&$form, &$form_state, $form_id)
     return;
   }
 
-  // Extact payment method instance id
+  // Extract payment method instance id
   $instance_id = $form['commerce_payment']['payment_method']['#default_value'];
 
   // Check to see if the currently selected payment method is Bank account on file
@@ -336,14 +407,14 @@ function commerce_bank_account_onfile_form_alter(&$form, &$form_state, $form_id)
     $storage = variable_get('commerce_bank_account_onfile_storage', 'opt-in');
 
     if (in_array($storage, array('opt-in', 'opt-out'))) {
-      $form['commerce_payment']['payment_details']['bank_account']['bank_account_store'] = array(
+      $form['commerce_payment']['payment_details']['echeck']['bank_account_store'] = array(
         '#type' => 'checkbox',
-        '#title' => t('Store this bank account details on file for future use.'),
+        '#title' => t('Store this bank account detail on file for future use.'),
         '#default_value' => $storage == 'opt-out',
       );
     }
     else {
-      $form['commerce_payment']['payment_details']['bank_account']['bank_account_store'] = array(
+      $form['commerce_payment']['payment_details']['echeck']['bank_account_store'] = array(
         '#type' => 'value',
         '#value' => TRUE,
       );
@@ -351,24 +422,21 @@ function commerce_bank_account_onfile_form_alter(&$form, &$form_state, $form_id)
 
   }
 
-  // Load existing active cards for the payment method instance and user.
-  $stored_bank_accounts = commerce_bank_account_file_load_multiple_by_uid($form_state['account']->uid, $payment_method['instance_id']);
+  // Load existing active bank accounts for the payment method instance and user.
+  $stored_bank_accounts = commerce_bank_account_onfile_load_multiple_by_uid($form_state['account']->uid, $payment_method['instance_id']);
 
   // Build options form
   $bank_account_options_form = array();
   $instance_default_bank_account_id = NULL;
 
-  // If have stored cards ...
+  // If have stored bank accounts ...
   if (!empty($stored_bank_accounts)) {
-    // Below is validation base on card expiration date.
-    // No identified expiration of bank accounts found.
-    // $valid_cards = array_filter($stored_bank_accounts, 'commerce_cardonfile_validate_card_expiration');
 
     $valid_bank_accounts = $stored_bank_accounts;
     // If have un-expired bank accounts ...
     if (!empty($valid_bank_accounts)) {
       // get options list with labels
-      $bank_account_options = commerce_bank_account_file_element_options_list($valid_bank_accounts);
+      $bank_account_options = commerce_bank_account_onfile_element_options_list($valid_bank_accounts);
 
       // determine default option
       $bank_account_options_default_value = key($bank_account_options);
@@ -402,18 +470,11 @@ function commerce_bank_account_onfile_form_alter(&$form, &$form_state, $form_id)
   if (!empty($bank_account_options_form)) {
     $form['commerce_payment']['payment_details']['bank_account_onfile'] = $bank_account_options_form;
 
-    // Add the CSS to hide a sole credit card icon if specified.
-    // if (variable_get('commerce_bank_account_file_hide_cc_radio_button', TRUE)) {
-    //   if (count($form['commerce_payment']['payment_method']['#options']) == 1) {
-    //     // TODO: make module own CSS file.
-    //     $form['commerce_payment']['payment_method']['#attached']['css'][] = drupal_get_path('module', 'commerce_cardonfile') . '/theme/commerce_cardonfile.checkout.css';
-    //   }
-    // }
-
     // If the current value for the bank account selection element is not to use
     // a different bank account, then hide the bank account form elements.
-    if (empty($form_state['values']['commerce_payment']['payment_details']['bank_account_onfile']) || $form_state['values']['commerce_payment']['payment_details']['bank_account_onfile'] !== 'new') {
-      $form['commerce_payment']['payment_details']['bank_account']['#access'] = FALSE;
+    if (empty($form_state['values']['commerce_payment']['payment_details']['bank_account_onfile']) ||
+      $form_state['values']['commerce_payment']['payment_details']['bank_account_onfile'] !== 'new') {
+      $form['commerce_payment']['payment_details']['echeck']['#access'] = FALSE;
     }
   }
   else {
@@ -442,7 +503,7 @@ function commerce_bank_account_onfile_form_alter(&$form, &$form_state, $form_id)
     '#disabled' => $force_instance_default,
     '#states' => array(
       'invisible' => array(
-        ':input[name$="[bank_account]"]' => array('value' => 'new'),
+        ':input[name$="[bank_account_onfile]"]' => array('value' => 'new'),
       ),
       'visible' => array(
         ':input[name$="[bank_account_store]"]' => array('checked' => TRUE),
@@ -451,27 +512,27 @@ function commerce_bank_account_onfile_form_alter(&$form, &$form_state, $form_id)
   );
 
   // Allow others to alter this alter
-  drupal_alter('commerce_bank_account_file_checkout_pane_form', $form['commerce_payment']['payment_details'], $form);
+  drupal_alter('commerce_bank_account_onfile_checkout_pane_form', $form['commerce_payment']['payment_details'], $form);
 
   // Add submit handler
   if (isset($form['buttons']['continue'])) {
-    $form['buttons']['continue']['#submit'][] = 'commerce_bank_account_file_commerce_checkout_form_submit';
+    $form['buttons']['continue']['#submit'][] = 'commerce_bank_account_onfile_commerce_checkout_form_submit';
   }
 }
 
 /**
  * Checkout form submit callback to process bank account on file options
  */
-function commerce_bank_account_file_commerce_checkout_form_submit($form, &$form_state) {
+function commerce_bank_account_onfile_commerce_checkout_form_submit($form, &$form_state) {
   if (!isset($form_state['order']) || empty($form_state['values']['commerce_payment'])) {
     return;
   }
 
   $pane_values = &$form_state['values']['commerce_payment'];
-  $stored_bank_account = !empty($pane_values['payment_details']['bank_account']['bank_account_store']);
+  $stored_bank_account = !empty($pane_values['payment_details']['echeck']['bank_account_store']);
 
   // Exit if no bank account selection
-  if (empty($pane_values['payment_details']['bank_account_onfile'])) {
+  if (empty($pane_values['payment_details']['echeck'])) {
     return;
   }
 
@@ -487,17 +548,101 @@ function commerce_bank_account_file_commerce_checkout_form_submit($form, &$form_
   if ($pane_values['payment_details']['bank_account_onfile'] != 'new') {
     $bank_account_id_selected = intval($pane_values['payment_details']['bank_account_onfile']);
   }
-
   // Submit actions for bank account selected
   if (!empty($bank_account_id_selected)) {
     // Mark as default
     if (!empty($pane_values['payment_details']['bank_account_onfile_instance_default'])) {
-      commerce_bank_account_file_set_default_bank_account($bank_account_id_selected);
+      commerce_bank_account_onfile_set_default_bank_account($bank_account_id_selected);
+    }
+
+  }
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * Adds card on file options to the admin payment terminal for card on file
+ * enabled payment methods.
+ */
+function commerce_bank_account_onfile_form_commerce_payment_order_transaction_add_form_alter(&$form, &$form_state) {
+  $payment_method = isset($form_state['payment_method']) ? $form_state['payment_method']:null;
+  $form['payment_terminal']['#prefix'] = '<div id="payment-terminal-ajax-wrapper">';
+  $form['payment_terminal']['#suffix'] = '</div>';
+
+  if (!_commerce_bank_account_onfile_capable_payment_method_check($payment_method) ||
+    (isset($payment_method['settings']['bank_account_onfile']) && !$payment_method['settings']['bank_account_onfile'])) {
+    return;
+  }
+
+  // Load existing active cards for the payment method instance and user.
+  $stored_bank_accounts = commerce_bank_account_onfile_load_multiple_by_uid($form_state['order']->uid, $payment_method['instance_id']);
+
+  // Build options form
+  $bank_account_options_form = array();
+  $instance_default_bank_account_id = NULL;
+
+  // If have stored accounts ...
+  if (!empty($stored_bank_accounts)) {
+
+    $valid_bank_accounts = $stored_bank_accounts;
+    // If have un-expired bank accounts ...
+    if (!empty($valid_bank_accounts)) {
+      // get options list with labels
+      $bank_account_options = commerce_bank_account_onfile_element_options_list($valid_bank_accounts);
+
+      // determine default option
+      $bank_account_options_default_value = key($bank_account_options);
+      foreach (array_keys($bank_account_options) as $bank_account_id) {
+        if (isset($valid_bank_accounts[$bank_account_id]) && !empty($valid_bank_accounts[$bank_account_id]->instance_default)) {
+          $bank_account_options_default_value = $instance_default_bank_account_id = $bank_account_id;
+
+          // move instance default to the top of the list
+          $bank_account_label = $bank_account_options[$bank_account_id];
+          unset($bank_account_options[$bank_account_id]);
+          $bank_account_options = array($bank_account_id => $bank_account_label) + $bank_account_options;
+          break;
+        }
+      }
+
+      $bank_account_options_form = array(
+        '#type' => variable_get('commerce_bank_account_onfile_selector', 'radios'),
+        '#title' => t('Select a stored bank account'),
+        '#options' => $bank_account_options,
+        '#default_value' => $bank_account_options_default_value,
+        '#weight' => -10,
+        '#ajax' => array(
+          'callback' => 'commerce_bank_account_onfile_payment_terminal_ajax',
+          'wrapper' => 'payment-terminal-ajax-wrapper',
+        ),
+      );
     }
+  }
+  // update form with options
+  if (!empty($bank_account_options_form)) {
+    $form['payment_terminal']['payment_details']['bank_account_onfile'] = $bank_account_options_form;
 
+    // If the current value for the bank account selection element is not to use
+    // a different bank account, then hide the bank account form elements.
+    if (empty($form_state['values']['payment_details']['bank_account_onfile']) ||
+      $form_state['values']['payment_details']['bank_account_onfile'] !== 'new') {
+      $form['payment_terminal']['payment_details']['echeck']['#access'] = FALSE;
+    }
+  }
+  else {
+    $form['payment_terminal']['payment_details']['bank_account_onfile'] = array(
+      '#type' => 'value',
+      '#value' => 'new',
+    );
   }
 }
 
+/**
+ * Ajax callback for payment terminal card on file operations.
+ */
+function commerce_bank_account_onfile_payment_terminal_ajax($form, &$form_state) {
+  return $form['payment_terminal'];
+}
+
 
 /**
  * Returns TRUE if a payment method is capable of bank account on file
@@ -528,10 +673,10 @@ function _commerce_bank_account_onfile_capable_payment_method_check($payment_met
  * @return
  *   An array of bank account entities indexed by bank_account_id.
  */
-function commerce_bank_account_file_load_multiple_by_uid($uid, $instance_id = NULL, $default_only = FALSE) {
+function commerce_bank_account_onfile_load_multiple_by_uid($uid, $instance_id = NULL, $default_only = FALSE) {
   $query = new EntityFieldQuery();
   $query
-    ->entityCondition('entity_type', 'commerce_bank_account_file')
+    ->entityCondition('entity_type', 'commerce_bank_account_onfile')
     ->propertyCondition('uid', $uid)
     ->propertyCondition('status', 1);
   if (!empty($instance_id)) {
@@ -542,8 +687,8 @@ function commerce_bank_account_file_load_multiple_by_uid($uid, $instance_id = NU
   }
   $result = $query->execute();
 
-  if (isset($result['commerce_bank_account_file'])) {
-    return commerce_bank_account_file_load_multiple(array_keys($result['commerce_bank_account_file']));
+  if (isset($result['commerce_bank_account_onfile'])) {
+    return commerce_bank_account_onfile_load_multiple(array_keys($result['commerce_bank_account_onfile']));
   }
   else {
     return array();
@@ -551,6 +696,36 @@ function commerce_bank_account_file_load_multiple_by_uid($uid, $instance_id = NU
 }
 
 /**
+ * Loads a bank account belonging to a specific user by account number.
+ *
+ * Only active bank accounts (status = 1) are returned.
+ *
+ * @param $acct_num
+ *   The last four digits of the bank account number of the account to be returned.
+ * @param $uid
+ *   The uid of the bank account owner.
+ * @param $instance_id
+ *   (Optional) The instance id of the payment method.
+ * @param $default_only
+ *   Whether to load only default bank accounts. A user can have one default bank account per
+ *   payment method used. Defaults to FALSE.
+ *
+ * @return
+ *   An bank account entities indexed or FALSE if none are found.
+ */
+function commerce_bank_account_onfile_load_by_acct($acct_num, $uid, $instance_id = NULL, $default_only = FALSE) {
+  $accounts = commerce_bank_account_onfile_load_multiple_by_uid($uid, $instance_id, $default_only);
+
+  foreach($accounts as $account_id => $account) {
+    if ($account->bank_account_number == $acct_num) {
+      return $account;
+    }
+  }
+
+  return FALSE;
+}
+
+/**
  * Returns an options array for selecting a bank account on file during checkout
  *
  * @param $stored_bank_accounts
@@ -559,13 +734,11 @@ function commerce_bank_account_file_load_multiple_by_uid($uid, $instance_id = NU
  * @return
  *   An options array for selecting a bank account on file.
  */
-function commerce_bank_account_file_element_options_list($stored_bank_accounts) {
+function commerce_bank_account_onfile_element_options_list($stored_bank_accounts) {
   $options = array();
   foreach ($stored_bank_accounts as $bank_account_id => $bank_account) {
     $replacements = array(
       '@bank_account' => $bank_account->label(),
-      // '@card_exp_month' => str_pad($bank_account->card_exp_month, 2, '0', STR_PAD_LEFT),
-      // '@card_exp_year' => $bank_account->card_exp_year,
     );
     $options[$bank_account_id] = t('@bank_account', $replacements);
   }
@@ -581,8 +754,8 @@ function commerce_bank_account_file_element_options_list($stored_bank_accounts)
  * @deprecated
  *   Set $bank_account->instance_default directly instead.
  */
-function commerce_bank_account_file_set_default_bank_account($bank_account_id) {
-  if ($bank_account = commerce_bank_account_file_load($bank_account_id)) {
+function commerce_bank_account_onfile_set_default_bank_account($bank_account_id) {
+  if ($bank_account = commerce_bank_account_onfile_load($bank_account_id)) {
     $bank_account->instance_default = 1;
     return $bank_account->save();
   }
@@ -603,7 +776,7 @@ function commerce_bank_account_file_set_default_bank_account($bank_account_id) {
  * @return
  *   An array of callback function names keyed by payment method id
  */
-function commerce_bank_account_file_payment_method_implements($callback) {
+function commerce_bank_account_onfile_payment_method_implements($callback) {
   $cache = &drupal_static(__FUNCTION__);
 
   if (!isset($cache)) {
@@ -611,8 +784,8 @@ function commerce_bank_account_file_payment_method_implements($callback) {
 
     // get payment methods and load module implements for hook_commerce_payment_method_info()
     // so that bank_account_onfile callback can be in same hook file, ie mymodule.commerce.inc
-    $payment_methods = _commerce_bank_account_file_capable_payment_methods();
-    $available_callbacks = commerce_bank_account_file_payment_method_available_callbacks();
+    $payment_methods = _commerce_bank_account_onfile_capable_payment_methods();
+    $available_callbacks = commerce_bank_account_onfile_payment_method_available_callbacks();
     foreach ($payment_methods as $method_id => $payment_method) {
       // Load include file if specified.
       if (!empty($payment_method['file'])) {
@@ -638,7 +811,7 @@ function commerce_bank_account_file_payment_method_implements($callback) {
  * @return
  *  An associative array of payment method objects keyed by the method_id.
  */
-function _commerce_bank_account_file_capable_payment_methods() {
+function _commerce_bank_account_onfile_capable_payment_methods() {
   $capable_methods = array();
   $payment_methods = commerce_payment_methods();
   foreach ($payment_methods as $method_id => $payment_method) {
@@ -653,7 +826,7 @@ function _commerce_bank_account_file_capable_payment_methods() {
 /**
  * Returns an array of all available payment method bank_account_onfile callbacks
  */
-function commerce_bank_account_file_payment_method_available_callbacks() {
+function commerce_bank_account_onfile_payment_method_available_callbacks() {
   return array(
     'create callback',
     'create form callback',
@@ -726,29 +899,17 @@ function _commerce_bank_account_onfile_payment_method_instances($method_id, $inc
 }
 
 /**
- * Implements hook_theme().
- */
-function commerce_bank_account_onfile_theme() {
-  return array(
-    'bank_account_add_list' => array(
-      'variables' => array('content' => array()),
-      'file' => 'includes/commerce_bank_account_onfile.pages.inc',
-    ),
-  );
-}
-
-/**
  * Determines if the user can create a bank account file on any payment method.
  *
  * @param $account
  *   The user account for which access should be checked.
  */
 function commerce_bank_account_onfile_add_any_access($account) {
-  $create_implements = commerce_bank_account_file_payment_method_implements('create callback');
+  $create_implements = commerce_bank_account_onfile_payment_method_implements('create callback');
   foreach ($create_implements as $method_id => $method_function) {
     $payment_method_instances = _commerce_bank_account_onfile_payment_method_instances($method_id);
     foreach ($payment_method_instances as $instance_id => $payment_method) {
-      $new_bank_account = commerce_bank_account_file_new(array(
+      $new_bank_account = commerce_bank_account_onfile_new(array(
         'instance_id' => $instance_id,
         'payment_method' => $payment_method['method_id'],
       ));
@@ -858,6 +1019,22 @@ function commerce_bank_account_onfile_access($op = 'view', $bank_account = NULL,
 }
 
 /**
+ * Implements hook_commerce_customer_profile_can_delete().
+ *
+ * Don't allow a customer profile to be deleted (and trigger duplication)
+ * if it's referenced by a card.
+ */
+function commerce_bank_account_onfile_commerce_customer_profile_can_delete($profile) {
+  $query = new EntityFieldQuery();
+  $query
+    ->entityCondition('entity_type', 'commerce_bank_account_onfile')
+    ->fieldCondition('commerce_bankonfile_profile', 'profile_id', $profile->profile_id)
+    ->count();
+  $count = $query->execute();
+  return $count == 0;
+}
+
+/**
  * Returns the Bank Account on File callback function for the given payment method.
  *
  * @param $payment_method
@@ -877,7 +1054,7 @@ function commerce_bank_account_onfile_access($op = 'view', $bank_account = NULL,
  */
 function commerce_bank_account_onfile_payment_method_callback($payment_method, $callback) {
   if (!empty($payment_method) && !empty($payment_method['method_id'])) {
-    $implements = commerce_bank_account_file_payment_method_implements($callback);
+    $implements = commerce_bank_account_onfile_payment_method_implements($callback);
     if (!empty($implements) && !empty($implements[$payment_method['method_id']])) {
       return $implements[$payment_method['method_id']];
     }
@@ -885,6 +1062,65 @@ function commerce_bank_account_onfile_payment_method_callback($payment_method, $
 }
 
 /**
+ * Implements hook_commerce_order_status_info().
+ */
+function commerce_bank_account_onfile_commerce_order_status_info() {
+  $order_statuses = array();
+  $order_statuses['bank_account_onfile_payment_failed_soft_decline'] = array(
+    'name' => 'bank_account_onfile_payment_failed_soft_decline',
+    'title' => t('Payment failed (soft decline) - BAOF'),
+    'state' => 'pending',
+    'weight' => -2,
+  );
+  $order_statuses['bank_account_onfile_payment_error_hard_decline'] = array(
+    'name' => 'bank_account_onfile_payment_error_hard_decline',
+    'title' => t('Payment failed (hard decline) - BAOF'),
+    'state' => 'pending',
+    'weight' => -3,
+  );
+
+  return $order_statuses;
+}
+
+/**
+ * Implements hook_hook_info().
+ */
+function commerce_bank_account_onfile_hook_info() {
+  $base_info = array(
+    'group' => 'commerce',
+  );
+
+  $hooks = array(
+    'commerce_bank_account_onfile_insert' => $base_info,
+    'commerce_bank_account_onfile_update' => $base_info,
+    'commerce_bank_account_onfile_delete' => $base_info,
+    'commerce_bank_account_onfile_chargeable_accounts' => $base_info,
+    'commerce_bank_account_onfile_checkout_pane_form_alter' => $base_info,
+  );
+
+  return $hooks;
+}
+
+/**
+ * Implements hook_views_api().
+ */
+function commerce_bank_account_onfile_views_api() {
+  return array(
+    'api' => 3,
+    'path' => drupal_get_path('module', 'commerce_bank_account_onfile') . '/includes/views',
+  );
+}
+
+/**
+ * Implement hook_views_pre_render().
+ */
+function commerce_bank_account_onfile_views_pre_render(&$view) {
+  if (in_array($view->name, array('commerce_bank_account_on_file'))) {
+    drupal_add_css(drupal_get_path('module', 'commerce_bank_account_onfile') . '/theme/commerce_bank_account_onfile.view.css');
+  }
+}
+
+/**
  * Implements hook_permission().
  */
 function commerce_bank_account_onfile_permission() {
@@ -939,7 +1175,7 @@ function commerce_bank_account_onfile_order_select_bank_account($order, $forced_
     'message' => 'No chargeable bank account on file is available for the order @order_id and user @uid.',
   );
 
-  if ($possible_bank_accounts = commerce_bank_account_file_load_multiple_by_uid($order->uid, $forced_instance_id, TRUE)) {
+  if ($possible_bank_accounts = commerce_bank_account_onfile_load_multiple_by_uid($order->uid, $forced_instance_id, TRUE)) {
     // Determine any chargeable bank accounts ...
     $unchargeable_bank_accounts = array();
     foreach ($possible_bank_accounts as $bank_account_id => $possible_bank_account) {
@@ -959,10 +1195,6 @@ function commerce_bank_account_onfile_order_select_bank_account($order, $forced_
       // check first unchargeable bank account for expiration
       $unchargeanble_bank_account_top = reset($unchargeable_bank_accounts);
       $response['bank_account_chosen'] = $unchargeanble_bank_account_top;
-      // if (!commerce_cardonfile_validate_card_expiration($unchargeanble_bank_account_top)) {
-      //   $response['code'] = COMMERCE_COF_PROCESS_CODE_CARD_EXPIRED;
-      //   $response['message'] = 'Card on file has expired for user @uid\'s card @bank_account_id when attempting to process order @order_id.';
-      // }
     }
   }
 
@@ -998,7 +1230,7 @@ function commerce_bank_account_onfile_charge_bank_account_response_property_info
     ),
     'bank_account_chosen' => array(
       'label' => t('Bank Account Chosen'),
-      'type' => 'commerce_bank_account_file',
+      'type' => 'commerce_bank_account_onfile',
     ),
   );
 
@@ -1113,10 +1345,6 @@ function commerce_bank_account_onfile_get_properties($item, array $options, $nam
   switch ($name) {
     case 'bank_account_number':
       return commerce_bank_account_onfile_format_bank_account_number($item->bank_account_number, $item->bank_account_type);
-    // case 'card_exp':
-    //   $month = str_pad($item->card_exp_month, 2, '0', STR_PAD_LEFT);
-    //   $year = $item->card_exp_year;
-    //   return format_string('@month/@year', array('@month' => $month, '@year' => $year));
   }
 }
 
@@ -1124,30 +1352,20 @@ function commerce_bank_account_onfile_get_properties($item, array $options, $nam
  * Formats a truncated bank account number for display.
  *
  * While most bank accounts have 12 numbers, some like have
- * less or more.
+ * less or more. Shortening to 8 to match displays from Authorize.net.
  *
  * @param $bank_account_number
  *   Truncated bank account number (last 4 digits).
- * @param $bank_account_type
- *   The bank account type.
  *
  * @return
  *   The formatted bank account number with unknown numbers represented by an X.
  */
-function commerce_bank_account_onfile_format_bank_account_number($bank_account_number, $bank_account_type) {
-  // $special_lengths = array(
-  //   'amex' => 15,
-  //   'cb' => 14,
-  //   'dc' => 14,
-  // );
-  // $length = isset($special_lengths[$card_type]) ? $special_lengths[$card_type] : 16;
+function commerce_bank_account_onfile_format_bank_account_number($bank_account_number) {
   // Consider more meaning full logic.
-  $length = 12;
+  $length = 8;
 
   $padded_number = str_pad($bank_account_number, $length, 'X', STR_PAD_LEFT);
-  // $new_number = chunk_split($padded_number, 4, '-');
-  // chunk_split() returns an extra '-' at the end of the number, ignore it.
-  // return substr($new_number, 0, -1);
+
   return $padded_number;
 }
 
@@ -1220,15 +1438,8 @@ function commerce_bank_account_onfile_order_charge_bank_account($order, $charge
   }
   $response['bank_account_chosen'] = $bank_account;
   if (!commerce_bank_account_onfile_order_can_charge_bank_account($order, $bank_account)) {
-    // check for expiration to set a specific code
-    // if (!commerce_cardonfile_validate_card_expiration($bank_account)) {
-    //   $response['code'] = COMMERCE_COF_PROCESS_CODE_CARD_EXPIRED;
-    //   $response['message'] = 'Card on file has expired for user @uid\'s card @bank_account_id when attempting to process order @order_id.';
-    // }
-    // else {
-      $response['code'] = COMMERCE_BAOF_PROCESS_CODE_BANK_ACCOUNT_NOT_CHARGEABLE;
-      $response['message'] = 'Bank account provided cannot be charged for the order @order_id and user @uid.';
-    // }
+    $response['code'] = COMMERCE_BAOF_PROCESS_CODE_BANK_ACCOUNT_NOT_CHARGEABLE;
+    $response['message'] = 'Bank account provided cannot be charged for the order @order_id and user @uid.';
     return $response;
   }
 
@@ -1366,11 +1577,6 @@ function commerce_bank_account_onfile_can_charge($bank_account) {
     return FALSE;
   }
 
-  // DENY if card is expired
-  // if (!commerce_cardonfile_validate_card_expiration($bank_account)) {
-  //   return FALSE;
-  // }
-
   // load payment method related to the bank account
   $payment_method = commerce_payment_method_instance_load($bank_account->instance_id);
 
@@ -1399,23 +1605,3 @@ function commerce_bank_account_onfile_statuses() {
   return $statuses;
 }
 
-/**
- * Implements hook_commerce_order_status_info().
- */
-function commerce_bank_account_onfile_commerce_order_status_info() {
-  $order_statuses = array();
-  $order_statuses['bank_account_onfile_payment_failed_soft_decline'] = array(
-    'name' => 'bank_account_onfile_payment_failed_soft_decline',
-    'title' => t('Payment failed (soft decline) - BAOF'),
-    'state' => 'pending',
-    'weight' => -2,
-  );
-  $order_statuses['bank_account_onfile_payment_error_hard_decline'] = array(
-    'name' => 'bank_account_onfile_payment_error_hard_decline',
-    'title' => t('Payment failed (hard decline) - BAOF'),
-    'state' => 'pending',
-    'weight' => -3,
-  );
-
-  return $order_statuses;
-}
diff --git a/commerce_bank_account_onfile.rules.inc b/commerce_bank_account_onfile.rules.inc
index 6f3cbb8..b4128d7 100644
--- a/commerce_bank_account_onfile.rules.inc
+++ b/commerce_bank_account_onfile.rules.inc
@@ -32,7 +32,7 @@ function commerce_bank_account_onfile_rules_event_info() {
     'access callback' => 'commerce_order_rules_access',
     'variables' => array(
       'bank_account_data' => array(
-        'type' => 'commerce_bank_account_file',
+        'type' => 'commerce_bank_account_onfile',
         'label' => t('Bank Account'),
         'optional' => TRUE,
       ),
@@ -57,7 +57,7 @@ function commerce_bank_account_onfile_rules_event_info() {
     'access callback' => 'commerce_order_rules_access',
     'variables' => array(
       'bank_account_data' => array(
-        'type' => 'commerce_bank_account_file',
+        'type' => 'commerce_bank_account_onfile',
         'label' => t('Bank Account'),
       ),
       'order' => array(
@@ -132,7 +132,7 @@ function commerce_bank_account_onfile_rules_action_info() {
         'optional' => TRUE,
       ),
       'bank_account_data' => array(
-        'type' => 'commerce_bank_account_file',
+        'type' => 'commerce_bank_account_onfile',
         'label' => t('Bank Account'),
         'description' => t('If provided, Select Bank Account Response will be ignored.'),
         'optional' => TRUE,
@@ -166,7 +166,7 @@ function commerce_bank_account_onfile_rules_action_info() {
  */
 function commerce_bank_account_onfile_rules_payment_instance_charge_options_list() {
   $options = array('' => t('- None -'));
-  $implements = commerce_bank_account_file_payment_method_implements('charge callback');
+  $implements = commerce_bank_account_onfile_payment_method_implements('charge callback');
 
 
   foreach ($implements as $method_id => $method_function) {
diff --git a/includes/commerce_bank_account_onfile.admin.inc b/includes/commerce_bank_account_onfile.admin.inc
index e0a704c..f88cd2c 100644
--- a/includes/commerce_bank_account_onfile.admin.inc
+++ b/includes/commerce_bank_account_onfile.admin.inc
@@ -34,12 +34,13 @@ function commerce_bank_account_onfile_settings_form($form, &$form_state) {
   );
 
   // TODO: Not used anywhere. Use it.
-  // $form['commerce_cardonfile_multiple'] = array(
-  //   '#type' => 'checkbox',
-  //   '#title' => t('Allow customers to store more than one card on file if it is supported by your payment gateway.'),
-  //   '#default_value' => variable_get('commerce_cardonfile_multiple', TRUE),
-  // );
+  $form['commerce_bank_account_onfile_multiple'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Allow customers to store more than one account on file if it is supported by your payment gateway.'),
+    '#default_value' => variable_get('commerce_bank_account_onfile_multiple', TRUE),
+  );
 
+  // TODO: Not used anywhere. Use it.
   // $form['commerce_cardonfile_hide_cc_radio_button'] = array(
   //   '#type' => 'checkbox',
   //   '#title' => t('If credit card is the only available payment method, hide its radio button if the customer has a card on file.'),
diff --git a/includes/commerce_bank_account_onfile.entity.inc b/includes/commerce_bank_account_onfile.entity.inc
index eeabcd6..2097d07 100644
--- a/includes/commerce_bank_account_onfile.entity.inc
+++ b/includes/commerce_bank_account_onfile.entity.inc
@@ -1,7 +1,7 @@
 <?php
 
 /**
- * Entity class representing the commerce_bank_account_file entity type.
+ * Entity class representing the commerce_bank_account_onfile entity type.
  */
 class CommerceBankAccountFile extends Entity {
 
@@ -118,7 +118,7 @@ class CommerceBankAccountFile extends Entity {
   public $changed;
 
   public function __construct($values = array()) {
-    parent::__construct($values, 'commerce_bank_account_file');
+    parent::__construct($values, 'commerce_bank_account_onfile');
   }
 
   /**
@@ -154,15 +154,7 @@ class CommerceBankAccountFile extends Entity {
       $this->created = REQUEST_TIME;
     }
     if ($this->bank_account_id) {
-      $this->original = $original = entity_load_unchanged('commerce_bank_account_file', $this->bank_account_id);
-      // Reactivate a declined bank account after its expiration date has been modified.
-      // if ($this->status == 3) {
-      //   $exp_month_changed = $this->original->card_exp_month != $this->card_exp_month;
-      //   $exp_year_changed = $this->original->card_exp_year != $this->card_exp_year;
-      //   if ($exp_month_changed || $exp_year_changed) {
-      //     $this->status = 1;
-      //   }
-      // }
+      $this->original = $original = entity_load_unchanged('commerce_bank_account_onfile', $this->bank_account_id);
     }
 
     // Perform the save.
@@ -173,18 +165,18 @@ class CommerceBankAccountFile extends Entity {
     $value_changed = $is_update && $this->instance_default != $original->instance_default;
     if ($this->instance_default && (!$is_update || $value_changed)) {
       $query = new EntityFieldQuery();
-      $query->entityCondition('entity_type', 'commerce_bank_account_file');
+      $query->entityCondition('entity_type', 'commerce_bank_account_onfile');
       $query->entityCondition('entity_id', $this->bank_account_id, '<>');
       $query->propertyCondition('instance_id', $this->instance_id);
       $query->propertyCondition('uid', $this->uid);
       $query->propertyCondition('instance_default', TRUE);
       $result = $query->execute();
-      if (isset($result['commerce_echek_file'])) {
-        $bank_account_ids = array_keys($result['commerce_bank_account_file']);
-        $other_accounts = commerce_bank_account_file_load_multiple($bank_account_ids);
+      if (isset($result['commerce_bank_account_onfile'])) {
+        $bank_account_ids = array_keys($result['commerce_bank_account_onfile']);
+        $other_accounts = commerce_bank_account_onfile_load_multiple($bank_account_ids);
         foreach ($other_accounts as $other_account) {
           $other_account->instance_default = 0;
-          commerce_bank_account_file_save($other_account);
+          commerce_bank_account_onfile_save($other_account);
         }
       }
     }
diff --git a/includes/commerce_bank_account_onfile.extra_fields_controller.inc b/includes/commerce_bank_account_onfile.extra_fields_controller.inc
index ee74ee6..e718c71 100644
--- a/includes/commerce_bank_account_onfile.extra_fields_controller.inc
+++ b/includes/commerce_bank_account_onfile.extra_fields_controller.inc
@@ -11,7 +11,7 @@ class CommerceBankAccountFileExtraFieldsController extends EntityDefaultExtraFie
    * Implements EntityExtraFieldsControllerInterface::fieldExtraFields().
    */
   public function fieldExtraFields() {
-  $properties = array('bank_account_type', 'bank_account_name', 'bank_account_number', 'aba_code'/*, 'card_exp'*/);
+  $properties = array('bank_account_type', 'bank_account_name', 'bank_account_number', 'aba_code');
 
     $extra = array();
     foreach ($properties as $index => $name) {
diff --git a/includes/commerce_bank_account_onfile.pages.inc b/includes/commerce_bank_account_onfile.pages.inc
index fa310cf..b5c5a1f 100644
--- a/includes/commerce_bank_account_onfile.pages.inc
+++ b/includes/commerce_bank_account_onfile.pages.inc
@@ -8,7 +8,7 @@
  */
 function commerce_bank_account_onfile_add_page() {
   $account = menu_get_object('user');
-  $create_implements = commerce_bank_account_file_payment_method_implements('create callback');
+  $create_implements = commerce_bank_account_onfile_payment_method_implements('create callback');
   $content = array();
   foreach ($create_implements as $method_id => $method_function) {
     $payment_method_instances = _commerce_bank_account_onfile_payment_method_instances($method_id);
@@ -21,7 +21,7 @@ function commerce_bank_account_onfile_add_page() {
     }
   }
 
-  // Bypass the user/%user/cards/add listing if only one payment method is
+  // Bypass the user/%user/bank-accounts/add listing if only one payment method is
   // available.
   if (count($content) == 1) {
     $item = array_shift($content);
@@ -51,7 +51,6 @@ function theme_bank_account_add_list($variables) {
  */
 function commerce_bank_account_onfile_bank_account_form_page($op, $bank_account, $account) {
   $payment_method = commerce_payment_method_instance_load($bank_account->instance_id);
-
   if ($op == 'update') {
     // This is not likely to happen, but if the payment method doesn't implement
     // the update callback, redirect the user back to the bank account listing page and
@@ -73,68 +72,179 @@ function commerce_bank_account_onfile_bank_account_form_page($op, $bank_account,
     return drupal_get_form($form_callback, $op, $bank_account);
   }
   else {
-    return drupal_get_form('commerce_cardonfile_card_form', $op, $bank_account);
+    return drupal_get_form('commerce_bank_account_onfile_account_form', $op, $bank_account);
   }
 }
 
 /**
  * Form callback: create or edit a bank account.
  */
-/*
-function commerce_cardonfile_card_form($form, &$form_state, $op, $bank_account) {
+
+function commerce_bank_account_onfile_account_form($form, &$form_state, $op, $bank_account) {
   // Load the credit card helper functions from the Payment module.
-  // module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
+  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.echeck');
 
   // Pass along information to the validate and submit handlers.
   $form_state['bank_account'] = $bank_account;
   $form_state['op'] = $op;
 
   $fields = array(
-    'owner' => '',
-    'code' => '',
+    'aba_code' => '',
+    'acct_num' => '',
     'type' => array(
-      'visa',
-      'mastercard',
-      'amex',
+      'checking',
+      'savings',
+      'business_checking',
     ),
+    'bank_name' => '',
+    'acct_name' => '',
   );
   $defaults = array();
   if ($op == 'update') {
-    unset($fields['code']);
     $defaults = array(
+      'aba_code' => $bank_account->aba_code,
+      'acct_num' => $bank_account->bank_account_number,
       'type' => $bank_account->bank_account_type,
-      'owner' => $bank_account->bank_account_name,
-      'exp_month' => $card->card_exp_month,
-      'exp_year' => $card->card_exp_year,
+      'bank_name' => $bank_account->bank_name,
+      'acct_name' => $bank_account->bank_account_name,
     );
   }
-  $form += commerce_payment_credit_card_form($fields, $defaults);
-  if ($op == 'update') {
-    $form['credit_card']['type']['#access'] = FALSE;
-    $form['credit_card']['number']['#access'] = FALSE;
+  $form += commerce_payment_echeck_form($fields, $defaults);
+
+  if (!empty($bank_account->commerce_bankonfile_profile)) {
+    $profile = commerce_customer_profile_load($bank_account->commerce_bankonfile_profile[LANGUAGE_NONE][0]['profile_id']);
+  }
+  else if (module_exists('commerce_addressbook')) {
+    $profile_id = commerce_addressbook_get_default_profile_id($form_state['bank_account']->uid, 'billing');
+    if ($profile_id) {
+      $profile = commerce_customer_profile_load($profile_id);
+    }
+  }
+  if (!isset($profile)) {
+    // Create a billing profile object and add the address form.
+   $profile = commerce_customer_profile_new('billing', $form_state['bank_account']->uid);
+  }
+  else {
+    drupal_set_message(t('Billing information has been prefilled with the data from your default billing profile.'));
   }
 
+  $form_state['commerce_customer_profile'] = $profile;
+  $form['commerce_customer_profile'] = array();
+  field_attach_form('commerce_customer_profile', $profile, $form['commerce_customer_profile'], $form_state);
+
+  $form['commerce_customer_profile']['#weight'] = -1;
+
   $payment_method = commerce_payment_method_load($bank_account->payment_method);
-  $form['credit_card']['cardonfile_instance_default'] = array(
+  $form['bank_account']['bank_account_instance_default'] = array(
     '#type' => 'checkbox',
-    '#title' => t('Use as default card for payments with %method', array('%method' => $payment_method['display_title'])),
+    '#title' => t('Use as default account for payments with %method', array('%method' => $payment_method['display_title'])),
     '#default_value' => FALSE,
   );
   // Disable the checkbox if we are adding a new card and the user doesn't have
   // any other active cards with the same instance ID. Also disable it, if we
   // are updating the current default card, so the user can't uncheck the
   // checkbox.
-  $existing_bank_accounts = commerce_cardonfile_load_multiple_by_uid($bank_account->uid, $bank_account->instance_id, TRUE);
+  $existing_bank_accounts = commerce_bank_account_onfile_load_multiple_by_uid($bank_account->uid, $bank_account->instance_id, TRUE);
   if (($op == 'create' && !$existing_bank_accounts) || ($op == 'update' && $bank_account->instance_default)) {
-    $form['credit_card']['cardonfile_instance_default']['#default_value'] = TRUE;
-    $form['credit_card']['cardonfile_instance_default']['#access'] = FALSE;
+    $form['bank_account']['bank_account_instance_default']['#default_value'] = TRUE;
+    $form['bank_account']['bank_account_instance_default']['#access'] = FALSE;
   }
 
   $form['submit'] = array(
     '#type' => 'submit',
-    '#value' => ($op == 'create') ? t('Add card') : t('Update card'),
-    '#suffix' => l(t('Cancel'), 'user/' . $bank_account->uid . '/cards'),
+    '#value' => ($op == 'create') ? t('Add bank account') : t('Update bank account'),
+    '#suffix' => l(t('Cancel'), 'user/' . $bank_account->uid . '/bank-accounts'),
   );
-
   return $form;
-}*/
+}
+
+/**
+ * Submit callback for commerce_bank_account_onfile_account_form().
+ */
+function commerce_bank_account_onfile_account_form_submit($form, &$form_state) {
+  $op = $form_state['op'];
+  $bank_account = $form_state['bank_account'];
+
+  $bank_account->bank_account_type = $form_state['values']['echeck']['type'];
+  $bank_account->bank_name = $form_state['values']['echeck']['bank_name'];
+  $bank_account->bank_account_name = $form_state['values']['echeck']['acct_name'];
+
+  if ($op == 'create') {
+    $bank_account->bank_account_number = substr($form_state['values']['echeck']['acct_num'], -4);
+    $bank_account->aba_code = $form_state['values']['echeck']['aba_code'];
+  }
+  $bank_account->instance_default = $form_state['values']['bank_account_instance_default'];
+
+  // Invoke the payment method's card create/update callback.
+  $payment_method = commerce_payment_method_instance_load($bank_account->instance_id);
+  $callback = $payment_method['bank_account_onfile'][$op . ' callback'];
+  $success = FALSE;
+  if (function_exists($callback)) {
+    $callback_return = $callback($form, $form_state, $payment_method, $bank_account);
+    if ($callback_return) {
+      if ($op == 'create') {
+        $bank_account_save = $callback_return;
+        $confirm_message = t('A new bank account has been added.');
+      }
+      else {
+        $bank_account_save = $bank_account;
+        $confirm_message = t('The bank account has been updated.');
+      }
+      commerce_bank_account_onfile_save($bank_account_save);
+      drupal_set_message($confirm_message);
+      $success = TRUE;
+    }
+  }
+
+  if (!$success) {
+    if ($op == 'create') {
+      drupal_set_message(t('We encountered an error attempting to save your bank account data. Please try again and contact us if this error persists.'), 'error');
+    }
+    else {
+      drupal_set_message(t('We encountered an error attempting to update your bank account data. Please try again and contact us if this error persists.'), 'error');
+    }
+  }
+  $form_state['redirect'] = 'user/' . $bank_account->uid . '/bank-accounts';
+}
+
+/**
+ * Builds the form for deleting bank_account_onfile data.
+ */
+function commerce_bank_account_onfile_delete_form($form, &$form_state, $bank_account) {
+  $entity_view = entity_view('commerce_bank_account_onfile', array($bank_account), 'customer', NULL, TRUE);
+  $rendered_account = drupal_render($entity_view);
+
+  return confirm_form(
+    $form,
+    t('Are you sure you want to delete this bank account?'),
+    'user/' . $bank_account->uid . '/bank-accounts',
+    $rendered_account,
+    t('Delete')
+  );
+}
+
+/**
+ * Form submit handler: delete stored card data.
+ */
+function commerce_bank_account_onfile_delete_form_submit($form, &$form_state) {
+  $bank_account = $form_state['build_info']['args'][0];
+
+  // Invoke the payment method's bank account delete callback.
+  $payment_method = commerce_payment_method_instance_load($bank_account->instance_id);
+  $callback = $payment_method['bank_account_onfile']['delete callback'];
+  if (function_exists($callback)) {
+    if (!$callback($form, $form_state, $payment_method, $bank_account)) {
+      // Display a message if we failed to communicate properly with the payment
+      // gateway in the Bank Account on File delete callback.
+      drupal_set_message(t('We encountered an error attempting to delete your bank account data. Please try again and contact us if this error persists.'), 'error');
+      $form_state['redirect'] = 'user/' . $bank_account->uid . '/cards';
+      return;
+    }
+  }
+
+  // Delete the bank account.
+  commerce_bank_account_onfile_delete($bank_account->bank_account_id);
+
+  drupal_set_message(t('The bank account has been deleted.'));
+  $form_state['redirect'] = 'user/' . $bank_account->uid . '/bank-accounts';
+}
diff --git a/includes/views/commerce_bank_account_onfile.views.inc b/includes/views/commerce_bank_account_onfile.views.inc
new file mode 100644
index 0000000..f186c00
--- /dev/null
+++ b/includes/views/commerce_bank_account_onfile.views.inc
@@ -0,0 +1,48 @@
+<?php
+
+/**
+ * @file
+ * Views integration for Commerce Card on File.
+ */
+
+/**
+ * Implements hook_views_data_alter().
+ */
+function commerce_bank_account_onfile_views_data_alter(&$data) {
+  // Override the data generated by Entity API.
+  $data['commerce_bank_account_onfile']['bank_account_number']['field']['handler'] = 'commerce_bank_account_onfile_handler_field_bank_account_number';
+  $data['commerce_bank_account_onfile']['aba_code']['field']['handler'] = 'commerce_bank_account_onfile_handler_field_aba_code';
+  $data['commerce_bank_account_onfile']['instance_default']['filter']['label'] = t('Default');
+  $data['commerce_bank_account_onfile']['instance_default']['filter']['type'] = 'yes-no';
+
+  $data['commerce_bank_account_onfile']['label'] = array(
+    'field' => array(
+      'title' => t('Label'),
+      'help' => t('Display the bank account label.'),
+      'handler' => 'commerce_bank_account_onfile_handler_field_acct_label',
+    ),
+  );
+  $data['commerce_bank_account_onfile']['operations'] = array(
+    'field' => array(
+      'title' => t('Operations links'),
+      'help' => t('Display all the available operations links for the bank accounts.'),
+      'handler' => 'commerce_bank_account_onfile_handler_field_account_operations',
+    ),
+  );
+
+  return $data;
+}
+
+/**
+ * Implements hook_views_plugins().
+ */
+function commerce_bank_account_onfile_views_plugins() {
+  return array(
+    'argument validator' => array(
+      'commerce_bof_owner' => array(
+        'title' => t('Bank account owner access'),
+        'handler' => 'commerce_bank_account_onfile_plugin_argument_validate_owner',
+      ),
+    ),
+  );
+}
diff --git a/includes/views/commerce_bank_account_onfile.views_default.inc b/includes/views/commerce_bank_account_onfile.views_default.inc
new file mode 100644
index 0000000..564acd0
--- /dev/null
+++ b/includes/views/commerce_bank_account_onfile.views_default.inc
@@ -0,0 +1,157 @@
+<?php
+
+/**
+ * @file
+ * Views for the default Commerce Bank Account on File.
+ */
+
+/**
+ * Implements hook_views_default_views().
+ */
+function commerce_bank_account_onfile_views_default_views() {
+  $views = array();
+
+  // Stored bank accounts: User
+  $view = new view();
+  $view->name = 'commerce_bank_account_on_file';
+  $view->description = '';
+  $view->tag = 'Commerce';
+  $view->base_table = 'commerce_bank_account_onfile';
+  $view->human_name = 'Commerce Bank Account on File: User Accounts';
+  $view->core = 7;
+  $view->api_version = '3.0';
+  $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
+
+  /* Display: Master */
+  $handler = $view->new_display('default', 'Master', 'default');
+  $handler->display->display_options['title'] = 'Stored Accounts';
+  $handler->display->display_options['use_more_always'] = FALSE;
+  $handler->display->display_options['access']['type'] = 'perm';
+  $handler->display->display_options['access']['perm'] = 'view own bank account data';
+  $handler->display->display_options['cache']['type'] = 'none';
+  $handler->display->display_options['query']['type'] = 'views_query';
+  $handler->display->display_options['exposed_form']['type'] = 'basic';
+  $handler->display->display_options['pager']['type'] = 'full';
+  $handler->display->display_options['pager']['options']['items_per_page'] = '10';
+  $handler->display->display_options['style_plugin'] = 'table';
+  $handler->display->display_options['style_options']['columns'] = array(
+    'bank_account_name' => 'bank_account_name',
+    'bank_account_number' => 'bank_account_number',
+    'aba_code' => 'aba_code',
+    'bank_name' => 'bank_name',
+    'instance_default' => 'instance_default',
+    'commerce_bankonfile_profile' => 'commerce_bankonfile_profile',
+  );
+  $handler->display->display_options['style_options']['default'] = '-1';
+  $handler->display->display_options['style_options']['info'] = array(
+    'bank_account_name' => array(
+      'sortable' => 0,
+      'default_sort_order' => 'asc',
+      'align' => '',
+      'separator' => '',
+      'empty_column' => 0,
+    ),
+    'bank_account_number' => array(
+      'sortable' => 0,
+      'default_sort_order' => 'asc',
+      'align' => '',
+      'separator' => '',
+      'empty_column' => 0,
+    ),
+    'aba_code' => array(
+      'sortable' => 0,
+      'default_sort_order' => 'asc',
+      'align' => '',
+      'separator' => '',
+      'empty_column' => 0,
+    ),
+    'bank_name' => array(
+      'sortable' => 0,
+      'default_sort_order' => 'asc',
+      'align' => '',
+      'separator' => '',
+      'empty_column' => 0,
+    ),
+    'instance_default' => array(
+      'sortable' => 0,
+      'default_sort_order' => 'asc',
+      'align' => '',
+      'separator' => '',
+      'empty_column' => 0,
+    ),
+    'commerce_bankonfile_profile' => array(
+      'sortable' => 0,
+      'default_sort_order' => 'asc',
+      'align' => '',
+      'separator' => '',
+      'empty_column' => 0,
+    ),
+  );
+  /* No results behavior: Global: Text area */
+  $handler->display->display_options['empty']['area']['id'] = 'area';
+  $handler->display->display_options['empty']['area']['table'] = 'views';
+  $handler->display->display_options['empty']['area']['field'] = 'area';
+  $handler->display->display_options['empty']['area']['empty'] = TRUE;
+  $handler->display->display_options['empty']['area']['content'] = 'You have no accounts on file.';
+  $handler->display->display_options['empty']['area']['format'] = 'full_html';
+  /* Field: Commerce Bank Account File: Bank account name */
+  $handler->display->display_options['fields']['bank_account_name']['id'] = 'bank_account_name';
+  $handler->display->display_options['fields']['bank_account_name']['table'] = 'commerce_bank_account_onfile';
+  $handler->display->display_options['fields']['bank_account_name']['field'] = 'bank_account_name';
+  $handler->display->display_options['fields']['bank_account_name']['label'] = 'Account name';
+  /* Field: Commerce Bank Account File: Instance default */
+  $handler->display->display_options['fields']['instance_default']['id'] = 'instance_default';
+  $handler->display->display_options['fields']['instance_default']['table'] = 'commerce_bank_account_onfile';
+  $handler->display->display_options['fields']['instance_default']['field'] = 'instance_default';
+  $handler->display->display_options['fields']['instance_default']['label'] = 'Default';
+  $handler->display->display_options['fields']['instance_default']['not'] = 0;
+  /* Field: Commerce Bank Account File: Bank name */
+  $handler->display->display_options['fields']['bank_name']['id'] = 'bank_name';
+  $handler->display->display_options['fields']['bank_name']['table'] = 'commerce_bank_account_onfile';
+  $handler->display->display_options['fields']['bank_name']['field'] = 'bank_name';
+  /* Field: Commerce Bank Account File: Bank account number */
+  $handler->display->display_options['fields']['bank_account_number']['id'] = 'bank_account_number';
+  $handler->display->display_options['fields']['bank_account_number']['table'] = 'commerce_bank_account_onfile';
+  $handler->display->display_options['fields']['bank_account_number']['field'] = 'bank_account_number';
+  $handler->display->display_options['fields']['bank_account_number']['label'] = 'Account number';
+  $handler->display->display_options['fields']['bank_account_number']['format'] = 1;
+  /* Field: Commerce Bank Account File: Routing */
+  $handler->display->display_options['fields']['aba_code']['id'] = 'aba_code';
+  $handler->display->display_options['fields']['aba_code']['table'] = 'commerce_bank_account_onfile';
+  $handler->display->display_options['fields']['aba_code']['field'] = 'aba_code';
+  $handler->display->display_options['fields']['aba_code']['label'] = 'Routing number';
+  $handler->display->display_options['fields']['aba_code']['format'] = 1;
+  /* Field: Commerce Bank Account File: Operations links */
+  $handler->display->display_options['fields']['operations']['id'] = 'operations';
+  $handler->display->display_options['fields']['operations']['table'] = 'commerce_bank_account_onfile';
+  $handler->display->display_options['fields']['operations']['field'] = 'operations';
+  $handler->display->display_options['fields']['operations']['label'] = '';
+  $handler->display->display_options['fields']['operations']['element_label_colon'] = FALSE;
+  /* Contextual filter: Commerce Bank Account File: User uid */
+  $handler->display->display_options['arguments']['user']['id'] = 'user';
+  $handler->display->display_options['arguments']['user']['table'] = 'commerce_bank_account_onfile';
+  $handler->display->display_options['arguments']['user']['field'] = 'user';
+  $handler->display->display_options['arguments']['user']['default_action'] = 'default';
+  $handler->display->display_options['arguments']['user']['exception']['value'] = '';
+  $handler->display->display_options['arguments']['user']['default_argument_type'] = 'user';
+  $handler->display->display_options['arguments']['user']['default_argument_options']['user'] = FALSE;
+  $handler->display->display_options['arguments']['user']['summary']['number_of_records'] = '0';
+  $handler->display->display_options['arguments']['user']['summary']['format'] = 'default_summary';
+  $handler->display->display_options['arguments']['user']['summary_options']['items_per_page'] = '25';
+  $handler->display->display_options['arguments']['user']['specify_validation'] = TRUE;
+  $handler->display->display_options['arguments']['user']['validate']['type'] = 'commerce_bof_owner';
+
+  /* Display: Page */
+  $handler = $view->new_display('page', 'Page', 'page');
+  $handler->display->display_options['path'] = 'user/%/bank-accounts';
+  $handler->display->display_options['menu']['type'] = 'tab';
+  $handler->display->display_options['menu']['title'] = 'Stored Accounts';
+  $handler->display->display_options['menu']['description'] = 'Manage your stored bank account data.';
+  $handler->display->display_options['menu']['weight'] = '0';
+  $handler->display->display_options['menu']['name'] = 'management';
+  $handler->display->display_options['menu']['context'] = 0;
+
+  $views[$view->name] = $view;
+
+  return $views;
+}
diff --git a/includes/views/handlers/commerce_bank_account_onfile_handler_field_aba_code.inc b/includes/views/handlers/commerce_bank_account_onfile_handler_field_aba_code.inc
new file mode 100644
index 0000000..dd747a5
--- /dev/null
+++ b/includes/views/handlers/commerce_bank_account_onfile_handler_field_aba_code.inc
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * Field handler to present the truncated card number with optional formatting.
+ */
+class commerce_bank_account_onfile_handler_field_aba_code extends views_handler_field_entity {
+
+  function option_definition() {
+    $options = parent::option_definition();
+    $options['format'] = array('default' => TRUE);
+
+    return $options;
+  }
+
+  /**
+   * Provide the link to card option.
+   */
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+
+    $form['format'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Display formatted'),
+      '#description' => t('If the truncated aba number is "1234", the output will be "XXXX1234".'),
+      '#default_value' => $this->options['format'],
+    );
+  }
+
+  function get_value($values, $field = NULL) {
+    if (!isset($this->entities[$this->view->row_index])) {
+      return '';
+    }
+
+    $bank_account = $this->entities[$this->view->row_index];
+    if ($this->options['format']) {
+      return commerce_bank_account_onfile_format_bank_account_number($bank_account->aba_code);
+    }
+    else {
+      return $bank_account->aba_code;
+    }
+  }
+}
diff --git a/includes/views/handlers/commerce_bank_account_onfile_handler_field_acct_label.inc b/includes/views/handlers/commerce_bank_account_onfile_handler_field_acct_label.inc
new file mode 100644
index 0000000..69ba8df
--- /dev/null
+++ b/includes/views/handlers/commerce_bank_account_onfile_handler_field_acct_label.inc
@@ -0,0 +1,16 @@
+<?php
+
+/**
+ * Field handler to present the card label.
+ */
+class commerce_bank_account_onfile_handler_field_acct_label extends views_handler_field_entity {
+
+  function get_value($values, $field = NULL) {
+    if (!isset($this->entities[$this->view->row_index])) {
+      return '';
+    }
+
+    $bank_account = $this->entities[$this->view->row_index];
+    return $bank_account->label();
+  }
+}
diff --git a/includes/views/handlers/commerce_bank_account_onfile_handler_field_bank_account_number.inc b/includes/views/handlers/commerce_bank_account_onfile_handler_field_bank_account_number.inc
new file mode 100644
index 0000000..3a37c85
--- /dev/null
+++ b/includes/views/handlers/commerce_bank_account_onfile_handler_field_bank_account_number.inc
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * Field handler to present the truncated card number with optional formatting.
+ */
+class commerce_bank_account_onfile_handler_field_bank_account_number extends views_handler_field_entity {
+
+  function option_definition() {
+    $options = parent::option_definition();
+    $options['format'] = array('default' => TRUE);
+
+    return $options;
+  }
+
+  /**
+   * Provide the link to card option.
+   */
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+
+    $form['format'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Display formatted'),
+      '#description' => t('If the truncated bank number is "1234", the output will be "XXXX1234".'),
+      '#default_value' => $this->options['format'],
+    );
+  }
+
+  function get_value($values, $field = NULL) {
+    if (!isset($this->entities[$this->view->row_index])) {
+      return '';
+    }
+
+    $bank_account = $this->entities[$this->view->row_index];
+    if ($this->options['format']) {
+      return commerce_bank_account_onfile_format_bank_account_number($bank_account->bank_account_number);
+    }
+    else {
+      return $bank_account->bank_account_number;
+    }
+  }
+}
diff --git a/includes/views/handlers/commerce_bank_account_onfile_handler_field_operations.inc b/includes/views/handlers/commerce_bank_account_onfile_handler_field_operations.inc
new file mode 100644
index 0000000..2fe941b
--- /dev/null
+++ b/includes/views/handlers/commerce_bank_account_onfile_handler_field_operations.inc
@@ -0,0 +1,59 @@
+<?php
+
+/**
+ * Field handler to present a card data's operations links.
+ */
+class commerce_bank_account_onfile_handler_field_account_operations extends views_handler_field {
+  function construct() {
+    parent::construct();
+
+    $this->additional_fields['bank_account_id'] = 'bank_account_id';
+    $this->additional_fields['uid'] = 'uid';
+  }
+
+  function option_definition() {
+    $options = parent::option_definition();
+
+    $options['add_destination'] = TRUE;
+
+    return $options;
+  }
+
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+
+    $form['add_destination'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Add a destination parameter to operation links so users return to this View on form submission.'),
+      '#default_value' => $this->options['add_destination'],
+    );
+  }
+
+  function query() {
+    $this->ensure_my_table();
+    $this->add_additional_fields();
+  }
+
+  function render($values) {
+    $bank_account_id = $this->get_value($values, 'bank_account_id');
+    $uid = $this->get_value($values, 'uid');
+
+    // Get the operations links.
+    $links = menu_contextual_links('commerce-bank-account-onfile', 'user/' . $uid . '/bank-accounts', array($bank_account_id));
+
+    if (!empty($links)) {
+      // Add the destination to the links if specified.
+      if ($this->options['add_destination']) {
+        foreach ($links as $id => &$link) {
+          // Only include the destination for the edit and delete forms.
+          if (in_array($id, array('commerce-bank-account-onfile-edit', 'commerce-bank-account-onfile-delete'))) {
+            $link['query'] = drupal_get_destination();
+          }
+        }
+      }
+
+      drupal_add_css(drupal_get_path('module', 'commerce_bank_account_onfile') . '/theme/commerce_bank_account_onfile.admin.css');
+      return theme('links', array('links' => $links, 'attributes' => array('class' => array('links', 'inline', 'operations'))));
+    }
+  }
+}
diff --git a/includes/views/handlers/commerce_bank_account_onfile_plugin_argument_validate_owner.inc b/includes/views/handlers/commerce_bank_account_onfile_plugin_argument_validate_owner.inc
new file mode 100644
index 0000000..e8bbf08
--- /dev/null
+++ b/includes/views/handlers/commerce_bank_account_onfile_plugin_argument_validate_owner.inc
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * @file
+ * Argument plugin for a valid access to bank account data owner.
+ */
+
+/**
+ * Validate whether an argument is a valid user.
+ */
+class commerce_bank_account_onfile_plugin_argument_validate_owner extends views_plugin_argument_validate_user {
+  function validate_argument($argument) {
+    global $user;
+
+    // call parent validate for user id
+    $return = parent::validate_argument($argument);
+
+    // If allowed based on user id alone ...
+    if ($return) {
+      // Check bank account data access
+      $account_uid = $this->argument->argument;
+      $account = user_load($account_uid);
+
+      // DENY if account does not have bank account access
+      if (!commerce_bank_account_onfile_access('view', $account)) {
+        $return = FALSE;
+        $this->argument->argument = NULL;
+        $this->argument->validated_title = NULL;
+      }
+    }
+
+    return $return;
+  }
+}
diff --git a/theme/commerce_bank_account_onfile.view.css b/theme/commerce_bank_account_onfile.view.css
new file mode 100644
index 0000000..ee4f1f5
--- /dev/null
+++ b/theme/commerce_bank_account_onfile.view.css
@@ -0,0 +1,13 @@
+
+/**
+ * @file
+ * Styles for the bank accounts view.
+ */
+
+.links.operations {
+  text-transform: lowercase;
+}
+
+.views-field-operations .links.operations {
+  margin-left: 0;
+}
-- 
2.4.5

