Added ability to limit number of card processing attempts.
authorChuck Scott <cscott@gaslightmedia.com>
Thu, 6 Aug 2015 19:02:23 +0000 (15:02 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Thu, 6 Aug 2015 19:02:23 +0000 (15:02 -0400)
classes/glmMesGateway.php

index 19e0dbe..ee6c69c 100644 (file)
@@ -552,6 +552,14 @@ class GlmMesGateway extends WC_Payment_Gateway
                         'default' => 'no'
                 ),
 
+                // Specify Maximum number of Card Attempts - both pre-auth and transcation
+                'max_card_attempts' => array(
+                        'title' => __('Maximum Card Attempts', 'glm-merchant_e_solutions'),
+                        'type' => 'decimal',
+                        'description' => __('Maximum number of times a credit card pre-authorization or transaction may be attempted. (0 = no limit)', 'glm-merchant_e_solutions'),
+                        'default' => '4'
+                ),
+
                 'sep_1' => array(
                         'title' => __('Test Mode Credentials', 'glm-merchant_e_solutions'),
                         'type' => 'title',
@@ -911,6 +919,51 @@ class GlmMesGateway extends WC_Payment_Gateway
 
     }
 
+    /*
+     * Check for maximum number of card attempts
+     *
+     * @access public
+     * @return boolean False if maximum not reached, true if reached
+     */
+    private function check_max_attempts()
+    {
+        global $woocommerce;
+
+        // Get the maximum number of credit card attempts permitted
+        $maxAttempts = $this->settings['max_card_attempts'];
+
+        // If maximum attempts is 0 then unlimited
+        if ($maxAttempts == 0) {
+            return false;
+        }
+
+        // Check if attempt count is already set in the WooCommerce session
+        if ( isset( $woocommerce->session->cc_attempts ) ) {
+
+            // Get the current number of attempts plus one attempt (this attempt)
+            $attempts = $woocommerce->session->cc_attempts + 1;
+
+            // If the maximum number of attempts have been exceeded
+            if ($attempts > $maxAttempts) {
+                return true;
+            }
+
+            // Max attempts have not been reached - Update # of attempts
+            $woocommerce->session->cc_attempts = $attempts;
+            return false;
+
+        // Otherwise initialize the attempt count
+        } else {
+
+            $attempts = 0;
+            $woocommerce->session->cc_attempts = $attempts;
+            return false;
+
+        }
+
+    }
+
+
     /*
      * Process order with MES Gateway
      */
@@ -921,6 +974,31 @@ class GlmMesGateway extends WC_Payment_Gateway
         global $woocommerce;
         $this->cust_order = new WC_Order($order_id);
 
+        // Check for a maximum number of attempts
+        if ($this->check_max_attempts()) {
+
+            wc_add_notice(
+                    __(
+                            '<p><h4><b style="color: red;">We\'re sorry, there was a problem with your payment...</b></h4><p><b>Reason:</b><br>' .
+                            'You have exceeded the maximum number of attempts to process your credit card.<br>
+                            Please contact us for assistance.',
+                            'woothemes'), 'notice');
+
+            // Add a note to the customer order for reference
+            $this->cust_order->add_order_note( 'Error: '. 'Number of credit card processing attempts exceeded.' );
+
+            if (GLM_MES_DEBUG && ! GLM_MES_DEBUG_VERBOSE) {
+                wc_add_notice(
+                        'GLM MeS Gateway: Maximum attempts exceeded!', 'notice');
+            }
+
+            return array(
+                    'result' => 'failure',
+                    'redirect' => $this->get_return_url($this->cust_order)
+            );
+
+        }
+
         // Get our invoice number
         $this->invNumb = str_replace("#", "", $this->cust_order->get_order_number());