Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# Copyright Katipo Communications 2002 |
4 |
# Copyright Biblibre 2007,2010 |
5 |
# |
6 |
# This file is part of Koha. |
7 |
# |
8 |
# Koha is free software; you can redistribute it and/or modify it under the |
9 |
# terms of the GNU General Public License as published by the Free Software |
10 |
# Foundation; either version 3 of the License, or (at your option) any later |
11 |
# version. |
12 |
# |
13 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
14 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
15 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License along |
18 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
19 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
|
21 |
use utf8; |
22 |
|
23 |
use Modern::Perl; |
24 |
|
25 |
use CGI; |
26 |
use HTTP::Request::Common; |
27 |
use LWP::UserAgent; |
28 |
use URL::Encode qw(url_encode url_params_mixed); |
29 |
use URI; |
30 |
|
31 |
use C4::Auth; |
32 |
use C4::Output; |
33 |
use C4::Context; |
34 |
use Koha::Database; |
35 |
|
36 |
my $cgi = new CGI; |
37 |
|
38 |
unless ( C4::Context->preference('EnablePayPalOpacPayments') ) { |
39 |
print $cgi->redirect("/cgi-bin/koha/errors/404.pl"); |
40 |
exit; |
41 |
} |
42 |
|
43 |
my ( $template, $borrowernumber, $cookie ) = get_template_and_user( |
44 |
{ |
45 |
template_name => "opac-account-pay-return.tt", |
46 |
query => $cgi, |
47 |
type => "opac", |
48 |
authnotrequired => 0, |
49 |
flagsrequired => { borrow => 1 }, |
50 |
debug => 1, |
51 |
} |
52 |
); |
53 |
|
54 |
my $payment_method = $cgi->param('payment_method'); |
55 |
my @accountlines = $cgi->param('accountline'); |
56 |
|
57 |
my $amount_to_pay = |
58 |
Koha::Database->new()->schema()->resultset('Accountline')->search( { accountlines_id => { -in => \@accountlines } } ) |
59 |
->get_column('amountoutstanding')->sum(); |
60 |
$amount_to_pay = sprintf( "%.2f", $amount_to_pay ); |
61 |
|
62 |
my $error = 0; |
63 |
if ( $payment_method eq 'paypal' ) { |
64 |
my $ua = LWP::UserAgent->new; |
65 |
|
66 |
my $amount = url_encode($amount_to_pay); |
67 |
|
68 |
my $url = |
69 |
C4::Context->preference('PayPalSandboxMode') |
70 |
? 'https://api-3t.sandbox.paypal.com/nvp' |
71 |
: 'https://api-3t.paypal.com/nvp'; |
72 |
|
73 |
my $opac_base_url = C4::Context->preference('OPACBaseURL'); |
74 |
|
75 |
my $return_url = URI->new( $opac_base_url . "/cgi-bin/koha/opac-account-pay-paypal-return.pl" ); |
76 |
$return_url->query_form( { amount => $amount, accountlines => \@accountlines } ); |
77 |
|
78 |
my $cancel_url = URI->new( $opac_base_url . "/cgi-bin/koha/opac-account.pl" ); |
79 |
|
80 |
my $nvp_params = { |
81 |
'USER' => C4::Context->preference('PayPalUser'), |
82 |
'PWD' => C4::Context->preference('PayPalPwd'), |
83 |
'SIGNATURE' => C4::Context->preference('PayPalSignature'), |
84 |
|
85 |
# API Version and Operation |
86 |
'METHOD' => 'SetExpressCheckout', |
87 |
'VERSION' => '82.0', |
88 |
|
89 |
# API specifics for SetExpressCheckout |
90 |
'NOSHIPPING' => 1, |
91 |
'REQCONFIRMSHIPPING' => 0, |
92 |
'ALLOWNOTE' => 0, |
93 |
'BRANDNAME' => C4::Context->preference('LibraryName'), |
94 |
'CANCELURL' => $cancel_url->as_string(), |
95 |
'RETURNURL' => $return_url->as_string(), |
96 |
'PAYMENTREQUEST_0_AMT' => $amount_to_pay, |
97 |
'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', |
98 |
'PAYMENTREQUEST_0_ALLOWEDPAYMENTMETHOD' => 'InstantPaymentOnly', |
99 |
'PAYMENTREQUEST_0_DESC' => C4::Context->preference('PayPalChargeDescription'), |
100 |
}; |
101 |
|
102 |
my $response = $ua->request( POST $url, $nvp_params ); |
103 |
|
104 |
if ( $response->is_success ) { |
105 |
my $params = url_params_mixed( $response->decoded_content ); |
106 |
|
107 |
if ( $params->{ACK} eq "Success" ) { |
108 |
my $token = $params->{TOKEN}; |
109 |
|
110 |
my $redirect_url = |
111 |
C4::Context->preference('PayPalSandboxMode') |
112 |
? "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" |
113 |
: "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token="; |
114 |
print $cgi->redirect( $redirect_url . $token ); |
115 |
|
116 |
} |
117 |
else { |
118 |
$template->param( error => "PAYPAL_ERROR_PROCESSING" ); |
119 |
$error = 1; |
120 |
} |
121 |
|
122 |
} |
123 |
else { |
124 |
$template->param( error => "PAYPAL_UNABLE_TO_CONNECT" ); |
125 |
$error = 1; |
126 |
} |
127 |
} |
128 |
|
129 |
output_html_with_http_headers( $cgi, $cookie, $template->output ) if $error; |