|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Plugin Name: Pagantis |
|
4
|
|
|
* Plugin URI: http://www.pagantis.com/ |
|
5
|
|
|
* Description: Financiar con Pagantis |
|
6
|
|
|
* Version: 8.0.0 |
|
7
|
|
|
* Author: Pagantis |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
//namespace Gateways; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
if (!defined('ABSPATH')) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
class WcPagantis |
|
18
|
|
|
{ |
|
19
|
|
|
const GIT_HUB_URL = 'https://github.com/pagantis/woocommerce'; |
|
20
|
|
|
const PAGANTIS_DOC_URL = 'https://developer.pagantis.com'; |
|
21
|
|
|
const SUPPORT_EML = 'mailto:[email protected]?Subject=woocommerce_plugin'; |
|
22
|
|
|
/** Concurrency tablename */ |
|
23
|
|
|
const LOGS_TABLE = 'pagantis_logs'; |
|
24
|
|
|
/** Config tablename */ |
|
25
|
|
|
const CONFIG_TABLE = 'pagantis_config'; |
|
26
|
|
|
|
|
27
|
|
|
public $defaultConfigs = array('PAGANTIS_TITLE'=>'Instant Financing', |
|
28
|
|
|
'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pgSDK.simulator.types.SIMPLE', |
|
29
|
|
|
'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pgSDK.simulator.skins.BLUE', |
|
30
|
|
|
'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons', |
|
31
|
|
|
|
|
32
|
|
|
'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3, |
|
33
|
|
|
'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12, |
|
34
|
|
|
'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default', |
|
35
|
|
|
'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pgSDK.simulator.positions.INNER', |
|
36
|
|
|
'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'default', |
|
37
|
|
|
'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'default', |
|
38
|
|
|
'PAGANTIS_FORM_DISPLAY_TYPE'=>0, |
|
39
|
|
|
'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1, |
|
40
|
|
|
'PAGANTIS_URL_OK'=>'', |
|
41
|
|
|
'PAGANTIS_URL_KO'=>'', |
|
42
|
|
|
'PAGANTIS_TITLE_EXTRA' => 'Pay up to 12 comfortable installments with Pagantis. Completely online and sympathetic request, and the answer is immediate!' |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
/** @var Array $extraConfig */ |
|
46
|
|
|
public $extraConfig; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* WC_Pagantis constructor. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct() |
|
52
|
|
|
{ |
|
53
|
|
|
require_once(plugin_dir_path(__FILE__).'/vendor/autoload.php'); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
$this->template_path = plugin_dir_path(__FILE__).'/templates/'; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$this->pagantisActivation(); |
|
58
|
|
|
|
|
59
|
|
|
$this->extraConfig = $this->getExtraConfig(); |
|
60
|
|
|
|
|
61
|
|
|
load_plugin_textdomain('pagantis', false, basename(dirname(__FILE__)).'/languages'); |
|
|
|
|
|
|
62
|
|
|
add_filter('woocommerce_payment_gateways', array($this, 'addPagantisGateway')); |
|
|
|
|
|
|
63
|
|
|
add_filter('woocommerce_available_payment_gateways', array($this, 'pagantisFilterGateways'), 9999); |
|
64
|
|
|
add_filter('plugin_row_meta', array($this, 'pagantisRowMeta'), 10, 2); |
|
65
|
|
|
add_filter('plugin_action_links_'.plugin_basename(__FILE__), array($this, 'pagantisActionLinks')); |
|
|
|
|
|
|
66
|
|
|
add_action('woocommerce_after_add_to_cart_form', array($this, 'pagantisAddProductSimulator')); |
|
|
|
|
|
|
67
|
|
|
add_action('wp_enqueue_scripts', 'add_widget_js'); |
|
68
|
|
|
add_action('rest_api_init', array($this, 'pagantisRegisterEndpoint')); //Endpoint |
|
69
|
|
|
add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/* |
|
73
|
|
|
* Replace 'textdomain' with your plugin's textdomain. e.g. 'woocommerce'. |
|
74
|
|
|
* File to be named, for example, yourtranslationfile-en_GB.mo |
|
75
|
|
|
* File to be placed, for example, wp-content/lanaguages/textdomain/yourtranslationfile-en_GB.mo |
|
76
|
|
|
*/ |
|
77
|
|
|
public function loadPagantisTranslation($mofile, $domain) |
|
78
|
|
|
{ |
|
79
|
|
|
if ('pagantis' === $domain) { |
|
80
|
|
|
$mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . get_locale() . '.mo'; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
return $mofile; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Sql table |
|
87
|
|
|
*/ |
|
88
|
|
|
public function pagantisActivation() |
|
89
|
|
|
{ |
|
90
|
|
|
global $wpdb; |
|
91
|
|
|
$tableName = $wpdb->prefix.self::CONFIG_TABLE; |
|
92
|
|
|
|
|
93
|
|
|
//Check if table exists |
|
94
|
|
|
$tableExists = $wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName; |
|
95
|
|
|
if ($tableExists) { |
|
96
|
|
|
$charset_collate = $wpdb->get_charset_collate(); |
|
97
|
|
|
$sql = "CREATE TABLE IF NOT EXISTS $tableName ( |
|
98
|
|
|
id int NOT NULL AUTO_INCREMENT, |
|
99
|
|
|
config varchar(60) NOT NULL, |
|
100
|
|
|
value varchar(1000) NOT NULL, |
|
101
|
|
|
UNIQUE KEY id(id)) $charset_collate"; |
|
102
|
|
|
|
|
103
|
|
|
require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
|
|
|
|
|
|
104
|
|
|
dbDelta($sql); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$dbConfigs = $wpdb->get_results("select * from $tableName", ARRAY_A); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
// Convert a multimple dimension array for SQL insert statements into a simple key/value |
|
110
|
|
|
$simpleDbConfigs = array(); |
|
111
|
|
|
foreach ($dbConfigs as $config) { |
|
112
|
|
|
$simpleDbConfigs[$config['config']] = $config['value']; |
|
113
|
|
|
} |
|
114
|
|
|
$newConfigs = array_diff_key($this->defaultConfigs, $simpleDbConfigs); |
|
115
|
|
|
if (!empty($newConfigs)) { |
|
116
|
|
|
foreach ($newConfigs as $key => $value) { |
|
117
|
|
|
$wpdb->insert($tableName, array('config' => $key, 'value' => $value), array('%s', '%s')); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
//Current plugin config: pagantis_public_key => New field --- public_key => Old field |
|
122
|
|
|
$settings = get_option('woocommerce_pagantis_settings'); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
if (!isset($settings['pagantis_public_key']) && $settings['public_key']) { |
|
125
|
|
|
$settings['pagantis_public_key'] = $settings['public_key']; |
|
126
|
|
|
unset($settings['public_key']); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (!isset($settings['pagantis_private_key']) && $settings['secret_key']) { |
|
130
|
|
|
$settings['pagantis_private_key'] = $settings['secret_key']; |
|
131
|
|
|
unset($settings['secret_key']); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
update_option('woocommerce_pagantis_settings', $settings); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Product simulator |
|
139
|
|
|
*/ |
|
140
|
|
|
public function pagantisAddProductSimulator() |
|
141
|
|
|
{ |
|
142
|
|
|
global $product; |
|
143
|
|
|
|
|
144
|
|
|
$cfg = get_option('woocommerce_pagantis_settings'); |
|
|
|
|
|
|
145
|
|
|
if ($cfg['enabled'] !== 'yes' || $cfg['pagantis_public_key'] == '' || $cfg['pagantis_private_key'] == '' || |
|
146
|
|
|
$cfg['simulator'] !== 'yes' || $product->price < $this->extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT'] ) { |
|
147
|
|
|
return; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$template_fields = array( |
|
151
|
|
|
'total' => is_numeric($product->price) ? $product->price : 0, |
|
152
|
|
|
'public_key' => $cfg['pagantis_public_key'], |
|
153
|
|
|
'simulator_type' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'], |
|
154
|
|
|
'positionSelector' => $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'], |
|
155
|
|
|
'quantitySelector' => $this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'], |
|
156
|
|
|
'priceSelector' => $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'], |
|
157
|
|
|
'totalAmount' => is_numeric($product->price) ? $product->price : 0 |
|
158
|
|
|
); |
|
159
|
|
|
wc_get_template('product_simulator.php', $template_fields, '', $this->template_path); |
|
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Add Pagantis to payments list. |
|
164
|
|
|
* |
|
165
|
|
|
* @param $methods |
|
166
|
|
|
* |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
public function addPagantisGateway($methods) |
|
170
|
|
|
{ |
|
171
|
|
|
if (! class_exists('WC_Payment_Gateway')) { |
|
172
|
|
|
return $methods; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
include_once('controllers/paymentController.php'); |
|
176
|
|
|
$methods[] = 'WcPagantisGateway'; |
|
177
|
|
|
|
|
178
|
|
|
return $methods; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Initialize WC_Pagantis class |
|
183
|
|
|
* |
|
184
|
|
|
* @param $methods |
|
185
|
|
|
* |
|
186
|
|
|
* @return mixed |
|
187
|
|
|
*/ |
|
188
|
|
|
public function pagantisFilterGateways($methods) |
|
189
|
|
|
{ |
|
190
|
|
|
$pagantis = new WcPagantisGateway(); |
|
191
|
|
|
if ($pagantis->is_available()) { |
|
192
|
|
|
$methods['pagantis'] = $pagantis; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return $methods; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Add links to Plugin description |
|
200
|
|
|
* |
|
201
|
|
|
* @param $links |
|
202
|
|
|
* |
|
203
|
|
|
* @return mixed |
|
204
|
|
|
*/ |
|
205
|
|
|
public function pagantisActionLinks($links) |
|
206
|
|
|
{ |
|
207
|
|
|
$params_array = array('page' => 'wc-settings', 'tab' => 'checkout', 'section' => 'pagantis'); |
|
208
|
|
|
$setting_url = esc_url(add_query_arg($params_array, admin_url('admin.php?'))); |
|
|
|
|
|
|
209
|
|
|
$setting_link = '<a href="'.$setting_url.'">'.__('Settings', 'pagantis').'</a>'; |
|
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
array_unshift($links, $setting_link); |
|
212
|
|
|
|
|
213
|
|
|
return $links; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Add links to Plugin options |
|
218
|
|
|
* |
|
219
|
|
|
* @param $links |
|
220
|
|
|
* @param $file |
|
221
|
|
|
* |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
|
|
public function pagantisRowMeta($links, $file) |
|
225
|
|
|
{ |
|
226
|
|
|
if ($file == plugin_basename(__FILE__)) { |
|
|
|
|
|
|
227
|
|
|
$links[] = '<a href="'.WcPagantis::GIT_HUB_URL.'" target="_blank">'.__('Documentation', 'pagantis').'</a>'; |
|
|
|
|
|
|
228
|
|
|
$links[] = '<a href="'.WcPagantis::PAGANTIS_DOC_URL.'" target="_blank">'. |
|
229
|
|
|
__('API documentation', 'pagantis').'</a>'; |
|
230
|
|
|
$links[] = '<a href="'.WcPagantis::SUPPORT_EML.'">'.__('Support', 'pagantis').'</a>'; |
|
231
|
|
|
|
|
232
|
|
|
return $links; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return $links; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Read logs |
|
240
|
|
|
*/ |
|
241
|
|
|
public function readLogs($data) |
|
242
|
|
|
{ |
|
243
|
|
|
global $wpdb; |
|
244
|
|
|
$filters = ($data->get_params()); |
|
245
|
|
|
$response = array(); |
|
246
|
|
|
$secretKey = $filters['secret']; |
|
247
|
|
|
$from = $filters['from']; |
|
248
|
|
|
$to = $filters['to']; |
|
249
|
|
|
$cfg = get_option('woocommerce_pagantis_settings'); |
|
|
|
|
|
|
250
|
|
|
$privateKey = isset($cfg['secret_key']) ? $cfg['secret_key'] : null; |
|
251
|
|
|
$tableName = $wpdb->prefix.self::LOGS_TABLE; |
|
252
|
|
|
$query = "select * from $tableName where createdAt>$from and createdAt<$to order by createdAt desc"; |
|
253
|
|
|
$results = $wpdb->get_results($query); |
|
254
|
|
|
if (isset($results) && $privateKey == $secretKey) { |
|
255
|
|
|
foreach ($results as $key => $result) { |
|
256
|
|
|
$response[$key]['timestamp'] = $result->createdAt; |
|
257
|
|
|
$response[$key]['log'] = json_decode($result->log); |
|
258
|
|
|
} |
|
259
|
|
|
} else { |
|
260
|
|
|
$response['result'] = 'Error'; |
|
261
|
|
|
} |
|
262
|
|
|
$response = json_encode($response); |
|
263
|
|
|
header("HTTP/1.1 200", true, 200); |
|
264
|
|
|
header('Content-Type: application/json', true); |
|
265
|
|
|
header('Content-Length: '.strlen($response)); |
|
266
|
|
|
echo($response); |
|
267
|
|
|
exit(); |
|
|
|
|
|
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Update extra config |
|
272
|
|
|
*/ |
|
273
|
|
|
public function updateExtraConfig($data) |
|
274
|
|
|
{ |
|
275
|
|
|
global $wpdb; |
|
276
|
|
|
$tableName = $wpdb->prefix.self::CONFIG_TABLE; |
|
277
|
|
|
$response = array('status'=>null); |
|
278
|
|
|
|
|
279
|
|
|
$filters = ($data->get_params()); |
|
280
|
|
|
$secretKey = $filters['secret']; |
|
281
|
|
|
$cfg = get_option('woocommerce_pagantis_settings'); |
|
|
|
|
|
|
282
|
|
|
$privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
|
283
|
|
|
if ($privateKey != $secretKey) { |
|
284
|
|
|
$response['status'] = 401; |
|
285
|
|
|
$response['result'] = 'Unauthorized'; |
|
286
|
|
|
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { |
|
287
|
|
|
if (count($_POST)) { |
|
288
|
|
|
foreach ($_POST as $config => $value) { |
|
289
|
|
|
if (isset($this->defaultConfigs[$config]) && $response['status']==null) { |
|
290
|
|
|
$wpdb->update( |
|
291
|
|
|
$tableName, |
|
292
|
|
|
array('value' => $value), |
|
293
|
|
|
array('config' => $config), |
|
294
|
|
|
array('%s'), |
|
295
|
|
|
array('%s') |
|
296
|
|
|
); |
|
297
|
|
|
} else { |
|
298
|
|
|
$response['status'] = 400; |
|
299
|
|
|
$response['result'] = 'Bad request'; |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
} else { |
|
303
|
|
|
$response['status'] = 422; |
|
304
|
|
|
$response['result'] = 'Empty data'; |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
if ($response['status']==null) { |
|
309
|
|
|
$tableName = $wpdb->prefix.self::CONFIG_TABLE; |
|
310
|
|
|
$dbResult = $wpdb->get_results("select config, value from $tableName", ARRAY_A); |
|
|
|
|
|
|
311
|
|
|
foreach ($dbResult as $value) { |
|
312
|
|
|
$formattedResult[$value['config']] = $value['value']; |
|
313
|
|
|
} |
|
314
|
|
|
$response['result'] = $formattedResult; |
|
|
|
|
|
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
$result = json_encode($response['result']); |
|
318
|
|
|
header("HTTP/1.1 ".$response['status'], true, $response['status']); |
|
319
|
|
|
header('Content-Type: application/json', true); |
|
320
|
|
|
header('Content-Length: '.strlen($result)); |
|
321
|
|
|
echo($result); |
|
322
|
|
|
exit(); |
|
|
|
|
|
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* ENDPOINT - Read logs -> Hook: rest_api_init |
|
327
|
|
|
* @return mixed |
|
328
|
|
|
*/ |
|
329
|
|
|
public function pagantisRegisterEndpoint() |
|
330
|
|
|
{ |
|
331
|
|
|
register_rest_route( |
|
|
|
|
|
|
332
|
|
|
'pagantis/v1', |
|
333
|
|
|
'/logs/(?P<secret>\w+)/(?P<from>\d+)/(?P<to>\d+)', |
|
334
|
|
|
array( |
|
335
|
|
|
'methods' => 'GET', |
|
336
|
|
|
'callback' => array( |
|
337
|
|
|
$this, |
|
338
|
|
|
'readLogs') |
|
339
|
|
|
), |
|
340
|
|
|
true |
|
341
|
|
|
); |
|
342
|
|
|
|
|
343
|
|
|
register_rest_route( |
|
344
|
|
|
'pagantis/v1', |
|
345
|
|
|
'/configController/(?P<secret>\w+)', |
|
346
|
|
|
array( |
|
347
|
|
|
'methods' => 'GET, POST', |
|
348
|
|
|
'callback' => array( |
|
349
|
|
|
$this, |
|
350
|
|
|
'updateExtraConfig') |
|
351
|
|
|
), |
|
352
|
|
|
true |
|
353
|
|
|
); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* @return array |
|
358
|
|
|
*/ |
|
359
|
|
|
private function getExtraConfig() |
|
360
|
|
|
{ |
|
361
|
|
|
global $wpdb; |
|
362
|
|
|
$tableName = $wpdb->prefix.self::CONFIG_TABLE; |
|
363
|
|
|
$response = array(); |
|
364
|
|
|
$dbResult = $wpdb->get_results("select config, value from $tableName", ARRAY_A); |
|
|
|
|
|
|
365
|
|
|
foreach ($dbResult as $value) { |
|
366
|
|
|
$response[$value['config']] = $value['value']; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
return $response; |
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* Add widget Js |
|
375
|
|
|
**/ |
|
376
|
|
|
function add_widget_js() |
|
377
|
|
|
{ |
|
378
|
|
|
wp_enqueue_script('pgSDK', 'https://cdn.pagantis.com/js/pg-v2/sdk.js', '', '', true); |
|
|
|
|
|
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
$WcPagantis = new WcPagantis(); |
|
382
|
|
|
|