Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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 Modern::Perl;
21 use utf8;
22
23 use CGI;
24 use HTTP::Request::Common;
25 use LWP::UserAgent;
26 use URI;
27
28 use C4::Auth;
29 use C4::Output;
30 use C4::Accounts;
31 use Koha::Acquisition::Currencies;
32 use Koha::Database;
33 use Koha::Patrons;
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         debug           => 1,
49     }
50 );
51
52 my $active_currency = Koha::Acquisition::Currencies->get_active;
53
54 my $token    = $cgi->param('token');
55 my $payer_id = $cgi->param('PayerID');
56 my $amount   = $cgi->param('amount');
57 my @accountlines = $cgi->multi_param('accountlines');
58
59 my $ua = LWP::UserAgent->new;
60
61 my $url =
62   C4::Context->preference('PayPalSandboxMode')
63   ? 'https://api-3t.sandbox.paypal.com/nvp'
64   : 'https://api-3t.paypal.com/nvp';
65
66 my $nvp_params = {
67     'USER'      => C4::Context->preference('PayPalUser'),
68     'PWD'       => C4::Context->preference('PayPalPwd'),
69     'SIGNATURE' => C4::Context->preference('PayPalSignature'),
70
71     # API Version and Operation
72     'METHOD'  => 'DoExpressCheckoutPayment',
73     'VERSION' => '82.0',
74
75     # API specifics for DoExpressCheckout
76     'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
77     'PAYERID'                        => $payer_id,
78     'TOKEN'                          => $token,
79     'PAYMENTREQUEST_0_AMT'           => $amount,
80     'PAYMENTREQUEST_0_CURRENCYCODE'  => $active_currency->currency,
81 };
82
83 my $response = $ua->request( POST $url, $nvp_params );
84
85 my $error = q{};
86 if ( $response->is_success ) {
87
88     my $urlencoded = $response->content;
89     my %params = URI->new( "?$urlencoded" )->query_form;
90
91
92     if ( $params{ACK} eq "Success" ) {
93         $amount = $params{PAYMENTINFO_0_AMT};
94
95         my $account = Koha::Account->new( { patron_id => $borrowernumber } );
96         my @lines = Koha::Account::Lines->search(
97             {
98                 accountlines_id => { -in => \@accountlines }
99             }
100         );
101
102         $account->pay(
103             {
104                 amount => $amount,
105                 lines  => \@lines,
106                 note   => 'PayPal',
107                 interface => C4::Context->interface
108             }
109         );
110     }
111     else {
112        $error = "PAYPAL_ERROR_PROCESSING";
113     }
114
115 }
116 else {
117     $error = "PAYPAL_UNABLE_TO_CONNECT";
118 }
119
120 my $patron = Koha::Patrons->find( $borrowernumber );
121 $template->param(
122     borrower    => $patron->unblessed,
123     accountview => 1
124 );
125
126 print $cgi->redirect("/cgi-bin/koha/opac-account.pl?payment=$amount&payment-error=$error");