Bug 11622 - Add ability to pay fees and fines from OPAC via PayPal
[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 URL::Encode qw(url_encode url_params_mixed);
28 use URI;
29
30 use C4::Auth;
31 use C4::Output;
32 use C4::Context;
33 use Koha::Database;
34
35 my $cgi = new CGI;
36
37 unless ( C4::Context->preference('EnablePayPalOpacPayments') ) {
38     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
39     exit;
40 }
41
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43     {
44         template_name   => "opac-account-pay-return.tt",
45         query           => $cgi,
46         type            => "opac",
47         authnotrequired => 0,
48         flagsrequired   => { borrow => 1 },
49         debug           => 1,
50     }
51 );
52
53 my $payment_method = $cgi->param('payment_method');
54 my @accountlines = $cgi->param('accountline');
55
56 my $amount_to_pay =
57   Koha::Database->new()->schema()->resultset('Accountline')->search( { accountlines_id => { -in => \@accountlines } } )
58   ->get_column('amountoutstanding')->sum();
59 $amount_to_pay = sprintf( "%.2f", $amount_to_pay );
60
61 my $error = 0;
62 if ( $payment_method eq 'paypal' ) {
63     my $ua = LWP::UserAgent->new;
64
65     my $amount = url_encode($amount_to_pay);
66
67     my $url =
68       C4::Context->preference('PayPalSandboxMode')
69       ? 'https://api-3t.sandbox.paypal.com/nvp'
70       : 'https://api-3t.paypal.com/nvp';
71
72     my $opac_base_url = C4::Context->preference('OPACBaseURL');
73
74     my $return_url = URI->new( $opac_base_url . "/cgi-bin/koha/opac-account-pay-paypal-return.pl" );
75     $return_url->query_form( { amount => $amount, accountlines => \@accountlines } );
76
77     my $cancel_url = URI->new( $opac_base_url . "/cgi-bin/koha/opac-account.pl" );
78
79     my $nvp_params = {
80         'USER'      => C4::Context->preference('PayPalUser'),
81         'PWD'       => C4::Context->preference('PayPalPwd'),
82         'SIGNATURE' => C4::Context->preference('PayPalSignature'),
83
84         # API Version and Operation
85         'METHOD'  => 'SetExpressCheckout',
86         'VERSION' => '82.0',
87
88         # API specifics for SetExpressCheckout
89         'NOSHIPPING'                            => 1,
90         'REQCONFIRMSHIPPING'                    => 0,
91         'ALLOWNOTE'                             => 0,
92         'BRANDNAME'                             => C4::Context->preference('LibraryName'),
93         'CANCELURL'                             => $cancel_url->as_string(),
94         'RETURNURL'                             => $return_url->as_string(),
95         'PAYMENTREQUEST_0_AMT'                  => $amount_to_pay,
96         'PAYMENTREQUEST_0_PAYMENTACTION'        => 'Sale',
97         'PAYMENTREQUEST_0_ALLOWEDPAYMENTMETHOD' => 'InstantPaymentOnly',
98         'PAYMENTREQUEST_0_DESC'                 => C4::Context->preference('PayPalChargeDescription'),
99     };
100
101     my $response = $ua->request( POST $url, $nvp_params );
102
103     if ( $response->is_success ) {
104         my $params = url_params_mixed( $response->decoded_content );
105
106         if ( $params->{ACK} eq "Success" ) {
107             my $token = $params->{TOKEN};
108
109             my $redirect_url =
110               C4::Context->preference('PayPalSandboxMode')
111               ? "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token="
112               : "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
113             print $cgi->redirect( $redirect_url . $token );
114
115         }
116         else {
117             $template->param( error => "PAYPAL_ERROR_PROCESSING" );
118             $error = 1;
119         }
120
121     }
122     else {
123         $template->param( error => "PAYPAL_UNABLE_TO_CONNECT" );
124         $error = 1;
125     }
126 }
127
128 output_html_with_http_headers( $cgi, $cookie, $template->output ) if $error;