Bug 11622 - Add ability to pay fees and fines from OPAC via PayPal
[koha.git] / opac / opac-account-pay-paypal-return.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 strict;
21 use warnings;
22 use utf8;
23
24 use CGI;
25 use HTTP::Request::Common;
26 use LWP::UserAgent;
27 use URL::Encode qw(url_params_mixed);
28
29 use C4::Auth;
30 use C4::Output;
31 use C4::Accounts;
32 use C4::Members;
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 $token    = $cgi->param('token');
54 my $payer_id = $cgi->param('PayerID');
55 my $amount   = $cgi->param('amount');
56 my @accountlines = $cgi->param('accountlines');
57
58 my $ua = LWP::UserAgent->new;
59
60 my $url =
61   C4::Context->preference('PayPalSandboxMode')
62   ? 'https://api-3t.sandbox.paypal.com/nvp'
63   : 'https://api-3t.paypal.com/nvp';
64
65 my $nvp_params = {
66     'USER'      => C4::Context->preference('PayPalUser'),
67     'PWD'       => C4::Context->preference('PayPalPwd'),
68     'SIGNATURE' => C4::Context->preference('PayPalSignature'),
69
70     # API Version and Operation
71     'METHOD'  => 'DoExpressCheckoutPayment',
72     'VERSION' => '82.0',
73
74     # API specifics for DoExpressCheckout
75     'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
76     'PAYERID'                        => $payer_id,
77     'TOKEN'                          => $token,
78     'PAYMENTREQUEST_0_AMT'           => $amount,
79 };
80
81 my $response = $ua->request( POST $url, $nvp_params );
82
83 if ( $response->is_success ) {
84     my $params = url_params_mixed( $response->decoded_content );
85
86     if ( $params->{ACK} eq "Success" ) {
87         $amount = $params->{PAYMENTINFO_0_AMT};
88         $template->param( amount => $amount );
89
90         my $accountlines_rs = Koha::Database->new()->schema()->resultset('Accountline');
91         foreach my $accountlines_id ( @accountlines ) {
92             my $accountline = $accountlines_rs->find( $accountlines_id );
93             makepayment( $accountlines_id, $borrowernumber, undef, $accountline->amountoutstanding, undef, undef, 'PayPal' );
94         }
95     }
96     else {
97         $template->param( error => "PAYPAL_ERROR_PROCESSING" );
98     }
99
100 }
101 else {
102     $template->param( error => "PAYPAL_UNABLE_TO_CONNECT" );
103 }
104
105 $template->param(
106     borrower    => GetMemberDetails($borrowernumber),
107     accountview => 1
108 );
109
110 output_html_with_http_headers( $cgi, $cookie, $template->output );