Bug 17785: Fix OAI-PMH's XSLT-generated URLs under Plack
[koha.git] / opac / opac-account-pay-paypal-return.pl
blob77b50d86de3af9933755989784df82bd3684823e
1 #!/usr/bin/perl
3 # Copyright ByWater Solutions 2015
5 # This file is part of Koha.
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.
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.
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.
20 use strict;
21 use warnings;
22 use utf8;
24 use CGI;
25 use HTTP::Request::Common;
26 use LWP::UserAgent;
27 use URI;
29 use C4::Auth;
30 use C4::Output;
31 use C4::Accounts;
32 use C4::Members;
33 use Koha::Acquisition::Currencies;
34 use Koha::Database;
36 my $cgi = new CGI;
38 unless ( C4::Context->preference('EnablePayPalOpacPayments') ) {
39 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
40 exit;
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45 template_name => "opac-account-pay-return.tt",
46 query => $cgi,
47 type => "opac",
48 authnotrequired => 0,
49 debug => 1,
53 my $active_currency = Koha::Acquisition::Currencies->get_active;
55 my $token = $cgi->param('token');
56 my $payer_id = $cgi->param('PayerID');
57 my $amount = $cgi->param('amount');
58 my @accountlines = $cgi->multi_param('accountlines');
60 my $ua = LWP::UserAgent->new;
62 my $url =
63 C4::Context->preference('PayPalSandboxMode')
64 ? 'https://api-3t.sandbox.paypal.com/nvp'
65 : 'https://api-3t.paypal.com/nvp';
67 my $nvp_params = {
68 'USER' => C4::Context->preference('PayPalUser'),
69 'PWD' => C4::Context->preference('PayPalPwd'),
70 'SIGNATURE' => C4::Context->preference('PayPalSignature'),
72 # API Version and Operation
73 'METHOD' => 'DoExpressCheckoutPayment',
74 'VERSION' => '82.0',
76 # API specifics for DoExpressCheckout
77 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
78 'PAYERID' => $payer_id,
79 'TOKEN' => $token,
80 'PAYMENTREQUEST_0_AMT' => $amount,
81 'PAYMENTREQUEST_0_CURRENCYCODE' => $active_currency->currency,
84 my $response = $ua->request( POST $url, $nvp_params );
86 my $error = q{};
87 if ( $response->is_success ) {
89 my $urlencoded = $response->content;
90 my %params = URI->new( "?$urlencoded" )->query_form;
93 if ( $params{ACK} eq "Success" ) {
94 $amount = $params{PAYMENTINFO_0_AMT};
96 my $accountlines_rs = Koha::Database->new()->schema()->resultset('Accountline');
97 foreach my $accountlines_id ( @accountlines ) {
98 my $accountline = $accountlines_rs->find( $accountlines_id );
99 makepayment( $accountlines_id, $borrowernumber, undef, $accountline->amountoutstanding, undef, undef, 'PayPal' );
102 else {
103 $error = "PAYPAL_ERROR_PROCESSING";
107 else {
108 $error = "PAYPAL_UNABLE_TO_CONNECT";
111 $template->param(
112 borrower => GetMember( borrowernumber => $borrowernumber ),
113 accountview => 1
116 print $cgi->redirect("/cgi-bin/koha/opac-account.pl?payment=$amount&payment-error=$error");