From 8a7eee38052b14839e3c193fa336b377b14fd12a Mon Sep 17 00:00:00 2001
From: wildkatana <leighton.whiting@gmail.com>
Date: Mon, 3 Apr 2017 21:50:51 -0600
Subject: [PATCH 1/2] =?UTF-8?q?improve(commerce=5Fpos):=20add=20default=20?=
 =?UTF-8?q?payment=20method=20setting=E2=80=A8=E2=80=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Adds a setting to specify the default payment method.  

Credit: https://www.drupal.org/u/tyler_marshall

  https://www.drupal.org/node/2800535
---
 commerce_pos.module                                | 12 ++++++++++++
 includes/commerce_pos.admin.inc                    | 22 +++++++++++++++++++++-
 includes/commerce_pos.payment.inc                  |  6 ++++++
 .../includes/commerce_pos_terminal.admin.inc       |  1 -
 4 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/commerce_pos.module b/commerce_pos.module
index 656a332..bf8c224 100644
--- a/commerce_pos.module
+++ b/commerce_pos.module
@@ -622,6 +622,18 @@ function commerce_pos_get_payment_methods() {
 }
 
 /**
+ * Returns default payment setting if set.
+ * @return null
+ */
+function commerce_pos_get_default_payment_option(){
+  $default_setting = variable_get('commerce_pos_default_payment_method');
+  if($default_setting == ''){
+    return null;
+  }
+  return $default_setting;
+}
+
+/**
  * Retrieves a list of payment options for the Point of Sale payment form.
  */
 function commerce_pos_get_payment_options() {
diff --git a/includes/commerce_pos.admin.inc b/includes/commerce_pos.admin.inc
index dd72e70..037bf6a 100644
--- a/includes/commerce_pos.admin.inc
+++ b/includes/commerce_pos.admin.inc
@@ -25,7 +25,6 @@ function commerce_pos_settings($form, &$form_state) {
       '#options' => $index_options,
       '#default_value' => variable_get('commerce_pos_search_api_index', NULL),
     );
-
   }
 
   $form['commerce_pos_search_results_count'] = array(
@@ -101,6 +100,25 @@ function commerce_pos_settings($form, &$form_state) {
     );
   }
 
+  //Grab a list of payments
+  $payments = array();
+  foreach(commerce_pos_get_payment_options() as $payment){
+    $payments[$payment['id']] = $payment['title'];
+  }
+
+  $form['payments'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Payment Settings'
+  );
+
+  $form['payments']['commerce_pos_default_payment_method'] = array (
+    '#type' => 'select',
+    '#title' => t('Default Payment Method'),
+    '#description' => t('Select the default payment method.'),
+    '#options' => $payments,
+    '#default_value' => variable_get('commerce_pos_default_payment_method', 0),
+  );
+
   $form['actions'] = array(
     '#type' => 'actions',
   );
