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