-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.php
More file actions
executable file
·103 lines (90 loc) · 2.27 KB
/
Copy pathclient.php
File metadata and controls
executable file
·103 lines (90 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types = 1);
use ADT\SobitEcr\SobitEcr;
use React\EventLoop\Loop;
require __DIR__ . '/../vendor/autoload.php';
$apiKey = $_ENV['API_KEY'] ?? $_SERVER['API_KEY'] ?? null;
if (!$apiKey) {
echo "API key not set." . PHP_EOL;
exit(1);
}
if (empty($argv[1])) {
echo "Identifier not set." . PHP_EOL;
exit(1);
}
$identifier = $argv[1];
$file = 'temp/' . $identifier . '.dat';
if (file_exists($file)) {
$token = file_get_contents($file);
} else {
$token = SobitEcr::generateToken();
file_put_contents($file, $token);
}
SobitEcr::$debug = true;
$sobitEcr = new SobitEcr($apiKey, $identifier, $token);
$loop = Loop::get();
echo "Enter messages to send (ctrl+c to quit):\n";
$loop->addReadStream(STDIN, function () use ($sobitEcr, $loop) {
$params = explode(' ', trim(fgets(STDIN)));
$op = array_shift($params);
if ($op === 'start_transaction') {
$sobitEcr->startTransaction(
json_encode([
'Amount' => '59.0',
'CurrencyCode' => '203',
'Operation' => 'CP',
'TransactionID' => $params[0],
'InvNumber' => '2532000001'
]),
$params[0],
function (string $message): void {
onResponse($message);
},
function (int $code, string $message): void {
onError($code, $message);
},
function (): void {
echo "Connection established.\n";
}
);
} elseif ($op === 'cancel_transaction') {
$sobitEcr->cancelTransaction(
SobitEcr::OP_CANCEL_TRANSACTION,
$params[0],
function(string $message): void {
onResponse($message);
},
function(int $code, string $message): void {
onError($code, $message);
}
);
} elseif ($op === 'notify_group') {
$sobitEcr->notifyGroup(
'update',
$params[0],
);
} else {
echo "Unknown operation." . PHP_EOL;
}
});
function onResponse(string $message): void
{
try {
$parsedMessage = json_decode($message, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
echo "Error parsing message.\n";
}
if ($parsedMessage['Result'] === "0") {
echo "Result: Accepted.\n";
} elseif ($parsedMessage['Result'] === "1") {
echo "Result: Declined.\n";
} elseif ($parsedMessage['Result'] === "9") {
echo "Result: Aborted.\n";
} else {
echo "Unknown result: $parsedMessage[Result].\n";
}
}
function onError(int $code, string $message): void
{
echo "Error: $code $message\n";
}