Bug 19173: Make OPAC online payments pluggable
authorKyle M Hall <kyle@bywatersolutions.com>
Thu, 24 Aug 2017 10:55:49 +0000 (06:55 -0400)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 19 Sep 2017 17:15:52 +0000 (14:15 -0300)
While PayPal is fairly universal, there is a plethora of online
payment system that are far more localized, servicing a single
country ( e.g. Bug 18968 ) or even a single  city! Instead of
adding support for each and every one of these payment options
directly into Koha, it makes more sense to add the ability to
create online payment plugins.

Test Plan:
1) Apply this patch
2) Download and install the Kitchen Sink plugin version 2.1.1 or later
   https://github.com/bywatersolutions/koha-plugin-kitchen-sink/releases
3) In the plugin options, enable the opac payments option
4) Create a patron with one or more fines
5) Log into the opac as that patron, note you now have the option
   to pay online via KitchenSink ImaginaryPay
6) Make an online payment
7) Note the payment was processed correctly

Sponsored-by: Washoe County Library System
Signed-off-by: Kyle M Hall <kyle@gmail.com>
Signed-off-by: Magnus Enger <magnus@libriotech.no>
Awesome enhancement! I know we want to add at least one Norwegian
payment service at some point.
I followed the test plan and everything works as advertised. Turning
off the "opac payments option" makes the option dissappear cleanly
from the OPAC. I have *not* looked at the code or done any
considerations about security.
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-account.tt
opac/opac-account-pay-return.pl [new file with mode: 0755]
opac/opac-account-pay.pl
opac/opac-account.pl
t/Koha/Plugin/Test.pm
t/db_dependent/Plugins.t

index 9c5ffad..a868d0f 100644 (file)
@@ -1,7 +1,7 @@
 [% USE Koha %]
 [% USE KohaDates %]
 [% USE Price %]
-[% SET ENABLE_OPAC_PAYMENTS = Koha.Preference('EnablePayPalOpacPayments') %]
+[% SET ENABLE_OPAC_PAYMENTS = Koha.Preference('EnablePayPalOpacPayments') || plugins %]
 [% SET DISPLAY_PAYMENT_BLOCK = 0 %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>[% IF ( LibraryNameTitle ) %][% LibraryNameTitle %][% ELSE %]Koha online[% END %] catalog &rsaquo; Your fines and charges</title>
                                 <fieldset class="pay-online hidden">
                                     <legend>Pay selected fines and charges</legend>
                                         <span class="help-block"><h3>Payment method</h3></span>
-                                        <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>
-                                        <!-- look to the future
-                                        <div class="control-group">
-                                            <label class="radio">
-                                                <input type="radio" name="payment_method" id="payment_method-paypal" value="paypal" checked="checked">
-                                                PayPal
-                                            </label>
-                                        </div>
-                                        -->
+
+                                        [% 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 %]" value="[% p.class %]" checked="checked">
+                                                    [% p.get_metadata.name %]
+                                                </label>
+                                            </div>
+                                        [% END %]
 
                                         <div class="control-group">
                                             <input type="hidden" id="payment-amount" name="payment_amount" value="0" />
diff --git a/opac/opac-account-pay-return.pl b/opac/opac-account-pay-return.pl
new file mode 100755 (executable)
index 0000000..73c5f56
--- /dev/null
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+
+# Copyright ByWater Solutions 2017
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use CGI;
+
+use C4::Auth;
+use Koha::Plugins::Handler;
+
+my $cgi = new CGI;
+
+my ( $userid, $cookie, $sessionID, $flags ) = checkauth( $cgi, 0, {}, 'opac' );
+
+# Check for payment method in both POST and GET vars
+my $payment_method = $cgi->param('payment_method') || $cgi->url_param('payment_method');
+
+my $can_handle_payment = Koha::Plugins::Handler->run(
+    {
+        class  => $payment_method,
+        method => 'opac_online_payment',
+        cgi    => $cgi,
+    }
+);
+
+if ($can_handle_payment) {
+    Koha::Plugins::Handler->run(
+        {
+            class  => $payment_method,
+            method => 'opac_online_payment_end',
+            cgi    => $cgi,
+        }
+    );
+}
+else {
+    print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
+    exit;
+}
index 6caebe2..6c8211d 100755 (executable)
@@ -31,10 +31,24 @@ use C4::Output;
 use C4::Context;
 use Koha::Acquisition::Currencies;
 use Koha::Database;
+use Koha::Plugins::Handler;
 
 my $cgi = new CGI;
+my $payment_method = $cgi->param('payment_method');
+my @accountlines   = $cgi->multi_param('accountline');
+
+my $use_plugin;
+if ( $payment_method ne 'paypal' ) {
+    $use_plugin = Koha::Plugins::Handler->run(
+        {
+            class  => $payment_method,
+            method => 'opac_online_payment',
+            cgi    => $cgi,
+        }
+    );
+}
 
-unless ( C4::Context->preference('EnablePayPalOpacPayments') ) {
+unless ( C4::Context->preference('EnablePayPalOpacPayments') || $use_plugin ) {
     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
     exit;
 }
@@ -49,9 +63,6 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     }
 );
 
