|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Parts Copyright (C) 2013 Mark Tompsett |
| 6 |
# |
| 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. |
| 11 |
# |
| 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. |
| 16 |
# |
| 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>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use CGI qw ( -utf8 ); |
| 22 |
use LWP::UserAgent; |
| 23 |
use C4::Members; |
| 24 |
use C4::Auth; # get_template_and_user |
| 25 |
use C4::Output; |
| 26 |
use C4::Context; |
| 27 |
use C4::Payment; |
| 28 |
use C4::Accounts; |
| 29 |
|
| 30 |
use Digest::MD5 qw ( md5_hex ); |
| 31 |
use DateTime::Format::MySQL; |
| 32 |
use Carp; |
| 33 |
|
| 34 |
my $cgi = new CGI; |
| 35 |
my $dbh = C4::Context->dbh; |
| 36 |
my $script_name = '/cgi-bin/koha/opac-pay-dibs.pl'; |
| 37 |
my $baseurl = C4::Context->preference('OPACBaseURL') |
| 38 |
or croak |
| 39 |
'OPACBaseURL not defined. Required for OPAC online payments to work.'; |
| 40 |
my $action = $cgi->param('action'); |
| 41 |
|
| 42 |
if ( $action eq 'callback' ) { |
| 43 |
my $orderid = $cgi->param('orderid'); |
| 44 |
my $authkey = $cgi->param('authkey'); |
| 45 |
|
| 46 |
# DIBS-specific authentication check |
| 47 |
my $k1 = C4::Context->preference('OpacPaymentK1'); |
| 48 |
my $k2 = C4::Context->preference('OpacPaymentK2'); |
| 49 |
my $my_authkey = md5_hex( |
| 50 |
$k2 |
| 51 |
. md5_hex( |
| 52 |
$k1 |
| 53 |
. 'transact=' |
| 54 |
. $cgi->param('transact') |
| 55 |
. '&amount=' |
| 56 |
. $cgi->param('amount') |
| 57 |
. '¤cy=' |
| 58 |
. $cgi->param('currency') |
| 59 |
) |
| 60 |
); |
| 61 |
|
| 62 |
if ( $authkey ne $my_authkey ) { |
| 63 |
print $cgi->header( |
| 64 |
-type=>'text/plain', |
| 65 |
-status=> '403 invalid authentication key' |
| 66 |
); |
| 67 |
exit; |
| 68 |
} |
| 69 |
|
| 70 |
# Authentication passed |
| 71 |
|
| 72 |
my $order = GetOrder($orderid); |
| 73 |
my $orderdetails = GetOrderDetails($orderid); |
| 74 |
|
| 75 |
# make payments for each accountline |
| 76 |
for my $od (@$orderdetails) { |
| 77 |
makepayment( |
| 78 |
$od->{'accountlines_id'}, |
| 79 |
$od->{'borrowernumber'}, |
| 80 |
$od->{'accountno'}, |
| 81 |
$od->{'amount'}, |
| 82 |
undef, # user |
| 83 |
C4::Context->userenv ? C4::Context->userenv->{branch} : undef, |
| 84 |
'Online payment by user', # payment note |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
$order->{'status'} = 'payed'; |
| 89 |
$order->{'modificationdate'} = |
| 90 |
DateTime::Format::MySQL->format_datetime( DateTime->now ); |
| 91 |
ModOrder($order); |
| 92 |
|
| 93 |
print $cgi->header( |
| 94 |
-type=>'text/plain', |
| 95 |
-status=> '200' |
| 96 |
); |
| 97 |
exit; |
| 98 |
} |
| 99 |
|
| 100 |
my ( $template, $borrowernumber, $cookie ) = get_template_and_user( |
| 101 |
{ |
| 102 |
template_name => 'opac-pay-dibs.tt', |
| 103 |
type => 'opac', |
| 104 |
query => $cgi, |
| 105 |
authnotrequired => ( C4::Context->preference('OpacPublic') ? 1 : 0 ), |
| 106 |
} |
| 107 |
); |
| 108 |
|
| 109 |
# get borrower information .... |
| 110 |
my $borr = GetMemberDetails($borrowernumber); |
| 111 |
|
| 112 |
if ( $action eq 'checkout' ) { |
| 113 |
#get account details |
| 114 |
my @acctids = $cgi->multi_param('accountlines_id'); |
| 115 |
my ( $total, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber); |
| 116 |
my %accts_h = map { $_->{'accountlines_id'} => $_ } @$accts; |
| 117 |
my @acctlines = map { $accts_h{$_} } @acctids; |
| 118 |
|
| 119 |
my $subunit = C4::Context->preference('OpacPaymentCurrencySubunit') || 1; |
| 120 |
my $amount = 0; |
| 121 |
$amount += $_->{'amountoutstanding'} for @acctlines; |
| 122 |
|
| 123 |
# Generate a new order id and add to db |
| 124 |
my $orderid = AddOrder( |
| 125 |
{ |
| 126 |
borrowernumber => $borrowernumber, |
| 127 |
amount => $amount, |
| 128 |
currency => C4::Context->preference('OpacPaymentCurrency') |
| 129 |
} |
| 130 |
); |
| 131 |
AddToOrder($orderid, $_->{'accountlines_id'} ) for @acctlines; |
| 132 |
|
| 133 |
$template->param( |
| 134 |
BORROWER_INFO => $borr, |
| 135 |
HTTP_COOKIE => $cookie, |
| 136 |
accountlines => \@acctlines, |
| 137 |
accepturl => $baseurl . $script_name . '?action=accept', |
| 138 |
cancelurl => $baseurl . $script_name . '?action=cancel', |
| 139 |
callbackurl => $baseurl . $script_name . '?action=callback', |
| 140 |
lang => C4::Languages::getlanguage($cgi) || 'en', |
| 141 |
amount => $amount / $subunit, |
| 142 |
total => $amount, |
| 143 |
currency => C4::Context->preference('OpacPaymentCurrency'), |
| 144 |
merchant => C4::Context->preference('OpacPaymentMerchantID'), |
| 145 |
order => GetOrder($orderid), |
| 146 |
md5key => GetOrderHash($orderid), |
| 147 |
state => 'checkout' |
| 148 |
); |
| 149 |
} |
| 150 |
elsif ( $action eq 'accept' ) { |
| 151 |
my $orderid = $cgi->param('orderid'); |
| 152 |
my $authkey = $cgi->param('authkey'); |
| 153 |
|
| 154 |
# DIBS-specific authentication check |
| 155 |
my $k1 = C4::Context->preference('OpacPaymentK1'); |
| 156 |
my $k2 = C4::Context->preference('OpacPaymentK2'); |
| 157 |
my $my_authkey = md5_hex( |
| 158 |
$k2 |
| 159 |
. md5_hex( |
| 160 |
$k1 |
| 161 |
. 'transact=' |
| 162 |
. $cgi->param('transact') |
| 163 |
. '&amount=' |
| 164 |
. $cgi->param('amount') |
| 165 |
. '¤cy=' |
| 166 |
. $cgi->param('currency') |
| 167 |
) |
| 168 |
); |
| 169 |
|
| 170 |
if ( $authkey ne $my_authkey ) { |
| 171 |
$template->param( |
| 172 |
BORROWER_INFO => $borr, |
| 173 |
state => 'error', |
| 174 |
error => 'authkey', |
| 175 |
orderid => $orderid |
| 176 |
); |
| 177 |
carp "Invalid authentication key returned for orderid $orderid"; |
| 178 |
output_html_with_http_headers $cgi, $cookie, $template->output; |
| 179 |
exit; |
| 180 |
} |
| 181 |
|
| 182 |
# Authentication passed |
| 183 |
|
| 184 |
my $order = GetOrder($orderid); |
| 185 |
my $orderdetails = GetOrderDetails($orderid); |
| 186 |
|
| 187 |
$template->param( |
| 188 |
BORROWER_INFO => $borr, |
| 189 |
accountlines => $orderdetails, |
| 190 |
order => $order, |
| 191 |
state => 'success' |
| 192 |
); |
| 193 |
} |
| 194 |
elsif ( $action eq 'cancel' ) { |
| 195 |
my $orderid = $cgi->param('orderid'); |
| 196 |
my $authkey = $cgi->param('authkey'); |
| 197 |
|
| 198 |
my $order = GetOrder($orderid); |
| 199 |
my $orderdetails = GetOrderDetails($orderid); |
| 200 |
|
| 201 |
$order->{'status'} = 'canceled'; |
| 202 |
$order->{'modificationdate'} = |
| 203 |
DateTime::Format::MySQL->format_datetime( DateTime->now ); |
| 204 |
ModOrder($order); |
| 205 |
|
| 206 |
$template->param( |
| 207 |
BORROWER_INFO => $borr, |
| 208 |
accountlines => $orderdetails, |
| 209 |
order => $order, |
| 210 |
state => 'canceled' |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
output_html_with_http_headers $cgi, $cookie, $template->output; |