Updating the support class
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 2 Apr 2018 20:45:21 +0000 (16:45 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 2 Apr 2018 20:45:21 +0000 (16:45 -0400)
Separate out the payment processing part.
Can now do multiple invoices.

classes/billingSupport.php
models/admin/member/billing.php

index ed2fe0a..1903541 100644 (file)
@@ -1030,18 +1030,7 @@ class GlmBillingSupport
         );
     }
 
-    /**
-     * processMemberRenewal
-     *
-     * @param int   $account_id Id of the account
-     * @param int   $invoice_id Id of the invoice
-     * @param float $amount     Amount of the total invoice
-     * @param array $employees  Array of employees
-     *
-     * @access public
-     * @return array
-     */
-    public function processMemberRenewal( $account_id, $invoice_id, $amount, $employees = array() )
+    public function processPayment()
     {
         $errors = array();
         // Get the selected credit card processor type
@@ -1085,7 +1074,6 @@ class GlmBillingSupport
             break;
 
         }
-
         // Get the credit card input
         $cardData = filter_var_array(
             $_REQUEST,
@@ -1143,7 +1131,6 @@ class GlmBillingSupport
         } else {
             $errors[] = 'You did not supply all required credit card information correctly.';
         }
-
         if (count($errors) == 0) {
 
             // Determine the directory of the payment processor to load and instantiate it.
@@ -1152,6 +1139,7 @@ class GlmBillingSupport
                 $CcProcessor = new PaymentGateway( $account );
             }
 
+            // TODO: Need to update the $invoice_id to something better for billing.
             $payment = array(
                 'name'    => $this->config['settings']['company_name'],  // Company Name
                 'charge'  => $amount,                                    // Total charges
@@ -1160,7 +1148,7 @@ class GlmBillingSupport
                 'ccnumb'  => $cardData['cc_numb'],                       // Card Number
                 'ccexp'   => $cardData['cc_exp'],                        // Expiration Date
                 'cccode'  => $cardData['cc_cvv'],                        // CCV - security code
-                'invoice' => 'billing-'.$invoice_id                      // Invoice # is "reg-" plus cart ID
+                'invoice' => 'billing-'//.$invoice_id                      // Invoice # is "reg-" plus cart ID
             );
 
             // Now try to run the card processor
@@ -1168,47 +1156,106 @@ class GlmBillingSupport
 
             // If successful submission - say we're complete
             if ( is_array( $ccResult ) && isset( $ccResult['status'] ) && $ccResult['status'] == 1 ) {
-                // Need to record the payment
-
-                // Set transaction_time to current time.
-                $_REQUEST['transaction_time'] = date( 'Y-m-d H:i:s' );
-                // Create new payment.
-                $payment_id = $this->createPayment( $account_id, $amount );
-                // Record the payment.
-                $this->recordPayment( $payment_id, $account_id, $amount, $invoice_id );
-
-                // $errors[] = $ccResult['statusText'];
-                // $errors[] = $ccResult['description'];
-
-                // Need to update the members account renewal_date
-                $this->wpdb->update(
-                    GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
-                    array( 'renewal_date' => date( 'Y-m-d' ) ),
-                    array( 'id' => $account_id ),
-                    array( '%s' ),
-                    array( '%d' )
+                $result = array(
+                    'status'   => 1,
+                    'errors'   => array(),
+                    'ccResult' => $ccResult
                 );
-                // Need to update any employees renewal dates
-                if ( isset( $employees ) && is_array( $employees ) && !empty( $employees ) ) {
-                    foreach ( $employees as $employee ) {
-                        $this->wpdb->update(
-                            GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
-                            array( 'renewal_date' => date( 'Y-m-d' ) ),
-                            array( 'id' => $employee ),
-                            array( '%s' ),
-                            array( '%d' )
-                        );
-                    }
-                }
 
             }
 
+        } else {
+            $result = array(
+                'status' => 0,
+                'errors' => $errors
+            );
+        }
+        return $result;
+    }
+
+    /**
+     * processMemberRenewal
+     *
+     * @param int   $account_id Id of the account
+     * @param int   $invoice_id Id of the invoice
+     * @param float $amount     Amount of the total invoice
+     * @param array $employees  Array of employees
+     *
+     * @access public
+     * @return array
+     */
+    public function processMemberRenewal( $account_id, $invoice_id, $amount, $employees = array() )
+    {
+        $result = $this->processPayment();
+
+        if ( $result['status'] === 1 ) {
+
+            // Need to record the payment
+            // Set transaction_time to current time.
+            $_REQUEST['transaction_time'] = date( 'Y-m-d H:i:s' );
+
+            // Create new payment.
+            $payment_id = $this->createPayment( $account_id, $amount );
+
+            // Record the payment.
+            $this->recordPayment( $payment_id, $account_id, $amount, $invoice_id );
+
+            // $errors[] = $ccResult['statusText'];
+            // $errors[] = $ccResult['description'];
+
+            // Need to update the members account renewal_date
+            $this->wpdb->update(
+                GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
+                array( 'renewal_date' => date( 'Y-m-d' ) ),
+                array( 'id' => $account_id ),
+                array( '%s' ),
+                array( '%d' )
+            );
+
+            // Need to update any employees renewal dates
+            if ( isset( $employees ) && is_array( $employees ) && !empty( $employees ) ) {
+                foreach ( $employees as $employee ) {
+                    $this->wpdb->update(
+                        GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'accounts',
+                        array( 'renewal_date' => date( 'Y-m-d' ) ),
+                        array( 'id' => $employee ),
+                        array( '%s' ),
+                        array( '%d' )
+                    );
+                }
+            }
+
+            $errors = $result['errors'];
+
+        } else {
+            $errors = $result['errors'];
         }
 
         return $errors;
 
     }
 
+    public function makePayment( $account_id, $invoices, $amount )
+    {
+        $result = $this->processPayment();
+        if ( $result['status'] === 1 ) {
+
+            // Need to record the payment.
+            // Set transaction_time to current time.
+            $_REQUEST['transaction_time'] = date( 'Y-m-d H:i:s' );
+
+            // Create new payment.
+            $payment_id = $this->createPayment( $account_id, $amount );
+
+            // Record the payment.
+            $this->recordPayment( $payment_id, $account_id, $amount, $invoices );
+
+            $errors = $result['errors'];
+        } else {
+            $errors = $result['errors'];
+        }
+    }
+
     /**
      * removeInvoiceById
      *
index 2c8c44c..8d2f6e1 100644 (file)
@@ -387,9 +387,14 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
 
             // if error
 
-            $messages = '<pre>'.print_r( $_REQUEST, true ).'</pre>';
+            $messages   = '<pre>'.print_r( $_REQUEST, true ).'</pre>';
             $account_id = filter_var( $_REQUEST['account_id'], FILTER_VALIDATE_INT );
-            $BillingSupport->processMemberRenewal( $account_id, $invoice_id, $amount );
+            $invoices   = filter_var( $_REQUEST['invoices'], FILTER_VALIDATE_INT, array( 'flags' => FILTER_REQUIRE_ARRAY ) );
+            $amount     = filter_var( $_REQUEST['amount'], FILTER_VALIDATE_FLOAT );
+            // echo '<pre>$account_id: ' . print_r( $account_id, true ) . '</pre>';
+            // echo '<pre>$invoices: ' . print_r( $invoices, true ) . '</pre>';
+            // echo '<pre>$amount: ' . print_r( $amount, true ) . '</pre>';
+            $BillingSupport->makePayment( $account_id, $invoices, $amount );