Bug 23213: Enable minimum payment threshold check in OPAC
authorAgustin Moyano <agustinmoyano@theke.io>
Wed, 26 Jun 2019 05:02:39 +0000 (02:02 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 27 Sep 2019 13:08:18 +0000 (14:08 +0100)
This patch enables OPAC to check for the miminum payment threshold per payment method.

If the total amount to pay is less than the threshold, payment for that method is disabled.

To test:
1) apply dependencies
2) get 2 payment plugins, one with threshold as https://gitlab.com/thekesolutions/plugins/koha-plugin-pay-via-paypal/uploads/4c66444ff697586dff3d8759c1e5746c/koha-plugin-pay-via-paypal-v1.0.0.kpz and one without threshold as https://github.com/bywatersolutions/koha-plugin-pay-via-paygov/releases/download/v1.0.5/koha-plugin-pay-via-paygov-v1.0.5.kpz
3) install both plugins
4) Configure both plugins to work correctly (can be invented data.. we are not interested in the actual payment page, but to test if payment method is allowed)
5) In plugin with threshold, set minimum threshold to 10
6) Fetch any user, and create 2 manual invoices, both for the amount of 5
7) Enter OPAC as user, and go to "my fines" tab
CHECK => both plugins appear as payment method
8) Check the checkbox of one of the fines
SUCCESS => plugin with threshold is now disabled, and has a warning message that says "Minimum amount needed by this service is 10.00"
        => plugin without threshold is enabled
        => if plugin with threshold was selected, "Make payment" button is disabled until an enabled payment method is selected
9) Check the second checkbox
SUCCESS => both payment methods are now enabled
10) Sign off

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

koha-tmpl/opac-tmpl/bootstrap/en/includes/account-table.inc
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-account.tt

index cf25163..cdc23e4 100644 (file)
             </tr>
         </tfoot>
     </table>
-    [% IF ENABLE_OPAC_PAYMENTS && DISPLAY_PAYMENT_BLOCK %]
-        <fieldset class="pay-online hidden">
-            <legend>Pay selected fines and charges</legend>
-                <span class="help-block"><h3>Payment method</h3></span>
-
-                [% IF Koha.Preference('EnablePayPalOpacPayments') %]
-                    <div class="control-group">
-                        <label class="radio">
-                            <input type="radio" name="payment_method" id="payment_method-paypal" value="paypal" checked="checked">
-                            <!-- PayPal Logo --><a href="https://www.paypal.com/webapps/mpp/paypal-popup" title="How PayPal Works" class="paypal"><img src="https://www.paypalobjects.com/webstatic/mktg/logo/AM_SbyPP_mc_vs_dc_ae.jpg" border="0" alt="PayPal Acceptance Mark"></a><!-- PayPal Logo -->
-                        </label>
-                    </div>
-                [% END %]
-
-                [% FOREACH p IN plugins %]
-                    <div class="control-group">
-                        <label class="radio">
-                            <input type="radio" name="payment_method" id="payment_method-[% p.class | html %]" value="[% p.class | html %]" checked="checked">
-                            [% p.get_metadata.name | html %]
-                        </label>
-                    </div>
-                [% END %]
+[% END %]
+
+[% IF ENABLE_OPAC_PAYMENTS && DISPLAY_PAYMENT_BLOCK %]
+    <fieldset class="pay-online hidden">
+        <legend>Pay selected fines and charges</legend>
+            <span class="help-block"><h3>Payment method</h3></span>
+
+            [% IF Koha.Preference('EnablePayPalOpacPayments') %]
+                <div class="control-group">
+                    <label class="radio">
+                        <input type="radio" name="payment_method" id="payment_method-paypal" value="paypal" checked="checked">
+                        <!-- PayPal Logo --><a href="https://www.paypal.com/webapps/mpp/paypal-popup" title="How PayPal Works" class="paypal"><img src="https://www.paypalobjects.com/webstatic/mktg/logo/AM_SbyPP_mc_vs_dc_ae.jpg" border="0" alt="PayPal Acceptance Mark"></a><!-- PayPal Logo -->
+                    </label>
+                </div>
+            [% END %]
 
+            [% FOREACH p IN plugins %]
                 <div class="control-group">
-                    <input type="hidden" id="payment-amount" name="payment_amount" value="0" />
-                    <button id="submit-pay" type="submit" class="btn" disabled="disabled">Make payment</button>
-                    <span id="amount-to-pay-label">
-                        Amount to pay: <span id="amount-to-pay">0.00</span>
-                    </span>
+                    <label class="radio">
+                        <input type="radio" data-threshold="[% p.opac_online_payment_threshold | html %]" name="payment_method" id="payment_method-[% p.class | html %]" value="[% p.class | html %]" checked="checked">
+                        [% p.get_metadata.name | html %]
+                    </label>
+                    <small class="help-block text-muted"></small>
                 </div>
-        </fieldset>
-    [% END %]
+            [% END %]
+
+            <div class="control-group">
+                <input type="hidden" id="payment-amount" name="payment_amount" value="0" />
+                <button id="submit-pay" type="submit" class="btn" disabled="disabled">Make payment</button>
+                <span id="amount-to-pay-label">
+                    Amount to pay: <span id="amount-to-pay">0.00</span>
+                </span>
+            </div>
+    </fieldset>
 [% END %]
 </form>
 
index 70da8a2..baeced4 100644 (file)
@@ -81,6 +81,8 @@
 <script>
 $( document ).ready(function() {
 
+    var MSG_MIN_THRESHOLD = _("Minimum amount needed by this service is %s");
+
     $("#finestable").dataTable($.extend(true, {}, dataTablesDefaults, {
         "columnDefs": [
              { "type": "title-string", "targets" : [ "title-string" ] }
@@ -97,9 +99,9 @@ $( document ).ready(function() {
 
     $("#amount-to-pay-label").hide();
 
-    $(".checkbox-pay").change( function() {
+    $(".checkbox-pay, input[name='payment_method']").change( function() {
         // Disable the pay button if no fees are selected
-        $("#submit-pay").prop("disabled", ! $(".checkbox-pay:checked").length );
+        //$("#submit-pay").prop("disabled", ! $(".checkbox-pay:checked").length );
 
         // Calculate the total amount to be paid based on selected fees
         var total = 0;
@@ -110,12 +112,35 @@ $( document ).ready(function() {
             }
         });
 
+        var p = Promise.resolve();
         if ( total ) {
+            p = Promise.all(
+                $('input[name="payment_method"]').map(function() {
+                    var self = this;
+                    return new Promise(function(resolve, reject) {
+                        var threshold = $(self).data('threshold');
+                        var help = $(self).parent().siblings('.help-block');
+                        if(threshold == '' || threshold <= total) {
+                            $(self).prop('disabled', false);
+                            help.addClass('hide');
+                        } else {
+                            $(self).prop('disabled', true);
+                            help.html(MSG_MIN_THRESHOLD.format(parseInt(threshold,10).toFixed(2))).removeClass('hide');
+                        }
+                        resolve();
+                    })
+                }).toArray()
+            );
+
             $("#amount-to-pay").html( total.toFixed(2) );
             $("#amount-to-pay-label").show();
         } else {
+            $('input[name="payment_method"]').prop('disabled', false).parent().siblings('.help-block').addClass('hide');
             $("#amount-to-pay-label").hide();
         }
+        p.then(function() {
+            $("#submit-pay").prop("disabled", ! $(".checkbox-pay:checked").length || ! $('input[name="payment_method"]:checked:not(:disabled)').length);
+        })
     });
 });
 </script>