5fb9b8c1a39f53aa7c4721d9f1229bb49d1bd51f
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 = CGI->new;
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         debug           => 1,
48     }
49 );
50
51 my $active_currency = Koha::Acquisition::Currencies->get_active;
52
53 my $token    = $cgi->param('token');
54 my $payer_id = $cgi->param('PayerID');
55 my $amount   = $cgi->param('amount');
56 my @accountlines = $cgi->multi_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     'PAYMENTREQUEST_0_CURRENCYCODE'  => $active_currency->currency,
80 };
81
82 my $response = $ua->request( POST $url, $nvp_params );
83
84 my $error = q{};
85 if ( $response->is_success ) {
86
87     my $urlencoded = $response->content;
88     my %params = URI->new( "?$urlencoded" )->query_form;
89
90
91     if ( $params{ACK} eq "Success" ) {
92         $amount = $params{PAYMENTINFO_0_AMT};
93
94         my $account = Koha::Account->new( { patron_id => $borrowernumber } );
95         my @lines = Koha::Account::Lines->search(
96             {
97                 accountlines_id => { -in => \@accountlines }
98             }
99         );
100
101         $account->pay(
102             {
103                 amount => $amount,
104                 lines  => \@lines,
105                 note   => 'PayPal',
106                 interface => C4::Context->interface
107             }
108         );
109     }
110     else {
111        $error = "PAYPAL_ERROR_PROCESSING";
112     }
113
114 }
115 else {
116     $error = "PAYPAL_UNABLE_TO_CONNECT";
117 }
118
119 my $patron = Koha::Patrons->find( $borrowernumber );
120 $template->param(
121     borrower    => $patron->unblessed,
122     accountview => 1
123 );
124
125 print $cgi->redirect("/cgi-bin/koha/opac-account.pl?payment=$amount&payment-error=$error");