Fix issue where refreshing the invoice page was resubmitting to Authorize.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 4 Oct 2019 14:19:56 +0000 (10:19 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 4 Oct 2019 14:42:37 +0000 (10:42 -0400)
Created a session which is checked before resubmitting to payment.
Session will store the invoice id which can be used to show the invoice.

index.php
models/front/billing/renew.php
views/front/billing/renew.html

index cb93e75..c2be846 100644 (file)
--- a/index.php
+++ b/index.php
@@ -3,7 +3,7 @@
  * Plugin Name: GLM Members Billing
  * Plugin URI: http://www.gaslightmedia.com/
  * Description: This is glmAssociate Billing Module
- * Version: 1.0.37
+ * Version: 1.0.38
  * Author: Gaslight Media
  * Author URI: http://www.gaslightmedia.com/
  * License: GPL2
@@ -19,7 +19,7 @@
  * @package glmMembersBillingAddOn
  * @author Chuck Scott <cscott@gaslightmedia.com>
  * @license http://www.gaslightmedia.com Gaslightmedia
- * @version 1.0.37
+ * @version 1.0.38
  */
 
 /*
@@ -37,7 +37,7 @@
  *  so that we're sure the other add-ons see an up to date
  *  version from this plugin.
  */
-define('GLM_MEMBERS_BILLING_PLUGIN_VERSION', '1.0.37');
+define('GLM_MEMBERS_BILLING_PLUGIN_VERSION', '1.0.38');
 
 // Database Version
 define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.40');
index a0989f6..81d76ac 100644 (file)
@@ -239,81 +239,60 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
 
         case 'renewMembership':
             $error = false;
-            $view  = 'renewMembership';
-
-            // Check for account_number
-            // If given then this is from unregistered user.
-            // Have to verify that this number does match with billing_account.
-            if ( isset( $_REQUEST['account_number'] ) && $account_number = filter_var( $_REQUEST['account_number'], FILTER_SANITIZE_STRING ) ) {
-                // Verify account number.
-                $accountID = $this->wpdb->get_var(
-                    $this->wpdb->prepare(
-                        "SELECT id
-                           FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts
-                          WHERE account_number = %s",
-                        $account_number
-                    )
-                );
-            }
 
-            $member_invoice_id = $BillingSupport->getMembersInvoiceTypeByRefDest( $this->memberId );
-            echo '<pre>$this->memberId: ' . print_r( $this->memberId, true ) . '</pre>';
-            echo '<pre>$member_invoice_id: ' . print_r( $member_invoice_id, true ) . '</pre>';
-            if ( $member_invoice_id ) {
-                $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
+            if ( isset( $_SESSION['billing_renewal_paid'] ) && $_SESSION['billing_renewal_paid'] ) {
+                // They're refreshing the page with the invoice on it.
+                // Reshow the invoice.
             } else {
-                $error        = true;
-                $paymentError = true;
-                trigger_error( 'member_invoice_id not found', E_USER_NOTICE );
-            }
+                $view  = 'renewMembership';
 
-            if ( $this->config['settings']['allow_employees'] ) {
-                $employees = $BillingSupport->getListOfAccountEmployees( $this->memberId );
-            }
+                $member_invoice_id = $BillingSupport->getMembersInvoiceTypeByRefDest( $this->memberID );
+                if ( $member_invoice_id ) {
+                    $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
+                } else {
+                    $error = true;
+                }
 
-            $invoice_data = array(
-                'account_id'     => $accountID,
-                'renew_type_id'  => filter_var( $_REQUEST['member_renewing'], FILTER_VALIDATE_INT ),
-                'amount'         => $_REQUEST['total_renew_amount'],
-                'due_date'       => date( 'Y-m-d' ),
-                'member_invoice' => $member_invoice,
-                'employee_data'  => $employees,
-                'employees'      => isset( $_REQUEST['employees'] ) ? $_REQUEST['employees'] : array(),
-            );
-            if ( !$invoice_data ) {
-                $error = true;
-            }
+                $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
 
-            // Start database transaction
-            $this->wpdb->show_errors();
-            $this->wpdb->query('START TRANSACTION');
+                $invoice_data = array(
+                    'account_id'     => $accountID,
+                    'renew_type_id'  => filter_var( $_REQUEST['member_renewing'], FILTER_VALIDATE_INT ),
+                    'amount'         => $_REQUEST['total_renew_amount'],
+                    'due_date'       => date( 'Y-m-d' ),
+                    'member_invoice' => $member_invoice,
+                    'employee_data'  => $employees,
+                    'employees'      => isset( $_REQUEST['employees'] ) ? $_REQUEST['employees'] : array(),
+                );
+                if ( !$invoice_data ) {
+                    $error = true;
+                }
 
-            // Before we create an invoice for this member see if they already have one.
-            // If we find one then delete it.
-            $old_invoice_ids = $this->wpdb->get_results(
-                $this->wpdb->prepare(
-                    "SELECT id
-                       FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices
-                      WHERE paid <> true
-                        AND renewal = true
-                        AND account = %d",
-                    $accountID
-                ),
-                ARRAY_A
-            );
-            if ( isset( $old_invoice_ids ) && $old_invoice_ids ) {
-                foreach ( $old_invoice_ids as $old_invoices ) {
-                    if ( $inv_id = filter_var( $old_invoices['id'], FILTER_VALIDATE_INT ) ) {
-                        $BillingSupport->removeInvoiceById( $inv_id );
+                // Before we create an invoice for this member see if they already have one.
+                // If we find one then delete it.
+                $old_invoice_ids = $this->wpdb->get_results(
+                    $this->wpdb->prepare(
+                        "SELECT id
+                           FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices
+                          WHERE paid <> true
+                            AND renewal = true
+                            AND account = %d",
+                        $accountID
+                    ),
+                    ARRAY_A
+                );
+                if ( isset( $old_invoice_ids ) && $old_invoice_ids ) {
+                    foreach ( $old_invoice_ids as $old_invoices ) {
+                        if ( $inv_id = filter_var( $old_invoices['id'], FILTER_VALIDATE_INT ) ) {
+                            $BillingSupport->removeInvoiceById( $inv_id );
+                        }
                     }
                 }
-            }
-            // Create the invoice for this member.
-            $invoice_id = $BillingSupport->createMemberInvoiceWithEmployees( $invoice_data );
-            if ( !$invoice_id ) {
-                $error        = true;
-                $paymentError = true;
-                trigger_error( 'invoice_id not found', E_USER_NOTICE );
+                // Create the invoice for this member.
+                $invoice_id = $BillingSupport->createMemberInvoiceWithEmployees( $invoice_data );
+                if ( !$invoice_id ) {
+                    $error = true;
+                }
             }
 
             // Now that the invoice is created. Do payment Processing.
@@ -352,57 +331,64 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling
                 $account = $Accounts->editEntry( $accountID );
             } else {
 
-                // Look at the payment option given
-                $payment_option = filter_var( $_REQUEST['payment_option'], FILTER_SANITIZE_STRING );
-
-                switch ( $payment_option ) {
-                case 'pay_by_credit_card':
-                    // Do the Payment Processing.
-                    $processErrors = $BillingSupport->processMemberRenewal( $accountID, $invoice_id, $invoice_data['amount'], $invoice_data['employees'] );
-                    if ( $processErrors ) {
-                        if ( isset( $processErrors ) && is_array( $processErrors ) && count( $processErrors ) > 0 ) {
-                            $error = true;
-                            $BillingSupport->removeInvoiceById( $invoice_id );
-                            foreach ( $processErrors as $error ) {
-                                $messages[] = '<span style="color: red;">'.$error.'</span>';
+                if ( isset( $_SESSION['billing_renewal_paid'] ) && $_SESSION['billing_renewal_paid'] ) {
+                    // They're refreshing the page with the invoice on it.
+                    // Reshow the invoice.
+                    $invoice_id = $_SESSION['billing_renewal_paid'];
+                } else {
+                    // Look at the payment option given
+                    $payment_option = filter_var( $_REQUEST['payment_option'], FILTER_SANITIZE_STRING );
+
+                    switch ( $payment_option ) {
+                    case 'pay_by_credit_card':
+                        // Do the Payment Processing.
+                        $processErrors = $BillingSupport->processMemberRenewal( $accountID, $invoice_id, $invoice_data['amount'], $invoice_data['employees'] );
+                        if ( $processErrors ) {
+                            if ( isset( $processErrors ) && is_array( $processErrors ) && count( $processErrors ) > 0 ) {
+                                $error = true;
+                                $BillingSupport->removeInvoiceById( $invoice_id );
+                                foreach ( $processErrors as $error ) {
+                                    $messages[] = '<span style="color: red;">'.$error.'</span>';
+                                }
+                            } else if ( isset( $processErrors ) && $processErrors ) {
+                                $error = true;
+                                $messages[] = '<span style="color: red;">'.$processErrors.'</span>';
+                            } else {
+                                $paymentSuccess = true;
                             }
-                        } else if ( isset( $processErrors ) && $processErrors ) {
-                            $error = true;
-                            $messages[] = '<span style="color: red;">'.$processErrors.'</span>';
-                        } else {
-                            $paymentSuccess = true;
-                        }
-                        $view = 'renew';
+                            $view = 'renew';
 
-                        // Get list of payable invoice_types
-                        $payable_types = $BillingSupport->getAllPayableInvoiceTypes();
+                            // Get list of payable invoice_types
+                            $payable_types = $BillingSupport->getAllPayableInvoiceTypes();
 
-                        $member_invoice_id = $BillingSupport->getMembersInvoiceTypeByRefDest( $this->memberId );
-                        if ( $member_invoice_id ) {
-                            $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
-                        }
 
-                        if ( $this->config['settings']['allow_employees'] ) {
+                            $member_invoice_id = $BillingSupport->getMembersInvoiceTypeByRefDest( $this->memberID );
+                            if ( $member_invoice_id ) {
+                                $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id );
+                            }
+
                             // Get a list of this accounts employees. If they have any.
-                            $employees = $BillingSupport->getListOfAccountEmployees( $this->memberId );
-                        }
+                            $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID );
 
-                        // Load DataClass for Management.
-                        require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataManagement.php';
-                        $Management = new GlmDataBillingManagement( $this->wpdb, $this->config );
-                        $management = $Management->getEntry( 1 );
+                            // Load DataClass for Management.
+                            require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataManagement.php';
+                            $Management = new GlmDataBillingManagement( $this->wpdb, $this->config );
+                            $management = $Management->getEntry( 1 );
 
-                        // Need to see if there's an account for this member.
-                        $account = $Accounts->editEntry( $accountID );
+                            // Need to see if there's an account for this member.
+                            $account = $Accounts->editEntry( $accountID );
+                        }
+                        break;
+                    case 'pay_by_check':
+                        break;
                     }
-                    break;
-                case 'pay_by_check':
-                    break;
                 }
 
                 // If there's no error reported then show the invoice
                 if ( !$error ) {
-                    $this->wpdb->query( 'COMMIT' );
+
+                    // Create a session for renewal paid.
+                    $_SESSION['billing_renewal_paid'] = $invoice_id;
                     // Now need to show the invoice.
                     $view = 'viewInvoice';
 
index 740104a..a0f3666 100644 (file)
@@ -187,5 +187,3 @@ jQuery(document).ready(function($){
 
 });
 </script>
-
-