-
Notifications
You must be signed in to change notification settings - Fork 9
/
AbstractApi.php
179 lines (155 loc) · 5.62 KB
/
AbstractApi.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Forter\Forter\Model;
use Forter\Forter\Model\Config as ForterConfig;
use Magento\Checkout\Model\Session;
use Magento\Framework\HTTP\Client\Curl as ClientInterface;
/**
* Class AbstractApi
* @package Forter\Forter\Model
*/
class AbstractApi
{
/**
*
*/
const ERROR_ENDPOINT = 'https://api.forter-secure.com/errors/';
/**
* @var ClientInterface
*/
private $checkoutSession;
/**
* @var ClientInterface
*/
private $clientInterface;
/**
* @var Config
*/
private $forterConfig;
/**
* @method __construct
* @param Session $checkoutSession
* @param ClientInterface $clientInterface
* @param ForterConfig $forterConfig
*/
public function __construct(
Session $checkoutSession,
ClientInterface $clientInterface,
ForterConfig $forterConfig
) {
$this->checkoutSession = $checkoutSession;
$this->clientInterface = $clientInterface;
$this->forterConfig = $forterConfig;
}
/**
* @param $url
* @param $data
* @return string
*/
public function sendApiRequest($url, $data, $type = 'post')
{
try {
$tries = 1;
do {
$tries++;
$timeOutStatus = $this->calcTimeOut($tries);
$this->setCurlOptions(strlen($data), $tries);
$this->forterConfig->log('[Forter Request attempt number] ' . $tries, "debug");
$this->forterConfig->log('[Forter Request Url] ' . $url, "debug");
$this->forterConfig->log('[Forter Request Body] ' . $data, "debug");
try {
if ($type == 'post') {
$this->clientInterface->post($url, $data);
} elseif ($type == 'get') {
$this->clientInterface->get($url);
}
$response = $this->clientInterface->getBody();
$this->forterConfig->log('[Forter Response Body] ' . $response, "debug");
$this->forterConfig->log('[Forter Response Header] ', "debug", [$this->clientInterface->getHeaders()]);
$response = json_decode($response);
if (isset($response->status) || isset($response->forterDecision)) {
return json_encode($response);
}
} catch (\Exception $e) {
$this->forterConfig->log('[Exception] ' . $e->getMessage() . "\n" . $e->getTraceAsString(), "error");
}
} while ($timeOutStatus);
return json_encode([
"status" => "failed",
"message" => "maximum retries reached, please see logs"
]);
} catch (\Exception $e) {
$this->forterConfig->log('Error:' . $e->getMessage());
return json_encode([
"status" => "failed",
"message" => "an error occurred, please see logs"
]);
}
}
/**
* @param $bodyLen
* @param $tries
*/
private function setCurlOptions($bodyLen, $tries)
{
/* Curl Headers */
$this->clientInterface->addHeader('x-forter-siteid', $this->forterConfig->getSiteId());
$this->clientInterface->addHeader('api-version', $this->forterConfig->getApiVersion());
$this->clientInterface->addHeader('x-forter-extver', $this->forterConfig->getModuleVersion());
$this->clientInterface->addHeader('x-forter-client', 'magento2');
$this->clientInterface->addHeader('Content-Type', 'application/json');
$this->clientInterface->addHeader('Content-Length', $bodyLen);
/* Curl Options */
$this->clientInterface->setOption(CURLOPT_USERNAME, $this->forterConfig->getSecretKey());
$this->clientInterface->setOption(CURLOPT_RETURNTRANSFER, true);
$this->clientInterface->setOption(CURLOPT_SSL_VERIFYPEER, true);
$this->clientInterface->setOption(CURLOPT_SSL_VERIFYHOST, 2);
}
/**
* @param $tries
* @return bool
*/
private function calcTimeOut($tries)
{
$timeOutSettingsArray = $this->forterConfig->getTimeOutSettings();
$connect_timeout_ms = min(
($tries * $timeOutSettingsArray['base_connection_timeout']),
$timeOutSettingsArray['max_connection_timeout']
);
$total_timeout_ms = min(
($tries * $timeOutSettingsArray['base_request_timeout']),
$timeOutSettingsArray['max_request_timeout']
);
$this->clientInterface->setOption(CURLOPT_CONNECTTIMEOUT_MS, $connect_timeout_ms);
$this->clientInterface->setOption(CURLOPT_TIMEOUT_MS, $total_timeout_ms);
if ($connect_timeout_ms >= $timeOutSettingsArray['max_connection_timeout']) {
if ($total_timeout_ms >= $timeOutSettingsArray['max_request_timeout']) {
return false;
}
}
return true;
}
/**
* @param $e
*/
public function reportToForterOnCatch($e)
{
$url = self::ERROR_ENDPOINT;
$orderId = $this->checkoutSession->getQuote()->getReservedOrderId();
$json = [
"orderID" => $orderId,
"exception" => [
"message" => [
"message" => $e->getMessage(),
"fileName" => $e->getFile(),
"lineNumber"=> $e->getLine(),
"name" => get_class($e),
"stack" => $e->getTrace()
],
"debugInfo" => ""
]
];
$json = json_encode($json);
$this->forterErrorLogger->info($json);
$this->sendApiRequest($url, $json);
}
}