-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonduClient.php
148 lines (125 loc) · 4.83 KB
/
MonduClient.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
<?php
namespace OxidEsales\MonduPayment\Core\Http;
use OxidEsales\MonduPayment\Core\Config;
use OxidEsales\Eshop\Core\Exception\StandardException;
use OxidEsales\MonduPayment\Core\Exception\InvalidRequestException;
use OxidEsales\MonduPayment\Core\Http\HttpRequest;
use OxidEsales\MonduPayment\Core\Utils\MonduHelper;
use OxidEsales\MonduPayment\Core\Logger;
class MonduClient
{
private Config $_config;
private HttpRequest $_client;
private $_baseUrl;
private $_logger;
public function __construct()
{
$this->_config = oxNew(Config::class);
$this->_baseUrl = $this->_config->getApiUrl();
$this->_logger = oxNew(Logger::class)->getLogger();
$this->_client = oxNew(
HttpRequest::class,
$this->_baseUrl,
[
'Content-Type: application/json',
'Api-Token: ' . $this->_config->getApiToken(),
'x-plugin-name: ' . $this->_config->getModuleName(),
'x-plugin-version: ' . $this->_config->getModuleVersion()
]
);
}
public function createOrder($data = [])
{
$order = $this->sendRequest('POST', 'orders', $data);
return $order['order'] ?? null;
}
public function authorizeOrder($data = [])
{
$order = $this->sendRequest('POST', 'orders', $data);
return $order['order'] ?? null;
}
public function updateOrderExternalInfo($orderUuid, $data = [])
{
$order = $this->sendRequest('POST', 'orders/' . $orderUuid . '/update_external_info', $data);
return $order['order'] ?? null;
}
public function confirmOrder($orderUuid, $data = [])
{
$order = $this->sendRequest('POST', 'orders/' . $orderUuid . '/confirm', $data);
return $order['order'] ?? null;
}
public function getMonduOrder($orderUuid)
{
$order = $this->sendRequest('GET', 'orders/' . $orderUuid);
return $order['order'] ?? null;
}
public function createInvoice($orderUuid, $data)
{
$invoice = $this->sendRequest('POST', 'orders/' . $orderUuid . '/invoices', $data);
return $invoice['invoice'] ?? null;
}
public function cancelInvoice($orderUuid, $invoiceUuid)
{
$invoice = $this->sendRequest('POST', 'orders/' . $orderUuid . '/invoices/' . $invoiceUuid . '/cancel');
return $invoice['invoice'] ?? null;
}
public function getPaymentMethods()
{
$paymentMethods = $this->sendRequest('GET', 'payment_methods');
return $paymentMethods['payment_methods'] ?? null;
}
public function cancelOrder($orderUuid)
{
$order = $this->sendRequest('POST', 'orders/' . $orderUuid . '/cancel');
return $order['order'] ?? null;
}
public function adjustOrder($orderUuid, $data = [])
{
$order = $this->sendRequest('POST', 'orders/' . $orderUuid . '/adjust', $data);
return $order['order'] ?? null;
}
public function getWebhooksSecret()
{
$response = $this->sendRequest('GET', 'webhooks/keys');
$this->_config->setWebhooksSecret($response['webhook_secret'] ?? '');
return $response['webhook_secret'];
}
public function registerWebhook($webhookParams)
{
$response = $this->sendRequest('POST', 'webhooks/', $webhookParams);
return $response;
}
public function logEvent($eventData)
{
try {
$this->_client->post('plugin/events', $eventData);
} catch (StandardException $e) {
$this->_logger->error('MonduClient::logEvent Failed with an exception message: ' . $e->getString());
}
}
protected function sendRequest($method = 'GET', $url = '', $body = [])
{
try {
$url = $this->_baseUrl . $url;
return $this->_client->send_request($url, $body, $method);
} catch (InvalidRequestException $e) {
$this->_logger->error("MonduClient [{$method} {$url}]: Failed with an exception message: {$e->getString()}");
$logParams = MonduHelper::removeEmptyElementsFromArray([
'plugin' => $this->_config->getModuleName(),
'version' => $this->_config->getModuleVersion(),
'language_version' => 'PHP ' . phpversion(),
'shop_version' => $this->_config->getShopVersion(),
'origin_event' => MonduHelper::camelToSnakeCase(debug_backtrace()[1]['function']),
'request_body' => $e->getRequestBody(),
'response_status' => (string) $e->getResponseStatus(),
'response_body' => $e->getResponseBody(),
'error_trace' => $e->getString()
]);
$this->logEvent($logParams);
return [
'status' => $e->getResponseStatus(),
'body' => $e->getResponseBody()
];
}
}
}