PluginProbe
PayPlug for WooCommerce (Official) / 2.7.0
PayPlug for WooCommerce (Official) v2.7.0
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 / PayplugWoocommerceHelper.php

PayplugWoocommerceHelper.php in PayPlug for WooCommerce (Official) 2.7.0, at src/PayplugWoocommerceHelper.php

795 lines 17.0 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;
4
5 // Exit if accessed directly
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 use Payplug\Exception\ForbiddenException;
11 use Payplug\PayplugWoocommerce\Gateway\PayplugGateway;
12 use Payplug\Resource\APIResource;
13 use Payplug\Payplug;
14 use Payplug\Authentication;
15 use Payplug\PayplugWoocommerce\Gateway\PayplugGatewayOney3x;
16 use Payplug\PayplugWoocommerce\Gateway\PayplugPermissions;
17
18 /**
19 * Helper class.
20 *
21 * @package Payplug\PayplugWoocommerce
22 */
23 class PayplugWoocommerceHelper {
24
25 /**
26 * Check if current WooCommerce version is below 3.0.0
27 *
28 * @return bool
29 */
30 public static function is_pre_30() {
31 $wc = function_exists( 'WC' ) ? WC() : $GLOBALS['woocommerce'];
32
33 return version_compare( $wc->version, '3.0.0', '<' );
34 }
35
36 /**
37 * Get a URL to the PayPlug gateway settings page.
38 *
39 * @return string
40 */
41 public static function get_setting_link() {
42 $use_id_as_section = function_exists( 'WC' ) ? version_compare( WC()->version, '2.6', '>=' ) : false;
43 $section_slug = $use_id_as_section ? 'payplug' : strtolower( 'PayplugGateway' );
44
45 return admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . $section_slug );
46 }
47
48 /**
49 * Get all country code supported by PayPlug.
50 *
51 * Those are ISO 3166-1 alpha-2. You can find more information on https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
52 *
53 * @return array
54 */
55 public static function get_supported_countries() {
56 return [
57 'AD',
58 'AO',
59 'AX',
60 'BG',
61 'BO',
62 'BY',
63 'CH',
64 'CR',
65 'DE',
66 'EE',
67 'FI',
68 'GB',
69 'GL',
70 'GT',
71 'HR',
72 'IN',
73 'JM',
74 'KM',
75 'KZ',
76 'LS',
77 'MD',
78 'MM',
79 'MT',
80 'NA',
81 'NO',
82 'PE',
83 'PN',
84 'RE',
85 'SC',
86 'SK',
87 'ST',
88 'TF',
89 'TN',
90 'UA',
91 'VC',
92 'WS',
93 'AE',
94 'AQ',
95 'AZ',
96 'BH',
97 'BQ',
98 'BZ',
99 'CI',
100 'CU',
101 'DJ',
102 'EG',
103 'FJ',
104 'GD',
105 'GM',
106 'GU',
107 'HT',
108 'IO',
109 'JO',
110 'KN',
111 'LA',
112 'LT',
113 'ME',
114 'MN',
115 'MU',
116 'NC',
117 'NP',
118 'PF',
119 'PR',
120 'RO',
121 'SD',
122 'SL',
123 'SV',
124 'TG',
125 'TO',
126 'UG',
127 'VE',
128 'YE',
129 'AM',
130 'AW',
131 'BF',
132 'BN',
133 'BW',
134 'CG',
135 'CO',
136 'AF',
137 'CZ',
138 'EC',
139 'EU',
140 'GA',
141 'GI',
142 'GS',
143 'HN',
144 'IM',
145 'JE',
146 'KI',
147 'KY',
148 'LR',
149 'MC',
150 'ML',
151 'MS',
152 'MZ',
153 'NL',
154 'PA',
155 'PM',
156 'QA',
157 'SB',
158 'SJ',
159 'SS',
160 'TD',
161 'TM',
162 'TZ',
163 'VA',
164 'WF',
165 'AR',
166 'BA',
167 'BI',
168 'BR',
169 'CA',
170 'CK',
171 'CV',
172 'DK',
173 'EH',
174 'FK',
175 'GE',
176 'GN',
177 'GW',
178 'HU',
179 'IQ',
180 'JP',
181 'KP',
182 'LB',
183 'LU',
184 'MF',
185 'MO',
186 'MV',
187 'NE',
188 'NR',
189 'PG',
190 'PS',
191 'RS',
192 'SE',
193 'SM',
194 'SX',
195 'TH',
196 'TR',
197 'UM',
198 'VG',
199 'YT',
200 'AL',
201 'AU',
202 'BE',
203 'BM',
204 'BV',
205 'CF',
206 'CN',
207 'CY',
208 'DZ',
209 'ET',
210 'FR',
211 'GH',
212 'GR',
213 'HM',
214 'IL',
215 'IT',
216 'KH',
217 'KW',
218 'LK',
219 'MA',
220 'MK',
221 'MR',
222 'MY',
223 'NI',
224 'OM',
225 'PL',
226 'PY',
227 'SA',
228 'SI',
229 'SR',
230 'TC',
231 'TL',
232 'TW',
233 'UZ',
234 'VU',
235 'ZW',
236 'AI',
237 'AT',
238 'BD',
239 'BL',
240 'BT',
241 'CD',
242 'CM',
243 'CX',
244 'DO',
245 'ES',
246 'FO',
247 'GG',
248 'GQ',
249 'HK',
250 'IE',
251 'IS',
252 'KG',
253 'KS',
254 'LI',
255 'LY',
256 'MH',
257 'MQ',
258 'MX',
259 'NG',
260 'NZ',
261 'PK',
262 'PW',
263 'RW',
264 'SH',
265 'SO',
266 'SZ',
267 'TK',
268 'TV',
269 'UY',
270 'VN',
271 'ZM',
272 'AG',
273 'AS',
274 'BB',
275 'BJ',
276 'BS',
277 'CC',
278 'CL',
279 'CW',
280 'DM',
281 'ER',
282 'FM',
283 'GF',
284 'GP',
285 'GY',
286 'ID',
287 'IR',
288 'KE',
289 'KR',
290 'LC',
291 'LV',
292 'MG',
293 'MP',
294 'MW',
295 'NF',
296 'NU',
297 'PH',
298 'PT',
299 'RU',
300 'SG',
301 'SN',
302 'SY',
303 'TJ',
304 'TT',
305 'US',
306 'VI',
307 'ZA'
308 ];
309 }
310
311 /**
312 * Ensure country code is supported by PayPlug.
313 *
314 * @param string $country The ISO 3166-1 alpha-2 code for the country
315 *
316 * @return bool
317 * @author Clément Boirie
318 */
319 public static function is_country_supported( $country ) {
320 $country = trim( $country );
321 if ( empty( $country ) ) {
322 return false;
323 }
324
325 return in_array( strtoupper( $country ), self::get_supported_countries() );
326 }
327
328 /**
329 * Get default country.
330 *
331 * @return string
332 */
333 public static function get_default_country() {
334 $country = \WC()->countries->get_base_country();
335
336 return ( self::is_country_supported( $country ) ) ? strtoupper( $country ) : 'FR';
337 }
338
339 /**
340 * Get minimum amount allowed by PayPlug.
341 *
342 * This amount is in cents.
343 *
344 * @return int
345 */
346 public static function get_minimum_amount() {
347 return 99;
348 }
349
350 /**
351 * Get maximum amount allowed by PayPlug.
352 *
353 * This amount is in cents.
354 *
355 * @return int
356 */
357 public static function get_maximum_amount() {
358 return 2000000;
359 }
360
361 /**
362 * Convert amount in cents.
363 *
364 * @param float $amount
365 *
366 * @return int
367 */
368 public static function get_payplug_amount( $amount ) {
369 if ( is_null( $amount ) ) {
370 return $amount;
371 }
372
373 return absint( wc_format_decimal( ( (float) $amount * 100 ), wc_get_price_decimals() ) );
374 }
375
376 /**
377 * Extract useful metadata from PayPlug response.
378 *
379 * @param APIResource $resource
380 *
381 * @return array
382 */
383 public static function extract_transaction_metadata( $resource ) {
384 return [
385 'transaction_id' => sanitize_text_field( $resource->id ),
386 'paid' => (bool) $resource->is_paid,
387 'refunded' => (bool) $resource->is_refunded,
388 'amount' => sanitize_text_field( $resource->amount ),
389 'amount_refunded' => sanitize_text_field( $resource->amount_refunded ),
390 '3ds' => (bool) $resource->is_3ds,
391 'live' => (bool) $resource->is_live,
392 'paid_at' => isset( $resource->hosted_payment->paid_at ) ? sanitize_text_field( $resource->hosted_payment->paid_at ) : sanitize_text_field( $resource->created_at ),
393 'card_last4' => sanitize_text_field( $resource->card->last4 ),
394 'card_exp_month' => sanitize_text_field( $resource->card->exp_month ),
395 'card_exp_year' => sanitize_text_field( $resource->card->exp_year ),
396 'card_brand' => sanitize_text_field( $resource->card->brand ),
397 'card_country' => sanitize_text_field( $resource->card->country ),
398 ];
399 }
400
401 /**
402 * @param \WC_Order $order
403 *
404 * @return array|bool
405 * @author Clément Boirie
406 */
407 public static function get_transaction_metadata( $order ) {
408
409 if ( PayplugWoocommerceHelper::is_pre_30() ) {
410 return get_post_meta( $order->id, '_payplug_metadata', true );
411 } else {
412 return $order->get_meta( '_payplug_metadata', true );
413 }
414 }
415
416 /**
417 * Save transaction metadata extracted from PayPlug response.
418 *
419 * @param \WC_Order $order
420 * @param array $metadata
421 *
422 * @return void
423 */
424 public static function save_transaction_metadata( $order, $metadata ) {
425
426 if ( PayplugWoocommerceHelper::is_pre_30() ) {
427 update_post_meta( $order->id, '_payplug_metadata', $metadata );
428 } else {
429 $order->add_meta_data( '_payplug_metadata', $metadata, true );
430
431 if ( is_callable( [ $order, 'save' ] ) ) {
432 $order->save();
433 }
434 }
435 }
436
437 /**
438 * Set flag ipn ( in progress / over ) on order
439 *
440 * @param \WC_Order $order
441 * @param array $metadata
442 * @param boolean $flag
443 *
444 * @return void
445 */
446 public static function set_flag_ipn_order ( $order, $metadata, $flag) {
447 $metadata['transaction_in_progress'] = $flag;
448 PayplugWoocommerceHelper::save_transaction_metadata($order, $metadata);
449 }
450
451 /**
452 * Get transient key from payplug option
453 *
454 * @return string
455 */
456 public static function get_transient_key($options)
457 {
458 $transient_key = PayplugGateway::OPTION_NAME . (array_key_exists('mode', $options) && $options['mode'] === 'yes' ? "_live" : "_test");
459 return $transient_key;
460 }
461
462
463 /**
464 * Get transient live key from payplug option
465 *
466 * @return string
467 */
468 public static function get_live_transient_key()
469 {
470 return PayplugGateway::OPTION_NAME . "_live";
471 }
472
473 /**
474 * Set transient data for payplug account
475 *
476 * @return string
477 */
478 public static function set_transient_data($data, $options = null)
479 {
480 $options = $options ? $options : get_option('woocommerce_payplug_settings', []);
481 $transient_key = PayplugWoocommerceHelper::get_transient_key($options);
482 set_transient($transient_key, isset($data['httpResponse']) ? $data['httpResponse'] : []);
483 }
484
485 /**
486 * Get current option from payplug settings
487 *
488 * @return array
489 */
490 public static function get_account_data_from_options()
491 {
492 $options = get_option('woocommerce_payplug_settings', []);
493 $transient_key = self::get_transient_key($options);
494 $account = get_transient($transient_key);
495 if (is_array($account)) {
496 $account['oneyEnabled'] = (isset($options['oney']) && !empty($options['oney'])) ? $options['oney'] : '';
497 $account['bancontact'] = !empty($options['bancontact']) ? $options['bancontact'] : '';
498 $account['american_express'] = !empty($options['american_express']) ? $options['american_express'] : '';
499 $apple_pay_enabled = !empty($options['apple_pay']) ? $options['apple_pay'] : '';
500
501 if( !isset($account['payment_methods']['apple_pay']) )
502 $account['payment_methods']['apple_pay'] = [ 'enabled' => false ];
503
504 if ($account['payment_methods']['apple_pay']['enabled']) {
505 if ($apple_pay_enabled == "yes" && in_array(strtr(get_site_url(), array("http://" => "", "https://" => "")), $account['payment_methods']['apple_pay']['allowed_domain_names'])) {
506 $account['apple_pay'] = "yes";
507 } else {
508 $account['apple_pay'] = "no";
509 $options['apple_pay'] = "no";
510 update_option( 'woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options) );
511 }
512
513 } else {
514 $account['apple_pay'] = "no";
515 $options['apple_pay'] = "no";
516 update_option( 'woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options) );
517 }
518 }
519 return $account;
520 }
521
522 /**
523 * Get current option from payplug settings
524 *this should replace get_account_data_from_options
525 *
526 * @return array
527 */
528 public static function generic_get_account_data_from_options($gateway_id){
529 $options = get_option('woocommerce_payplug_settings', []);
530 $transient_key = self::get_transient_key($options);
531 $account = get_transient($transient_key);
532
533 if( isset($options[$gateway_id]) && $options[$gateway_id] =="yes"){
534 $account['permissions'][$gateway_id] = true;
535 }
536
537 return $account;
538
539 }
540
541 /**
542 *
543 * Retrieve the local oney threshold configration
544 *
545 * @return array
546 */
547
548 public static function getOneySettings() {
549 return get_option('woocommerce_payplug_settings', []);
550 }
551
552 /**
553 * Set current option from payplug settings and api call
554 *
555 * @return void
556 */
557 public static function set_account_data_from_options()
558 {
559 $options = get_option('woocommerce_payplug_settings', []);
560 $payplug_test_key = !empty($options['payplug_test_key']) ? $options['payplug_test_key'] : '';
561 $payplug_live_key = !empty($options['payplug_live_key']) ? $options['payplug_live_key'] : '';
562 if (empty($payplug_test_key) && empty($payplug_live_key)) {
563 return array();
564 }
565
566 try {
567 $parameters_account = Authentication::getAccount(new Payplug($options['mode'] === 'yes' ? $payplug_live_key : $payplug_test_key));
568 self::set_transient_data($parameters_account, $options);
569 } catch (\Payplug\Exception\UnauthorizedException $e) {
570 } catch (\Payplug\Exception\ConfigurationNotSetException $e) {
571 } catch( \Payplug\Exception\ForbiddenException $e){
572 } catch (\Payplug\Exception\ForbiddenException $e){return array();}
573
574 }
575
576 /**
577 * Get min and max for oney payment
578 *
579 * @return array
580 */
581 public static function get_min_max_oney() {
582 $account = self::get_account_data_from_options();
583 if (!$account) {
584 return array();
585 }
586 return [
587 'min' => floatval($account['configuration']['oney']['min_amounts']['EUR'])/100,
588 'max' => floatval($account['configuration']['oney']['max_amounts']['EUR'])/100
589 ];
590 }
591
592 /**
593 * Check if oney is available with current settings
594 *
595 * @return boolean
596 */
597 public static function is_oney_available() {
598 $account = self::get_account_data_from_options();
599 if (!$account) {
600 return false;
601 }
602 return ($account && $account['permissions'][PayplugPermissions::USE_ONEY] == "1" && $account['oneyEnabled'] === "yes");
603 }
604
605 /**
606 * Hide popup for if country_code != payplug country
607 * https://payplug-prod.atlassian.net/browse/WOOC-249
608 *
609 * @return bool
610 */
611 public static function show_oney_popup()
612 {
613 $account = self::get_account_data_from_options();
614 if( $account && $account['permissions'][PayplugPermissions::USE_ONEY] == true && $account["country"] == self::getISOCountryCode() ){
615 return true;
616 }
617 return false;
618 }
619
620 /**
621 * @return bool
622 */
623 public static function check_order_max_amount($order_total){
624 if ($order_total < PayplugGatewayOney3x::MIN_AMOUNT || $order_total > PayplugGatewayOney3x::MAX_AMOUNT) {
625 return false;
626 }
627 return true;
628 }
629
630 /**
631 * Load translations from plugin languages folder.
632 *
633 * @param string $plugin_rel_path
634 *
635 * @return bool
636 */
637 public static function load_plugin_textdomain( $plugin_rel_path ) {
638
639 $domain = 'payplug';
640
641 $locale = apply_filters( 'plugin_locale', is_admin() ? get_user_locale() : get_locale(), $domain );
642
643 $mofile = $domain . '-' . $locale . '.mo';
644
645 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
646
647 return load_textdomain( $domain, $path . '/' . $mofile );
648 }
649
650 /**
651 * Check and update value for oney simulation
652 *
653 * @return void
654 */
655 public static function oney_simulation_values ($keys_array, &$array) {
656 foreach($keys_array as $key) {
657 if (array_key_exists($key, $array)) {
658 $array[$key]['down_payment_amount'] = floatval($array[$key]['down_payment_amount']) / 100;
659 foreach ($array[$key]['installments'] as $k => $value) {
660 $array[$key]['installments'][$k]['amount'] = floatval($value['amount']) / 100;
661 }
662 }
663 }
664 }
665
666 public static function getISOCountryCode()
667 {
668 preg_match( '([a-z-]+)', get_locale(), $country );
669 return strtoupper($country[0]);
670 }
671
672 /**
673 * Get country of the payplug merchant account and save it to the database
674 *
675 * @return string payplug merchant account country
676 */
677 public static function get_payplug_merchant_country()
678 {
679 $data = get_option('woocommerce_payplug_settings');
680
681 //in order to reduce the getAccount calls
682 if (isset($data['payplug_live_key'])) {
683
684 if (!isset($data['payplug_merchant_country'])) {
685 return self::UpdateCountryOption($data);
686 }
687
688 return $data['payplug_merchant_country'];
689 }
690
691 $country = wc_get_base_location();
692 return $country['country'];
693
694 }
695
696 /**
697 * Update payplug options and return the country
698 *
699 * @param $options
700 * @return string
701 * @throws \Payplug\Exception\ConfigurationException
702 */
703 public static function UpdateCountryOption($options){
704
705 try {
706
707 //fail safe for non activated account
708 if ((isset($options['mode'])) && $options['payplug_test_key']){
709 $key = $options['mode'] === "yes" && !empty($options['payplug_live_key']) ? $options['payplug_live_key'] : $options['payplug_test_key'];
710 }
711
712 if( empty($options['payplug_live_key'] )){
713 $options['mode'] = "no";
714 }
715
716 if (isset($key)){
717 $response = Authentication::getAccount(new Payplug($key));
718 }
719
720 if (isset($response['httpResponse']['country'])) {
721 $options['payplug_merchant_country'] = $response['httpResponse']['country'];
722 update_option( 'woocommerce_payplug_settings', apply_filters('woocommerce_settings_api_sanitized_fields_payplug', $options) );
723 }else{
724
725 //default value for merchant country
726 $options['payplug_merchant_country'] = 'FR';
727 }
728
729 } catch (ForbiddenException $e) {
730 PayplugGateway::log('Error while getting account : ' . $e->getMessage(), 'error');
731 \WC_Admin_Settings::add_error($e->getMessage());
732 $options['payplug_merchant_country'] = 'FR';
733 }
734
735 return $options['payplug_merchant_country'];
736
737 }
738
739 public static function get_live_key()
740 {
741 return get_option('woocommerce_payplug_settings', [])['payplug_live_key'];
742 }
743
744 public static function get_test_key()
745 {
746 return get_option('woocommerce_payplug_settings', [])['payplug_test_key'];
747 }
748
749 /**
750 *
751 * Checks fi the user is logged in
752 *
753 * @return bool
754 */
755 public static function user_logged_in()
756 {
757 return !empty(get_option( 'woocommerce_payplug_settings', [] )['payplug_test_key']);
758 }
759
760 public static function check_mode(){
761 return get_option( 'woocommerce_payplug_settings', [] )['mode'] === "yes" ? true : false;
762 }
763
764 public static function payplug_logout($gateway) {
765
766 if ($gateway->user_logged_in()) {
767 $data = get_option($gateway->get_option_key());
768 $data['payplug_test_key'] = '';
769 $data['payplug_live_key'] = '';
770 $data['payplug_merchant_id'] = '';
771 $data['enabled'] = 'no';
772 $data['mode'] = 'no';
773 $data['oneclick'] = 'no';
774 update_option(
775 $gateway->get_option_key(),
776 apply_filters('woocommerce_settings_api_sanitized_fields_' . $gateway->id, $data)
777 );
778 if("payplug" === $gateway->id) {
779 return true;
780 }
781 } else {
782 return false;
783 }
784 }
785
786 public static function plugin_deactivation(){
787 $option_name = 'woocommerce_payplug_settings';
788 delete_option( $option_name );
789 // for site options in Multisite
790 delete_site_option( $option_name );
791 \Payplug\PayplugWoocommerce\Model\Lock::delete_lock_table();
792 }
793
794 }
795