Update payment form in member billing section.
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 15 May 2018 19:19:50 +0000 (15:19 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 15 May 2018 19:19:50 +0000 (15:19 -0400)
Deal with more than one invoice correctly.

classes/billingSupport.php
models/admin/member/billing.php
views/admin/billing/makePayment.html

index 1d2cf54..70d552d 100644 (file)
@@ -1359,7 +1359,7 @@ class GlmBillingSupport
             } else {
                 $result = array(
                     'status' => 0,
-                    'errors' => $ccResult['description']
+                    'errors' => array( $ccResult['description'] )
                 );
             }
 
index 4059216..efe8131 100644 (file)
@@ -267,7 +267,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
 
             // Now that the invoice is created. Do payment Processing.
 
-            // If there's any errors then re-show the form.
+            // If there's any rrors then re-show the form.
             if ( $error ) {
                 // Remove the invoice created.
                 if ( $invoice_id ) {
@@ -554,11 +554,14 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
                 // Make the Payment using Billing Support Class
                 $errors = $BillingSupport->makePayment( $account_id, $invoices, $amount );
 
-                // echo '<pre>$errors: ' . print_r( $errors, true ) . '</pre>';
+                echo '<pre>$errors: ' . print_r( $errors, true ) . '</pre>';
 
-                if ( $errors ) {
+                // Errors will be an array
+                if ( isset( $errors ) && !empty( $errors ) ) {
                     $paymentError = true;
-                    $messages[] = '<span style="color: red;">'.$errors.'</span>';
+                    foreach ( $errors as $error ) {
+                        $messages[] = '<span style="color: red;">'.$error.'</span>';
+                    }
                     // Load DataClass for Management.
                     require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/data/dataManagement.php';
                     $Management = new GlmDataBillingManagement( $this->wpdb, $this->config );
index f71c0fa..38a036a 100644 (file)
@@ -13,7 +13,7 @@
     {/if}
 
 
-    <form action="{$thisUrl}?page={$thisPage}&glm_action=billing&option=makePayment" method="post">
+    <form id="PaymentForm" action="{$thisUrl}?page={$thisPage}&glm_action=billing&option=makePayment" method="post">
         <input type="hidden" name="page" value="{$thisPage}">
         <input type="hidden" name="glm_action" value="billing">
         <input type="hidden" name="option" value="createPayment">
@@ -33,7 +33,7 @@
                         {if $invoices}
                             {foreach $invoices as $invoice}
                                 <label>
-                                    <input type="checkbox" name="invoices[]" data-amount="{$invoice.balance}" value="{$invoice.id}" checked required />
+                                    <input type="checkbox" name="invoices[]" data-amount="{$invoice.balance}" value="{$invoice.id}" checked />
                                     ${$invoice.balance} Due {$invoice.due_date}
                                 </label>
                             {/foreach}
@@ -68,12 +68,28 @@ jQuery(document).ready(function($){
         $('#billing-amount').val( $(this).find('option:selected').data( 'amount') );
     });
 
+    $('#PaymentForm').submit(function(){
+        var invoice_amount = $('#total_amount').val();
+        if ( invoice_amount == 0 ) {
+            alert( 'You have to select at least one invoice with an amount due.' );
+            return false;
+        } else {
+            return true;
+        }
+        return false;
+    });
+
     function getPageTotal(){
 
         // Calculate the total for this page.
         // Get the member_renewing amount.
-        if ( $('input[name^=invoices]:checked').length ) {
+        if ( $('input[name^=invoices]:checked').length === 1 ) {
             var invoice_amount = parseFloat( $('input[name^=invoices]:checked').data('amount') );
+        } else if (  $('input[name^=invoices]:checked').length > 1 ) {
+            var invoice_amount = 0.00;
+            $('input[name^=invoices]:checked').each(function(){
+                invoice_amount += parseFloat( $(this).data('amount') );
+            });
         } else {
             var invoice_amount = 0.00;
         }