Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2017 R-Bit technology, s.r.o. |
4 |
# |
5 |
# This file is part of Koha. |
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 |
|
21 |
use Modern::Perl; |
22 |
|
23 |
use CGI qw ( -utf8 ); |
24 |
use C4::Auth qw( check_api_auth ); |
25 |
use C4::Context; |
26 |
use JSON qw( to_json ); |
27 |
use IO::Socket::INET; |
28 |
use Koha::PosTerminal::Message; |
29 |
use Koha::PosTerminal::Client; |
30 |
use Koha::PosTerminal::Transaction; |
31 |
use Koha::PosTerminal::Transactions; |
32 |
use XML::Simple; |
33 |
use Scalar::Util qw( looks_like_number ); |
34 |
use DateTime; |
35 |
use Data::Dumper; |
36 |
|
37 |
my $query = new CGI; |
38 |
my ($status, $cookie, $sessionID) = check_api_auth($query, { updatecharges => 1 } ); |
39 |
my $transactionId = $query->param('transaction_id'); |
40 |
my $accountlinesId = $query->param('accountlines_id'); |
41 |
my $action = $query->param('action') || q{}; |
42 |
my $result = { |
43 |
status => $status, |
44 |
transaction_id => defined($transactionId) ? $transactionId : -1 |
45 |
}; |
46 |
|
47 |
binmode STDOUT, ":encoding(UTF-8)"; |
48 |
print $query->header( |
49 |
-type => 'text/xml', |
50 |
-charset => 'UTF-8' |
51 |
); |
52 |
|
53 |
if ($status eq 'ok') { |
54 |
|
55 |
if (!$action) { |
56 |
start_transaction($result, $accountlinesId); |
57 |
} |
58 |
|
59 |
elsif ($action eq 'status') { |
60 |
get_transaction_status($result, $transactionId); |
61 |
} |
62 |
|
63 |
elsif (($action eq 'request-payment') || ($action eq 'request-refund') || ($action eq 'abort')) { |
64 |
my $transaction = Koha::PosTerminal::Transactions->find($transactionId); |
65 |
my $client = new Koha::PosTerminal::Client( |
66 |
C4::Context->preference('PosTerminalIP'), |
67 |
C4::Context->preference('PosTerminalPort'), |
68 |
); |
69 |
|
70 |
if ($client->connect()) { |
71 |
$transaction->set({status => 'connected'})->store(); |
72 |
|
73 |
if ($action eq 'abort') { |
74 |
my $abort = abort_transaction($client, $transaction); |
75 |
my $field = $abort->getField($abort->F_RESPONSE_CODE); |
76 |
$result->{response_code} = $field ? $field->value : 0; |
77 |
$result->{status} = "abort"; |
78 |
$transaction->set({status => $result->{status}, response_code => $result->{response_code}})->store(); |
79 |
print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1); |
80 |
exit 0; |
81 |
} |
82 |
|
83 |
my $transactionRequest = send_transaction_request($client, $transaction, $action eq 'request-payment' ? Koha::PosTerminal::Message->TTYPE_SALE : Koha::PosTerminal::Message->TTYPE_REFUND, scalar $query->param('paid')); |
84 |
|
85 |
my $transactionMessage = receive_transaction_message($client, $transaction); |
86 |
|
87 |
my $field = $transactionMessage->getField($transactionMessage->F_RESPONSE_CODE); |
88 |
$result->{response_code} = $field ? $field->value : 0; |
89 |
if ($result->{response_code} <= 10) { |
90 |
|
91 |
my $transactionResponse = receive_transaction_response($client, $transaction); |
92 |
if (!looks_like_number($transactionResponse)) { |
93 |
send_confirmation_message($client, $transaction, $transactionResponse); |
94 |
|
95 |
$client->disconnect(); |
96 |
$transaction->set({status => 'disconnected'})->store(); |
97 |
|
98 |
$field = $transactionResponse->getField($transactionResponse->F_RESPONSE_CODE); |
99 |
$result->{response_code} = $field ? $field->value : 0; |
100 |
|
101 |
if ($result->{response_code} <= 10) { |
102 |
if ( $field = $transactionResponse->getField($transactionResponse->F_CARD_NUMBER) ) { |
103 |
$result->{cardnumber} = $field->value; |
104 |
$result->{status} = "success"; |
105 |
} |
106 |
else { |
107 |
$result->{status} = "ERR_NO_CARD_NUMBER"; |
108 |
} |
109 |
} |
110 |
else { |
111 |
$result->{status} = "ERR_TRANSACTION_REJECTED"; |
112 |
} |
113 |
} |
114 |
else { |
115 |
$result->{status} = "ERR_CONNECTION_FAILED"; |
116 |
} |
117 |
} |
118 |
else { |
119 |
$result->{status} = "ERR_REQUEST_REJECTED"; |
120 |
} |
121 |
} |
122 |
else { |
123 |
$result->{status} = "ERR_NO_CONNECTION"; |
124 |
} |
125 |
$transaction->set({status => $result->{status}, response_code => $result->{response_code}})->store(); |
126 |
} |
127 |
elsif ($action eq 'request-refund') { |
128 |
} |
129 |
} |
130 |
|
131 |
print XMLout($result, NoAttr => 1, RootName => 'response', XMLDecl => 1); |
132 |
|
133 |
exit 0; |
134 |
|
135 |
sub log_communication { |
136 |
my ( $terminalMsg, $transactionId ) = @_; |
137 |
|
138 |
my $transaction = Koha::PosTerminal::Transactions->find($transactionId); |
139 |
my $now = DateTime->now(); |
140 |
my $message = "[" . $now->ymd . " " . $now->hms ."] data: "; |
141 |
if (looks_like_number($terminalMsg)) { |
142 |
$message .= "error " . $terminalMsg . "\n"; |
143 |
} |
144 |
else { |
145 |
$message .= $terminalMsg->fieldCount() . ", " . $terminalMsg->getDirection() . ": " . $terminalMsg->decodeControlCharacters($terminalMsg->getContent()) . "\n"; |
146 |
} |
147 |
$transaction->set({message_log => (defined $transaction->message_log ? $transaction->message_log : "") . $message})->store(); |
148 |
} |
149 |
|
150 |
sub start_transaction { |
151 |
my ( $result, $accountlinesId ) = @_; |
152 |
|
153 |
my $transaction = Koha::PosTerminal::Transaction->new({accountlines_id => $accountlinesId, status => 'new' })->store(); |
154 |
$result->{status} = $transaction->status; |
155 |
$result->{transaction_id} = $transaction->id; |
156 |
} |
157 |
|
158 |
sub abort_transaction { |
159 |
my ( $client, $transaction ) = @_; |
160 |
|
161 |
# send abort message |
162 |
my $abort = new Koha::PosTerminal::Message(Koha::PosTerminal::Message::DIR_SENT); |
163 |
my $hdrAbort = $abort->getHeader(); |
164 |
die(Dumper($transaction ? $transaction->getHeader() : "BUBU")); |
165 |
my $hdrTransaction = $transaction->getHeader(); |
166 |
|
167 |
$hdrAbort->dateTime($hdrTransaction->dateTime()); |
168 |
$hdrAbort->terminalID($hdrTransaction->terminalID()); |
169 |
$hdrAbort->protocolType($hdrTransaction->protocolType()); |
170 |
$hdrAbort->protocolVersion($hdrTransaction->protocolVersion()); |
171 |
$abort->addField($abort->F_TRANSACTION_TYPE, $abort->TTYPE_ABORT); |
172 |
$client->send($abort); |
173 |
|
174 |
$abort->set({status => 'abort'})->store(); |
175 |
log_communication($abort, $transaction->id); |
176 |
|
177 |
return $abort; |
178 |
} |
179 |
|
180 |
sub get_transaction_status { |
181 |
my ( $result, $transactionId ) = @_; |
182 |
|
183 |
my $transaction = Koha::PosTerminal::Transactions->find($transactionId); |
184 |
$result->{status} = $transaction->status; |
185 |
} |
186 |
|
187 |
sub send_transaction_request { |
188 |
my ( $client, $transaction, $type, $paid ) = @_; |
189 |
|
190 |
# send transaction request |
191 |
my $transactionRequest = new Koha::PosTerminal::Message(Koha::PosTerminal::Message::DIR_SENT); |
192 |
$transactionRequest->getHeader()->dateTime(0); |
193 |
$transactionRequest->addField($transactionRequest->F_TRANSACTION_TYPE, $type); |
194 |
$transactionRequest->addField($transactionRequest->F_PAID_AMOUNT, $paid * 100); |
195 |
$transactionRequest->addField($transactionRequest->F_INVOICE_NUMBER, $transaction->accountlines_id); |
196 |
$transactionRequest->addField($transactionRequest->F_CURRENCY_CODE, C4::Context->preference('PosTerminalCurrencyCode')); |
197 |
$client->send($transactionRequest); |
198 |
$transaction->set({status => 'sent-request'})->store(); |
199 |
log_communication($transactionRequest, $transaction->id); |
200 |
|
201 |
return $transactionRequest; |
202 |
} |
203 |
|
204 |
sub receive_transaction_message { |
205 |
my ( $client, $transaction ) = @_; |
206 |
|
207 |
# receive transaction message |
208 |
my $transactionMessage = $client->receive(); |
209 |
|
210 |
$transaction->set({status => 'received-message'})->store(); |
211 |
log_communication($transactionMessage, $transaction->id); |
212 |
|
213 |
return $transactionMessage; |
214 |
} |
215 |
|
216 |
sub receive_transaction_response { |
217 |
my ( $client, $transaction ) = @_; |
218 |
my $transactionResponse; |
219 |
my $status; |
220 |
|
221 |
# receive transaction response |
222 |
for (;;) { |
223 |
$transactionResponse = $client->receive(); |
224 |
$status = looks_like_number($transactionResponse) ? 'ERR_CONNECTION_FAILED' |
225 |
: ($transactionResponse->fieldCount() ? 'received-response' : 'activity-message'); |
226 |
$transaction->set({status => $status})->store(); |
227 |
log_communication($transactionResponse, $transaction->id); |
228 |
|
229 |
last if (looks_like_number($transactionResponse) || $transactionResponse->fieldCount()); |
230 |
} |
231 |
|
232 |
return $transactionResponse; |
233 |
} |
234 |
|
235 |
sub send_confirmation_message { |
236 |
my ( $client, $transaction, $transactionResponse ) = @_; |
237 |
|
238 |
# send confirmation message |
239 |
my $confirmation = new Koha::PosTerminal::Message(Koha::PosTerminal::Message::DIR_SENT); |
240 |
my $hdrConfirm = $confirmation->getHeader(); |
241 |
my $hdrResponse = $transactionResponse->getHeader(); |
242 |
$hdrConfirm->dateTime($hdrResponse->dateTime()); |
243 |
$hdrConfirm->terminalID($hdrResponse->terminalID()); |
244 |
$hdrConfirm->protocolType($hdrResponse->protocolType()); |
245 |
$hdrConfirm->protocolVersion($hdrResponse->protocolVersion()); |
246 |
$client->send($confirmation); |
247 |
|
248 |
$transaction->set({status => 'sent-confirmation'})->store(); |
249 |
log_communication($confirmation, $transaction->id); |
250 |
} |