-
Notifications
You must be signed in to change notification settings - Fork 0
/
paysera.php
320 lines (268 loc) · 9.07 KB
/
paysera.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
<?php
/**
* This file is part of the paysera module.
*
* @author Šarūnas Jonušas, https://github.com/sarjon
* @copyright Copyright (c) Šarūnas Jonušas
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
/**
* Class Paysera
*/
class Paysera extends PaymentModule
{
/**
* Since Paysera accepts all prices in cents,
* we need to multiply each price by defined value.
*
* @var int
*/
const PRICE_MULTIPLIER = 100;
/**
* Paysera payment constants
*/
const PAYMENT_NOT_EXECUTED = 0;
const PAYMENT_ACCEPTED = 1;
const PAYMENT_ACCEPTED_NOT_EXECUTED = 2;
/**
* @var ContainerBuilder
*/
protected $container;
/**
* Paysera constructor.
*/
public function __construct()
{
$this->name = 'paysera';
$this->author = 'Šarūnas Jonušas';
$this->version = '1.3.0';
$this->tab = 'payments_gateways';
$this->compatibility = ['min' => '1.7.1.0', 'max' => _PS_VERSION_];
$this->controllers = ['redirect', 'callback', 'accept', 'cancel', 'validation'];
parent::__construct();
$this->autoload();
$this->compile();
$this->displayName = $this->l('Paysera');
$this->description = $this->l('Accept payments by Paysera system.');
}
/**
* Redirect to configuration controller
*/
public function getContent()
{
Tools::redirectAdmin($this->context->link->getAdminLink('AdminPayseraConfiguration'));
}
/**
* Install module
*
* @return bool
*/
public function install()
{
$installer = $this->container->get('paysera.installer');
return parent::install() && $installer->installl();
}
/**
* Uninstall module
*
* @return bool
*/
public function uninstall()
{
$installer = $this->container->get('paysera.installer');
return $installer->uninstall() && parent::uninstall();
}
/**
* Module tabs
*
* @return array
*/
public function getTabs()
{
$installer = $this->container->get('paysera.installer');
return $installer->getTabs();
}
/**
* Add custom content to header
*
* @return string
*/
public function hookHeader()
{
$includeVerificationContent = (bool) Configuration::get('PAYSERA_INCLUDE_VERIFICATION');
if ($includeVerificationContent) {
$verificationCode = Configuration::get('PAYSERA_VERIFICATION_CODE');
$this->context->smarty->assign('verificationCode', $verificationCode);
return $this->context->smarty->fetch('module:paysera/views/templates/hook/header.tpl');
}
return '';
}
/**
* Add JS & CSS to front controller
*/
public function hookActionFrontControllerSetMedia()
{
$controller = $this->context->controller->php_self;
if ('order' == $controller) {
$displayPaymentMethods = (bool) Configuration::get('PAYSERA_DISPLAY_PAYMENT_METHODS');
if ($displayPaymentMethods) {
$this->context->controller->registerJavascript(
sha1('modules-paysera-order'),
'modules/paysera/views/js/front/payment-methods.js'
);
}
}
$displayWidget = (bool) Configuration::get('PAYSERA_DISPLAY_WIDGET');
if ($displayWidget) {
$projectId = Configuration::get('PAYSERA_PROJECT_ID');
$langIso = $this->context->language->iso_code;
$defaultLang = $this->container->getParameter('default_language');
$supportedLangs = $this->container->getParameter('supported_languages');
$jsParams = [
'wtpQualitySign_projectId' => $projectId,
'wtpQualitySign_language' => in_array($langIso, $supportedLangs) ? $langIso : $defaultLang,
];
Media::addJsDef($jsParams);
$this->context->controller->registerJavascript(
sha1('modules-paysera-widget'),
$this->container->getParameter('widget_js'),
['server' => 'remote']
);
}
}
/**
* Get module payment options
*
* @return array|PaymentOption[]
*/
public function hookPaymentOptions()
{
if (!$this->areCredentialsNonEmpty()) {
return [];
}
$payseraOption = new PaymentOption();
$payseraOption->setCallToActionText($this->l('Pay by Paysera'));
$payseraOption->setAction($this->context->link->getModuleLink($this->name, 'validation'));
$displayPaymentMethods = (bool) Configuration::get('PAYSERA_DISPLAY_PAYMENT_METHODS');
if ($displayPaymentMethods) {
$projectID = Configuration::get('PAYSERA_PROJECT_ID');
$defaultCountry = Configuration::get('PAYSERA_DEFAULT_COUNTRY');
$supportedLangs = $this->container->getParameter('supported_languages');
$currencyISO = $this->context->currency->iso_code;
$amount = $this->getPayAmmountInCents($this->context->cart);
$langIso = strtolower($this->context->language->iso_code);
$langIso = in_array($langIso, $supportedLangs) ? $langIso : 'en';
$methods = WebToPay::getPaymentMethodList($projectID, $currencyISO)
->filterForAmount($amount, $currencyISO)
->setDefaultLanguage($langIso)
->getCountries();
$this->context->smarty->assign([
'defaultCountry' => $defaultCountry,
'payMethods' => $methods,
]);
$additionalInfo = $this->context->smarty->fetch('module:paysera/views/templates/hook/payment-options.tpl');
$payseraOption->setAdditionalInformation($additionalInfo);
$payseraOption->setInputs([
'paysera_payment_method' => [
'name' => 'paysera_payment_method',
'type' => 'hidden',
'value' => '',
],
]);
}
return [$payseraOption];
}
/**
* Display payment content if order is not paid
*
* @param array $params
*
* @return string
*/
public function hookDisplayOrderDetail(array $params)
{
/** @var Order $order */
$order = $params['order'];
$customer = $this->context->customer;
if (!$this->active || $order->module != $this->name || $order->id_customer != $customer->id) {
return '';
}
if ($order->hasBeenPaid()) {
return '';
}
$paymentRedirectUrl = $this->context->link->getModuleLink($this->name, 'redirect', ['id_order' => $order->id]);
$this->context->smarty->assign([
'paymentRedirectUrl' => $paymentRedirectUrl,
]);
return $this->context->smarty->fetch('module:paysera/views/templates/hook/display-order-detail.tpl');
}
/**
* Check if module supports cart currency
*
* @return bool
*/
public function checkCurrency()
{
$idCurrentCurrency = $this->context->cart->id_currency;
$currency = new Currency($idCurrentCurrency);
$moduleCurrencies = $this->getCurrency($idCurrentCurrency);
if (is_array($moduleCurrencies)) {
foreach ($moduleCurrencies as $moduleCurrency) {
if ($currency->id == $moduleCurrency['id_currency']) {
return true;
}
}
}
return false;
}
/**
* Get pay amount in cents
*
* @param Cart $cart
*
* @return int
*/
public function getPayAmmountInCents(Cart $cart)
{
$total = $cart->getOrderTotal();
$amount = (float) (string) ($total * self::PRICE_MULTIPLIER);
return $amount;
}
/**
* Check if merchant has configured it's credentials
*
* @return bool
*/
protected function areCredentialsNonEmpty()
{
$projectID = Configuration::get('PAYSERA_PROJECT_ID');
$projectPassword = Configuration::get('PAYSERA_PROJECT_PASSWORD');
return !empty($projectID) && !empty($projectPassword);
}
/**
* Build module service contaienr
*/
private function compile()
{
$this->container = new ContainerBuilder();
$this->container->addCompilerPass(new LegacyCompilerPass());
$this->container->set('paysera.module', $this);
$locator = new FileLocator($this->getLocalPath().'config');
$loader = new YamlFileLoader($this->container, $locator);
$loader->load('config.yml');
$this->container->compile();
}
/**
* Require autoloader
*/
private function autoload()
{
require_once $this->getLocalPath().'vendor/autoload.php';
}
}