-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bootstrap.php
515 lines (460 loc) · 16.6 KB
/
Bootstrap.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php
/**
* Novalnet payment plugin
*
* NOTICE OF LICENSE
*
* This source file is subject to the GNU General Public License
* that is bundled with this package in the file freeware_license_agreement.txt
*
* DISCLAIMER
*
* If you wish to customize Novalnet payment extension for your needs, please contact [email protected] for more information.
*
* @category Novalnet
* @package NovalPayment
* @copyright Copyright (c) Novalnet
* @license https://www.novalnet.de/payment-plugins/kostenlos/lizenz GNU General Public License
*/
require_once __DIR__ . '/Components/CSRFWhitelistAware.php';
use Shopware\Models\Payment\Payment;
use Shopware\Plugins\NovalPayment\Setup\PluginForm;
use Shopware\CustomModels\Transaction\Transaction;
use Shopware\CustomModels\Api\Api;
use Shopware\CustomModels\ChangeSubscriptionPayment\ChangeSubscriptionPayment;
use Shopware\Plugins\NovalPayment\Setup\Mailer;
use Shopware\Models\Config\Element;
use Doctrine\Common\Collections\ArrayCollection;
use Shopware\Plugins\NovalPayment\Subscriber\ControllerRegistration\BackendController;
use Shopware\Plugins\NovalPayment\Subscriber\ControllerRegistration\FrontendController;
use Shopware\Plugins\NovalPayment\Subscriber\Frontend\Frontend;
use Shopware\Plugins\NovalPayment\Components\Classes\NovalnetHelper;
use Shopware\Plugins\NovalPayment\Components\Classes\DataHandler;
use Shopware\Plugins\NovalPayment\Components\Classes\ManageRequest;
class Shopware_Plugins_Frontend_NovalPayment_Bootstrap extends Shopware_Components_Plugin_Bootstrap
{
/**
* @var null|PluginForm
*/
private $pluginForm;
/**
* @var string
*/
private $novalPaymentName = "novalnetpay";
public function getVersion()
{
$info = json_decode(file_get_contents(__DIR__ . '/plugin.json'), true);
if ($info) {
return $info['currentVersion'];
} else {
throw new Exception('The plugin has an invalid version file.');
}
}
/**
* registers the custom plugin models and plugin namespaces
*
* @return void
*/
/**
* Returns the informations of plugin as array.
*
* @param null
* @return array
*/
public function getInfo()
{
$lang = (string) Shopware()->Container()->get('locale');
if (strpos($lang, 'en') !== false) {
$description = file_get_contents(__DIR__ . '/descriptionEN.html');
$label = 'Novalnet Payment';
} else {
$description = file_get_contents(__DIR__ . '/descriptionDE.html');
$label = 'Novalnet-Zahlung';
}
$info = json_decode(file_get_contents(__DIR__ . '/plugin.json'), true);
return array(
'version' => $this->getVersion(),
'author' => $info['author'],
'label' => $label,
'link' => $info['link'],
'copyright' => 'Copyright © ' . date('Y') . ', Novalnet AG',
'support' => $info['link'],
'description' => $description
);
}
/**
* @return boolean
*/
public function enable()
{
return true;
}
/**
* To disable all novalnet payments method is always called when the plugin is deactivated.
* Here, you should immediately disable active payment methods.
*/
public function disable()
{
$this->deactivatePayments();
return true;
}
public function getCapabilties()
{
return array(
'install' => true,
'update' => true,
'enable' => true,
'delete' => true
);
}
/**
* composer autoloading
*
* @return void
*
* @throws InvalidArgumentException
*/
public function afterInit()
{
$this->registerCustomModels();
$this->get('loader')->registerNamespace('Shopware\\Plugins\\NovalPayment', $this->Path());
$this->get('loader')->registerNamespace('NovalPayment', $this->Path() . 'Components/Classes/');
$this->get('snippets')->addConfigDir($this->Path() . 'Snippets/');
if (version_compare(self::getShopwareVersion(), '5.2.0', '<')) {
return;
}
}
public static function getShopwareVersion()
{
$currentVersion = '';
if (defined('\Shopware::VERSION')) {
$currentVersion = \Shopware::VERSION;
}
//get old composer versions
if ($currentVersion === '___VERSION___' && class_exists('ShopwareVersion') && class_exists('PackageVersions\Versions')) {
$currentVersion = \ShopwareVersion::parseVersion(
\PackageVersions\Versions::getVersion('shopware/shopware')
)['version'];
}
if (!$currentVersion || $currentVersion === '___VERSION___') {
$currentVersion = Shopware()->Container()->getParameter('shopware.release.version');
}
return $currentVersion;
}
/**
* perform all neccessary install tasks
*
* @return array
*/
public function install($version = '13.2.0')
{
$lang = (string) Shopware()->Container()->get('locale');
/** @var DatabaseHandler $databaseHandler */
$databaseHandler = Shopware()->Container()->get('shopware.snippet_database_handler');
$databaseHandler->loadToDatabase($this->Path() . 'Snippets/');
$service = Shopware()->Container()->get('shopware_attribute.crud_service');
$service->update('s_order_attributes', 'novalnet_payment_name', 'string');
$this->registerEvents();
$this->createPayments();
$this->createPaymentForm();
$this->createNovalnetTables();
$this->removeMailTemplate();
$this->insertMailTemplate();
if (version_compare($version, '13.0.0', '>=')) {
$this->deactivateV12Payments();
}
$returnArray = array(
'success' => true,
'invalidateCache' => array('config', 'backend', 'proxy', 'template','frontend', 'theme')
);
if (version_compare($version, '12.2.0', '>=')) {
$returnArray = array_merge($returnArray, array(
'message' => $this->getInfoMessage($lang)
));
}
return $returnArray;
}
/**
* Update function of the plugin
*
* @param string $version
* @return array
*/
public function update($version)
{
$this->install();
return array(
'success' => true,
'invalidateCache' => array('config', 'backend', 'proxy', 'template','frontend', 'theme')
);
}
/**
* To uninstall the installed Novalnet payment modules
*
* @return array
*/
public function uninstall()
{
$service = Shopware()->Container()->get('shopware_attribute.crud_service');
$tableMapping = Shopware()->Container()->get('shopware_attribute.table_mapping');
$service->update('s_order_attributes', 'novalnet_payment_name', 'string');
if ($tableMapping->isTableColumn('s_order_attributes', 'novalnet_payment_name') === true) {
$service->delete('s_order_attributes', 'novalnet_payment_name');
}
$this->deactivatePayments();
$this->removeMailTemplate();
Shopware()->Container()->get('shopware.snippet_database_handler')->removeFromDatabase($this->Path() . 'Snippets/');
$this->removeNovalnetApiDetails();
$this->secureUninstall();
return array(
'success' => true,
'invalidateCache' => array('config', 'backend', 'proxy', 'template','frontend', 'theme')
);
}
/**
* register for several events to extend shop functions
*/
protected function registerEvents()
{
$this->subscribeEvent(
'Enlight_Controller_Front_DispatchLoopStartup',
'onDispatchLoopStartup'
);
$this->subscribeEvent(
'Theme_Compiler_Collect_Plugin_Javascript',
'addJsFiles'
);
}
/**
* Register Template directory
*
* @return void
*/
public function registerTemplateDir($viewDir = 'Views')
{
$template = $this->get('template');
$template->addTemplateDir($this->Path() . $viewDir.'/');
}
/**
* Return the update information to end customer
*
* @param $lang
*
* @return string
*/
private function getInfoMessage($lang)
{
if (strpos($lang, 'en') !== false) {
return 'Thank you for installing/upgrading Novalnet payment plugin. For setup and handling of the Novalnet-Payment plugin you can find the installation guide <a href="https://www.novalnet.com/docs/plugins/installation-guides/shopware-5-installation-guide.pdf" target="_blank" style="text-decoration: underline; font-weight: bold; color:#0080c9;">Here</a>.';
} else {
return 'Vielen Dank für die Installation/Aktualisierung des Novalnet-Zahlungsplugins. Für die Einrichtung und Verwendung des Plugins finden Sie die Installationsanleitung <a href="https://www.novalnet.com/docs/plugins/installation-guides/shopware-5-installation-guide.pdf" target="_blank" style="text-decoration: underline; font-weight: bold; color:#0080c9;">Here</a>';
}
}
/**
* create payment methods
*/
protected function createPayments()
{
/* @var Repository $existingPayment */
$existingPayment = Shopware()->Models()->getRepository('Shopware\Models\Payment\Payment')->findOneBy(['name' => $this->novalPaymentName ]);
# create the payment method if not exists
$lang = (string) Shopware()->Container()->get('locale');
$novalPaymentDesc = (strpos($lang, 'en') !== false) ? 'Novalnet Payment' : 'Novalnet-Zahlung' ;
if (!$existingPayment) {
$paymentMethodData = [
'name' => $this->novalPaymentName,
'description' => $novalPaymentDesc,
'action' => 'NovalPayment',
'active' => 0,
'position' => '1',
'additionalDescription' => '',
'template' => '',
];
$this->createPayment($paymentMethodData);
}
}
/**
* Create the payment form.
*
* @return void
*/
private function createPaymentForm()
{
if ($this->pluginForm == null) {
$this->pluginForm = new PluginForm();
}
$formElementOrder = ['novalnet_api', 'novalnet_secret_key', 'novalnet_password', 'novalnet_clientkey','NClass', 'novalnet_tariff', 'novalnet_callback', 'novalnet_callback_url', 'novalnetcallback', 'novalnetcallback_test_mode', 'novalnet_callback_mail_send_to', 'novalnet_after_payment_status' ];
$formElementName = Shopware()->Db()->fetchAll('SELECT name FROM s_core_config_elements WHERE name LIKE "%novalnet%"');
foreach ($formElementName as $rmvalElement) {
if (! in_array($rmvalElement['name'], $formElementOrder)) {
Shopware()->Db()->query('DELETE FROM s_core_config_elements WHERE name = "' . $rmvalElement['name'] . '"');
}
}
$form = $this->Form();
$Config = $this->pluginForm->getPaymentForm();
foreach ($Config as $key => $value) {
$form->setElement($value['type'], $key, $value['options']);
}
// assign translation for the elements
$this->pluginForm->getPaymentFormTranslations($form);
}
/**
* Create Transaction and API tables
*
* @return void
*/
private function createNovalnetTables()
{
$em = $this->Application()->Models();
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em);
$schema = [
$em->getClassMetadata('Shopware\CustomModels\Transaction\Transaction'),
$em->getClassMetadata('Shopware\CustomModels\Api\Api'),
$em->getClassMetadata('Shopware\CustomModels\ChangeSubscriptionPayment\ChangeSubscriptionPayment')
];
$schemaTool->updateSchema($schema, true);
}
/**
* Insert novalet mail template into database
*
*/
private function insertMailTemplate()
{
$mailer = new Mailer(Shopware()->Container()->get('models'));
$mailer->createMailTemplate();
}
/**
* Remove novalet mail template from database
*
*/
private function removeMailTemplate()
{
$mailer = new Mailer(Shopware()->Container()->get('models'));
$mailer->remove();
}
/**
* Register the subscriber for autoload
*/
public function onDispatchLoopStartup(\Enlight_Event_EventArgs $args)
{
$helper = new NovalnetHelper(Shopware()->Container(), Shopware()->Container()->get('snippets'));
$dataHandler = new DataHandler(Shopware()->Models());
$requestHandler = new ManageRequest($helper);
$this->get('events')->addSubscriber(new BackendController($this));
$this->get('events')->addSubscriber(new FrontendController());
$this->get('events')->addSubscriber(new Frontend($helper, $requestHandler, $dataHandler));
}
/**
* Make directory for js
*
* @return ArrayCollection
*/
public function addJsFiles()
{
return new ArrayCollection([
$this->Path() . '/Views/frontend/_public/src/js/jquery.novalnet-payment.js',
]);
}
/**
* deactivate the payments
*
* @return void
*/
public function deactivatePayments()
{
$paymentType = Shopware()->Container()->get('models')->getRepository('Shopware\Models\Payment\Payment')->findOneBy([
'name' => $this->novalPaymentName,
]);
if ($paymentType) {
$paymentType->setActive(false);
Shopware()->Container()->get('models')->persist($paymentType);
Shopware()->Container()->get('models')->flush($paymentType);
}
}
/**
* Remove the API data from the table.
*
* @return void
*/
public function removeNovalnetApiDetails()
{
$count = Shopware()->Db()->fetchAll('SHOW TABLES LIKE "s_novalnet_api"');
if (!empty($count)) {
/** @var Repository $apiRepository */
$apiRepository = Shopware()->Container()->get('models')->getRepository('Shopware\CustomModels\Api\Api');
/** @var API[] $apiData */
$apiData = $apiRepository->findAll();
foreach ($apiData as $data) {
Shopware()->Container()->get('models')->transactional(static function ($em) use ($data) {
if (!empty($data)) {
/** @var ModelManager $em */
$em->remove($data);
$em->flush();
}
});
}
}
}
/**
* @return bool
*/
public function secureUninstall()
{
return true;
}
/**
* deactivate the payments
*
* @return void
*/
public function deactivateV12Payments()
{
$v12Payments = $this->getV12Payments();
foreach ($v12Payments as $paymentName) {
$paymentType = Shopware()->Models()->getRepository('Shopware\Models\Payment\Payment')->findOneBy([
'name' => $paymentName,
]);
if ($paymentType) {
$paymentType->setActive(false);
Shopware()->Models()->persist($paymentType);
Shopware()->Models()->flush($paymentType);
}
}
}
/**
* get version 12 payment names
*
* @return void
*/
public function getV12Payments()
{
$paymentTypes = [
'novalnetsepa' ,
'novalnetcc' ,
'novalnetapplepay',
'novalnetinvoice',
'novalnetgooglepay',
'novalnetprepayment',
'novalnetinvoiceGuarantee',
'novalnetsepaGuarantee',
'novalnetideal',
'novalnetinstant',
'novalnetgiropay',
'novalnetcashpayment',
'novalnetprzelewy24',
'novalneteps',
'novalnetinvoiceinstalment',
'novalnetsepainstalment',
'novalnetpaypal',
'novalnetpostfinancecard',
'novalnetpostfinance',
'novalnetbancontact',
'novalnetmultibanco',
'novalnetonlinebanktransfer',
'novalnetalipay',
'novalnetwechatpay',
'novalnettrustly'
];
return $paymentTypes;
}
}