-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminRender.php
103 lines (84 loc) · 2.8 KB
/
AdminRender.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
<?php
declare(strict_types=1);
namespace Plugin\MonduPayment;
use JTL\Plugin\PluginInterface;
use JTL\Shop;
use JTL\Smarty\JTLSmarty;
use Plugin\MonduPayment\Src\Helpers\Response;
use Plugin\MonduPayment\Src\Support\HttpClients\MonduClient;
/**
* Class AdminRender
* @package Plugin\MonduPayment
*/
class AdminRender
{
/**
* @var path
*/
private $plugin;
/**
* @var path
*/
private $path;
/**
* AdminRender constructor.
* @param Object $plugin
*/
public function __construct(PluginInterface $plugin)
{
$this->plugin = $plugin;
$this->path = $this->plugin->getPaths()->getAdminPath() . '/templates/';
}
/**
* @param string $tabName
* @param JTLSmarty $smarty
* @param $request
* @return string
* @throws \SmartyException
*/
public function renderPage(string $tabName, JTLSmarty $smarty, $request): string
{
$monduRequestType = !empty($request['request_type']) ? $request['request_type'] : null;
if ($monduRequestType === 'registerWebhooks') {
$this->handleRegisterWebhooksRequest();
}
$smarty->assign('pluginPath', $this->plugin->getPaths()->getAdminURL());
$smarty->assign('pluginURL', $this->plugin->getPaths()->getShopURL());
if ($tabName === 'Info') {
return $smarty
->assign('postUrl', Shop::getURL() . '/' . \PFAD_ADMIN . 'plugin.php?kPlugin=' . $this->plugin->getID())
->fetch($this->path . 'mondu_info.tpl');
}
return '';
}
private function handleRegisterWebhooksRequest()
{
$monduClient = new MonduClient();
$webhookTopics = ['order', 'invoice'];
$webhookSecret = $monduClient->getWebhookKeys();
if (isset($webhookSecret['error'])) {
Response::json(
$webhookSecret,
Response::HTTP_UNPROCESSABLE_ENTITY
);
exit;
}
Shop::Container()->getDB()->update('tplugineinstellungen' , 'cName' , 'webhooks_secret', (object) ['cWert' => $webhookSecret['webhook_secret']]);
foreach ($webhookTopics as $topic) {
$requestData = [
'topic' => $topic,
'address' => Shop::getURL() . '/mondu-api?return=webhook'
];
$response = $monduClient->registerWebhooks($requestData);
if (isset($response['error'])) {
Response::json(
$response,
Response::HTTP_UNPROCESSABLE_ENTITY
);
}
}
Response::json(
['success' => true, 'webhooks_secret' => $webhookSecret['webhook_secret']]
);
}
}