ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / opac / opac-account-pay.pl
index 8dfdfcd..6c8211d 100755 (executable)
@@ -24,7 +24,6 @@ use Modern::Perl;
 use CGI;
 use HTTP::Request::Common;
 use LWP::UserAgent;
-use URL::Encode qw(url_encode url_params_mixed);
 use URI;
 
 use C4::Auth;
@@ -32,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;
 }
@@ -50,9 +63,6 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     }
 );
 
-my $payment_method = $cgi->param('payment_method');
-my @accountlines = $cgi->param('accountline');
-
 my $amount_to_pay =
   Koha::Database->new()->schema()->resultset('Accountline')->search( { accountlines_id => { -in => \@accountlines } } )
   ->get_column('amountoutstanding')->sum();
@@ -64,8 +74,6 @@ my $error = 0;
 if ( $payment_method eq 'paypal' ) {
     my $ua = LWP::UserAgent->new;
 
-    my $amount = url_encode($amount_to_pay);
-
     my $url =
       C4::Context->preference('PayPalSandboxMode')
       ? 'https://api-3t.sandbox.paypal.com/nvp'
@@ -74,7 +82,7 @@ if ( $payment_method eq 'paypal' ) {
     my $opac_base_url = C4::Context->preference('OPACBaseURL');
 
     my $return_url = URI->new( $opac_base_url . "/cgi-bin/koha/opac-account-pay-paypal-return.pl" );
-    $return_url->query_form( { amount => $amount, accountlines => \@accountlines } );
+    $return_url->query_form( { amount => $amount_to_pay, accountlines => \@accountlines } );
 
     my $cancel_url = URI->new( $opac_base_url . "/cgi-bin/koha/opac-account.pl" );
 
@@ -99,15 +107,18 @@ if ( $payment_method eq 'paypal' ) {
         'PAYMENTREQUEST_0_PAYMENTACTION'        => 'Sale',
         'PAYMENTREQUEST_0_ALLOWEDPAYMENTMETHOD' => 'InstantPaymentOnly',
         'PAYMENTREQUEST_0_DESC'                 => C4::Context->preference('PayPalChargeDescription'),
+        'SOLUTIONTYPE'                          => 'Sole',
     };
 
     my $response = $ua->request( POST $url, $nvp_params );
 
     if ( $response->is_success ) {
-        my $params = url_params_mixed( $response->decoded_content );
 
-        if ( $params->{ACK} eq "Success" ) {
-            my $token = $params->{TOKEN};
+        my $urlencoded = $response->content;
+        my %params = URI->new( "?$urlencoded" )->query_form;
+
+        if ( $params{ACK} eq "Success" ) {
+            my $token = $params{TOKEN};
 
             my $redirect_url =
               C4::Context->preference('PayPalSandboxMode')
@@ -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,
+        }
+    );
+}