Skip to content

Commit b7ed634

Browse files
PT-852: Pending in Plugins for JTL5 (#46)
Co-authored-by: Tigran <[email protected]>
1 parent 42adb91 commit b7ed634

File tree

25 files changed

+672
-274
lines changed

25 files changed

+672
-274
lines changed

AdminRender.php

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
namespace Plugin\MonduPayment;
66

7-
use InvalidArgumentException;
7+
use JTL\Plugin\PluginInterface;
8+
use JTL\Shop;
89
use JTL\Smarty\JTLSmarty;
10+
use Plugin\MonduPayment\Src\Helpers\Response;
11+
use Plugin\MonduPayment\Src\Support\HttpClients\MonduClient;
912

1013
/**
1114
* Class AdminRender
@@ -27,25 +30,74 @@ class AdminRender
2730
* AdminRender constructor.
2831
* @param Object $plugin
2932
*/
30-
public function __construct(Object $plugin)
33+
public function __construct(PluginInterface $plugin)
3134
{
3235
$this->plugin = $plugin;
3336
$this->path = $this->plugin->getPaths()->getAdminPath() . '/templates/';
3437
}
3538

3639
/**
37-
* @param string $template
38-
* @param int $menuID
39-
* @param Object $smarty
40+
* @param string $tabName
41+
* @param JTLSmarty $smarty
42+
* @param $request
4043
* @return string
4144
* @throws \SmartyException
4245
*/
43-
public function renderPage(string $template, JTLSmarty $smarty): string
46+
public function renderPage(string $tabName, JTLSmarty $smarty, $request): string
4447
{
48+
$monduRequestType = !empty($request['request_type']) ? $request['request_type'] : null;
49+
50+
if ($monduRequestType === 'registerWebhooks') {
51+
$this->handleRegisterWebhooksRequest();
52+
}
53+
4554
$smarty->assign('pluginPath', $this->plugin->getPaths()->getAdminURL());
4655

4756
$smarty->assign('pluginURL', $this->plugin->getPaths()->getShopURL());
48-
49-
return $smarty->fetch($this->path . 'post/layout.tpl');
57+
58+
if ($tabName === 'Info') {
59+
return $smarty
60+
->assign('postUrl', Shop::getURL() . '/' . \PFAD_ADMIN . 'plugin.php?kPlugin=' . $this->plugin->getID())
61+
->fetch($this->path . 'mondu_info.tpl');
62+
}
63+
64+
return '';
65+
}
66+
67+
private function handleRegisterWebhooksRequest()
68+
{
69+
$monduClient = new MonduClient();
70+
$webhookTopics = ['order', 'invoice'];
71+
$webhookSecret = $monduClient->getWebhookKeys();
72+
73+
if (isset($webhookSecret['error'])) {
74+
Response::json(
75+
$webhookSecret,
76+
Response::HTTP_UNPROCESSABLE_ENTITY
77+
);
78+
exit;
79+
}
80+
81+
Shop::Container()->getDB()->update('tplugineinstellungen' , 'cName' , 'webhooks_secret', (object) ['cWert' => $webhookSecret['webhook_secret']]);
82+
83+
foreach ($webhookTopics as $topic) {
84+
$requestData = [
85+
'topic' => $topic,
86+
'address' => Shop::getURL() . '/mondu-api?return=webhook'
87+
];
88+
89+
$response = $monduClient->registerWebhooks($requestData);
90+
91+
if (isset($response['error'])) {
92+
Response::json(
93+
$response,
94+
Response::HTTP_UNPROCESSABLE_ENTITY
95+
);
96+
}
97+
}
98+
99+
Response::json(
100+
['success' => true, 'webhooks_secret' => $webhookSecret['webhook_secret']]
101+
);
50102
}
51103
}

Bootstrap.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ public function uninstalled(bool $deleteData = false)
5757
* writing adminpanel routes for retriving data from database
5858
* @return string
5959
*/
60-
public function renderAdminMenuTab(string $template, int $menuID, JTLSmarty $smarty): string
60+
public function renderAdminMenuTab(string $tabName, int $menuID, JTLSmarty $smarty): string
6161
{
62-
$routes = new RoutesService;
63-
$routes->adminRoutes($this->getPlugin());
62+
$request = $_REQUEST;
6463

6564
$render = new AdminRender($this->getPlugin());
66-
return $render->renderPage($template, $smarty);
65+
return $render->renderPage($tabName, $smarty, $request);
6766
}
6867

