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
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.
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.
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>.
25 use HTTP
::Request
::Common
;
32 use Koha
::Acquisition
::Currencies
;
34 use Koha
::Plugins
::Handler
;
37 my $payment_method = $cgi->param('payment_method');
38 my @accountlines = $cgi->multi_param('accountline');
41 if ( $payment_method ne 'paypal' ) {
42 $use_plugin = Koha
::Plugins
::Handler
->run(
44 class => $payment_method,
45 method
=> 'opac_online_payment',
51 unless ( C4
::Context
->preference('EnablePayPalOpacPayments') || $use_plugin ) {
52 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
58 template_name
=> "opac-account-pay-error.tt",
66 Koha
::Database
->new()->schema()->resultset('Accountline')->search( { accountlines_id
=> { -in => \
@accountlines } } )
67 ->get_column('amountoutstanding')->sum();
68 $amount_to_pay = sprintf( "%.2f", $amount_to_pay );
70 my $active_currency = Koha
::Acquisition
::Currencies
->get_active;
73 if ( $payment_method eq 'paypal' ) {
74 my $ua = LWP
::UserAgent
->new;
77 C4
::Context
->preference('PayPalSandboxMode')
78 ?
'https://api-3t.sandbox.paypal.com/nvp'
79 : 'https://api-3t.paypal.com/nvp';
82 C4
::Context
->preference('PayPalReturnURL') eq 'BaseURL'
83 ? C4
::Context
->preference('OPACBaseURL')
84 : $cgi->url(-base
=>1);
86 my $return_url = URI
->new( $opac_base_url . "/cgi-bin/koha/opac-account-pay-paypal-return.pl" );
87 $return_url->query_form( { amount
=> $amount_to_pay, accountlines
=> \
@accountlines } );
89 my $cancel_url = URI
->new( $opac_base_url . "/cgi-bin/koha/opac-account.pl" );
92 'USER' => C4
::Context
->preference('PayPalUser'),
93 'PWD' => C4
::Context
->preference('PayPalPwd'),
94 'SIGNATURE' => C4
::Context
->preference('PayPalSignature'),
96 # API Version and Operation
97 'METHOD' => 'SetExpressCheckout',
100 # API specifics for SetExpressCheckout
102 'REQCONFIRMSHIPPING' => 0,
104 'BRANDNAME' => C4
::Context
->preference('LibraryName'),
105 'CANCELURL' => $cancel_url->as_string(),
106 'RETURNURL' => $return_url->as_string(),
107 'PAYMENTREQUEST_0_CURRENCYCODE' => $active_currency->currency,
108 'PAYMENTREQUEST_0_AMT' => $amount_to_pay,
109 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
110 'PAYMENTREQUEST_0_ALLOWEDPAYMENTMETHOD' => 'InstantPaymentOnly',
111 'PAYMENTREQUEST_0_DESC' => C4
::Context
->preference('PayPalChargeDescription'),
112 'SOLUTIONTYPE' => 'Sole',
115 my $response = $ua->request( POST
$url, $nvp_params );
117 if ( $response->is_success ) {
119 my $urlencoded = $response->content;
120 my %params = URI
->new( "?$urlencoded" )->query_form;
122 if ( $params{ACK
} eq "Success" ) {
123 my $token = $params{TOKEN
};
126 C4
::Context
->preference('PayPalSandboxMode')
127 ?
"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token="
128 : "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
129 print $cgi->redirect( $redirect_url . $token );
133 $template->param( error
=> "PAYPAL_ERROR_PROCESSING" );
139 $template->param( error
=> "PAYPAL_UNABLE_TO_CONNECT" );
143 output_html_with_http_headers
( $cgi, $cookie, $template->output, undef, { force_no_caching
=> 1 } ) if $error;
146 Koha
::Plugins
::Handler
->run(
148 class => $payment_method,
149 method
=> 'opac_online_payment_begin',