PluginProbe
PayPlug for WooCommerce (Official) / trunk
PayPlug for WooCommerce (Official) vtrunk
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Service / Configuration.php

Configuration.php in PayPlug for WooCommerce (Official) trunk, at src/Service/Configuration.php

800 lines 29.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Service;
4
5 class Configuration
6 {
7 private $current_configuration = [];
8 private $expected_fields = [
9 // merchant configuration
10 'email' => [
11 'type' => 'string',
12 'default' => '',
13 ],
14 'company_id' => [
15 'type' => 'string',
16 'default' => '',
17 ],
18 'company_iso' => [
19 'type' => 'string',
20 'default' => '',
21 ],
22 'country' => [
23 'type' => 'string',
24 'default' => '',
25 ],
26 'currencies' => [
27 'type' => 'string',
28 'default' => '{}',
29 ],
30
31 // authentication
32 'jwt' => [
33 'type' => 'string',
34 'default' => '{}',
35 ],
36 'api_key' => [
37 'type' => 'string',
38 'default' => '{}',
39 ],
40 'oauth_client_data' => [
41 'type' => 'string',
42 'default' => '{}',
43 ],
44 'oauth_client_id' => [
45 'type' => 'string',
46 'default' => '',
47 ],
48 'oauth_callback_uri' => [
49 'type' => 'string',
50 'default' => '',
51 ],
52 'oauth_code_verifier' => [
53 'type' => 'string',
54 'default' => '',
55 ],
56 'oauth_company_id' => [
57 'type' => 'string',
58 'default' => '',
59 ],
60
61 // payment_methods permissions
62 'payment_methods' => [
63 'permissions' => [
64 //
65 'payplug' => [
66 'enabled' => [
67 'type' => 'bool',
68 'default' => false,
69 ],
70 'countries' => [
71 'type' => 'string',
72 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
73 ],
74 'amounts' => [
75 'type' => 'string',
76 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
77 ],
78 ],
79 'installment' => [
80 'enabled' => [
81 'type' => 'bool',
82 'default' => false,
83 ],
84 'countries' => [
85 'type' => 'string',
86 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
87 ],
88 'amounts' => [
89 'type' => 'string',
90 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
91 ],
92 ],
93 'deferred' => [
94 'enabled' => [
95 'type' => 'bool',
96 'default' => false,
97 ],
98 'countries' => [
99 'type' => 'string',
100 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
101 ],
102 'amounts' => [
103 'type' => 'string',
104 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
105 ],
106 ],
107 'one_click' => [
108 'enabled' => [
109 'type' => 'bool',
110 'default' => false,
111 ],
112 'countries' => [
113 'type' => 'string',
114 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
115 ],
116 'amounts' => [
117 'type' => 'string',
118 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
119 ],
120 ],
121
122 //
123 'apple_pay' => [
124 'allowed_domains' => [
125 'type' => 'string',
126 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
127 ],
128 'enabled' => [
129 'type' => 'bool',
130 'default' => false,
131 ],
132 'countries' => [
133 'type' => 'string',
134 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
135 ],
136 'amounts' => [
137 'type' => 'string',
138 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
139 ],
140 ],
141 'american_express' => [
142 'enabled' => [
143 'type' => 'bool',
144 'default' => false,
145 ],
146 'countries' => [
147 'type' => 'string',
148 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
149 ],
150 'amounts' => [
151 'type' => 'string',
152 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
153 ],
154 ],
155 'oney_x3_with_fees' => [
156 'enabled' => [
157 'type' => 'bool',
158 'default' => false,
159 ],
160 'countries' => [
161 'type' => 'string',
162 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
163 ],
164 'amounts' => [
165 'type' => 'string',
166 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
167 ],
168 ],
169 'oney_x4_with_fees' => [
170 'enabled' => [
171 'type' => 'bool',
172 'default' => false,
173 ],
174 'countries' => [
175 'type' => 'string',
176 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
177 ],
178 'amounts' => [
179 'type' => 'string',
180 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
181 ],
182 ],
183 'oney_x3_without_fees' => [
184 'enabled' => [
185 'type' => 'bool',
186 'default' => false,
187 ],
188 'countries' => [
189 'type' => 'string',
190 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
191 ],
192 'amounts' => [
193 'type' => 'string',
194 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
195 ],
196 ],
197 'oney_x4_without_fees' => [
198 'enabled' => [
199 'type' => 'bool',
200 'default' => false,
201 ],
202 'countries' => [
203 'type' => 'string',
204 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
205 ],
206 'amounts' => [
207 'type' => 'string',
208 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
209 ],
210 ],
211 'bancontact' => [
212 'enabled' => [
213 'type' => 'bool',
214 'default' => false,
215 ],
216 'countries' => [
217 'type' => 'string',
218 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
219 ],
220 'amounts' => [
221 'type' => 'string',
222 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
223 ],
224 ],
225 'giropay' => [
226 'enabled' => [
227 'type' => 'bool',
228 'default' => false,
229 ],
230 'countries' => [
231 'type' => 'string',
232 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
233 ],
234 'amounts' => [
235 'type' => 'string',
236 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
237 ],
238 ],
239 'ideal' => [
240 'enabled' => [
241 'type' => 'bool',
242 'default' => false,
243 ],
244 'countries' => [
245 'type' => 'string',
246 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
247 ],
248 'amounts' => [
249 'type' => 'string',
250 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
251 ],
252 ],
253 'mybank' => [
254 'enabled' => [
255 'type' => 'bool',
256 'default' => false,
257 ],
258 'countries' => [
259 'type' => 'string',
260 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
261 ],
262 'amounts' => [
263 'type' => 'string',
264 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
265 ],
266 ],
267 'satispay' => [
268 'enabled' => [
269 'type' => 'bool',
270 'default' => false,
271 ],
272 'countries' => [
273 'type' => 'string',
274 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
275 ],
276 'amounts' => [
277 'type' => 'string',
278 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
279 ],
280 ],
281 'sofort' => [
282 'enabled' => [
283 'type' => 'bool',
284 'default' => false,
285 ],
286 'countries' => [
287 'type' => 'string',
288 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
289 ],
290 'amounts' => [
291 'type' => 'string',
292 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
293 ],
294 ],
295 'wero' => [
296 'enabled' => [
297 'type' => 'bool',
298 'default' => false,
299 ],
300 'countries' => [
301 'type' => 'string',
302 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
303 ],
304 'amounts' => [
305 'type' => 'string',
306 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
307 ],
308 ],
309 'bizum' => [
310 'enabled' => [
311 'type' => 'bool',
312 'default' => false,
313 ],
314 'countries' => [
315 'type' => 'string',
316 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
317 ],
318 'amounts' => [
319 'type' => 'string',
320 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
321 ],
322 ],
323 'scalapay' => [
324 'enabled' => [
325 'type' => 'bool',
326 'default' => false,
327 ],
328 'countries' => [
329 'type' => 'string',
330 'default' => '{}', // Precise iso code if require e.g. "0 => 'FR'" or "0 => 'ALL'"
331 ],
332 'amounts' => [
333 'type' => 'string',
334 'default' => '{}', // Contain currency e.g. "{"min":{"EUR": 30}, "max":{"EUR": 2000000}}"
335 ],
336 ],
337 ],
338 'configuration' => [
339 'payplug' => [
340 'active' => [
341 'type' => 'bool',
342 'default' => true,
343 ],
344 'title' => [
345 'type' => 'string',
346 'default' => '',
347 ],
348 'description' => [
349 'type' => 'string',
350 'default' => '',
351 ],
352 'save_card' => [
353 'type' => 'bool',
354 'default' => false,
355 ],
356 'defered' => [
357 'type' => 'bool',
358 'default' => false,
359 ],
360 'auto_capture' => [
361 'type' => 'integer',
362 'default' => 0,
363 ],
364 'embedded_mode' => [
365 'type' => 'string',
366 'default' => 'redirect',
367 ],
368 ],
369 'installments' => [
370 'active' => [
371 'type' => 'bool',
372 'default' => false,
373 ],
374 'min_amount' => [
375 'type' => 'integer',
376 'default' => 150,
377 ],
378 'mode' => [
379 'type' => 'integer',
380 'default' => 3,
381 ],
382 ],
383 'apple_pay' => [
384 'active' => [
385 'type' => 'bool',
386 'default' => false,
387 ],
388 'carriers' => [
389 'type' => 'string',
390 'default' => '{}',
391 ],
392 'display' => [
393 'type' => 'string',
394 'default' => '{"cart":true,"checkout":true,"product":false}',
395 ],
396 ],
397 'oney' => [
398 'active' => [
399 'type' => 'bool',
400 'default' => false,
401 ],
402 'cta_cart' => [
403 'type' => 'bool',
404 'default' => false,
405 ],
406 'cta_product' => [
407 'type' => 'bool',
408 'default' => false,
409 ],
410 'default_amounts' => [
411 'type' => 'string',
412 'default' => '{"min":10000, "max":300000}',
413 ],
414 'custom_amounts' => [
415 'type' => 'string',
416 'default' => '{"min":10000, "max":300000}',
417 ],
418 'with_fees' => [
419 'type' => 'bool',
420 'default' => false,
421 ],
422 'optimized' => [
423 'type' => 'bool',
424 'default' => false,
425 ],
426 ],
427 'american_express' => [
428 'active' => [
429 'type' => 'bool',
430 'default' => false,
431 ],
432 ],
433 'bancontact' => [
434 'active' => [
435 'type' => 'bool',
436 'default' => false,
437 ],
438 ],
439 'giropay' => [
440 'active' => [
441 'type' => 'bool',
442 'default' => false,
443 ],
444 ],
445 'ideal' => [
446 'active' => [
447 'type' => 'bool',
448 'default' => false,
449 ],
450 ],
451 'mybank' => [
452 'active' => [
453 'type' => 'bool',
454 'default' => false,
455 ],
456 ],
457 'satispay' => [
458 'active' => [
459 'type' => 'bool',
460 'default' => false,
461 ],
462 ],
463 'sofort' => [
464 'active' => [
465 'type' => 'bool',
466 'default' => false,
467 ],
468 ],
469 'wero' => [
470 'active' => [
471 'type' => 'bool',
472 'default' => false,
473 ],
474 ],
475 'bizum' => [
476 'active' => [
477 'type' => 'bool',
478 'default' => false,
479 ],
480 ],
481 'scalapay' => [
482 'active' => [
483 'type' => 'bool',
484 'default' => false,
485 ],
486 ],
487 ],
488 ],
489
490 // other configuration
491 'telemetry_hash' => [
492 'type' => 'string',
493 'default' => '',
494 ],
495 'enabled' => [
496 'type' => 'bool',
497 'default' => true,
498 ],
499 'debug' => [
500 'type' => 'bool',
501 'default' => false,
502 ],
503 'mode' => [
504 'type' => 'bool',
505 'default' => false,
506 ],
507 'version' => [
508 'type' => 'string',
509 'default' => '',
510 ],
511 ];
512 private $old_keys = [
513 'american_express' => 'payment_methods.configuration.american_express.active',
514 'apple_pay' => 'payment_methods.configuration.apple_pay.active',
515 'applepay_carriers' => 'payment_methods.configuration.apple_pay.carriers',
516 'applepay_cart' => 'payment_methods.configuration.apple_pay.display.cart',
517 'applepay_checkout' => 'payment_methods.configuration.apple_pay.display.checkout',
518 'applepay_product' => 'payment_methods.configuration.apple_pay.display.product',
519 'bancontact' => 'payment_methods.configuration.bancontact.active',
520 'debug' => 'debug',
521 'description' => 'payment_methods.configuration.payplug.description',
522 'email' => 'email',
523 'enabled' => 'enabled',
524 'ideal' => 'payment_methods.configuration.ideal.active',
525 'mode' => 'mode',
526 'mybank' => 'payment_methods.configuration.mybank.active',
527 'oneclick' => 'payment_methods.configuration.payplug.save_card',
528 'oney' => 'payment_methods.configuration.oney.active',
529 'oney_product_animation' => 'payment_methods.configuration.oney.cta_product',
530 'oney_thresholds_max' => 'payment_methods.configuration.oney.custom_amounts.max',
531 'oney_thresholds_min' => 'payment_methods.configuration.oney.custom_amounts.min',
532 'oney_type' => 'payment_methods.configuration.oney.with_fees',
533 'payment_method' => 'payment_methods.configuration.payplug.embedded_mode',
534 'payplug' => 'payment_methods.configuration.payplug.active',
535 'payplug_live_key' => 'api_key.live',
536 'payplug_merchant_id' => 'company_id',
537 'payplug_test_key' => 'api_key.test',
538 'satispay' => 'payment_methods.configuration.satispay.active',
539 'title' => 'payment_methods.configuration.payplug.title',
540 ];
541
542 public function __construct()
543 {
544 $this->current_configuration = get_option('woocommerce_payplug_settings', []);
545 }
546
547 /**
548 * @description initialize option from expected fields
549 *
550 * @return array
551 */
552 public function initialize_option()
553 {
554 $options = $this->extract_options_from_fields();
555
556 return $this->update_options($options);
557 }
558
559 /**
560 * @description get default configuration value from expected fields (recursively)
561 *
562 * @param $fields
563 *
564 * @return array
565 */
566 public function extract_options_from_fields($fields = null)
567 {
568 if ($fields === null) {
569 $fields = $this->get_expected_fields();
570 }
571 $options = [];
572 foreach ($fields as $key => $value) {
573 if (is_array($value) && array_key_exists('default', $value)) {
574 $options[$key] = $value['default'];
575 } elseif (is_array($value)) {
576 $options[$key] = $this->extract_options_from_fields($value);
577 }
578 }
579
580 return $options;
581 }
582
583 /**
584 * @description Delete all configurations related to the plugin
585 *
586 * @return void
587 */
588 public function clean_option(): void
589 {
590 $this->current_configuration = null;
591 delete_option('woocommerce_payplug_settings');
592 delete_site_option('woocommerce_payplug_settings');
593 }
594
595 /**
596 * @description Get configurations
597 *
598 * @return mixed
599 */
600 public function get_options()
601 {
602 return $this->current_configuration;
603 }
604
605 /**
606 * @description Return configuration value for a given key
607 * todo: add default return value
608 *
609 * @param $option_name
610 * @param $options
611 *
612 * @return mixed|null
613 */
614 public function get_option($option_name = '', $options = null)
615 {
616 if (!is_string($option_name) || empty($option_name)) {
617 return null;
618 }
619
620 $options = $options ? $options : $this->get_options();
621
622 // Convert name to array
623 $option_name = explode('.', $option_name);
624
625 // Get first iteration
626 $name = reset($option_name);
627 if (!array_key_exists($name, $options)) {
628 return null;
629 }
630
631 // if no more sub option, return current one
632 unset($option_name[0]);
633 if (empty($option_name)) {
634 return $options[$name];
635 }
636
637 // else return the sub option
638 $option_name = implode('.', $option_name);
639
640 return $this->get_option($option_name, $options[$name]);
641 }
642
643 /**
644 * @description Update confurations values
645 * todo: check return value type
646 *
647 * @param $options_to_update
648 *
649 * @return mixed
650 */
651 public function update_options($options_to_update = [])
652 {
653 $options = $this->get_options();
654 foreach ($options_to_update as $key => $value) {
655 $options[$key] = $value;
656 }
657 $this->current_configuration = $options;
658
659 return update_option('woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options));
660 }
661
662 /**
663 * @description Update confurations for a given key
664 *
665 * @param $key
666 * @param $value
667 *
668 * @return array
669 */
670 public function update_option($key = '', $value = null)
671 {
672 if (!is_string($key) || empty($key)) {
673 return [];
674 }
675
676 if (!array_key_exists($key, $this->expected_fields)) {
677 return [];
678 }
679
680 $options = $this->get_options();
681 $options[$key] = $value;
682
683 return $this->update_options($options);
684 }
685
686 /**
687 * @description Update payment configuration permission
688 *
689 * @param $payment_permissions
690 *
691 * @return mixed
692 */
693 public function update_payment_permissions($payment_permissions = [])
694 {
695 $options = $this->get_options();
696 $options['payment_methods']['permissions'] = $payment_permissions;
697 $this->current_configuration = $options;
698
699 return update_option('woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options));
700 }
701
702 /**
703 * @description Get the expected fields
704 *
705 * @return array
706 */
707 public function get_expected_fields()
708 {
709 return $this->expected_fields;
710 }
711
712 /**
713 * @description Validate if the current configuration is valide (for example: when plugin is upgraded)
714 *
715 * @return array
716 */
717 public function validate_configuration()
718 {
719 $options = $this->extract_options_from_fields();
720 foreach ($this->old_keys as $key => $value) {
721 if (!isset($this->current_configuration[$key])) {
722 continue;
723 }
724 switch ($key) {
725 case 'applepay_carriers':
726 $carriers = $this->current_configuration['applepay_carriers'];
727 if (!empty($carriers)) {
728 $options['payment_methods']['configuration']['apple_pay']['carriers'] = json_encode($carriers);
729 }
730 break;
731
732 case 'applepay_cart':
733 case 'applepay_checkout':
734 case 'applepay_product':
735 $display = str_replace('applepay_', '', $key);
736 $applepay_display = json_decode($options['payment_methods']['configuration']['apple_pay']['display'], true);
737 $applepay_display[$display] = 'yes' == $this->current_configuration[$key];
738 $options['payment_methods']['configuration']['apple_pay']['display'] = json_encode($applepay_display);
739 break;
740 case 'oney_product_animation':
741 $options['payment_methods']['configuration']['oney']['cta_product'] = 'yes' == $this->current_configuration['oney_product_animation'];
742 break;
743 case 'oney_thresholds_min':
744 case 'oney_thresholds_max':
745 $thresholds = str_replace('oney_thresholds_', '', $key);
746 $custom_amounts = json_decode($options['payment_methods']['configuration']['oney']['custom_amounts'], true);
747 $custom_amounts[$thresholds] = $this->current_configuration[$key];
748 $options['payment_methods']['configuration']['oney']['custom_amounts'] = json_encode($custom_amounts);
749 break;
750 case 'oney_type':
751 $options['payment_methods']['configuration']['oney']['with_fees'] = $this->current_configuration['oney_type'];
752 break;
753 case 'description':
754 $options['payment_methods']['configuration']['payplug']['description'] = $this->current_configuration['description'];
755 break;
756 case 'payment_method':
757 $options['payment_methods']['configuration']['payplug']['embedded_mode'] = $this->current_configuration['payment_method'];
758 break;
759 case 'oneclick':
760 $options['payment_methods']['configuration']['payplug']['save_card'] = 'yes' == $this->current_configuration['oneclick'];
761 break;
762 case 'title':
763 $options['payment_methods']['configuration']['payplug']['title'] = $this->current_configuration['title'];
764 break;
765
766 case 'payplug_test_key':
767 case 'payplug_live_key':
768 $mode = str_replace('payplug_', '', $key);
769 $mode = str_replace('_key', '', $mode);
770 $api = json_decode($options['api_key'], true);
771 $api[$mode] = $this->current_configuration['payplug_' . $mode . '_key'];
772 $options['api_key'] = json_encode($api);
773 break;
774
775 case 'payplug_merchant_id':
776 $options['company_id'] = $this->current_configuration['payplug_merchant_id'];
777 break;
778
779 case 'american_express':
780 case 'apple_pay':
781 case 'bancontact':
782 case 'ideal':
783 case 'mybank':
784 case 'oney':
785 case 'payplug':
786 case 'satispay':
787 $options['payment_methods']['configuration'][$key]['active'] = 'yes' == $this->current_configuration[$key];
788 break;
789
790 default:
791 $value = 'no' == $this->current_configuration[$key] ? false : $this->current_configuration[$key];
792 $options[$key] = 'yes' == $value ? true : $value;
793 break;
794 }
795 }
796
797 return $options;
798 }
799 }
800