Koha/opac/opac-account-pay-paypal-return.pl
Kyle M Hall 85c5efa0c7 Bug 11622 - Add ability to pay fees and fines from OPAC via PayPal
This patch adds the ability for a logged in user to pay fines and
fees from the OPAC via PayPal.

Test Plan:
 1) Apply this patch
 2) Create a paypal developer account
 3) Create two test accounts, a Personal account and a Business account
 4) Enable PayPal in Sandbox mode via the system preferences.
 5) Enter the business account API credentials into the new system
    preferences.
 6) Create a new patron, add some fines/fees
 7) Log in as that patron in the OPAC
 8) Choose to pay via PayPal, log in as the sandbox Personal account
 9) Complete the transaction
10) Note the fee is now paid

Signed-off-by: Carol Corrales <ccorrales@losgatosca.gov>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2015-12-31 13:27:14 +00:00

110 lines
3.2 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright ByWater Solutions 2015
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use strict;
use warnings;
use utf8;
use CGI;
use HTTP::Request::Common;
use LWP::UserAgent;
use URL::Encode qw(url_params_mixed);
use C4::Auth;
use C4::Output;
use C4::Accounts;
use C4::Members;
use Koha::Database;
my $cgi = new CGI;
unless ( C4::Context->preference('EnablePayPalOpacPayments') ) {
print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
exit;
}
my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
{
template_name => "opac-account-pay-return.tt",
query => $cgi,
type => "opac",
authnotrequired => 0,
flagsrequired => { borrow => 1 },
debug => 1,
}
);
my $token = $cgi->param('token');
my $payer_id = $cgi->param('PayerID');
my $amount = $cgi->param('amount');
my @accountlines = $cgi->param('accountlines');
my $ua = LWP::UserAgent->new;
my $url =
C4::Context->preference('PayPalSandboxMode')
? 'https://api-3t.sandbox.paypal.com/nvp'
: 'https://api-3t.paypal.com/nvp';
my $nvp_params = {
'USER' => C4::Context->preference('PayPalUser'),
'PWD' => C4::Context->preference('PayPalPwd'),
'SIGNATURE' => C4::Context->preference('PayPalSignature'),
# API Version and Operation
'METHOD' => 'DoExpressCheckoutPayment',
'VERSION' => '82.0',
# API specifics for DoExpressCheckout
'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
'PAYERID' => $payer_id,
'TOKEN' => $token,
'PAYMENTREQUEST_0_AMT' => $amount,
};
my $response = $ua->request( POST $url, $nvp_params );
if ( $response->is_success ) {
my $params = url_params_mixed( $response->decoded_content );
if ( $params->{ACK} eq "Success" ) {
$amount = $params->{PAYMENTINFO_0_AMT};
$template->param( amount => $amount );
my $accountlines_rs = Koha::Database->new()->schema()->resultset('Accountline');
foreach my $accountlines_id ( @accountlines ) {
my $accountline = $accountlines_rs->find( $accountlines_id );
makepayment( $accountlines_id, $borrowernumber, undef, $accountline->amountoutstanding, undef, undef, 'PayPal' );
}
}
else {
$template->param( error => "PAYPAL_ERROR_PROCESSING" );
}
}
else {
$template->param( error => "PAYPAL_UNABLE_TO_CONNECT" );
}
$template->param(
borrower => GetMemberDetails($borrowernumber),
accountview => 1
);
output_html_with_http_headers( $cgi, $cookie, $template->output );