This repository has been archived by the owner on Oct 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monetivo.php
426 lines (362 loc) · 12.4 KB
/
monetivo.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
<?php
/**
* Monetivo for Prestashop
*
* @author monetivo <hello@monetivo.com>
*/
if (!defined('_PS_VERSION_')) {
exit;
}
/**
* Monetivo payment module
*/
class Monetivo extends PaymentModule
{
/**
* Disable config form
*/
protected $config_form = false;
/**
* Set prestashop module pharams
*/
public function __construct()
{
$this->name = 'monetivo';
$this->tab = 'payments_gateways';
$this->version = '1.0.0';
$this->author = 'monetivo';
$this->need_instance = 1;
/**
* Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
*/
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('monetivo for Prestashop');
$this->description = $this->l('monetivo for Prestashop');
$this->limited_countries = array('PL');
$this->limited_currencies = array('PLN');
$this->ps_versions_compliancy = array('min' => '1.6',
'max' => _PS_VERSION_);
/**
* Include API
*
* @see https://docs.monetivo.com/#php
*/
require_once('vendor/monetivo/monetivo-php/autoload.php');
// admin notices
$notset = false;
foreach ($this->getConfigFormValues() as $v)
if(empty($v)) $notset = true;
if($notset)
$this->warning = $this->l('Module needs to be configured before usage');
elseif(substr(trim(Configuration::get('MONETIVO_APP_TOKEN')),0,4) == 'test')
$this->warning = $this->l('Module is set in SANDBOX mode');
else
$this->warning = null;
// all is set but
if(!$notset && empty(Configuration::get('MONETIVO_PAYMENT_STATUS_NEW')))
{
// add status - disable module on fail
if(!Configuration::updateValue(
'MONETIVO_PAYMENT_STATUS_NEW', $this->_addNewOrderState(
'MONETIVO_PAYMENT_STATUS_NEW', array('en' => 'Waiting for monetivo payment',
'pl' => 'Oczekiwanie na płatność monetivo'))
))
$this->disable();
}
}
/**
* Adding new order state
*
* @param $state
* @param $names
*
* @return integer return Order State ID
*/
private function _addNewOrderState($state, $names)
{
error_log('Do we have state '.$state.': '.Configuration::get($state));
if (!(Validate::isInt(Configuration::get($state))
&& Validate::isLoadedObject(
$order_state = new OrderState(Configuration::get($state))
))
) {
$order_state = new OrderState();
if (!empty($names)) {
foreach ($names as $code => $name) {
$order_state->name[Language::getIdByIso($code)] = $name;
}
}
$order_state->send_email = false;
$order_state->invoice = false;
$order_state->unremovable = true;
$order_state->color = '#77b3e0';
$order_state->module_name = 'monetivo';
if (!$order_state->add() || !Configuration::updateValue(
$state, $order_state->id
)
) {
error_log('State '.$state.' not added');
return false;
}
copy(
_PS_MODULE_DIR_ . $this->name . '/img/mvo_status.png', _PS_IMG_DIR_ . 'os/' . $order_state->id . '.gif'
);
error_log('State id '.$order_state->id);
return $order_state->id;
}
return false;
}
/**
* Instalation of module Monetivo for prestashop
*
* @return registerHook|false The Prestashop hooks or false if curl extension not enabled or country limit is set.
*/
public function install()
{
/**
* cURL extension is require for the API
*/
if (extension_loaded('curl') == false) {
$this->_errors[] = $this->l(
'You have to enable the cURL extension on your server to install this module'
);
return false;
}
$iso_code = Country::getIsoById(
Configuration::get('PS_COUNTRY_DEFAULT')
);
if (in_array($iso_code, $this->limited_countries) == false) {
$this->_errors[] = $this->l(
'This module is not available in your country'
);
return false;
}
return parent::install()
&& $this->createHooks()
&& Configuration::updateValue(
'MONETIVO_PAYMENT_STATUS_NEW', $this->_addNewOrderState(
'MONETIVO_PAYMENT_STATUS_NEW', array('en' => 'Waiting for monetivo payment',
'pl' => 'Oczekiwanie na płatność monetivo'))
);
}
private function createHooks()
{
$registerStatus = $this->registerHook('header') &&
$this->registerHook('paymentReturn') &&
$this->registerHook('backOfficeHeader');
if (version_compare(_PS_VERSION_, '1.7', 'lt')) {
$registerStatus &= $this->registerHook('payment');
} else {
$registerStatus &= $this->registerHook('paymentOptions');
}
return $registerStatus;
}
/**
* Uninstall of module Monetivo for prestashop
*
* @return bool
*/
public function uninstall()
{
Configuration::deleteByName('MONETIVO_APP_TOKEN');
Configuration::deleteByName('MONETIVO_LOGIN');
Configuration::deleteByName('MONETIVO_PASSWORD');
return parent::uninstall();
}
/**
* Load the configuration form
*
* @return string renderForm()
*/
public function getContent()
{
/**
* If values have been submitted in the form, process.
*/
$output = '';
if (((bool)Tools::isSubmit('submitMonetivoModule')) == true) {
$output .= $this->postProcess();
}
$this->context->smarty->assign('module_dir', $this->_path);
$output .= $this->context->smarty->fetch(
$this->local_path . 'views/templates/admin/configure.tpl'
);
return $output . $this->renderForm();
}
/**
* Create the form that will be displayed in the configuration of your module.
*
* @return rendered form
*/
protected function renderForm()
{
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get(
'PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0
);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitMonetivoModule';
$helper->currentIndex = $this->context->link->getAdminLink(
'AdminModules', false
)
. '&configure=' . $this->name . '&tab_module=' . $this->tab
. '&module_name=' . $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(),
/* Add values for your inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
/**
* Create the structure of your form.
*
* @return array form structure
*/
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'col' => 3,
'type' => 'text',
'desc' => $this->l('Enter a valid Monetivo login'),
'name' => 'MONETIVO_LOGIN',
'label' => $this->l('Monetivo login'),
),
array(
'col' => 3,
'type' => 'password',
'desc' => $this->l('Enter a valid Monetivo password'),
'name' => 'MONETIVO_PASSWORD',
'label' => $this->l('Monetivo Password'),
),
array(
'col' => 5,
'type' => 'text',
'desc' => $this->l('Enter a valid Monetivo token'),
'name' => 'MONETIVO_APP_TOKEN',
'label' => $this->l('Monetivo token'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
/**
* Set values for the inputs.
*
* @return array of configuration values
*/
protected function getConfigFormValues()
{
return array(
'MONETIVO_LOGIN' => Configuration::get(
'MONETIVO_LOGIN'
),
'MONETIVO_PASSWORD' => Configuration::get(
'MONETIVO_PASSWORD'
),
'MONETIVO_APP_TOKEN' => Configuration::get(
'MONETIVO_APP_TOKEN'
),
);
}
/**
* Save form data.
*/
protected function postProcess()
{
$form_values = $this->getConfigFormValues();
// check data validity
try {
$app_token = Tools::getValue('MONETIVO_APP_TOKEN');
$login = Tools::getValue('MONETIVO_LOGIN');
$password = Tools::getValue('MONETIVO_PASSWORD');
$api = new \Monetivo\MerchantApi($app_token);
$api->auth($login, $password);
$api->call('get', 'auth/check_token');
} catch (\Monetivo\Exceptions\MonetivoException $e) {
$error = $this->l('Cannot save configuration') . ': ';
if ($e->getHttpCode() == 401)
$error .= $this->l('Invalid monetivo credentials');
else
$error .= $this->l('API error') . ' (' . $e->getMessage() . ')';
return $this->displayError($error);
}
foreach (array_keys($form_values) as $key) {
Configuration::updateValue($key, trim(Tools::getValue($key)));
}
return $this->displayConfirmation($this->l('Settings updated'));
}
/**
* The CSS files added on the BO.
*
* @return void
*/
public function hookBackOfficeHeader()
{
$this->context->controller->addCSS($this->_path . 'views/css/back.css');
}
/**
* The CSS files added on the FO.
*
* @return void
*/
public function hookHeader()
{
$this->context->controller->addCSS(
$this->_path . '/views/css/front.css'
);
}
/**
* This method is used to render the payment button,
* Take care if the button should be displayed or not.
*
* @param $params ['cart']
*
* @return string|false Check if currency is allowed
*/
public function hookPayment($params)
{
$currency_id = $params['cart']->id_currency;
$currency = new Currency((int)$currency_id);
if (in_array($currency->iso_code, $this->limited_currencies) == false) {
return false;
}
$this->smarty->assign('module_dir', $this->_path);
return $this->display(__FILE__, 'views/templates/hook/payment.tpl');
}
/**
* Only for >=1.7
* @param $params
* @return array|void
*/
public function hookPaymentOptions($params)
{
$currency_id = $params['cart']->id_currency;
$currency = new Currency((int)$currency_id);
if (in_array($currency->iso_code, $this->limited_currencies) == false)
return;
$paymentOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption();
$paymentOption->setCallToActionText($this->l('Pay with monetivo'))
->setLogo(Media::getMediaPath(_PS_MODULE_DIR_ . $this->name . '/img/mvo_20.png'))
->setModuleName($this->name)
->setAction($this->context->link->getModuleLink($this->name, 'redirect'));
return array($paymentOption);
}
}