6968
/**

Src/Controllers/Frontend/InvoicesController.php

100644100755
Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@
22

33
namespace Plugin\MonduPayment\Src\Controllers\Frontend;
44

5-
use Plugin\MonduPayment\Src\Support\Http\Request;
65
use Plugin\MonduPayment\Src\Helpers\Response;
76
use Plugin\MonduPayment\Src\Support\HttpClients\MonduClient;
8-
use JTL\Shop;
9-
use JTL\Cart\CartHelper;
10-
use JTL\Session\Frontend;
11-
use Plugin\MonduPayment\Src\Support\Debug\Debugger;
127
use JTL\Checkout\Bestellung;
138
use Plugin\MonduPayment\Src\Models\Order;
149
use Plugin\MonduPayment\Src\Models\MonduOrder;
1510
use Plugin\MonduPayment\Src\Models\MonduInvoice;
1611

17-
18-
1912
class InvoicesController
2013
{
2114
private MonduClient $monduClient;
@@ -26,7 +19,7 @@ public function __construct()
2619
}
2720

2821
public function create()
29-
{
22+
{
3023
$requestData = $_REQUEST;
3124

3225
$orderId = $requestData['order_id'];
@@ -42,8 +35,7 @@ public function create()
4235
$invoiceLineItems = [];
4336

4437
foreach ($bestellung->Positionen as $lineItem) {
45-
if ($lineItem->kArtikel == 0)
46-
{
38+
if ($lineItem->kArtikel == 0) {
4739
continue;
4840
}
4941

@@ -72,8 +64,7 @@ public function create()
7264
'invoice_uuid' => $invoice['invoice']['uuid']
7365
]);
7466

75-
return Response::json(
76-
[
67+
return Response::json([
7768
'error' => false
7869
]
7970
);
@@ -93,8 +84,7 @@ public function cancel()
9384

9485
$this->monduClient->cancelInvoice(['invoice_uuid' => $monduInvoice->invoice_uuid, 'order_uuid' => $monduOrder->order_uuid]);
9586

96-
return Response::json(
97-
[
87+
return Response::json([
9888
'error' => false
9989
]
10090
);

Src/Controllers/Frontend/OrdersController.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@
22

33
namespace Plugin\MonduPayment\Src\Controllers\Frontend;
44

5-
use Plugin\MonduPayment\Src\Support\Http\Request;
65
use Plugin\MonduPayment\Src\Helpers\Response;
76
use Plugin\MonduPayment\Src\Support\HttpClients\MonduClient;
8-
use JTL\Shop;
9-
use JTL\Cart\CartHelper;
10-
use JTL\Session\Frontend;
11-
use Plugin\MonduPayment\Src\Support\Debug\Debugger;
12-
use JTL\Checkout\Bestellung;
13-
use Plugin\MonduPayment\Src\Models\Order;
147
use Plugin\MonduPayment\Src\Models\MonduOrder;
158

16-
179
class OrdersController
1810
{
1911
private MonduClient $monduClient;
@@ -23,6 +15,9 @@ public function __construct()
2315
$this->monduClient = new MonduClient();
2416
}
2517

18+
/**
19+
* @return null
20+
*/
2621
public function cancel()
2722
{
2823
$requestData = $_REQUEST;
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace Plugin\MonduPayment\Src\Controllers\Frontend;
4+
5+
use JTL\Shop;
6+
use Plugin\MonduPayment\PaymentMethod\MonduPayment;
7+
use Plugin\MonduPayment\Src\Helpers\Response;
8+
use Plugin\MonduPayment\Src\Middlewares\CheckWebhookSecret;
9+
use Plugin\MonduPayment\Src\Models\MonduInvoice;
10+
use Plugin\MonduPayment\Src\Models\MonduOrder;
11+
use Plugin\MonduPayment\Src\Models\Order;
12+
use Plugin\MonduPayment\Src\Services\ConfigService;
13+
use Plugin\MonduPayment\Src\Support\Http\Request;
14+
use Plugin\MonduPayment\Src\Support\HttpClients\MonduClient;
15+
16+
class WebhookController
17+
{
18+
public const MONDU_JTL_MAPPING = [
19+
MonduPayment::STATE_CONFIRMED => \BESTELLUNG_STATUS_BEZAHLT,
20+
MonduPayment::STATE_PENDING => \BESTELLUNG_STATUS_IN_BEARBEITUNG,
21+
MonduPayment::STATE_CANCELED => \BESTELLUNG_STATUS_STORNO,
22+
MonduPayment::STATE_DECLINED => \BESTELLUNG_STATUS_STORNO,
23+
];
24+
25+
private MonduClient $monduClient;
26+
private Request $request;
27+
private MonduInvoice $monduInvoice;
28+
private MonduOrder $monduOrder;
29+
private Order $order;
30+
private CheckWebhookSecret $checkWebhookSecret;
31+
private ConfigService $configService;
32+
33+
public function __construct()
34+
{
35+
$this->monduClient = new MonduClient();
36+
$this->request = new Request;
37+
$this->monduInvoice = new MonduInvoice();
38+
$this->monduOrder = new MonduOrder();
39+
$this->order = new Order();
40+
$this->checkWebhookSecret = new CheckWebhookSecret();
41+
$this->configService = new ConfigService();
42+
}
43+
44+
public function index()
45+
{
46+
[$response, $status] = $this->handleWebhook();
47+
48+
Response::json($response, $status);
49+
}
50+
51+
/**
52+
* @return array
53+
* @throws \Exception
54+
*/
55+
private function handleWebhook()
56+
{
57+
$requestData = $this->request->all();
58+
59+
switch ($requestData['topic']) {
60+
case 'order/confirmed':
61+
case 'order/declined':
62+
case 'order/pending':
63+
return $this->handleOrderStateChanged($requestData);
64+
case 'invoice/canceled':
65+
return $this->handleInvoiceStateChanged($requestData, 'canceled');
66+
default:
67+
return [['message' => 'Unregistered topic'], Response::HTTP_OK];
68+
}
69+
}
70+
71+
/**
72+
* @param $params
73+
*
74+
* @return array
75+
*/
76+
public function handleOrderStateChanged($requestData)
77+
{
78+
$params = [
79+
'order_id' => $requestData['external_reference_id'],
80+
'order_state' => $requestData['order_state']
81+
];
82+
83+
$monduOrder = $this->getOrder($params['order_id']);
84+
85+
if ($monduOrder) {
86+
$this->monduOrder->update(['state' => $params['order_state']], $monduOrder->id);
87+
88+
if (isset(self::MONDU_JTL_MAPPING[$params['order_state']])) {
89+
$this->updateOrderStatus($monduOrder, self::MONDU_JTL_MAPPING[$params['order_state']]);
90+
}
91+
92+
return [['message' => 'ok'], Response::HTTP_OK];
93+
}
94+
95+
return [['message' => 'Order not found'], Response::HTTP_NOT_FOUND];
96+
}
97+
98+
/**
99+
* @param $params
100+
* @param $state
101+
*
102+
* @return array
103+
*/
104+
public function handleInvoiceStateChanged($requestData, $state)
105+
{
106+
$params = [
107+
'invoice_uuid' => $requestData['invoice_uuid']
108+
];
109+
110+
$monduInvoice = $this->getInvoice($params['invoice_uuid']);
111+
112+
if ($monduInvoice) {
113+
$this->monduInvoice->update(['state' => $state], $monduInvoice->id);
114+
return [['message' => 'ok'], Response::HTTP_OK];
115+
}
116+
117+
return [['message' => 'Invoice not found'], Response::HTTP_NOT_FOUND];
118+
}
119+
120+
/**
121+
* @param $orderUuid
122+
*
123+
* @return mixed
124+
*/
125+
private function getOrder($orderUuid)
126+
{
127+
return $this->monduOrder->select('id', 'order_uuid', 'order_id')->where('external_reference_id', $orderUuid)->first()[0];
128+
}
129+
130+
/**
131+
* @param $invoiceUuid
132+
*
133+
* @return mixed
134+
*/
135+
private function getInvoice($invoiceUuid)
136+
{
137+
return $this->monduInvoice->select('id', 'invoice_uuid')->where('invoice_uuid', $invoiceUuid)->first()[0];
138+
}
139+
140+
/**
141+
* @param $monduOrder
142+
* @param $status
143+
* @return int
144+
*/
145+
private function updateOrderStatus($monduOrder, $status)
146+
{
147+
Shop::Container()->getDB()->update(
148+
'tbestellung',
149+
'kBestellung',
150+
$monduOrder->order_id,
151+
(object)['cStatus' => $status]
152+
);
153+
}
154+
}

0 commit comments

Comments
 (0)