@@ -156,6 +174,8 @@ function commerce_pos_settings_submit($form, &$form_state) {
 
   variable_set('commerce_pos_available_products', $form_state['values']['commerce_pos_available_products']);
 
+  variable_set('commerce_pos_default_payment_method', $form_state['values']['commerce_pos_default_payment_method']);
+
   foreach (commerce_product_types() as $name => $type) {
     variable_set('commerce_pos_image_field_' . $name, $form_state['values']['commerce_pos_image_field_' . $name]);
 
diff --git a/includes/commerce_pos.payment.inc b/includes/commerce_pos.payment.inc
index 3487d1f..49210e5 100644
--- a/includes/commerce_pos.payment.inc
+++ b/includes/commerce_pos.payment.inc
@@ -156,6 +156,12 @@ function commerce_pos_payment(&$form, &$form_state, $transaction_type) {
       '#theme' => 'commerce_pos_keypad',
     );
 
+    //If no triggering element is set, grab the default payment method.
+    $default_payment_method = commerce_pos_get_default_payment_option();
+    if(empty($form_state['triggering_element']['#payment_option_id'])){
+      $form_state['triggering_element']['#payment_option_id'] = $default_payment_method;
+    }
+
     if (!empty($form_state['triggering_element']['#payment_option_id'])) {
       $option_id = $form_state['triggering_element']['#payment_option_id'];
 
diff --git a/modules/terminal/includes/commerce_pos_terminal.admin.inc b/modules/terminal/includes/commerce_pos_terminal.admin.inc
index 949d7dc..3091a50 100644
--- a/modules/terminal/includes/commerce_pos_terminal.admin.inc
+++ b/modules/terminal/includes/commerce_pos_terminal.admin.inc
@@ -161,7 +161,6 @@ function commerce_pos_terminal_settings_form_submit($form, &$form_state) {
       variable_set($setting, $form_state['values'][$setting]);
     }
   }
-
 }
 
 /**

From ea279b3e274f687d2a41c40760ba8727080ca98a Mon Sep 17 00:00:00 2001
From: wildkatana <leighton.whiting@gmail.com>
Date: Mon, 3 Apr 2017 22:35:05 -0600
Subject: [PATCH 2/2] improve(commerce_pos): make default payment option
 friendly

Add option for 'none' when selecting default payment method. Graceful fallback. Also fixing various Coding Standards issues.

https://www.drupal.org/node/2800535
---
 commerce_pos.module                                       | 12 ------------
 includes/commerce_pos.admin.inc                           | 10 ++++++----
 includes/commerce_pos.payment.inc                         |  6 +++---
 modules/terminal/includes/commerce_pos_terminal.admin.inc |  1 +
 4 files changed, 10 insertions(+), 19 deletions(-)

diff --git a/commerce_pos.module b/commerce_pos.module
index bf8c224..656a332 100644
--- a/commerce_pos.module
+++ b/commerce_pos.module
@@ -622,18 +622,6 @@ function commerce_pos_get_payment_methods() {
 }
 
 /**
- * Returns default payment setting if set.
- * @return null
- */
-function commerce_pos_get_default_payment_option(){
-  $default_setting = variable_get('commerce_pos_default_payment_method');
-  if($default_setting == ''){
-    return null;
-  }
-  return $default_setting;
-}
-
-/**
  * Retrieves a list of payment options for the Point of Sale payment form.
  */
 function commerce_pos_get_payment_options() {
diff --git a/includes/commerce_pos.admin.inc b/includes/commerce_pos.admin.inc
index 037bf6a..6750844 100644
--- a/includes/commerce_pos.admin.inc
+++ b/includes/commerce_pos.admin.inc
@@ -25,6 +25,7 @@ function commerce_pos_settings($form, &$form_state) {
       '#options' => $index_options,
       '#default_value' => variable_get('commerce_pos_search_api_index', NULL),
     );
+
   }
 
   $form['commerce_pos_search_results_count'] = array(
@@ -100,23 +101,24 @@ function commerce_pos_settings($form, &$form_state) {
     );
   }
 
-  //Grab a list of payments
+  // Grab a list of payments.
   $payments = array();
-  foreach(commerce_pos_get_payment_options() as $payment){
+  foreach (commerce_pos_get_payment_options() as $payment) {
     $payments[$payment['id']] = $payment['title'];
   }
 
   $form['payments'] = array(
     '#type' => 'fieldset',
-    '#title' => 'Payment Settings'
+    '#title' => t('Payment Settings'),
   );
 
   $form['payments']['commerce_pos_default_payment_method'] = array (
     '#type' => 'select',
     '#title' => t('Default Payment Method'),
     '#description' => t('Select the default payment method.'),
+    '#empty_option' => t('- None -'),
     '#options' => $payments,
-    '#default_value' => variable_get('commerce_pos_default_payment_method', 0),
+    '#default_value' => variable_get('commerce_pos_default_payment_method', ''),
   );
 
   $form['actions'] = array(
diff --git a/includes/commerce_pos.payment.inc b/includes/commerce_pos.payment.inc
index 49210e5..06c930e 100644
--- a/includes/commerce_pos.payment.inc
+++ b/includes/commerce_pos.payment.inc
@@ -156,9 +156,9 @@ function commerce_pos_payment(&$form, &$form_state, $transaction_type) {
       '#theme' => 'commerce_pos_keypad',
     );
 
-    //If no triggering element is set, grab the default payment method.
-    $default_payment_method = commerce_pos_get_default_payment_option();
-    if(empty($form_state['triggering_element']['#payment_option_id'])){
+    // If no triggering element is set, grab the default payment method.
+    $default_payment_method = variable_get('commerce_pos_default_payment_method', '');
+    if (!empty($default_payment_method) && !empty($payment_options[$default_payment_method]) && empty($form_state['triggering_element']['#payment_option_id'])) {
       $form_state['triggering_element']['#payment_option_id'] = $default_payment_method;
     }
 
diff --git a/modules/terminal/includes/commerce_pos_terminal.admin.inc b/modules/terminal/includes/commerce_pos_terminal.admin.inc
index 3091a50..949d7dc 100644
--- a/modules/terminal/includes/commerce_pos_terminal.admin.inc
+++ b/modules/terminal/includes/commerce_pos_terminal.admin.inc
@@ -161,6 +161,7 @@ function commerce_pos_terminal_settings_form_submit($form, &$form_state) {
       variable_set($setting, $form_state['values'][$setting]);
     }
   }
+
 }
 
 /**
