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
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.
25 use HTTP
::Request
::Common
;
27 use URL
::Encode
qw(url_encode url_params_mixed);
37 unless ( C4
::Context
->preference('EnablePayPalOpacPayments') ) {
38 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
44 template_name
=> "opac-account-pay-return.tt",
52 my $payment_method = $cgi->param('payment_method');
53 my @accountlines = $cgi->param('accountline');
56 Koha
::Database
->new()->schema()->resultset('Accountline')->search( { accountlines_id
=> { -in => \
@accountlines } } )
57 ->get_column('amountoutstanding')->sum();
58 $amount_to_pay = sprintf( "%.2f", $amount_to_pay );
61 if ( $payment_method eq 'paypal' ) {
62 my $ua = LWP
::UserAgent
->new;
64 my $amount = url_encode
($amount_to_pay);
67 C4
::Context
->preference('PayPalSandboxMode')
68 ?
'https://api-3t.sandbox.paypal.com/nvp'
69 : 'https://api-3t.paypal.com/nvp';
71 my $opac_base_url = C4
::Context
->preference('OPACBaseURL');
73 my $return_url = URI
->new( $opac_base_url . "/cgi-bin/koha/opac-account-pay-paypal-return.pl" );
74 $return_url->query_form( { amount
=> $amount, accountlines
=> \
@accountlines } );
76 my $cancel_url = URI
->new( $opac_base_url . "/cgi-bin/koha/opac-account.pl" );
79 'USER' => C4
::Context
->preference('PayPalUser'),
80 'PWD' => C4
::Context
->preference('PayPalPwd'),
81 'SIGNATURE' => C4
::Context
->preference('PayPalSignature'),
83 # API Version and Operation
84 'METHOD' => 'SetExpressCheckout',
87 # API specifics for SetExpressCheckout
89 'REQCONFIRMSHIPPING' => 0,
91 'BRANDNAME' => C4
::Context
->preference('LibraryName'),
92 'CANCELURL' => $cancel_url->as_string(),
93 'RETURNURL' => $return_url->as_string(),
94 'PAYMENTREQUEST_0_AMT' => $amount_to_pay,
95 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
96 'PAYMENTREQUEST_0_ALLOWEDPAYMENTMETHOD' => 'InstantPaymentOnly',
97 'PAYMENTREQUEST_0_DESC' => C4
::Context
->preference('PayPalChargeDescription'),
100 my $response = $ua->request( POST
$url, $nvp_params );
102 if ( $response->is_success ) {
103 my $params = url_params_mixed
( $response->decoded_content );
105 if ( $params->{ACK
} eq "Success" ) {
106 my $token = $params->{TOKEN
};
109 C4
::Context
->preference('PayPalSandboxMode')
110 ?
"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token="
111 : "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
112 print $cgi->redirect( $redirect_url . $token );
116 $template->param( error
=> "PAYPAL_ERROR_PROCESSING" );
122 $template->param( error
=> "PAYPAL_UNABLE_TO_CONNECT" );
127 output_html_with_http_headers
( $cgi, $cookie, $template->output ) if $error;