Skip to content

Commit 6089f8e

Browse files
authored
Added mondu preselection, fixed z-index, event logging... (#12)
* Added mondu preselection, fixed z-index, added event logging... * Updated assets name * Updated versions * Reverted checkout.php
1 parent 61ed2cf commit 6089f8e

File tree

11 files changed

+342
-378
lines changed

11 files changed

+342
-378
lines changed

Src/Controllers/Frontend/CheckoutController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function getOrderData()
9595
$buyer['phone'] = html_entity_decode($buyerPhone);
9696

9797
if (!empty($customer->cStrasse))
98-
$buyer['address_line1'] = html_entity_decode($customer->cStrasse);
98+
$buyer['address_line1'] = html_entity_decode($customer->cStrasse . " " . $customer->cHausnummer);
9999

100100
if (!empty($customer->cPLZ))
101101
$buyer['zip_code'] = $customer->cPLZ;
@@ -110,13 +110,13 @@ public function getOrderData()
110110
'external_reference_id' => uniqid('M_JTL_'),
111111
'buyer' => $buyer,
112112
'billing_address' => [
113-
'address_line1' => html_entity_decode($customer->cStrasse),
113+
'address_line1' => html_entity_decode($customer->cStrasse . " " . $customer->cHausnummer),
114114
'city' => html_entity_decode($customer->cOrt),
115115
'country_code' => $customer->cLand,
116116
'zip_code' => $customer->cPLZ
117117
],
118118
'shipping_address' => [
119-
'address_line1' => html_entity_decode($shippingAddress->cStrasse),
119+
'address_line1' => html_entity_decode($shippingAddress->cStrasse . " " . $shippingAddress->cHausnummer),
120120
'city' => html_entity_decode($shippingAddress->cOrt),
121121
'country_code' => $shippingAddress->cLand,
122122
'zip_code' => $shippingAddress->cPLZ

Src/Exceptions/InvalidRequestException.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,21 @@
66

77
class InvalidRequestException extends \Exception
88
{
9-
public function __construct($message = 'this request is invalid')
9+
protected $_exceptionData;
10+
11+
public function __construct($data)
1012
{
1113
$debugger = new Debugger();
12-
$debugger->log($this->message);
14+
$debugger->log('[REQUEST FAIL]: Error ocurred at ' . $data->request_url . ' with data: ' . print_r($data->request_body, true));
15+
$debugger->log('[REQUEST FAIL]: Response: ' . print_r($data->response_body, true));
16+
17+
$this->_exceptionData = $data;
18+
19+
parent::__construct('Mondu Request Failed');
20+
}
21+
22+
public function getExceptionData()
23+
{
24+
return $this->_exceptionData;
1325
}
1426
}

Src/Services/ConfigService.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,27 @@ class ConfigService
1616
const WIDGET_PRODUCTION_URL = 'https://checkout.mondu.ai/widget.js';
1717

1818
private $config;
19+
private $plugin;
1920

2021
public function __construct()
2122
{
22-
$this->config = Helper::getPluginById('MonduPayment')->getConfig();
23+
$this->plugin = Helper::getPluginById('MonduPayment');
24+
$this->config = $this->plugin->getConfig();
2325
}
2426

2527
public function getConfig()
2628
{
2729
return $this->config;
2830
}
2931

32+
public function getPluginVersion() {
33+
return $this->plugin->getCurrentVersion()->getOriginalVersion();
34+
}
35+
36+
public function getPluginName() {
37+
return $this->plugin->getMeta()->getName() . " " . "JTL5";
38+
}
39+
3040
public function getSandboxMode()
3141
{
3242
return $this->config->getValue('sandbox_mode') == '1';

Src/Support/Http/HttpRequest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,21 @@ public function send_request(string $url, array $data, string $method)
138138
curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
139139

140140
$response = curl_exec($this->curl);
141+
$info = curl_getinfo($this->curl);
141142

142143
curl_close($this->curl);
143144

144-
if (!$response) {
145-
$this->debugger->log('[REQUEST FAIL]: No response at ' . $url . ' with data: ' . print_r($data, true));
146-
throw new InvalidRequestException();
147-
}
148-
$response = json_decode($response, true);
149-
150-
if (@$response['errors'] != null) {
151-
$this->debugger->log('[REQUEST FAIL]: Error ocurred at ' . $url . ' with data: ' . print_r($data, true));
152-
$this->debugger->log('[REQUEST FAIL]: Response: ' . print_r($response, true));
153-
throw new InvalidRequestException($response);
145+
if (!$response || !($info['http_code'] >= 200 && $info['http_code'] <= 299)) {
146+
$exception = new \stdClass();
147+
$exception->method = $method;
148+
$exception->request_url = $url;
149+
$exception->request_body = $data;
150+
$exception->response_body = $response;
151+
$exception->response_code = $info['http_code'];
152+
153+
throw new InvalidRequestException($exception);
154154
}
155155

156-
return $response;
156+
return json_decode($response, true);
157157
}
158158
}

Src/Support/HttpClients/MonduClient.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,40 @@ public function __construct()
1616
$this->config = new ConfigService();
1717
$this->client = new HttpRequest(
1818
$this->config->getApiUrl(),
19-
['Content-Type: application/json', 'Api-Token: '. $this->config->getApiSecret()]
19+
[
20+
'Content-Type: application/json',
21+
'Api-Token: '. $this->config->getApiSecret(),
22+
'x-plugin-name: '. $this->config->getPluginName(),
23+
'x-plugin-version: '.$this->config->getPluginVersion()
24+
]
2025
);
2126
}
2227

28+
public function logEvent($object)
29+
{
30+
try {
31+
$data = $object->getExceptionData();
32+
33+
$this->client->post('plugin/events', [
34+
'plugin' => $this->config->getPluginName(),
35+
'version' => $this->config->getPluginVersion(),
36+
'response_status' => strval($data->response_code),
37+
'response_body' => json_decode($data->response_body) ?: null,
38+
'request_body' => $data->request_body ?: null,
39+
'origin_event' => $data->request_url
40+
]);
41+
}
42+
catch (\Exception $e) { }
43+
}
44+
2345
public function createOrder(array $data = []): ?array
2446
{
2547
try {
2648
$order = $this->client->post('orders', $data);
2749
return $order;
2850
}
2951
catch (InvalidRequestException $e) {
52+
$this->logEvent($e);
3053
return ['error' => true];
3154
}
3255
}
@@ -39,6 +62,7 @@ public function cancelOrder(array $data = []): ?array
3962
return $order;
4063
}
4164
catch (InvalidRequestException $e) {
65+
$this->logEvent($e);
4266
return ['error' => true];
4367
}
4468
}
@@ -51,6 +75,7 @@ public function cancelInvoice(array $data = []): ?array
5175
return $order;
5276
}
5377
catch (InvalidRequestException $e) {
78+
$this->logEvent($e);
5479
return ['error' => true];
5580
}
5681
}
@@ -63,6 +88,7 @@ public function createInvoice(array $data = []): ?array
6388
return $invoice;
6489
}
6590
catch (InvalidRequestException $e) {
91+
$this->logEvent($e);
6692
return ['error' => true];
6793
}
6894
}
@@ -75,6 +101,7 @@ public function getPaymentMethods(): ?array
75101
return $paymentMethods;
76102
}
77103
catch (InvalidRequestException $e) {
104+
$this->logEvent($e);
78105
return ['error' => true];
79106
}
80107
}
@@ -87,6 +114,7 @@ public function getNetTerms(): ?array
87114
return $paymentTerms;
88115
}
89116
catch (InvalidRequestException $e) {
117+
$this->logEvent($e);
90118
return ['error' => true];
91119
}
92120
}
@@ -99,6 +127,7 @@ public function updateExternalInfo(array $data = []): ?array
99127
return $order;
100128
}
101129
catch (InvalidRequestException $e) {
130+
$this->logEvent($e);
102131
return ['error' => true];
103132
}
104133
}

0 commit comments

Comments
 (0)