Skip to content

Commit 75801de

Browse files
authored
[PT-1311] Prevent WAWI sync for pending orders. (#48)
1 parent b7ed634 commit 75801de

File tree

11 files changed

+45
-30
lines changed

11 files changed

+45
-30
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ JTL 5 Integration plugin for Mondu Payment.
1919
3. Configure the fields:
2020
* API Sandbox Mode: Select yes to point the plugin to the sandbox environment
2121
* Fill in API Secret
22-
* Fill in Webhooks Secret
22+
* Save configuration
23+
* Click register webhooks button
2324

2425
**Configure Payment Methods**
2526

Src/Controllers/Frontend/InvoicesController.php

100755100644
File mode changed.

Src/Controllers/Frontend/WebhookController.php

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
use JTL\Shop;
66
use Plugin\MonduPayment\PaymentMethod\MonduPayment;
7+
use Plugin\MonduPayment\Src\Exceptions\DatabaseQueryException;
78
use Plugin\MonduPayment\Src\Helpers\Response;
8-
use Plugin\MonduPayment\Src\Middlewares\CheckWebhookSecret;
99
use Plugin\MonduPayment\Src\Models\MonduInvoice;
1010
use Plugin\MonduPayment\Src\Models\MonduOrder;
11-
use Plugin\MonduPayment\Src\Models\Order;
12-
use Plugin\MonduPayment\Src\Services\ConfigService;
1311
use Plugin\MonduPayment\Src\Support\Http\Request;
14-
use Plugin\MonduPayment\Src\Support\HttpClients\MonduClient;
1512

1613
class WebhookController
1714
{
@@ -22,23 +19,15 @@ class WebhookController
2219
MonduPayment::STATE_DECLINED => \BESTELLUNG_STATUS_STORNO,
2320
];
2421

25-
private MonduClient $monduClient;
2622
private Request $request;
2723
private MonduInvoice $monduInvoice;
2824
private MonduOrder $monduOrder;
29-
private Order $order;
30-
private CheckWebhookSecret $checkWebhookSecret;
31-
private ConfigService $configService;
3225

3326
public function __construct()
3427
{
35-
$this->monduClient = new MonduClient();
3628
$this->request = new Request;
3729
$this->monduInvoice = new MonduInvoice();
3830
$this->monduOrder = new MonduOrder();
39-
$this->order = new Order();
40-
$this->checkWebhookSecret = new CheckWebhookSecret();
41-
$this->configService = new ConfigService();
4231
}
4332

4433
public function index()
@@ -69,9 +58,9 @@ private function handleWebhook()
6958
}
7059

7160
/**
72-
* @param $params
73-
*
61+
* @param $requestData
7462
* @return array
63+
* @throws DatabaseQueryException
7564
*/
7665
public function handleOrderStateChanged($requestData)
7766
{
@@ -82,39 +71,48 @@ public function handleOrderStateChanged($requestData)
8271

8372
$monduOrder = $this->getOrder($params['order_id']);
8473

85-
if ($monduOrder) {
86-
$this->monduOrder->update(['state' => $params['order_state']], $monduOrder->id);
74+
if (!$monduOrder) {
75+
return [['message' => 'Order not found'], Response::HTTP_NOT_FOUND];
76+
}
8777

88-
if (isset(self::MONDU_JTL_MAPPING[$params['order_state']])) {
89-
$this->updateOrderStatus($monduOrder, self::MONDU_JTL_MAPPING[$params['order_state']]);
90-
}
78+
$this->monduOrder->update(['state' => $params['order_state']], $monduOrder->id);
9179

92-
return [['message' => 'ok'], Response::HTTP_OK];
80+
if (isset(self::MONDU_JTL_MAPPING[$params['order_state']])) {
81+
$this->updateOrderStatus($monduOrder, self::MONDU_JTL_MAPPING[$params['order_state']]);
9382
}
9483

95-
return [['message' => 'Order not found'], Response::HTTP_NOT_FOUND];
84+
if ($params['order_state'] === MonduPayment::STATE_CONFIRMED) {
85+
$this->unlockOrderForWawiSync($monduOrder);
86+
}
87+
88+
return [['message' => 'ok'], Response::HTTP_OK];
9689
}
9790

9891
/**
99-
* @param $params
92+
* @param $requestData
10093
* @param $state
10194
*
10295
* @return array
10396
*/
104-
public function handleInvoiceStateChanged($requestData, $state)
97+
public function handleInvoiceStateChanged($requestData, $state): array
10598
{
10699
$params = [
107100
'invoice_uuid' => $requestData['invoice_uuid']
108101
];
109102

110103
$monduInvoice = $this->getInvoice($params['invoice_uuid']);
111104

112-
if ($monduInvoice) {
105+
if (!$monduInvoice) {
106+
return [['message' => 'Invoice not found'], Response::HTTP_NOT_FOUND];
107+
}
108+
109+
try {
113110
$this->monduInvoice->update(['state' => $state], $monduInvoice->id);
114-
return [['message' => 'ok'], Response::HTTP_OK];
111+
} catch (DatabaseQueryException $e) {
112+
return [['message' => 'Invoice not found'], Response::HTTP_NOT_FOUND];
115113
}
116114

117-
return [['message' => 'Invoice not found'], Response::HTTP_NOT_FOUND];
115+
return [['message' => 'ok'], Response::HTTP_OK];
118116
}
119117

120118
/**
@@ -140,15 +138,29 @@ private function getInvoice($invoiceUuid)
140138
/**
141139
* @param $monduOrder
142140
* @param $status
143-
* @return int
141+
* @return void
144142
*/
145143
private function updateOrderStatus($monduOrder, $status)
146144
{
147145
Shop::Container()->getDB()->update(
148146
'tbestellung',
149147
'kBestellung',
150148
$monduOrder->order_id,
151-
(object)['cStatus' => $status]
149+
(object) ['cStatus' => $status]
150+
);
151+
}
152+
153+
/**
154+
* @param $monduOrder
155+
* @return void
156+
*/
157+
private function unlockOrderForWawiSync($monduOrder)
158+
{
159+
Shop::Container()->getDB()->update(
160+
'tbestellung',
161+
'kBestellung',
162+
$monduOrder->order_id,
163+
(object) ['cAbgeholt' => 'N']
152164
);
153165
}
154166
}

Src/Services/ConfigService.php

100755100644
File mode changed.

Src/Support/Http/HttpRequest.php

100755100644
File mode changed.

Src/Support/HttpClients/MonduClient.php

100755100644
File mode changed.

frontend/hooks/Checkout.php

100755100644
File mode changed.

frontend/hooks/CheckoutConfirmPage.php

100755100644
File mode changed.

frontend/hooks/CheckoutPaymentMethod.php

100755100644
File mode changed.

info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<PluginID>MonduPayment</PluginID>
88
<XMLVersion>100</XMLVersion>
99
<ShopVersion>5.0.0</ShopVersion>
10-
<Version>3.0.2</Version>
10+
<Version>3.0.3</Version>
1111
<CreateDate>2022-06-07</CreateDate>
1212
<Install>
1313
<Hooks>

0 commit comments

Comments
 (0)