Bug 7317: (QA followup) Make query parameters consistent with other endpoints
[koha.git] / opac / opac-account-pay.pl
1 #!/usr/bin/perl
2
3 # Copyright ByWater Solutions 2015
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use utf8;
21
22 use Modern::Perl;
23
24 use CGI;
25 use HTTP::Request::Common;
26 use LWP::UserAgent;
27 use URI;
28
29 use C4::Auth;
30 use C4::Output;
31 use C4::Context;
32 use Koha::Acquisition::Currencies;
33 use Koha::Database;
34 use Koha::Plugins::Handler;
35
36 my $cgi = new CGI;
37 my $payment_method = $cgi->param('payment_method');
38 my @accountlines   = $cgi->multi_param('accountline');
39
40 my $use_plugin;
41 if ( $payment_method ne 'paypal' ) {
42     $use_plugin = Koha::Plugins::Handler->run(
43         {
44             class  => $payment_method,
45             method => 'opac_online_payment',
46             cgi    => $cgi,
47         }
48     );
49 }
50
51 unless ( C4::Context->preference('EnablePayPalOpacPayments') || $use_plugin ) {
52     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
53     exit;
54 }
55
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
57     {
58         template_name   => "opac-account-pay-error.tt",
59         query           => $cgi,
60         type            => "opac",
61         authnotrequired => 0,
62         debug           => 1,
63     }
64 );
65
66 my $amount_to_pay =
67   Koha::Database->new()->schema()->resultset('Accountline')->search( { accountlines_id => { -in => \@accountlines } } )
68   ->get_column('amountoutstanding')->sum();
69 $amount_to_pay = sprintf( "%.2f", $amount_to_pay );
70
71 my $active_currency = Koha::Acquisition::Currencies->get_active;
72
73 my $error = 0;
74 if ( $payment_method eq 'paypal' ) {
75     my $ua = LWP::UserAgent->new;
76
77     my $url =
78       C4::Context->preference('PayPalSandboxMode')
79       ? 'https://api-3t.sandbox.paypal.com/nvp'
80       : 'https://api-3t.paypal.com/nvp';
81
82     my $opac_base_url = C4::Context->preference('OPACBaseURL');
83
84     my $return_url = URI->new( $opac_base_url . "/cgi-bin/koha/opac-account-pay-paypal-return.pl" );
85     $return_url->query_form( { amount => $amount_to_pay, accountlines => \@accountlines } );
86
87     my $cancel_url = URI->new( $opac_base_url . "/cgi-bin/koha/opac-account.pl" );
88
89     my $nvp_params = {
90         'USER'      => C4::Context->preference('PayPalUser'),
91         'PWD'       => C4::Context->preference('PayPalPwd'),
92         'SIGNATURE' => C4::Context->preference('PayPalSignature'),
93
94         # API Version and Operation
95         'METHOD'  => 'SetExpressCheckout',
96         'VERSION' => '82.0',
97
98         # API specifics for SetExpressCheckout
99         'NOSHIPPING'                            => 1,
100         'REQCONFIRMSHIPPING'                    => 0,
101         'ALLOWNOTE'                             => 0,
102         'BRANDNAME'                             => C4::Context->preference('LibraryName'),
103         'CANCELURL'                             => $cancel_url->as_string(),
104         'RETURNURL'                             => $return_url->as_string(),
105         'PAYMENTREQUEST_0_CURRENCYCODE'         => $active_currency->currency,
106         'PAYMENTREQUEST_0_AMT'                  => $amount_to_pay,
107         'PAYMENTREQUEST_0_PAYMENTACTION'        => 'Sale',
108         'PAYMENTREQUEST_0_ALLOWEDPAYMENTMETHOD' => 'InstantPaymentOnly',
109         'PAYMENTREQUEST_0_DESC'                 => C4::Context->preference('PayPalChargeDescription'),
110         'SOLUTIONTYPE'                          => 'Sole',
111     };
112
113     my $response = $ua->request( POST $url, $nvp_params );
114
115     if ( $response->is_success ) {
116
117         my $urlencoded = $response->content;
118         my %params = URI->new( "?$urlencoded" )->query_form;
119
120         if ( $params{ACK} eq "Success" ) {
121             my $token = $params{TOKEN};
122
123             my $redirect_url =
124               C4::Context->preference('PayPalSandboxMode')
125               ? "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token="
126               : "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
127             print $cgi->redirect( $redirect_url . $token );
128
129         }
130         else {
131             $template->param( error => "PAYPAL_ERROR_PROCESSING" );
132             $error = 1;
133         }
134
135     }
136     else {
137         $template->param( error => "PAYPAL_UNABLE_TO_CONNECT" );
138         $error = 1;
139     }
140
141     output_html_with_http_headers( $cgi, $cookie, $template->output ) if $error;
142 }
143 else {
144     Koha::Plugins::Handler->run(
145         {
146             class  => $payment_method,
147             method => 'opac_online_payment_begin',
148             cgi    => $cgi,
149         }
150     );
151 }