-my $payment_method = $cgi->param('payment_method');
-my @accountlines = $cgi->multi_param('accountline');
-
 my $amount_to_pay =
   Koha::Database->new()->schema()->resultset('Accountline')->search( { accountlines_id => { -in => \@accountlines } } )
   ->get_column('amountoutstanding')->sum();
@@ -126,6 +137,15 @@ if ( $payment_method eq 'paypal' ) {
         $template->param( error => "PAYPAL_UNABLE_TO_CONNECT" );
         $error = 1;
     }
-}
 
-output_html_with_http_headers( $cgi, $cookie, $template->output ) if $error;
+    output_html_with_http_headers( $cgi, $cookie, $template->output ) if $error;
+}
+else {
+    Koha::Plugins::Handler->run(
+        {
+            class  => $payment_method,
+            method => 'opac_online_payment_begin',
+            cgi    => $cgi,
+        }
+    );
+}
index 9888f88..29d73a8 100755 (executable)
@@ -25,6 +25,7 @@ use C4::Members;
 use C4::Auth;
 use C4::Output;
 use Koha::Patrons;
+use Koha::Plugins;
 
 my $query = new CGI;
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
@@ -77,4 +78,14 @@ $template->param(
     payment_error => scalar $query->param('payment-error') || q{},
 );
 
+my $plugins_enabled = C4::Context->preference('UseKohaPlugins') && C4::Context->config("enable_plugins");
+if ( $plugins_enabled ) {
+       my @plugins = Koha::Plugins->new()->GetPlugins({
+               method => 'opac_online_payment',
+       });
+    # Only pass in plugins where opac online payment is enabled
+    @plugins = grep { $_->opac_online_payment } @plugins;
+       $template->param( plugins => \@plugins );
+}
+
 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
index 401ce3d..636554d 100644 (file)
@@ -43,6 +43,21 @@ sub to_marc {
     return "Koha::Plugin::Test::to_marc";
 }
 
+sub opac_online_payment {
+    my ( $self, $args ) = @_;
+    return "Koha::Plugin::Test::opac_online_payment";
+}
+
+sub opac_online_payment_begin {
+    my ( $self, $args ) = @_;
+    return "Koha::Plugin::Test::opac_online_payment_begin";
+}
+
+sub opac_online_payment_end {
+    my ( $self, $args ) = @_;
+    return "Koha::Plugin::Test::opac_online_payment_end";
+}
+
 sub configure {
     my ( $self, $args ) = @_;
     return "Koha::Plugin::Test::configure";;
index c9d24a7..dffcea7 100755 (executable)
@@ -2,7 +2,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 28;
+use Test::More tests => 31;
 use File::Basename;
 use File::Temp qw( tempdir );
 use FindBin qw($Bin);
@@ -31,6 +31,9 @@ isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
 ok( $plugin->can('report'), 'Test plugin can report' );
 ok( $plugin->can('tool'), 'Test plugin can tool' );
 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
+ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
+ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
+ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
 ok( $plugin->can('configure'), 'Test plugin can configure' );
 ok( $plugin->can('install'), 'Test plugin can install' );
 ok( $plugin->can('uninstall'), 'Test plugin can install' );