forked from paynl/shopware5-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaynlPayment.php
168 lines (135 loc) · 4.7 KB
/
PaynlPayment.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
<?php
namespace PaynlPayment;
/**
* The plugin can be installed via composer or via the package file.
* When installed via composer, the sdk is automaticly loaded in the vendor directory.
* In the package file this is included, but need to be loaded
*/
if(!class_exists('\Paynl\Config') && file_exists(__DIR__.'/vendor/autoload.php')){
require_once (__DIR__.'/vendor/autoload.php');
}
use Doctrine\ORM\Tools\SchemaTool;
use Paynl\Paymentmethods;
use PaynlPayment\Components\Config;
use PaynlPayment\Models\Transaction;
use Shopware\Components\Model\ModelManager;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
use Shopware\Components\Plugin\Context\UpdateContext;
class PaynlPayment extends Plugin
{
public function install(InstallContext $context)
{
$this->createTables();
$this->initPaymentIdIncrementer();
parent::install($context);
}
public function update(UpdateContext $context)
{
$this->createTables();
$this->initPaymentIdIncrementer();
parent::update($context);
}
public function uninstall(UninstallContext $context)
{
$this->disablePaymentMethods($context->getPlugin());
parent::uninstall($context);
}
public function activate(ActivateContext $context)
{
$plugin = $context->getPlugin();
$this->disablePaymentMethods($plugin);
$this->installPaymentMethods($plugin);
parent::activate($context);
}
public function deactivate(DeactivateContext $context)
{
$this->disablePaymentMethods($context->getPlugin());
parent::deactivate($context);
}
public static function getSubscribedEvents()
{
return [
'Enlight_Controller_Front_StartDispatch' => 'requireAutoloader',
];
}
/**
* Require composer autoloader
*/
public function requireAutoloader()
{
if (file_exists($this->getPath() . '/vendor/autoload.php')) {
require_once($this->getPath() . '/vendor/autoload.php');
}
}
private function disablePaymentMethods(\Shopware\Models\Plugin\Plugin $plugin)
{
$em = $this->container->get('models');
$payments = $plugin->getPayments();
foreach ($payments as $payment) {
$payment->setActive(false);
}
$em->flush();
}
/**
* @param \Shopware\Models\Plugin\Plugin $plugin
*/
private function installPaymentMethods(\Shopware\Models\Plugin\Plugin $plugin)
{
/** @var Config $config */
$config = new Config($this->container->get('shopware.plugin.cached_config_reader'));
$config->loginSDK();
$methods = Paymentmethods::getList();
/** @var \Shopware\Components\Plugin\PaymentInstaller $installer */
$installer = $this->container->get('shopware.plugin_payment_installer');
foreach ($methods as $key => $method) {
$options = [
'name' => 'paynl_' . $method['id'],
'description' => $method['name'],
'action' => 'PaynlPayment',
'active' => true,
'additionalDescription' =>
'<img src="https://www.pay.nl/images/payment_profiles/50x32/' . $method['id'] . '.png" />'
];
$installer->createOrUpdate($plugin->getName(), $options);
}
}
/**
* Create tables required for this plugin
*/
private function createTables()
{
$modelManager = $this->container->get('models');
$tool = new SchemaTool($modelManager);
$classes = $this->getClasses($modelManager);
$tool->updateSchema($classes, true);
}
/**
* Get the model classes for this plugin
*
* @param ModelManager $modelManager
* @return array
*/
private function getClasses(ModelManager $modelManager)
{
return [
$modelManager->getClassMetadata(Transaction\Transaction::class)
];
}
private function initPaymentIdIncrementer()
{
$db = $this->container->get('db');
$name = 'paynl_payment_id';
$rows = $db->executeQuery('SELECT * FROM s_order_number WHERE name = :name', ['name' => $name])->fetchAll();
if (count($rows) < 1) {
$db->executeQuery('INSERT INTO `s_order_number` (`number`, `name`, `desc`) VALUES (:number, :name, :description)', [
'number' => 1000000,
'name' => $name,
'description' => 'Payment id for pay.nl payments'
]);
}
}
}